Search in sources :

Example 6 with Span

use of brave.Span 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 7 with Span

use of brave.Span in project brave by openzipkin.

the class ExtraFieldPropagationTest method nextSpanExtraWithImplicitParent_butNoExtractedExtraFields.

@Test
public void nextSpanExtraWithImplicitParent_butNoExtractedExtraFields() {
    try (Tracing tracing = Tracing.newBuilder().propagationFactory(factory).build()) {
        ExtraFieldPropagation.set(context, "x-vcap-request-id", "foo");
        Span span = tracing.tracer().toSpan(context);
        try (Tracer.SpanInScope ws = tracing.tracer().withSpanInScope(span)) {
            TraceContext context1 = tracing.tracer().nextSpan(extractor.extract(carrier)).context();
            // merged
            assertThat(context1.extra()).hasSize(1);
            ExtraFieldPropagation.Extra extra = ((ExtraFieldPropagation.Extra) context1.extra().get(0));
            assertThat(extra.values).containsExactly("foo", null);
            assertThat(extra.context).isSameAs(context1);
        }
    }
}
Also used : Tracer(brave.Tracer) Tracing(brave.Tracing) Span(brave.Span) Test(org.junit.Test)

Example 8 with Span

use of brave.Span in project brave by openzipkin.

the class ExtraFieldPropagationTest method toSpan_selfLinksContext.

@Test
public void toSpan_selfLinksContext() {
    try (Tracing t = Tracing.newBuilder().propagationFactory(factory).build()) {
        ExtraFieldPropagation.set(context, "x-amzn-trace-id", awsTraceId);
        Span span = t.tracer().toSpan(context);
        ExtraFieldPropagation.Extra extra = (ExtraFieldPropagation.Extra) span.context().extra().get(0);
        assertThat(extra.context).isSameAs(span.context());
    }
}
Also used : Tracing(brave.Tracing) Span(brave.Span) Test(org.junit.Test)

Example 9 with Span

use of brave.Span in project incubator-servicecomb-java-chassis by apache.

the class ZipkinTracingHandler method handle.

@Override
public void handle(Invocation invocation, AsyncResponse asyncResp) throws Exception {
    Span span = tracingDelegate.createSpan(invocation);
    try (SpanInScope scope = tracer.tracer().withSpanInScope(span)) {
        LOGGER.debug("{}: Generated tracing span for {}", tracingDelegate.name(), invocation.getOperationName());
        invocation.next(onResponse(invocation, asyncResp, span));
    } catch (Exception e) {
        LOGGER.debug("{}: Failed invocation on {}", tracingDelegate.name(), invocation.getOperationName(), e);
        tracingDelegate.onResponse(span, null, e);
        throw e;
    }
}
Also used : SpanInScope(brave.Tracer.SpanInScope) Span(brave.Span)

Example 10 with Span

use of brave.Span 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)

Aggregations

Span (brave.Span)279 Test (org.junit.Test)156 Tracer (brave.Tracer)100 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)45 SpanInScope (brave.Tracer.SpanInScope)38 Tracing (brave.Tracing)19 ThreadLocalSpan (brave.propagation.ThreadLocalSpan)19 TraceContextOrSamplingFlags (brave.propagation.TraceContextOrSamplingFlags)19 TraceContext (brave.propagation.TraceContext)17 Scope (brave.propagation.CurrentTraceContext.Scope)16 Bean (org.springframework.context.annotation.Bean)13 Configuration (org.springframework.context.annotation.Configuration)13 Log (org.apache.commons.logging.Log)12 LogFactory (org.apache.commons.logging.LogFactory)12 Sampler (brave.sampler.Sampler)11 BDDAssertions.then (org.assertj.core.api.BDDAssertions.then)11 Autowired (org.springframework.beans.factory.annotation.Autowired)11 CurrentTraceContext (brave.propagation.CurrentTraceContext)10 RunWith (org.junit.runner.RunWith)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)9