Search in sources :

Example 1 with MutableSpan

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

the class Tracer method newScopedSpan.

ScopedSpan newScopedSpan(@Nullable TraceContext parent, TraceContext context, String name) {
    Scope scope = currentTraceContext.newScope(context);
    if (isNoop(context))
        return new NoopScopedSpan(context, scope);
    PendingSpan pendingSpan = pendingSpans.getOrCreate(parent, context, true);
    Clock clock = pendingSpan.clock();
    MutableSpan state = pendingSpan.state();
    state.name(name);
    return new RealScopedSpan(context, scope, state, clock, pendingSpans);
}
Also used : MutableSpan(brave.handler.MutableSpan) Scope(brave.propagation.CurrentTraceContext.Scope) PendingSpan(brave.internal.recorder.PendingSpan)

Example 2 with MutableSpan

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

the class PendingSpans method getOrCreate.

public PendingSpan getOrCreate(@Nullable TraceContext parent, TraceContext context, boolean start) {
    PendingSpan result = get(context);
    if (result != null)
        return result;
    MutableSpan span = new MutableSpan(context, defaultSpan);
    PendingSpan parentSpan = parent != null ? get(parent) : null;
    // save overhead calculating time if the parent is in-progress (usually is)
    TickClock clock;
    if (parentSpan != null) {
        TraceContext parentContext = parentSpan.context();
        if (parentContext != null)
            parent = parentContext;
        clock = parentSpan.clock;
        if (start)
            span.startTimestamp(clock.currentTimeMicroseconds());
    } else {
        long currentTimeMicroseconds = this.clock.currentTimeMicroseconds();
        clock = new TickClock(currentTimeMicroseconds, System.nanoTime());
        if (start)
            span.startTimestamp(currentTimeMicroseconds);
    }
    PendingSpan newSpan = new PendingSpan(context, span, clock);
    // Probably absent because we already checked with get() at the entrance of this method
    PendingSpan previousSpan = putIfProbablyAbsent(context, newSpan);
    // lost race
    if (previousSpan != null)
        return previousSpan;
    // We've now allocated a new trace context.
    assert parent != null || context.isLocalRoot() : "Bug (or unexpected call to internal code): parent can only be null in a local root!";
    spanHandler.begin(newSpan.handlerContext, newSpan.span, parentSpan != null ? parentSpan.handlerContext : null);
    return newSpan;
}
Also used : MutableSpan(brave.handler.MutableSpan) TraceContext(brave.propagation.TraceContext)

Example 3 with MutableSpan

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

the class LazySpan method span.

/**
 * This does not guard on all concurrent edge cases assigning the delegate field. That's because
 * this type is only used when a user calls {@link Tracer#currentSpan()}, which is unlikley to be
 * exposed in such a way that multiple threads end up in a race assigning the field. Finally,
 * there is no state risk if {@link Tracer#toSpan(TraceContext)} is called concurrently. Duplicate
 * instances of span may occur, but they would share the same {@link MutableSpan} instance
 * internally.
 */
Span span() {
    Span result = delegate;
    if (result != null)
        return result;
    delegate = tracer.toSpan(context);
    context = delegate.context();
    return delegate;
}
Also used : MutableSpan(brave.handler.MutableSpan)

Example 4 with MutableSpan

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

the class ITRemoteTest method tracer_includesClassName.

@Test
public void tracer_includesClassName() {
    ITRemoteDummy itRemote = new ITRemoteDummy();
    itRemote.tracing.tracer().newTrace().start(1L).finish(1L);
    MutableSpan span = itRemote.testSpanHandler.takeLocalSpan();
    assertThat(span.localServiceName()).isEqualTo(// much better than "unknown"
    "ITRemoteDummy");
}
Also used : MutableSpan(brave.handler.MutableSpan) Test(org.junit.Test)

Example 5 with MutableSpan

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

the class ITHttpClient method redirect.

@Test
public void redirect() throws IOException {
    server.enqueue(new MockResponse().setResponseCode(302).addHeader("Location: " + url("/bar")));
    // hehe to a bad location!
    server.enqueue(new MockResponse().setResponseCode(404));
    TraceContext parent = newTraceContext(SamplingFlags.SAMPLED);
    try (Scope scope = currentTraceContext.newScope(parent)) {
        get(client, "/foo");
    } catch (RuntimeException e) {
    // some clients raise 404 as an exception such as HttpClientError
    }
    MutableSpan initial = testSpanHandler.takeRemoteSpan(CLIENT);
    MutableSpan redirected = testSpanHandler.takeRemoteSpanWithErrorTag(CLIENT, "404");
    for (MutableSpan child : Arrays.asList(initial, redirected)) {
        assertChildOf(child, parent);
    }
    assertSequential(initial, redirected);
    assertThat(initial.tags().get("http.path")).isEqualTo("/foo");
    assertThat(redirected.tags().get("http.path")).isEqualTo("/bar");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MutableSpan(brave.handler.MutableSpan) Scope(brave.propagation.CurrentTraceContext.Scope) TraceContext(brave.propagation.TraceContext) Test(org.junit.Test)

Aggregations

MutableSpan (brave.handler.MutableSpan)141 Test (org.junit.Test)107 TraceContext (brave.propagation.TraceContext)36 KafkaStreams (org.apache.kafka.streams.KafkaStreams)28 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)28 Topology (org.apache.kafka.streams.Topology)28 Scope (brave.propagation.CurrentTraceContext.Scope)14 ProcessorContext (org.apache.kafka.streams.processor.ProcessorContext)12 KeyValue (org.apache.kafka.streams.KeyValue)9 ArrayList (java.util.ArrayList)8 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)8 MessagingTracing (brave.messaging.MessagingTracing)7 FileNotFoundException (java.io.FileNotFoundException)7 Arrays (java.util.Arrays)7 List (java.util.List)7 CONSUMER (brave.Span.Kind.CONSUMER)6 PRODUCER (brave.Span.Kind.PRODUCER)6 SpanHandler (brave.handler.SpanHandler)6 KafkaTracing (brave.kafka.clients.KafkaTracing)6 KAFKA_STREAMS_FILTERED_TAG (brave.kafka.streams.KafkaStreamsTags.KAFKA_STREAMS_FILTERED_TAG)6