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);
}
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;
}
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;
}
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");
}
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");
}
Aggregations