/** * Returns a new child span if there's a {@link #currentSpan()} or a new trace if there isn't. * * <p>Prefer {@link #startScopedSpan(String)} if you are tracing a synchronous function or code * block. */ public Span nextSpan() { TraceContext parent = currentTraceContext.get(); return parent != null ? newChild(parent) : newTrace(); }
void newChildWithSpanInScope(Tracer tracer, TraceContext context) { Span span = tracer.newChild(context).name("encode").start(); try (Tracer.SpanInScope scope = tracer.withSpanInScope(span)) { span.tag("foo", "bar"); span.tag("baz", "qux"); } finally { span.finish(); } }
/** * MethodInterceptor for {@link SimpleMessageListenerContainer.ContainerDelegate#invokeListener(Channel, * Message)} */ @Override public Object invoke(MethodInvocation methodInvocation) throws Throwable { Message message = (Message) methodInvocation.getArguments()[1]; TraceContextOrSamplingFlags extracted = springRabbitTracing.extractAndClearHeaders(message); // named for BlockingQueueConsumer.nextMessage, which we can't currently see Span consumerSpan = tracer.nextSpan(extracted); Span listenerSpan = tracer.newChild(consumerSpan.context()); if (!consumerSpan.isNoop()) { setConsumerSpan(consumerSpan, message.getMessageProperties()); // incur timestamp overhead only once long timestamp = tracing.clock(consumerSpan.context()).currentTimeMicroseconds(); consumerSpan.start(timestamp); long consumerFinish = timestamp + 1L; // save a clock reading consumerSpan.finish(consumerFinish); // not using scoped span as we want to start with a pre-configured time listenerSpan.name("on-message").start(consumerFinish); } try (SpanInScope ws = tracer.withSpanInScope(listenerSpan)) { return methodInvocation.proceed(); } catch (Throwable t) { listenerSpan.error(t); throw t; } finally { listenerSpan.finish(); } }
if (!supportsJoin) return newChild(context); int flags = InternalPropagation.instance.flags(context); if (alwaysSampleLocal && (flags & FLAG_SAMPLED_LOCAL) != FLAG_SAMPLED_LOCAL) {
Span createAndStartProducerSpan(Destination destination, M message) { TraceContext maybeParent = current.get(); // Unlike message consumers, we try current span before trying extraction. This is the proper // order because the span in scope should take precedence over a potentially stale header entry. // // NOTE: Brave instrumentation used properly does not result in stale header entries, as we // always clear message headers after reading. Span span; if (maybeParent == null) { span = tracer.nextSpan(extractAndClearMessage(message)); } else { // As JMS is sensitive about write access to headers, we defensively clear even if it seems // upstream would have cleared (because there is a span in scope!). span = tracer.newChild(maybeParent); clearPropagationHeaders(message); } if (!span.isNoop()) { span.kind(Span.Kind.PRODUCER).name("send"); if (destination == null) destination = destination(message); if (destination != null) jmsTracing.tagQueueOrTopic(destination, span); if (remoteServiceName != null) span.remoteServiceName(remoteServiceName); span.start(); } addB3SingleHeader(message, span.context()); return span; }
} else { span = tracer.newChild(maybeParent);
@Override public Message postProcessMessage(Message message) { TraceContext maybeParent = currentTraceContext.get(); // Unlike message consumers, we try current span before trying extraction. This is the proper // order because the span in scope should take precedence over a potentially stale header entry. // // NOTE: Brave instrumentation used properly does not result in stale header entries, as we // always clear message headers after reading. Span span; if (maybeParent == null) { span = tracer.nextSpan(springRabbitTracing.extractAndClearHeaders(message)); } else { // If we have a span in scope assume headers were cleared before span = tracer.newChild(maybeParent); } if (!span.isNoop()) { span.kind(Span.Kind.PRODUCER).name("publish"); if (remoteServiceName != null) span.remoteServiceName(remoteServiceName); // incur timestamp overhead only once long timestamp = tracing.clock(span.context()).currentTimeMicroseconds(); span.start(timestamp).finish(timestamp); } injector.inject(span.context(), message.getMessageProperties()); return message; } }
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; } }
if (extracted == null) throw new NullPointerException("extracted == null"); TraceContext context = extracted.context(); if (context != null) return newChild(context);
private Span createSpan(String spanName, String path) { Span currentSpan = tracer.currentSpan(); if (currentSpan != null) { return tracer.newChild(currentSpan.context()).name(spanName).tag(CALL_PATH, path).start(); } return tracer.newTrace().name(spanName).tag(CALL_PATH, path).start(); }
/** * Returns a new child span if there's a {@link #currentSpan()} or a new trace if there isn't. * * <p>Prefer {@link #startScopedSpan(String)} if you are tracing a synchronous function or code * block. */ public Span nextSpan() { TraceContext parent = currentTraceContext.get(); return parent != null ? newChild(parent) : newTrace(); }
@Override Span nextSpan(@Nullable SpanId maybeParent) { brave.Span span = maybeParent != null ? tracer.newChild(toTraceContext(maybeParent)) : tracer.newTrace(); return Brave.toSpan(toSpanId(span.context())); }
private TraceScope newOrChildSpan(final String description, final Span parent) { if (parent == null) { return new TraceScope(brave, tracer.newTrace().name(description).start()); } return new TraceScope(brave, tracer.newChild(parent.context()).name(description).start()); } }
private TraceScope newOrChildSpan(final String description, final Span parent) { if (parent == null) { return new TraceScope(brave, tracer.newTrace().name(description).start()); } return new TraceScope(brave, tracer.newChild(parent.context()).name(description).start()); } }
Span span; if (last != null) { span = brave.tracer().newChild(last.context()); } else { span = brave.tracer().nextSpan();
private Span createSpan(String spanName, String path) { Span currentSpan = tracer.currentSpan(); if (currentSpan != null) { return tracer.newChild(currentSpan.context()).name(spanName).tag(CALL_PATH, path).start(); } return tracer.newTrace().name(spanName).tag(CALL_PATH, path).start(); }
private Span createSpan(String spanName, String path) { Span currentSpan = tracer.currentSpan(); if (currentSpan != null) { return tracer.newChild(currentSpan.context()).name(spanName).tag(CALL_PATH, path).start(); } return tracer.newTrace().name(spanName).tag(CALL_PATH, path).start(); }
@Override public void createChildSpan(TracingWrapper<ZipkinWrapper> parent) { operationName = UUID.randomUUID().toString().replace("-",""); if (parent == null) { // root node we start a new trace span = tracing.tracer().newTrace().name(operationName + "-root") .start(); } else { brave.Span parentClient = parent.get().tracing.tracer().newChild(parent.get().span.context()) .kind(Kind.CLIENT) .name(operationName + "-client") .start(); // TODO if I finish this later the span is cached // and joined with server span and reported as a single span. // to properly solve this we have to look into the tags. // However there is another problem jaeger adds only one span.kind // (even if span contains cs,cr,sr,ss) // And it filters out core annotations, so there is no way how to find out // that there is a dependency link in this span. // https://github.com/jaegertracing/jaeger/issues/451 parentClient.finish(); span = tracing.tracer().joinSpan(parentClient.context()) .name(operationName + "-server") .kind(Kind.SERVER) .start(); } }
} else { span = tracer.newChild(maybeParent);
@Override public void channelRead0(ChannelHandlerContext ctx, Request request) throws Exception { ByteBuf content = Unpooled.copiedBuffer("Here is the default content that is returned", CharsetUtil.UTF_8); HttpResponseStatus status = OK; Tracer tracer = httpTracing.tracing().tracer(); if (request.endOfMessage()) { request .httpTraceInfo() .getSpan() .ifPresent( parent -> { if (parent instanceof brave.Span) { Span span = tracer.newChild(((brave.Span) parent).context()).name("child").start(); span.finish(); } }); val response = DefaultFullResponse.builder() .status(status) .headers(new DefaultHeaders()) .httpTraceInfo(request.httpTraceInfo()) .status(status) .body(content) .build(); ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } } }