diff --git a/armeria/src/main/java/brave/example/Backend.java b/armeria/src/main/java/brave/example/Backend.java index eb2cad4..74a4ae7 100644 --- a/armeria/src/main/java/brave/example/Backend.java +++ b/armeria/src/main/java/brave/example/Backend.java @@ -5,6 +5,7 @@ import com.linecorp.armeria.server.Server; import com.linecorp.armeria.server.brave.BraveService; import com.linecorp.armeria.server.healthcheck.HealthCheckService; +import com.linecorp.armeria.server.logging.AccessLogWriter; import com.linecorp.armeria.server.logging.LoggingService; import java.util.Date; @@ -13,8 +14,12 @@ public final class Backend { public static void main(String[] args) { final HttpTracing httpTracing = HttpTracingFactory.create("backend"); + final AccessLogWriter accessLogWriter = + HttpTracingFactory.accessLogWriter(httpTracing, AccessLogWriter.common()); + final Server server = Server.builder() .http(9000) + .accessLogWriter(accessLogWriter, true) .service("/health", HealthCheckService.builder().build()) .service("/api", (ctx, req) -> { String response = new Date().toString(); diff --git a/armeria/src/main/java/brave/example/Frontend.java b/armeria/src/main/java/brave/example/Frontend.java index 815e2fc..d8dc57d 100644 --- a/armeria/src/main/java/brave/example/Frontend.java +++ b/armeria/src/main/java/brave/example/Frontend.java @@ -6,6 +6,7 @@ import com.linecorp.armeria.server.Server; import com.linecorp.armeria.server.brave.BraveService; import com.linecorp.armeria.server.healthcheck.HealthCheckService; +import com.linecorp.armeria.server.logging.AccessLogWriter; import com.linecorp.armeria.server.logging.LoggingService; public final class Frontend { @@ -18,9 +19,13 @@ public static void main(String[] args) { .decorator(BraveClient.newDecorator(httpTracing.clientOf("backend"))) .build(); + final AccessLogWriter accessLogWriter = + HttpTracingFactory.accessLogWriter(httpTracing, AccessLogWriter.common()); + final Server server = Server.builder() .http(8081) + .accessLogWriter(accessLogWriter, true) .service("/health", HealthCheckService.builder().build()) .service("/", (ctx, req) -> backendClient.get("")) .decorator(BraveService.newDecorator(httpTracing)) diff --git a/armeria/src/main/java/brave/example/HttpTracingFactory.java b/armeria/src/main/java/brave/example/HttpTracingFactory.java index 605533e..1ad2217 100644 --- a/armeria/src/main/java/brave/example/HttpTracingFactory.java +++ b/armeria/src/main/java/brave/example/HttpTracingFactory.java @@ -9,14 +9,19 @@ import brave.http.HttpTracing; import brave.propagation.B3Propagation; import brave.propagation.CurrentTraceContext; +import brave.propagation.CurrentTraceContext.Scope; import brave.propagation.CurrentTraceContext.ScopeDecorator; import brave.propagation.Propagation; +import brave.propagation.TraceContext; import com.linecorp.armeria.client.WebClient; import com.linecorp.armeria.client.eureka.EurekaEndpointGroup; import com.linecorp.armeria.client.eureka.EurekaEndpointGroupBuilder; import com.linecorp.armeria.common.SessionProtocol; import com.linecorp.armeria.common.auth.BasicToken; import com.linecorp.armeria.common.brave.RequestContextCurrentTraceContext; +import com.linecorp.armeria.internal.common.brave.TraceContextUtil; +import com.linecorp.armeria.server.logging.AccessLogWriter; +import com.linecorp.armeria.server.logging.LoggingService; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; @@ -52,6 +57,23 @@ static ScopeDecorator correlationScopeDecorator() { .add(CorrelationScopeConfig.SingleCorrelationField.create(USER_NAME)).build(); } + /** + * Unlike {@link LoggingService#newDecorator}, the trace context isn't yet integrated for + * {@link AccessLogWriter}. Put it into scope manually until this is done in Armeria. + */ + static AccessLogWriter accessLogWriter(HttpTracing httpTracing, AccessLogWriter delegate) { + CurrentTraceContext current = httpTracing.tracing().currentTraceContext(); + // Adrian isn't sure if this is really the best way, but you have to make the thread + // "context aware" to avoid log warnings. + return log -> log.context().makeContextAware(() -> { + TraceContext ctx = TraceContextUtil.traceContext(log.context()); + try (Scope scope = current.maybeScope(ctx)) { + delegate.log(log); + } + } + ).run(); + } + /** Propagates trace context between threads. */ static CurrentTraceContext currentTraceContext(ScopeDecorator correlationScopeDecorator) { return RequestContextCurrentTraceContext.builder() diff --git a/armeria/src/main/resources/logback.xml b/armeria/src/main/resources/logback.xml index 13dde5f..d731f12 100644 --- a/armeria/src/main/resources/logback.xml +++ b/armeria/src/main/resources/logback.xml @@ -8,7 +8,18 @@ - + + + [%X{traceId}/%X{spanId}] %msg%n + + + + + + + + +