Search in sources :

Example 76 with TraceContext

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

the class ExtraFactoryTest method decorate_extractedExtra_plus_emptyParent.

@Test
public void decorate_extractedExtra_plus_emptyParent() {
    TraceContext decorated = propagationFactory.decorate(context);
    BasicMapExtra extra1 = decorated.findExtra(BasicMapExtra.class);
    BasicMapExtra extracted = factory.create();
    extracted.put("2", "three");
    context2 = propagationFactory.decorate(context2.toBuilder().addExtra(extra1).addExtra(extracted).build());
    BasicMapExtra extra2 = context2.findExtra(BasicMapExtra.class);
    // merged
    assertThat(context2.extra()).containsExactly(extra2);
    assertThat(extra2.get("2")).isEqualTo("three");
    assertExtraClaimed(context2);
}
Also used : CurrentTraceContext(brave.propagation.CurrentTraceContext) TraceContext(brave.propagation.TraceContext) Test(org.junit.Test)

Example 77 with TraceContext

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

the class NoopAwareSpanHandlerTest method doesntCrashOnNonFatalThrowable.

@Test
public void doesntCrashOnNonFatalThrowable() {
    Throwable[] toThrow = new Throwable[1];
    SpanHandler handler = NoopAwareSpanHandler.create(new SpanHandler[] { new SpanHandler() {

        @Override
        public boolean end(TraceContext context, MutableSpan span, Cause cause) {
            doThrowUnsafely(toThrow[0]);
            return true;
        }
    } }, noop);
    toThrow[0] = new RuntimeException();
    assertThat(handler.end(context, span, Cause.FINISHED)).isTrue();
    toThrow[0] = new Exception();
    assertThat(handler.end(context, span, Cause.FINISHED)).isTrue();
    toThrow[0] = new Error();
    assertThat(handler.end(context, span, Cause.FINISHED)).isTrue();
    // fatal
    toThrow[0] = new StackOverflowError();
    try {
        // assertThatThrownBy doesn't work with StackOverflowError
        handler.end(context, span, Cause.FINISHED);
        failBecauseExceptionWasNotThrown(StackOverflowError.class);
    } catch (StackOverflowError e) {
    }
}
Also used : MutableSpan(brave.handler.MutableSpan) Cause(brave.handler.SpanHandler.Cause) TraceContext(brave.propagation.TraceContext) SpanHandler(brave.handler.SpanHandler) Test(org.junit.Test)

Example 78 with TraceContext

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

the class PendingSpansTest method noop_afterGC.

@Test
public void noop_afterGC() {
    TraceContext context1 = context.toBuilder().spanId(1).build();
    pendingSpans.getOrCreate(null, context1, false);
    TraceContext context2 = context.toBuilder().spanId(2).build();
    pendingSpans.getOrCreate(null, context2, false);
    TraceContext context3 = context.toBuilder().spanId(3).build();
    pendingSpans.getOrCreate(null, context3, false);
    TraceContext context4 = context.toBuilder().spanId(4).build();
    pendingSpans.getOrCreate(null, context4, false);
    int initialClockVal = clock.get();
    pendingSpans.noop.set(true);
    // By clearing strong references in this test, we are left with the weak ones in the map
    context1 = context2 = null;
    pendingSpans.expungeStaleEntries();
    // since this is noop, we don't expect any spans to be reported
    assertThat(spans).isEmpty();
    // we also expect the clock to not have been called
    assertThat(clock.get()).isEqualTo(initialClockVal);
}
Also used : TraceContext(brave.propagation.TraceContext) Test(org.junit.Test)

Example 79 with TraceContext

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

the class PendingSpansTest method orphanContext_includesAllFlags.

@Test
public void orphanContext_includesAllFlags() {
    TraceContext context1 = context.toBuilder().sampled(null).sampledLocal(true).shared(true).build();
    TraceContext context = context1.toBuilder().build();
    pendingSpans.getOrCreate(null, context, false).state().tag("foo", "bar");
    // We drop the reference to the context, which means the next GC should attempt to flush it
    context = null;
    GarbageCollectors.blockOnGC();
    pendingSpans.expungeStaleEntries();
    assertThat(contexts).hasSize(1);
    assertThat(InternalPropagation.instance.flags(contexts.get(0))).isEqualTo(// no flags lost
    InternalPropagation.instance.flags(context1));
}
Also used : TraceContext(brave.propagation.TraceContext) Test(org.junit.Test)

Example 80 with TraceContext

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

the class PendingSpansTest method getOrCreate_reusesClockFromParent.

/**
 * Ensure we use the same clock for traces that started in-process
 */
@Test
public void getOrCreate_reusesClockFromParent() {
    TraceContext trace = context;
    TraceContext traceJoin = trace.toBuilder().shared(true).build();
    TraceContext trace2 = context.toBuilder().traceId(2L).build();
    TraceContext traceChild = TraceContext.newBuilder().traceId(1L).parentId(trace.spanId()).spanId(3L).build();
    PendingSpan traceSpan = pendingSpans.getOrCreate(null, trace, false);
    PendingSpan traceJoinSpan = pendingSpans.getOrCreate(trace, traceJoin, false);
    PendingSpan trace2Span = pendingSpans.getOrCreate(null, trace2, false);
    PendingSpan traceChildSpan = pendingSpans.getOrCreate(trace, traceChild, false);
    assertThat(traceSpan.clock).isSameAs(traceChildSpan.clock);
    assertThat(traceSpan.clock).isSameAs(traceJoinSpan.clock);
    assertThat(traceSpan.clock).isNotSameAs(trace2Span.clock);
}
Also used : 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