@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(); }
@Override public Set<Object> getSingletons() { return new LinkedHashSet<>(asList(new Resource(), TracingApplicationEventListener.create( HttpTracing.create(Tracing.newBuilder() .sampler(Sampler.NEVER_SAMPLE) .spanReporter(Reporter.NOOP) .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 buildZipkinTracing(@NonNull String name, @NonNull String zipkinUrl, float samplingRate) { if (zipkinUrl.isEmpty() || !(samplingRate > 0f)) { return null; } return Tracing.newBuilder() .localServiceName(name) // puts trace IDs into logs .currentTraceContext(MDCCurrentTraceContext.create()) .spanReporter(buildReporter(zipkinUrl)) .sampler(Sampler.create(samplingRate)) .build(); } }
@Override protected Tracing buildZipkinTracing(String name, String zipkinUrl, float samplingRate) { if (currentTraceContext == null) { currentTraceContext = new StrictCurrentTraceContext(); } return Tracing.newBuilder() .spanReporter(ignored -> latch.countDown()) .currentTraceContext(currentTraceContext) .sampler(Sampler.ALWAYS_SAMPLE) .build(); } }
protected Tracing tracing() { this.tracing= Tracing .newBuilder() .localServiceName(this.serviceName) .sampler(Sampler.ALWAYS_SAMPLE) .spanReporter(spanReporter()) .build(); return this.tracing; }
/** Controls aspects of tracing such as the name that shows up in the UI */ @Bean Tracing tracing( @Lazy Reporter<Span> reporter, @Value("${zipkin.self-tracing.sample-rate:1.0}") float rate) { return Tracing.newBuilder() .localServiceName("zipkin-server") .sampler(rate < 0.01 ? BoundarySampler.create(rate) : Sampler.create(rate)) .currentTraceContext(currentTraceContext()) .spanReporter(reporter) .build(); }
@Before public void setUp() { TestSpanReporter.clear(); brave = Tracing .newBuilder() .spanReporter(new TestSpanReporter()) .sampler(Sampler.ALWAYS_SAMPLE) .build(); braveClientProvider = new BraveClientProvider(brave); random = new Random(); }
@Test public void testThatNoSpansAreRecordedWhenNotSampled() { final Tracing never = Tracing .newBuilder() .spanReporter(new TestSpanReporter()) .sampler(Sampler.NEVER_SAMPLE) .build(); final Response r = createWebClient("/bookstore/books", new BraveClientProvider(never)).get(); assertEquals(Status.OK.getStatusCode(), r.getStatus()); assertThat(TestSpanReporter.getAllSpans().size(), equalTo(0)); assertThatTraceHeadersArePresent(r, false); }
.localServiceName(TEST_SERVICE) .spanReporter(reporter) .sampler(Sampler.create(samplingRate)) .build();
@Test(timeout = 20000) public void shouldSubmitSpanWhenSampled() throws Exception { final SpanCollectingReporter reporter = new SpanCollectingReporter(); final Tracing tracing = Tracing.newBuilder() .localServiceName(TEST_SERVICE) .spanReporter(reporter) .sampler(Sampler.create(1.0f)) .build(); testRemoteInvocation(tracing, null); // check span name final Span span = reporter.spans().take(); assertThat(span.name()).isEqualTo(TEST_SPAN); // check kind assertThat(span.kind()).isSameAs(Kind.CLIENT); // only one span should be submitted assertThat(reporter.spans().poll(1, TimeUnit.SECONDS)).isNull(); // check # of annotations (we add wire annotations) assertThat(span.annotations()).hasSize(2); // check tags assertThat(span.tags()).containsAllEntriesOf(ImmutableMap.of( "http.host", "foo.com", "http.method", "POST", "http.path", "/hello/armeria", "http.status_code", "200", "http.url", "none+h2c://foo.com/hello/armeria")); // check service name assertThat(span.localServiceName()).isEqualTo(TEST_SERVICE); // check remote service name assertThat(span.remoteServiceName()).isEqualTo("foo.com"); }
@Test(timeout = 20000) public void shouldSubmitSpanWithCustomRemoteName() throws Exception { final SpanCollectingReporter reporter = new SpanCollectingReporter(); final Tracing tracing = Tracing.newBuilder() .localServiceName(TEST_SERVICE) .spanReporter(reporter) .sampler(Sampler.create(1.0f)) .build(); testRemoteInvocation(tracing, "fooService"); // check span name final Span span = reporter.spans().take(); // check tags assertThat(span.tags()).containsAllEntriesOf(ImmutableMap.of( "http.host", "foo.com", "http.method", "POST", "http.path", "/hello/armeria", "http.status_code", "200", "http.url", "none+h2c://foo.com/hello/armeria")); // check service name assertThat(span.localServiceName()).isEqualTo(TEST_SERVICE); // check remote service name, lower-cased assertThat(span.remoteServiceName()).isEqualTo("fooservice"); }
@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() throws Exception { server = Undertow.builder() .addHttpListener(0, "127.0.0.1") .setHandler(exchange -> { exchange.getResponseHeaders().put(CONTENT_TYPE, "text/plain; charset=UTF-8"); exchange.getResponseSender().send("hello world"); }).build(); server.start(); baseUrl = "http://127.0.0.1:" + ((InetSocketAddress) server.getListenerInfo().get(0).getAddress()).getPort(); client = newClient(); tracedClient = newClient(HttpTracing.create( Tracing.newBuilder().spanReporter(Reporter.NOOP).build() )); unsampledClient = newClient(HttpTracing.create( Tracing.newBuilder().sampler(Sampler.NEVER_SAMPLE).spanReporter(Reporter.NOOP).build() )); }
public OnlySampledLocal() { super(Tracing.newBuilder() .addFinishedSpanHandler(new FinishedSpanHandler() { @Override public boolean handle(TraceContext context, MutableSpan span) { return true; } @Override public boolean alwaysSampleLocal() { return true; } }) .sampler(Sampler.NEVER_SAMPLE) .spanReporter(AsyncReporter.create(new NoopSender())) .build()); } }
public Unsampled() { super(Tracing.newBuilder() .sampler(Sampler.NEVER_SAMPLE) .spanReporter(AsyncReporter.create(new NoopSender())) .build()); } }
@Override public Tracer getTracer(StagemonitorPlugin.InitArguments initArguments) { final Tracing braveTracer = Tracing.newBuilder() .localServiceName(initArguments.getMeasurementSession().getApplicationName()) .reporter(getZipkinReporterBuilder(initArguments).build()) .sampler(getSampler()) .build(); return BraveTracer.newBuilder(braveTracer) .textMapPropagation(B3HeaderFormat.INSTANCE, Propagation.B3_STRING) .build(); }
private static Tracing newTracing(String name) { final CurrentTraceContext currentTraceContext = RequestContextCurrentTraceContext.newBuilder() .addScopeDecorator(StrictScopeDecorator.create()) .build(); return Tracing.newBuilder() .currentTraceContext(currentTraceContext) .localServiceName(name) .spanReporter(spanReporter) .sampler(Sampler.ALWAYS_SAMPLE) .build(); }
@Test public void shouldNotSubmitSpanWhenNotSampled() throws Exception { final SpanCollectingReporter reporter = new SpanCollectingReporter(); final Tracing tracing = Tracing.newBuilder() .localServiceName(TEST_SERVICE) .spanReporter(reporter) .sampler(Sampler.create(0.0f)) .build(); testRemoteInvocation(tracing, null); assertThat(reporter.spans().poll(1, TimeUnit.SECONDS)).isNull(); }
public Unsampled() { super(TracingFilter.create( Tracing.newBuilder().sampler(Sampler.NEVER_SAMPLE).spanReporter(Reporter.NOOP).build() )); } }