Java Code Examples for com.linecorp.armeria.server.ServiceRequestContext#logBuilder()

The following examples show how to use com.linecorp.armeria.server.ServiceRequestContext#logBuilder() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AccessLogFormatsTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void requestLogAvailabilityException() {
    final String fullName = AccessLogFormatsTest.class.getSimpleName() + "/rpcMethod";
    final String expectedLogMessage = "\"GET /armeria/log#" + fullName + " h2c\" 200 1024";

    final ServiceRequestContext ctx = ServiceRequestContext.builder(
            HttpRequest.of(RequestHeaders.of(HttpMethod.GET, "/armeria/log",
                                             HttpHeaderNames.USER_AGENT, "armeria/x.y.z",
                                             HttpHeaderNames.REFERER, "http://log.example.com",
                                             HttpHeaderNames.COOKIE, "a=1;b=2"))).build();
    final RequestLog log = ctx.log().partial();
    final RequestLogBuilder logBuilder = ctx.logBuilder();

    // AccessLogger#format will be called after response is finished.
    final AtomicReference<RequestLog> logHolder = new AtomicReference<>();
    log.whenComplete().thenAccept(logHolder::set);

    // RequestLogAvailabilityException will be raised inside AccessLogger#format before injecting each
    // component to RequestLog. So we cannot get the expected log message here.
    assertThat(AccessLogger.format(AccessLogFormats.COMMON, log)).doesNotEndWith(expectedLogMessage);
    logBuilder.requestContent(RpcRequest.of(AccessLogFormatsTest.class, "rpcMethod"), null);
    assertThat(AccessLogger.format(AccessLogFormats.COMMON, log)).doesNotEndWith(expectedLogMessage);
    logBuilder.endRequest();
    assertThat(AccessLogger.format(AccessLogFormats.COMMON, log)).doesNotEndWith(expectedLogMessage);
    logBuilder.responseHeaders(ResponseHeaders.of(HttpStatus.OK));
    assertThat(AccessLogger.format(AccessLogFormats.COMMON, log)).doesNotEndWith(expectedLogMessage);
    logBuilder.responseLength(1024);
    assertThat(AccessLogger.format(AccessLogFormats.COMMON, log)).doesNotEndWith(expectedLogMessage);
    logBuilder.endResponse();

    assertThat(AccessLogger.format(AccessLogFormats.COMMON, logHolder.get()))
            .endsWith(expectedLogMessage);
}
 
Example 2
Source File: AccessLogFormatsTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void requestLogWithEmptyCause() {
    final ServiceRequestContext ctx = ServiceRequestContext.of(HttpRequest.of(HttpMethod.GET, "/"));

    final RequestLogBuilder logBuilder = ctx.logBuilder();

    final List<AccessLogComponent> format =
            AccessLogFormats.parseCustom("%{requestCause}L %{responseCause}L");

    logBuilder.endRequest();
    logBuilder.endResponse();

    assertThat(AccessLogger.format(format, ctx.log().ensureComplete())).isEqualTo("- -");
}
 
Example 3
Source File: RequestContextExportingAppenderTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Test
void testServiceContextWithMinimalLogs() throws Exception {
    final List<ILoggingEvent> events = prepare(a -> {
        // Export all properties.
        for (BuiltInProperty p : BuiltInProperty.values()) {
            a.addBuiltIn(p);
        }
    });

    final ServiceRequestContext ctx = newServiceContext("/foo", "name=alice");
    try (SafeCloseable ignored = ctx.push()) {
        final RequestLogBuilder log = ctx.logBuilder();
        log.endRequest();
        log.endResponse();

        final ILoggingEvent e = log(events);
        final Map<String, String> mdc = e.getMDCPropertyMap();
        assertThat(mdc).containsEntry("local.host", "server.com")
                       .containsEntry("local.ip", "5.6.7.8")
                       .containsEntry("local.port", "8080")
                       .containsEntry("remote.host", "client.com")
                       .containsEntry("remote.ip", "1.2.3.4")
                       .containsEntry("remote.port", "5678")
                       .containsEntry("client.ip", "9.10.11.12")
                       .containsEntry("req.direction", "INBOUND")
                       .containsEntry("req.authority", "server.com:8080")
                       .containsEntry("req.name", "GET")
                       .containsEntry("req.serviceName", ctx.config().service().getClass().getName())
                       .containsEntry("req.method", "GET")
                       .containsEntry("req.path", "/foo")
                       .containsEntry("req.query", "name=alice")
                       .containsEntry("scheme", "none+h2")
                       .containsEntry("req.content_length", "0")
                       .containsEntry("res.status_code", "0")
                       .containsEntry("res.content_length", "0")
                       .containsEntry("tls.session_id", "0101020305080d15")
                       .containsEntry("tls.proto", "TLSv1.2")
                       .containsEntry("tls.cipher", "some-cipher")
                       .containsKey("elapsed_nanos")
                       .containsKey("req.id")
                       .hasSize(23);
    }
}
 
Example 4
Source File: RequestContextExportingAppenderTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Test
void testServiceContextWithFullLogs() throws Exception {
    final List<ILoggingEvent> events = prepare(a -> {
        // Export all properties.
        for (BuiltInProperty p : BuiltInProperty.values()) {
            a.addBuiltIn(p);
        }
        // .. and an attribute.
        a.addAttribute("attrs.my_attr_name", MY_ATTR, new CustomObjectNameStringifier());
        a.addAttribute("attrs.my_attr_value", MY_ATTR, new CustomObjectValueStringifier());
        // .. and some HTTP headers.
        a.addRequestHeader(HttpHeaderNames.USER_AGENT);
        a.addResponseHeader(HttpHeaderNames.DATE);
    });

    final ServiceRequestContext ctx = newServiceContext("/foo", "bar=baz");
    try (SafeCloseable ignored = ctx.push()) {
        final RequestLogBuilder log = ctx.logBuilder();
        log.serializationFormat(ThriftSerializationFormats.BINARY);
        log.requestLength(64);
        log.requestHeaders(RequestHeaders.of(HttpMethod.GET, "/foo?bar=baz",
                                             HttpHeaderNames.USER_AGENT, "some-client"));
        log.requestContent(RPC_REQ, THRIFT_CALL);
        log.endRequest();
        log.responseLength(128);
        log.responseHeaders(ResponseHeaders.of(HttpStatus.OK,
                                               HttpHeaderNames.DATE, "some-date"));
        log.responseContent(RPC_RES, THRIFT_REPLY);
        log.endResponse();

        final ILoggingEvent e = log(events);
        final Map<String, String> mdc = e.getMDCPropertyMap();
        assertThat(mdc).containsEntry("local.host", "server.com")
                       .containsEntry("local.ip", "5.6.7.8")
                       .containsEntry("local.port", "8080")
                       .containsEntry("remote.host", "client.com")
                       .containsEntry("remote.ip", "1.2.3.4")
                       .containsEntry("remote.port", "5678")
                       .containsEntry("client.ip", "9.10.11.12")
                       .containsEntry("req.direction", "INBOUND")
                       .containsEntry("req.authority", "server.com:8080")
                       .containsEntry("req.method", "GET")
                       .containsEntry("req.name", RPC_REQ.method())
                       .containsEntry("req.serviceName", RPC_REQ.serviceType().getName())
                       .containsEntry("req.path", "/foo")
                       .containsEntry("req.query", "bar=baz")
                       .containsEntry("scheme", "tbinary+h2")
                       .containsEntry("req.content_length", "64")
                       .containsEntry("req.content", "[world]")
                       .containsEntry("res.status_code", "200")
                       .containsEntry("res.content_length", "128")
                       .containsEntry("res.content", "Hello, world!")
                       .containsEntry("req.headers.user-agent", "some-client")
                       .containsEntry("res.headers.date", "some-date")
                       .containsEntry("tls.session_id", "0101020305080d15")
                       .containsEntry("tls.proto", "TLSv1.2")
                       .containsEntry("tls.cipher", "some-cipher")
                       .containsEntry("attrs.my_attr_name", "some-name")
                       .containsEntry("attrs.my_attr_value", "some-value")
                       .containsKey("req.id")
                       .containsKey("elapsed_nanos")
                       .hasSize(29);
    }
}
 
Example 5
Source File: BraveServiceTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
private static RequestLog testServiceInvocation(SpanHandler spanHandler,
                                                CurrentTraceContext traceContext,
                                                float samplingRate) throws Exception {
    final Tracing tracing = Tracing.newBuilder()
                                   .localServiceName(TEST_SERVICE)
                                   .addSpanHandler(spanHandler)
                                   .currentTraceContext(traceContext)
                                   .sampler(Sampler.create(samplingRate))
                                   .build();

    final HttpTracing httpTracing = HttpTracing.newBuilder(tracing)
                                               .serverRequestParser(ArmeriaHttpServerParser.get())
                                               .serverResponseParser(ArmeriaHttpServerParser.get())
                                               .build();

    final HttpRequest req = HttpRequest.of(RequestHeaders.of(HttpMethod.POST, "/hello/trustin",
                                                             HttpHeaderNames.SCHEME, "http",
                                                             HttpHeaderNames.AUTHORITY, "foo.com"));
    final ServiceRequestContext ctx = ServiceRequestContext.builder(req).build();
    final RpcRequest rpcReq = RpcRequest.of(HelloService.Iface.class, "hello", "trustin");
    final HttpResponse res = HttpResponse.of(HttpStatus.OK);
    final RpcResponse rpcRes = RpcResponse.of("Hello, trustin!");
    final RequestLogBuilder logBuilder = ctx.logBuilder();
    logBuilder.requestContent(rpcReq, req);
    logBuilder.endRequest();

    try (SafeCloseable ignored = ctx.push()) {
        final HttpService delegate = mock(HttpService.class);
        final BraveService service = BraveService.newDecorator(httpTracing).apply(delegate);
        when(delegate.serve(ctx, req)).thenReturn(res);

        // do invoke
        service.serve(ctx, req);

        verify(delegate, times(1)).serve(eq(ctx), eq(req));
    }

    logBuilder.responseHeaders(ResponseHeaders.of(HttpStatus.OK));
    logBuilder.responseFirstBytesTransferred();
    logBuilder.responseContent(rpcRes, res);
    logBuilder.endResponse();
    return ctx.log().ensureComplete();
}