Search in sources :

Example 66 with TraceContext

use of brave.propagation.TraceContext in project brave by openzipkin.

the class TracingTest method alwaysReportSpans_reportsEvenWhenUnsampled.

@Test
public void alwaysReportSpans_reportsEvenWhenUnsampled() {
    TraceContext sampledLocal = TraceContext.newBuilder().traceId(1).spanId(1).sampledLocal(true).build();
    try (Tracing tracing = Tracing.newBuilder().addSpanHandler(spans).sampler(Sampler.NEVER_SAMPLE).alwaysReportSpans().build()) {
        tracing.tracer().toSpan(sampledLocal).start().finish();
    }
    assertThat(spans).isNotEmpty();
}
Also used : StrictCurrentTraceContext(brave.propagation.StrictCurrentTraceContext) TraceContext(brave.propagation.TraceContext) Test(org.junit.Test)

Example 67 with TraceContext

use of brave.propagation.TraceContext in project brave by openzipkin.

the class OpenTracingAdapterTest method injectTraceContext.

@Test
public void injectTraceContext() {
    TraceContext context = TraceContext.newBuilder().traceId(1L).spanId(2L).sampled(true).build();
    Map<String, String> map = new LinkedHashMap<>();
    TextMapAdapter request = new TextMapAdapter(map);
    opentracing.inject(new BraveSpanContext(context), Format.Builtin.HTTP_HEADERS, request);
    assertThat(map).containsExactly(entry("X-B3-TraceId", "0000000000000001"), entry("X-B3-SpanId", "0000000000000002"), entry("X-B3-Sampled", "1"));
}
Also used : TraceContext(brave.propagation.TraceContext) TextMapAdapter(io.opentracing.propagation.TextMapAdapter) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 68 with TraceContext

use of brave.propagation.TraceContext in project brave by openzipkin.

the class InternalPropagationTest method shallowCopy.

@Test
public void shallowCopy() {
    TraceContext context = TraceContext.newBuilder().traceId(1).spanId(2).debug(true).extra(Collections.singletonList(1L)).build();
    assertThat(InternalPropagation.instance.shallowCopy(context)).isNotSameAs(context).usingRecursiveComparison().isEqualTo(context);
}
Also used : TraceContext(brave.propagation.TraceContext) Test(org.junit.Test)

Example 69 with TraceContext

use of brave.propagation.TraceContext in project brave by openzipkin.

the class TracingInterceptor method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    RequestWrapper request = new RequestWrapper(chain.request());
    Span span;
    TraceContext parent = chain.request().tag(TraceContext.class);
    if (parent != null) {
        // TracingCallFactory setup this request
        span = handler.handleSendWithParent(request, parent != NULL_SENTINEL ? parent : null);
    } else {
        // This is using interceptors only
        span = handler.handleSend(request);
    }
    parseRouteAddress(chain, span);
    Response response = null;
    Throwable error = null;
    try (Scope ws = currentTraceContext.newScope(span.context())) {
        return response = chain.proceed(request.build());
    } catch (Throwable t) {
        error = t;
        throw t;
    } finally {
        // Intentionally not the same instance as chain.proceed, as properties may have changed
        if (response != null)
            request = new RequestWrapper(response.request());
        handler.handleReceive(new ResponseWrapper(request, response, error), span);
    }
}
Also used : HttpClientResponse(brave.http.HttpClientResponse) Response(okhttp3.Response) Scope(brave.propagation.CurrentTraceContext.Scope) CurrentTraceContext(brave.propagation.CurrentTraceContext) TraceContext(brave.propagation.TraceContext) Span(brave.Span)

Example 70 with TraceContext

use of brave.propagation.TraceContext in project brave by openzipkin.

the class ITKafkaTracing method continues_a_trace_when_only_trace_id_propagated.

@Test
public void continues_a_trace_when_only_trace_id_propagated() {
    consumerTracing = KafkaTracing.create(tracingBuilder(Sampler.ALWAYS_SAMPLE).clearSpanHandlers().addSpanHandler(consumerSpanHandler).propagationFactory(new TraceIdOnlyPropagation()).build());
    producerTracing = KafkaTracing.create(tracingBuilder(Sampler.ALWAYS_SAMPLE).clearSpanHandlers().addSpanHandler(producerSpanHandler).propagationFactory(new TraceIdOnlyPropagation()).build());
    producer = createTracingProducer();
    consumer = createTracingConsumer();
    send(new ProducerRecord<>(testName.getMethodName(), TEST_KEY, TEST_VALUE));
    // intentionally using deprecated method as we are checking the same class in an invoker test
    // under src/it. If we want to explicitly tests the Duration arg, we will have to subclass.
    ConsumerRecords<String, String> records = consumer.poll(10_000L);
    assertThat(records).hasSize(1);
    MutableSpan producerSpan = takeProducerSpan();
    MutableSpan consumerSpan = takeConsumerSpan();
    assertThat(producerSpan.traceId()).isEqualTo(consumerSpan.traceId());
    for (ConsumerRecord<String, String> record : records) {
        TraceContext forProcessor = consumerTracing.nextSpan(record).context();
        assertThat(forProcessor.traceIdString()).isEqualTo(consumerSpan.traceId());
    }
}
Also used : MutableSpan(brave.handler.MutableSpan) TraceContext(brave.propagation.TraceContext) Test(org.junit.Test)

Aggregations

TraceContext (brave.propagation.TraceContext)200 Test (org.junit.Test)163 CurrentTraceContext (brave.propagation.CurrentTraceContext)77 Scope (brave.propagation.CurrentTraceContext.Scope)52 StrictCurrentTraceContext (brave.propagation.StrictCurrentTraceContext)52 MutableSpan (brave.handler.MutableSpan)38 Span (brave.Span)17 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)13 MockResponse (okhttp3.mockwebserver.MockResponse)12 SpanHandler (brave.handler.SpanHandler)9 Clock (brave.Clock)7 SamplingFlags (brave.propagation.SamplingFlags)7 TraceContextOrSamplingFlags (brave.propagation.TraceContextOrSamplingFlags)7 ArrayList (java.util.ArrayList)7 List (java.util.List)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 TestSpanHandler (brave.test.TestSpanHandler)6 AssertableCallback (brave.test.util.AssertableCallback)6 Message (javax.jms.Message)6 After (org.junit.After)6