Search in sources :

Example 21 with TraceContext

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

the class BraveTracer method inject.

@Override
public <C> void inject(SpanContext spanContext, Format<C> format, C carrier) {
    if (format != Format.Builtin.HTTP_HEADERS) {
        throw new UnsupportedOperationException(format + " != Format.Builtin.HTTP_HEADERS");
    }
    TraceContext traceContext = ((BraveSpanContext) spanContext).context;
    injector.inject(traceContext, (TextMap) carrier);
}
Also used : TraceContext(brave.propagation.TraceContext)

Example 22 with TraceContext

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

the class OpenTracingAdapterTest method injectTraceContext.

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

Example 23 with TraceContext

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

the class NonStringPropagationKeysTest method injectExtractTraceContext.

@Test
public void injectExtractTraceContext() throws Exception {
    TraceContext context = tracing.tracer().newTrace().context();
    Metadata metadata = new Metadata();
    injector.inject(context, metadata);
    assertThat(metadata.keys()).containsExactly("x-b3-traceid", "x-b3-spanid", "x-b3-sampled");
    assertThat(extractor.extract(metadata).context()).isEqualTo(context);
}
Also used : Metadata(io.grpc.Metadata) TraceContext(brave.propagation.TraceContext) Test(org.junit.Test)

Example 24 with TraceContext

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

the class MutableSpanMapTest method reportOrphanedSpans_afterGC.

/**
 * This is the key feature. Spans orphaned via GC are reported to zipkin on the next action.
 *
 * <p>This is a customized version of https://github.com/raphw/weak-lock-free/blob/master/src/test/java/com/blogspot/mydailyjava/weaklockfree/WeakConcurrentMapTest.java
 */
@Test
public void reportOrphanedSpans_afterGC() {
    TraceContext context1 = context.toBuilder().spanId(1).build();
    map.getOrCreate(context1);
    TraceContext context2 = context.toBuilder().spanId(2).build();
    map.getOrCreate(context2);
    TraceContext context3 = context.toBuilder().spanId(3).build();
    map.getOrCreate(context3);
    TraceContext context4 = context.toBuilder().spanId(4).build();
    map.getOrCreate(context4);
    // By clearing strong references in this test, we are left with the weak ones in the map
    context1 = context2 = null;
    blockOnGC();
    // After GC, we expect that the weak references of context1 and context2 to be cleared
    assertThat(map.delegate.keySet()).extracting(o -> ((Reference) o).get()).containsExactlyInAnyOrder(null, null, context3, context4);
    map.reportOrphanedSpans();
    // After reporting, we expect no the weak references of null
    assertThat(map.delegate.keySet()).extracting(o -> ((Reference) o).get()).containsExactlyInAnyOrder(context3, context4);
    // We also expect the spans to have been reported
    assertThat(spans).flatExtracting(Span::annotations).extracting(Annotation::value).containsExactly("brave.flush", "brave.flush");
}
Also used : Tracing(brave.Tracing) PowerMockito.mockStatic(org.powermock.api.mockito.PowerMockito.mockStatic) PowerMockito.when(org.powermock.api.mockito.PowerMockito.when) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Span(zipkin2.Span) Test(org.junit.Test) TraceContext(brave.propagation.TraceContext) ArrayList(java.util.ArrayList) Reference(java.lang.ref.Reference) List(java.util.List) Annotation(zipkin2.Annotation) Platform(brave.internal.Platform) Endpoint(zipkin2.Endpoint) After(org.junit.After) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockRunner(org.powermock.modules.junit4.PowerMockRunner) PowerMockIgnore(org.powermock.core.classloader.annotations.PowerMockIgnore) Reference(java.lang.ref.Reference) TraceContext(brave.propagation.TraceContext) Span(zipkin2.Span) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 25 with TraceContext

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

the class MutableSpanMapTest method noop_afterGC.

@Test
public void noop_afterGC() {
    TraceContext context1 = context.toBuilder().spanId(1).build();
    map.getOrCreate(context1);
    TraceContext context2 = context.toBuilder().spanId(2).build();
    map.getOrCreate(context2);
    TraceContext context3 = context.toBuilder().spanId(3).build();
    map.getOrCreate(context3);
    TraceContext context4 = context.toBuilder().spanId(4).build();
    map.getOrCreate(context4);
    map.noop.set(true);
    // By clearing strong references in this test, we are left with the weak ones in the map
    context1 = context2 = null;
    blockOnGC();
    // After GC, we expect that the weak references of context1 and context2 to be cleared
    assertThat(map.delegate.keySet()).extracting(o -> ((Reference) o).get()).containsExactlyInAnyOrder(null, null, context3, context4);
    map.reportOrphanedSpans();
    // After reporting, we expect no the weak references of null
    assertThat(map.delegate.keySet()).extracting(o -> ((Reference) o).get()).containsExactlyInAnyOrder(context3, context4);
    // since this is noop, we don't expect any spans to be reported
    assertThat(spans).isEmpty();
}
Also used : Tracing(brave.Tracing) PowerMockito.mockStatic(org.powermock.api.mockito.PowerMockito.mockStatic) PowerMockito.when(org.powermock.api.mockito.PowerMockito.when) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Span(zipkin2.Span) Test(org.junit.Test) TraceContext(brave.propagation.TraceContext) ArrayList(java.util.ArrayList) Reference(java.lang.ref.Reference) List(java.util.List) Annotation(zipkin2.Annotation) Platform(brave.internal.Platform) Endpoint(zipkin2.Endpoint) After(org.junit.After) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockRunner(org.powermock.modules.junit4.PowerMockRunner) PowerMockIgnore(org.powermock.core.classloader.annotations.PowerMockIgnore) Reference(java.lang.ref.Reference) TraceContext(brave.propagation.TraceContext) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

TraceContext (brave.propagation.TraceContext)54 Test (org.junit.Test)46 StrictCurrentTraceContext (brave.propagation.StrictCurrentTraceContext)21 CurrentTraceContext (brave.propagation.CurrentTraceContext)10 TraceContextOrSamplingFlags (brave.propagation.TraceContextOrSamplingFlags)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 Span (brave.Span)4 Tracer (brave.Tracer)4 Tracing (brave.Tracing)4 ThreadContextCurrentTraceContext (brave.context.log4j2.ThreadContextCurrentTraceContext)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 After (org.junit.After)4 Endpoint (zipkin2.Endpoint)4 Span (zipkin2.Span)4 Platform (brave.internal.Platform)3 Propagation (brave.propagation.Propagation)3 Reference (java.lang.ref.Reference)3