Search in sources :

Example 1 with SpanHandler

use of brave.handler.SpanHandler in project brave by openzipkin.

the class TracingTest method basicUsage.

@Test
public void basicUsage() {
    try (Tracing tracing = Tracing.newBuilder().addSpanHandler(new SpanHandler() {
    }).build()) {
        ScopedSpan parent = tracing.tracer().startScopedSpan("parent");
        tracing.tracer().newChild(parent.context()).finish();
        parent.finish();
    }
}
Also used : SpanHandler(brave.handler.SpanHandler) Tracing(brave.Tracing) ScopedSpan(brave.ScopedSpan) Test(org.junit.Test)

Example 2 with SpanHandler

use of brave.handler.SpanHandler in project brave by openzipkin.

the class ITHttpClient method spanHandlerSeesError.

/**
 * This ensures custom span handlers can see the actual exception thrown, not just the "error"
 * tag value.
 */
void spanHandlerSeesError(Callable<Void> get) throws IOException {
    ConcurrentLinkedDeque<Throwable> caughtThrowables = new ConcurrentLinkedDeque<>();
    closeClient(client);
    httpTracing = HttpTracing.create(tracingBuilder(Sampler.ALWAYS_SAMPLE).clearSpanHandlers().addSpanHandler(new SpanHandler() {

        @Override
        public boolean end(TraceContext context, MutableSpan span, Cause cause) {
            Throwable error = span.error();
            if (error != null) {
                caughtThrowables.add(error);
            } else {
                caughtThrowables.add(new RuntimeException("Unexpected additional call to end"));
            }
            return true;
        }
    }).addSpanHandler(testSpanHandler).build());
    client = newClient(server.getPort());
    // If this passes, a span was reported with an error
    checkReportsSpanOnTransportException(get);
    assertThat(caughtThrowables).withFailMessage("Span finished with error, but caughtThrowables empty").isNotEmpty();
    if (caughtThrowables.size() > 1) {
        for (Throwable throwable : caughtThrowables) {
            Logger.getAnonymousLogger().log(Level.SEVERE, "multiple calls to finish", throwable);
        }
        assertThat(caughtThrowables).hasSize(1);
    }
}
Also used : MutableSpan(brave.handler.MutableSpan) TraceContext(brave.propagation.TraceContext) SpanHandler(brave.handler.SpanHandler) ConcurrentLinkedDeque(java.util.concurrent.ConcurrentLinkedDeque)

Example 3 with SpanHandler

use of brave.handler.SpanHandler in project brave by openzipkin.

the class TracerTest method tracerThatPartitionsNamesOnlocalRootId.

Map<Long, List<String>> tracerThatPartitionsNamesOnlocalRootId() {
    Map<Long, List<String>> reportedNames = new LinkedHashMap<>();
    tracer = Tracing.newBuilder().addSpanHandler(new SpanHandler() {

        @Override
        public boolean end(TraceContext context, MutableSpan span, Cause cause) {
            assertThat(context.localRootId()).isNotZero();
            reportedNames.computeIfAbsent(context.localRootId(), k -> new ArrayList<>()).add(span.name());
            // retain
            return true;
        }
    }).alwaysSampleLocal().build().tracer();
    return reportedNames;
}
Also used : MutableSpan(brave.handler.MutableSpan) StrictCurrentTraceContext(brave.propagation.StrictCurrentTraceContext) CurrentTraceContext(brave.propagation.CurrentTraceContext) TraceContext(brave.propagation.TraceContext) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) TestSpanHandler(brave.test.TestSpanHandler) SpanHandler(brave.handler.SpanHandler) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with SpanHandler

use of brave.handler.SpanHandler in project brave by openzipkin.

the class TracingTest method spanHandler_dataChangesVisibleToZipkin.

@Test
public void spanHandler_dataChangesVisibleToZipkin() {
    String serviceNameOverride = "favistar";
    SpanHandler spanHandler = new SpanHandler() {

        @Override
        public boolean end(TraceContext context, MutableSpan span, Cause cause) {
            span.localServiceName(serviceNameOverride);
            return true;
        }
    };
    List<zipkin2.Span> zipkinSpans = new ArrayList<>();
    try (Tracing tracing = Tracing.newBuilder().spanReporter(zipkinSpans::add).addSpanHandler(spanHandler).build()) {
        tracing.tracer().newTrace().start().finish();
    }
    assertThat(zipkinSpans.get(0).localServiceName()).isEqualTo(serviceNameOverride);
}
Also used : MutableSpan(brave.handler.MutableSpan) ArrayList(java.util.ArrayList) StrictCurrentTraceContext(brave.propagation.StrictCurrentTraceContext) TraceContext(brave.propagation.TraceContext) TestSpanHandler(brave.test.TestSpanHandler) FinishedSpanHandler(brave.handler.FinishedSpanHandler) SpanHandler(brave.handler.SpanHandler) MutableSpan(brave.handler.MutableSpan) Test(org.junit.Test)

Example 5 with SpanHandler

use of brave.handler.SpanHandler in project brave by openzipkin.

the class TracingTest method spanHandler_multiple.

@Test
public void spanHandler_multiple() {
    SpanHandler one = new SpanHandler() {

        @Override
        public boolean end(TraceContext context, MutableSpan span, Cause cause) {
            return true;
        }
    };
    SpanHandler two = new SpanHandler() {

        @Override
        public boolean end(TraceContext context, MutableSpan span, Cause cause) {
            return true;
        }
    };
    try (Tracing tracing = Tracing.newBuilder().addSpanHandler(one).addSpanHandler(two).build()) {
        assertThat((Object) tracing.tracer().pendingSpans).extracting("spanHandler.delegate.handlers").asInstanceOf(InstanceOfAssertFactories.array(SpanHandler[].class)).containsExactly(one, two);
    }
}
Also used : MutableSpan(brave.handler.MutableSpan) StrictCurrentTraceContext(brave.propagation.StrictCurrentTraceContext) TraceContext(brave.propagation.TraceContext) TestSpanHandler(brave.test.TestSpanHandler) FinishedSpanHandler(brave.handler.FinishedSpanHandler) SpanHandler(brave.handler.SpanHandler) Test(org.junit.Test)

Aggregations

SpanHandler (brave.handler.SpanHandler)15 Test (org.junit.Test)11 MutableSpan (brave.handler.MutableSpan)8 TraceContext (brave.propagation.TraceContext)8 TestSpanHandler (brave.test.TestSpanHandler)6 StrictCurrentTraceContext (brave.propagation.StrictCurrentTraceContext)4 FinishedSpanHandler (brave.handler.FinishedSpanHandler)3 CurrentTraceContext (brave.propagation.CurrentTraceContext)2 ArrayList (java.util.ArrayList)2 ConcurrentLinkedDeque (java.util.concurrent.ConcurrentLinkedDeque)2 Clock (brave.Clock)1 ScopedSpan (brave.ScopedSpan)1 Tracing (brave.Tracing)1 Cause (brave.handler.SpanHandler.Cause)1 Nullable (brave.internal.Nullable)1 Arrays.asList (java.util.Arrays.asList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Before (org.junit.Before)1