-
Notifications
You must be signed in to change notification settings - Fork 829
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add distributed tracing in server logs
- based on this example app using brave lib (https://github.com/openzipkin/brave-example/tree/59215c36ce8ec12cbb2358fd6e7e6b494bc42b29/webmvc4-boot) - aside from the brave docs and example app, we also have to manually add the TracingFilter bean to the filter chain [#185000950] Co-authored-by: Hongchol Sinn <[email protected]> Co-authored-by: Danny Faught <[email protected]>
- Loading branch information
Showing
5 changed files
with
100 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
uaa/src/main/java/org/cloudfoundry/identity/uaa/TracingAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package org.cloudfoundry.identity.uaa; | ||
|
||
import javax.servlet.Filter; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; | ||
|
||
import brave.CurrentSpanCustomizer; | ||
import brave.SpanCustomizer; | ||
import brave.Tracing; | ||
import brave.context.slf4j.MDCScopeDecorator; | ||
import brave.http.HttpTracing; | ||
import brave.propagation.CurrentTraceContext; | ||
import brave.propagation.CurrentTraceContext.ScopeDecorator; | ||
import brave.propagation.ThreadLocalCurrentTraceContext; | ||
import brave.servlet.TracingFilter; | ||
import brave.spring.webmvc.SpanCustomizingAsyncHandlerInterceptor; | ||
|
||
/** This adds tracing configuration to any web mvc controllers or rest template clients. */ | ||
@Configuration | ||
// Importing a class is effectively the same as declaring bean methods | ||
@Import(SpanCustomizingAsyncHandlerInterceptor.class) | ||
public class TracingAutoConfiguration { | ||
|
||
/** Allows log patterns to use {@code %{traceId}} {@code %{spanId}} and {@code %{userName}} */ | ||
@Bean ScopeDecorator correlationScopeDecorator() { | ||
return MDCScopeDecorator.newBuilder() | ||
.build(); | ||
} | ||
|
||
/** Propagates trace context between threads. */ | ||
@Bean CurrentTraceContext currentTraceContext(ScopeDecorator correlationScopeDecorator) { | ||
return ThreadLocalCurrentTraceContext.newBuilder() | ||
.addScopeDecorator(correlationScopeDecorator) | ||
.build(); | ||
} | ||
|
||
/** Controls aspects of tracing such as the service name that shows up in the UI */ | ||
@Bean Tracing tracing( | ||
@Value("${brave.localServiceName:uaa}") String serviceName, | ||
@Value("${brave.supportsJoin:true}") boolean supportsJoin, | ||
@Value("${brave.traceId128Bit:false}") boolean traceId128Bit, | ||
CurrentTraceContext currentTraceContext) { | ||
return Tracing.newBuilder() | ||
.localServiceName(serviceName) | ||
.supportsJoin(supportsJoin) | ||
.traceId128Bit(traceId128Bit) | ||
.currentTraceContext(currentTraceContext) | ||
.build(); | ||
} | ||
|
||
/** Allows someone to add tags to a span if a trace is in progress. */ | ||
@Bean SpanCustomizer spanCustomizer(Tracing tracing) { | ||
return CurrentSpanCustomizer.create(tracing); | ||
} | ||
|
||
/** Decides how to name and tag spans. By default they are named the same as the http method. */ | ||
@Bean HttpTracing httpTracing(Tracing tracing) { | ||
return HttpTracing.create(tracing); | ||
} | ||
|
||
/** Creates server spans for HTTP requests */ | ||
@Bean Filter tracingFilter(HttpTracing httpTracing) { | ||
return TracingFilter.create(httpTracing); | ||
} | ||
|
||
/** Adds MVC Controller tags to server spans */ | ||
@Bean WebMvcConfigurer tracingWebMvcConfigurer( | ||
final SpanCustomizingAsyncHandlerInterceptor webMvcTracingCustomizer) { | ||
return new WebMvcConfigurerAdapter() { | ||
/** Adds application-defined web controller details to HTTP server spans */ | ||
@Override public void addInterceptors(InterceptorRegistry registry) { | ||
registry.addInterceptor(webMvcTracingCustomizer); | ||
} | ||
}; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters