|
| 1 | +package cn.iocoder.springboot.lab40.zpkindemo.consumerdemo.config; |
| 2 | + |
| 3 | +import brave.CurrentSpanCustomizer; |
| 4 | +import brave.SpanCustomizer; |
| 5 | +import brave.Tracing; |
| 6 | +import brave.http.HttpTracing; |
| 7 | +import brave.servlet.TracingFilter; |
| 8 | +import org.springframework.beans.factory.annotation.Value; |
| 9 | +import org.springframework.context.annotation.Bean; |
| 10 | +import org.springframework.context.annotation.Configuration; |
| 11 | +import zipkin2.Span; |
| 12 | +import zipkin2.reporter.AsyncReporter; |
| 13 | +import zipkin2.reporter.Sender; |
| 14 | +import zipkin2.reporter.okhttp3.OkHttpSender; |
| 15 | + |
| 16 | +import javax.servlet.Filter; |
| 17 | + |
| 18 | +@Configuration |
| 19 | +public class ZipkinConfiguration { |
| 20 | + |
| 21 | + // ==================== 通用配置 ==================== |
| 22 | + |
| 23 | + /** |
| 24 | + * Configuration for how to send spans to Zipkin |
| 25 | + */ |
| 26 | + @Bean |
| 27 | + public Sender sender() { |
| 28 | + return OkHttpSender.create("http://127.0.0.1:9411/api/v2/spans"); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Configuration for how to buffer spans into messages for Zipkin |
| 33 | + */ |
| 34 | + @Bean |
| 35 | + public AsyncReporter<Span> spanReporter() { |
| 36 | + return AsyncReporter.create(sender()); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Controls aspects of tracing such as the service name that shows up in the UI |
| 41 | + */ |
| 42 | + @Bean |
| 43 | + public Tracing tracing(@Value("${spring.application.name}") String serviceName) { |
| 44 | + return Tracing.newBuilder() |
| 45 | + .localServiceName(serviceName) |
| 46 | +// .currentTraceContext(ThreadLocalCurrentTraceContext.newBuilder() |
| 47 | +// .addScopeDecorator(MDCScopeDecorator.create()) // puts trace IDs into logs |
| 48 | +// .build() |
| 49 | +// ) |
| 50 | + .spanReporter(spanReporter()).build(); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Allows someone to add tags to a span if a trace is in progress |
| 55 | + */ |
| 56 | + @Bean |
| 57 | + public SpanCustomizer spanCustomizer(Tracing tracing) { |
| 58 | + return CurrentSpanCustomizer.create(tracing); |
| 59 | + } |
| 60 | + |
| 61 | + // ==================== HTTP 相关 ==================== |
| 62 | + |
| 63 | + /** |
| 64 | + * Decides how to name and tag spans. By default they are named the same as the http method |
| 65 | + */ |
| 66 | + @Bean |
| 67 | + public HttpTracing httpTracing(Tracing tracing) { |
| 68 | + return HttpTracing.create(tracing); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Creates server spans for http requests |
| 73 | + */ |
| 74 | + @Bean |
| 75 | + public Filter tracingFilter(HttpTracing httpTracing) { |
| 76 | + return TracingFilter.create(httpTracing); |
| 77 | + } |
| 78 | + |
| 79 | + // ==================== SpringMVC 相关 ==================== |
| 80 | + // @see SpringMvcConfiguration 类上的,@Import(SpanCustomizingAsyncHandlerInterceptor.class) |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | +} |
0 commit comments