@Bean @ConditionalOnMissingBean // NOTE: stable bean name as might be used outside sleuth Tracing tracing( @Value("${spring.zipkin.service.name:${spring.application.name:default}}") String serviceName, Propagation.Factory factory, CurrentTraceContext currentTraceContext, Sampler sampler, ErrorParser errorParser, SleuthProperties sleuthProperties, @Nullable List<Reporter<zipkin2.Span>> spanReporters) { Tracing.Builder builder = Tracing.newBuilder().sampler(sampler) .errorParser(errorParser) .localServiceName(StringUtils.isEmpty(serviceName) ? DEFAULT_SERVICE_NAME : serviceName) .propagationFactory(factory).currentTraceContext(currentTraceContext) .spanReporter(new CompositeReporter(this.spanAdjusters, spanReporters != null ? spanReporters : Collections.emptyList())) .traceId128Bit(sleuthProperties.isTraceId128()) .supportsJoin(sleuthProperties.isSupportsJoin()); for (FinishedSpanHandler finishedSpanHandlerFactory : this.finishedSpanHandlers) { builder.addFinishedSpanHandler(finishedSpanHandlerFactory); } return builder.build(); }
protected Tracing.Builder tracingBuilder(Sampler sampler) { return Tracing.newBuilder() .spanReporter(s -> { // make sure the context was cleared prior to finish.. no leaks! TraceContext current = httpTracing.tracing().currentTraceContext().get(); boolean contextLeak = false; if (current != null) { // add annotation in addition to throwing, in case we are off the main thread if (current.spanIdString().equals(s.id())) { s = s.toBuilder().addAnnotation(s.timestampAsLong(), CONTEXT_LEAK).build(); contextLeak = true; } } spans.add(s); // throw so that we can see the path to the code that leaked the context if (contextLeak) { throw new AssertionError(CONTEXT_LEAK + " on " + Thread.currentThread().getName()); } }) .propagationFactory(ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, EXTRA_KEY)) .currentTraceContext(currentTraceContext) .sampler(sampler); } }
@Override public Set<Object> getSingletons() { return new LinkedHashSet<>(asList(new Resource(), TracingApplicationEventListener.create( HttpTracing.create(Tracing.newBuilder() .propagationFactory(ExtraFieldPropagation.newFactoryBuilder(B3Propagation.FACTORY) .addField("x-vcap-request-id") .addPrefixedFields("baggage-", asList("country-code", "user-id")) .build() ) .spanReporter(Reporter.NOOP) .build()) ))); } }
@Override public Tracer getTracer(String serviceName) { String hostname = configuration.getFirstProperty(TracingConstants.ZIPKIN_CONFIG_HOST) != null ? configuration.getFirstProperty(TracingConstants.ZIPKIN_CONFIG_HOST) : TracingConstants.ZIPKIN_DEFAULT_HOST; int port = configuration.getFirstProperty(TracingConstants.ZIPKIN_CONFIG_PORT) != null ? Integer.parseInt(configuration.getFirstProperty(TracingConstants.ZIPKIN_CONFIG_PORT)) : TracingConstants.ZIPKIN_DEFAULT_PORT; boolean tracerLogEnabled = Boolean.parseBoolean(configuration.getFirstProperty(TracingConstants.CONFIG_TRACER_LOG_ENABLED) != null ? configuration.getFirstProperty(TracingConstants.CONFIG_TRACER_LOG_ENABLED) : TracingConstants.DEFAULT_TRACER_LOG_ENABLED); OkHttpSender sender = OkHttpSender.create("http://" + hostname + ":" + port + TracingConstants.ZIPKIN_API_CONTEXT); Tracer tracer = BraveTracer.create(Tracing.newBuilder() .localServiceName(serviceName) .spanReporter(AsyncReporter.builder(sender).build()) .propagationFactory(ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, TracingConstants.REQUEST_ID)) .build()); if (tracerLogEnabled) { Reporter reporter = new TracingReporter(LogFactory.getLog(TracingConstants.TRACER)); Tracer tracerR = new TracerR(tracer, reporter, new ThreadLocalScopeManager()); GlobalTracer.register(tracerR); return tracerR; } else { GlobalTracer.register(tracer); return tracer; } }
protected Tracing.Builder tracingBuilder(Sampler sampler) { return Tracing.newBuilder() .spanReporter(s -> { // make sure the context was cleared prior to finish.. no leaks! TraceContext current = httpTracing.tracing().currentTraceContext().get(); boolean contextLeak = false; if (current != null) { // add annotation in addition to throwing, in case we are off the main thread if (current.spanIdString().equals(s.id())) { s = s.toBuilder().addAnnotation(s.timestampAsLong(), CONTEXT_LEAK).build(); contextLeak = true; } } spans.add(s); // throw so that we can see the path to the code that leaked the context if (contextLeak) { throw new AssertionError(CONTEXT_LEAK + " on " + Thread.currentThread().getName()); } }) .propagationFactory(ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, EXTRA_KEY)) .currentTraceContext(currentTraceContext) .sampler(sampler); } }
@Bean @ConditionalOnMissingBean // NOTE: stable bean name as might be used outside sleuth Tracing tracing( @Value("${spring.zipkin.service.name:${spring.application.name:default}}") String serviceName, Propagation.Factory factory, CurrentTraceContext currentTraceContext, Sampler sampler, ErrorParser errorParser, SleuthProperties sleuthProperties) { Tracing.Builder builder = Tracing.newBuilder().sampler(sampler) .errorParser(errorParser) .localServiceName(StringUtils.isEmpty(serviceName) ? DEFAULT_SERVICE_NAME : serviceName) .propagationFactory(factory).currentTraceContext(currentTraceContext) .spanReporter( new CompositeReporter(this.spanAdjusters, this.spanReporters)) .traceId128Bit(sleuthProperties.isTraceId128()) .supportsJoin(sleuthProperties.isSupportsJoin()); for (FinishedSpanHandler finishedSpanHandlerFactory : this.finishedSpanHandlers) { builder.addFinishedSpanHandler(finishedSpanHandlerFactory); } return builder.build(); }
@Override protected Tracing createInstance() { Tracing.Builder builder = Tracing.newBuilder(); if (localServiceName != null) builder.localServiceName(localServiceName); if (localEndpoint != null) builder.endpoint(localEndpoint); if (endpoint != null) builder.endpoint(endpoint); if (spanReporter != null) builder.spanReporter(spanReporter); if (finishedSpanHandlers != null) { for (FinishedSpanHandler finishedSpanHandler : finishedSpanHandlers) { builder.addFinishedSpanHandler(finishedSpanHandler); } } if (errorParser != null) builder.errorParser(errorParser); if (clock != null) builder.clock(clock); if (sampler != null) builder.sampler(sampler); if (currentTraceContext != null) builder.currentTraceContext(currentTraceContext); if (propagationFactory != null) builder.propagationFactory(propagationFactory); if (traceId128Bit != null) builder.traceId128Bit(traceId128Bit); if (supportsJoin != null) builder.supportsJoin(supportsJoin); return builder.build(); }
/** * 构建Tracing * @param serviceName * @param zipkinV2Url * @return */ private Tracing getTracing(String serviceName ,String zipkinV2Url) { tracing = Tracing.newBuilder() .localServiceName(serviceName) .spanReporter(spanReporter(zipkinV2Url)) .sampler(Sampler.ALWAYS_SAMPLE) .propagationFactory(ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, "user-name")) .build(); return tracing; }
/** Controls aspects of tracing such as the service name that shows up in the UI */ @Bean Tracing tracing(@Value("${spring.application.name}") String serviceName) { return Tracing.newBuilder() .localServiceName(serviceName) .propagationFactory(ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, "user-name")) .currentTraceContext(ThreadLocalCurrentTraceContext.newBuilder() .addScopeDecorator(MDCScopeDecorator.create()) // puts trace IDs into logs .build() ) .spanReporter(spanReporter()).build(); }
@Override protected Tracing createInstance() { Tracing.Builder builder = Tracing.newBuilder(); if (localServiceName != null) builder.localServiceName(localServiceName); if (localEndpoint != null) builder.endpoint(localEndpoint); if (endpoint != null) builder.endpoint(endpoint); if (spanReporter != null) builder.spanReporter(spanReporter); if (finishedSpanHandlers != null) { for (FinishedSpanHandler finishedSpanHandler : finishedSpanHandlers) { builder.addFinishedSpanHandler(finishedSpanHandler); } } if (errorParser != null) builder.errorParser(errorParser); if (clock != null) builder.clock(clock); if (sampler != null) builder.sampler(sampler); if (currentTraceContext != null) builder.currentTraceContext(currentTraceContext); if (propagationFactory != null) builder.propagationFactory(propagationFactory); if (traceId128Bit != null) builder.traceId128Bit(traceId128Bit); if (supportsJoin != null) builder.supportsJoin(supportsJoin); return builder.build(); }
@Setup(Level.Trial) public void init() { tracer = Tracing.newBuilder() .addFinishedSpanHandler(new FinishedSpanHandler() { @Override public boolean handle(TraceContext context, MutableSpan span) { return true; // anonymous subtype prevents all recording from being no-op } }) .spanReporter(Reporter.NOOP).build().tracer(); tracerExtra = Tracing.newBuilder() .propagationFactory(ExtraFieldPropagation.newFactory( B3Propagation.FACTORY, "x-vcap-request-id")) .addFinishedSpanHandler(new FinishedSpanHandler() { @Override public boolean handle(TraceContext context, MutableSpan span) { return true; // anonymous subtype prevents all recording from being no-op } }) .spanReporter(Reporter.NOOP).build().tracer(); }
public TracedExtra() { super(Tracing.newBuilder() .propagationFactory(ExtraFieldPropagation.newFactoryBuilder(B3Propagation.FACTORY) .addField("x-vcap-request-id") .addPrefixedFields("baggage-", Arrays.asList("country-code", "user-id")) .build() ) .spanReporter(AsyncReporter.create(new NoopSender())) .build()); } }
public TracedExtra() { super(TracingFilter.create(Tracing.newBuilder() .propagationFactory(ExtraFieldPropagation.newFactoryBuilder(B3Propagation.FACTORY) .addField("x-vcap-request-id") .addPrefixedFields("baggage-", Arrays.asList("country-code", "user-id")) .build() ) .spanReporter(Reporter.NOOP) .build())); } }