@Override public void request(long n) { try (CurrentTraceContext.Scope scope = this.currentTraceContext .maybeScope(this.span.context())) { this.parent.request(n); } }
ScopePassingSpanSubscriber(Subscriber<? super T> subscriber, Context ctx, Tracing tracing, @Nullable Span root) { this.subscriber = subscriber; this.currentTraceContext = tracing.currentTraceContext(); this.traceContext = root == null ? null : root.context(); this.context = ctx != null && root != null ? ctx.put(Span.class, root) : ctx != null ? ctx : Context.empty(); if (log.isTraceEnabled()) { log.trace("Root span [" + root + "], context [" + this.context + "]"); } }
/** * Like {@link #handleSend(TraceContext.Injector, Object, Object)}, except explicitly controls the * span representing the request. * * @since 4.4 */ public <C> Span handleSend(TraceContext.Injector<C> injector, C carrier, Req request, Span span) { injector.inject(span.context(), carrier); return handleStart(request, span); }
@Override public void onSubscribe(Subscription subscription) { this.parent = subscription; try (CurrentTraceContext.Scope scope = this.currentTraceContext .maybeScope(this.span.context())) { this.actual.onSubscribe(this); } }
@Override public void onNext(Object o) { try (CurrentTraceContext.Scope scope = this.currentTraceContext .maybeScope(this.span.context())) { this.actual.onNext(o); } }
@Override public TraceContext getTraceContext() { brave.Tracer tracer = brave.Tracing.currentTracer(); if (tracer != null) { Span span = tracer.currentSpan(); if (span != null) { return new BraveTraceContext(span.context()); } } return null; }
@Override public void onCompletion(Message message) { try (Scope ws = current.maybeScope(span.context())) { delegate.onCompletion(message); } finally { span.finish(); } }
@Override public boolean isSampled(Span span) { // TODO replace with Span#unwrap once https://github.com/opentracing/opentracing-java/pull/211 is merged if (span instanceof SpanWrapper) { span = ((SpanWrapper) span).getDelegate(); } if (span instanceof BraveSpan) { final BraveSpan braveSpan = (BraveSpan) span; return braveSpan.unwrap().context().sampled(); } return false; }
@Override public boolean isRoot(Span span) { // TODO replace with Span#unwrap once https://github.com/opentracing/opentracing-java/pull/211 is merged if (span instanceof SpanWrapper) { span = ((SpanWrapper) span).getDelegate(); } if (span instanceof BraveSpan) { final BraveSpan braveSpan = (BraveSpan) span; return braveSpan.unwrap().context().parentId() == null; } return false; }
@Override public void onComplete() { try (CurrentTraceContext.Scope scope = this.currentTraceContext .maybeScope(this.span.context())) { this.actual.onComplete(); } finally { this.processor.after(this.span, this.isNewSpan, this.log, this.hasLog); } }
@Override public void cancel() { try (CurrentTraceContext.Scope scope = this.currentTraceContext .maybeScope(this.span.context())) { this.parent.cancel(); } finally { this.processor.after(this.span, this.isNewSpan, this.log, this.hasLog); } }
@Override public void onCompletion(RecordMetadata metadata, @Nullable Exception exception) { try (Scope ws = current.maybeScope(span.context())) { delegate.onCompletion(metadata, exception); } finally { super.onCompletion(metadata, exception); } } }
@Override public void onError(Throwable error) { try (CurrentTraceContext.Scope scope = this.currentTraceContext .maybeScope(this.span.context())) { this.processor.onFailure(this.span, this.log, this.hasLog, error); this.actual.onError(error); } finally { this.processor.after(this.span, this.isNewSpan, this.log, this.hasLog); } }
@Override public void process(HttpRequest request, HttpContext context) { HttpHost host = HttpClientContext.adapt(context).getTargetHost(); TraceContext parent = (TraceContext) context.getAttribute(TraceContext.class.getName()); Span span; try (Scope scope = currentTraceContext.maybeScope(parent)) { span = handler.nextSpan(request); } HttpRequestWrapper requestWrapper = HttpRequestWrapper.wrap(request, host); parseTargetAddress(requestWrapper, span); handler.handleSend(injector, request, requestWrapper, span); context.setAttribute(Span.class.getName(), span); context.setAttribute(Scope.class.getName(), currentTraceContext.newScope(span.context())); } }
Span handleStart(Req request, Span span) { if (span.isNoop()) return span; Scope ws = currentTraceContext.maybeScope(span.context()); try { parseRequest(request, span); } finally { ws.close(); } // all of the above parsing happened before a timestamp on the span return span.start(); }
@Override public Mono<TraceContext> getTraceContextLater() { return Mono.subscriberContext() .filter(it -> it.hasKey(Span.class) || it.hasKey(brave.propagation.TraceContext.class)).map(it -> { if (it.hasKey(Span.class)) { return new BraveTraceContext(it.get(Span.class).context()); } return new BraveTraceContext(it.get(brave.propagation.TraceContext.class)); }); } }
@Override public void onMessage(Message message, Session session) throws JMSException { Span span = this.jmsTracing.nextSpan(message).name("on-message").start(); try (CurrentTraceContext.Scope ws = this.current.newScope(span.context())) { super.onMessage(message, session); } catch (JMSException | RuntimeException | Error e) { span.error(e); throw e; } finally { span.finish(); } }
void handleFinish(@Nullable Resp response, @Nullable Throwable error, Span span) { if (span.isNoop()) return; try { Scope ws = currentTraceContext.maybeScope(span.context()); try { parser.response(adapter, response, error, span.customizer()); } finally { ws.close(); // close the scope before finishing the span } } finally { finishInNullScope(span); } }
void handleReceive(Message message) { if (message == null || tracing.isNoop()) return; // remove prior propagation headers from the message TraceContextOrSamplingFlags extracted = jmsTracing.extractAndClearMessage(message); Span span = tracer.nextSpan(extracted); if (!span.isNoop()) { span.name("receive").kind(Span.Kind.CONSUMER); Destination destination = destination(message); if (destination != null) jmsTracing.tagQueueOrTopic(destination, span); if (remoteServiceName != null) span.remoteServiceName(remoteServiceName); // incur timestamp overhead only once long timestamp = tracing.clock(span.context()).currentTimeMicroseconds(); span.start(timestamp).finish(timestamp); } jmsTracing.setNextParent(message, span.context()); }
Span startMessageListenerSpan(Message message) { if (!addConsumerSpan) return jmsTracing.nextSpan(message).name("on-message").start(); TraceContextOrSamplingFlags extracted = jmsTracing.extractAndClearMessage(message); // JMS has no visibility of the incoming message, which incidentally could be local! Span consumerSpan = tracer.nextSpan(extracted).kind(CONSUMER).name("receive"); Span listenerSpan = tracer.newChild(consumerSpan.context()); if (!consumerSpan.isNoop()) { long timestamp = tracing.clock(consumerSpan.context()).currentTimeMicroseconds(); consumerSpan.start(timestamp); if (remoteServiceName != null) consumerSpan.remoteServiceName(remoteServiceName); jmsTracing.tagQueueOrTopic(message, consumerSpan); long consumerFinish = timestamp + 1L; // save a clock reading consumerSpan.finish(consumerFinish); // not using scoped span as we want to start late listenerSpan.name("on-message").start(consumerFinish); } return listenerSpan; } }