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); } }
@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(); }
/** 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(); }
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); } }
/** * 构建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; }
@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; } }