Search in sources :

Example 1 with Tracer

use of brave.Tracer in project brave by openzipkin.

the class ITHttpClient method propagatesExtra_unsampledTrace.

@Test
public void propagatesExtra_unsampledTrace() throws Exception {
    Tracer tracer = httpTracing.tracing().tracer();
    server.enqueue(new MockResponse());
    brave.Span parent = tracer.newTrace(SamplingFlags.NOT_SAMPLED).name("test").start();
    try (SpanInScope ws = tracer.withSpanInScope(parent)) {
        ExtraFieldPropagation.set(parent.context(), EXTRA_KEY, "joey");
        get(client, "/foo");
    } finally {
        parent.finish();
    }
    assertThat(server.takeRequest().getHeader(EXTRA_KEY)).isEqualTo("joey");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Tracer(brave.Tracer) SpanInScope(brave.Tracer.SpanInScope) Test(org.junit.Test)

Example 2 with Tracer

use of brave.Tracer in project brave by openzipkin.

the class Log4JThreadContextTest method customCurrentTraceContext.

/**
 * One common request is to support SLF4J or Log4J2 log correlation. This shows you can make a
 * custom implementation
 */
@Test
public void customCurrentTraceContext() {
    assertThat(ThreadContext.get("traceID")).isNull();
    Tracer tracer = Tracing.newBuilder().currentTraceContext(new Log4J2CurrentTraceContext()).build().tracer();
    Span parent = tracer.newTrace();
    try (Tracer.SpanInScope wsParent = tracer.withSpanInScope(parent)) {
        // the trace id is now in the logging context
        assertThat(ThreadContext.get("traceId")).isEqualTo(parent.context().traceIdString());
        // Clear a scope temporarily
        try (Tracer.SpanInScope noScope = tracer.withSpanInScope(null)) {
            assertThat(tracer.currentSpan()).isNull();
        }
        Span child = tracer.newChild(parent.context());
        try (Tracer.SpanInScope wsChild = tracer.withSpanInScope(child)) {
            // nesting worked
            assertThat(ThreadContext.get("traceId")).isEqualTo(child.context().traceIdString());
        }
        // old parent reverted
        assertThat(ThreadContext.get("traceId")).isEqualTo(parent.context().traceIdString());
    }
    assertThat(ThreadContext.get("traceId")).isNull();
    Tracing.current().close();
}
Also used : Tracer(brave.Tracer) Span(brave.Span) Test(org.junit.Test)

Example 3 with Tracer

use of brave.Tracer in project spring-cloud-gcp by spring-cloud.

the class StackdriverTraceAutoConfigurationTests method test.

@Test
public void test() {
    this.contextRunner.run(context -> {
        SleuthProperties sleuthProperties = context.getBean(SleuthProperties.class);
        assertThat(sleuthProperties.isTraceId128()).isTrue();
        assertThat(sleuthProperties.isSupportsJoin()).isFalse();
        Reporter<zipkin2.Span> reporter = context.getBean(Reporter.class);
        assertThat(reporter).isInstanceOf(StackdriverTraceReporter.class);
        Tracer tracer = context.getBean(Tracer.class);
        Span span = tracer.newTrace().start().kind(Span.Kind.CLIENT).name("test").start();
        span.finish();
        // There should be one trace received
        MockConfiguration configuration = context.getBean(MockConfiguration.class);
        assertThat(configuration.tracesList.size()).isEqualTo(1);
        Traces traces = configuration.tracesList.get(0);
        assertThat(traces.getTracesCount()).isEqualTo(1);
        Trace trace = traces.getTraces(0);
        assertThat(trace.getSpansCount()).isEqualTo(1);
        TraceSpan traceSpan = trace.getSpans(0);
    });
}
Also used : Trace(com.google.devtools.cloudtrace.v1.Trace) TraceSpan(com.google.devtools.cloudtrace.v1.TraceSpan) SleuthProperties(org.springframework.cloud.sleuth.autoconfig.SleuthProperties) Traces(com.google.devtools.cloudtrace.v1.Traces) Tracer(brave.Tracer) Span(brave.Span) TraceSpan(com.google.devtools.cloudtrace.v1.TraceSpan) Test(org.junit.Test)

Example 4 with Tracer

use of brave.Tracer in project spring-cloud-sleuth by spring-cloud.

the class WebClientTests method shouldPropagateNotSamplingHeader.

@Test
@Parameters
@SuppressWarnings("unchecked")
public void shouldPropagateNotSamplingHeader(ResponseEntityProvider provider) {
    Span span = this.tracer.nextSpan(TraceContextOrSamplingFlags.create(SamplingFlags.NOT_SAMPLED)).name("foo").start();
    try (Tracer.SpanInScope ws = this.tracer.withSpanInScope(span)) {
        ResponseEntity<Map<String, String>> response = provider.get(this);
        then(response.getBody().get(TRACE_ID_NAME.toLowerCase())).isNotNull();
        then(response.getBody().get(SAMPLED_NAME.toLowerCase())).isEqualTo("0");
    } finally {
        span.finish();
    }
    then(this.reporter.getSpans()).isEmpty();
    then(Tracing.current().tracer().currentSpan()).isNull();
}
Also used : Tracer(brave.Tracer) Span(brave.Span) Map(java.util.Map) HashMap(java.util.HashMap) Parameters(junitparams.Parameters) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 5 with Tracer

use of brave.Tracer in project spring-cloud-sleuth by spring-cloud.

the class TraceCommandTests method should_pass_tracing_information_when_using_Hystrix_commands.

@Test
public void should_pass_tracing_information_when_using_Hystrix_commands() {
    Tracer tracer = this.tracer;
    TraceKeys traceKeys = new TraceKeys();
    HystrixCommand.Setter setter = withGroupKey(asKey("group")).andCommandKey(HystrixCommandKey.Factory.asKey("command"));
    // tag::hystrix_command[]
    HystrixCommand<String> hystrixCommand = new HystrixCommand<String>(setter) {

        @Override
        protected String run() throws Exception {
            return someLogic();
        }
    };
    // end::hystrix_command[]
    // tag::trace_hystrix_command[]
    TraceCommand<String> traceCommand = new TraceCommand<String>(tracer, traceKeys, setter) {

        @Override
        public String doRun() throws Exception {
            return someLogic();
        }
    };
    // end::trace_hystrix_command[]
    String resultFromHystrixCommand = hystrixCommand.execute();
    String resultFromTraceCommand = traceCommand.execute();
    then(resultFromHystrixCommand).isEqualTo(resultFromTraceCommand);
}
Also used : TraceKeys(org.springframework.cloud.sleuth.TraceKeys) HystrixCommand(com.netflix.hystrix.HystrixCommand) Tracer(brave.Tracer) Test(org.junit.Test)

Aggregations

Tracer (brave.Tracer)20 Span (brave.Span)13 Test (org.junit.Test)12 TraceContext (brave.propagation.TraceContext)4 Log (org.apache.commons.logging.Log)4 LogFactory (org.apache.commons.logging.LogFactory)4 Mono (reactor.core.publisher.Mono)4 SpanInScope (brave.Tracer.SpanInScope)3 Tracing (brave.Tracing)3 Nullable (brave.internal.Nullable)3 MockResponse (okhttp3.mockwebserver.MockResponse)3 HttpTracing (brave.http.HttpTracing)2 Propagation (brave.propagation.Propagation)2 Sampler (brave.sampler.Sampler)2 RepositoryRequestSpec (io.crnk.core.engine.dispatcher.RepositoryRequestSpec)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 BDDAssertions.then (org.assertj.core.api.BDDAssertions.then)2 Awaitility (org.awaitility.Awaitility)2 AfterClass (org.junit.AfterClass)2 BeforeClass (org.junit.BeforeClass)2