Refine search
/** * 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(); } }
@RequestMapping("/traced") public String traced() throws InterruptedException { Span span = this.tracer.nextSpan().name("http:customTraceEndpoint").start(); int millis = this.random.nextInt(1000); log.info(String.format("Sleeping for [%d] millis", millis)); Thread.sleep(millis); this.tracer.currentSpan().tag("random-sleep-millis", String.valueOf(millis)); String s = this.restTemplate .getForObject("http://localhost:" + this.port + "/call", String.class); span.finish(); return "traced/" + s; }
private Span startOrContinueRenamedSpan(String spanName) { Span currentSpan = this.tracer.currentSpan(); if (currentSpan != null) { return currentSpan.name(spanName); } return this.tracer.nextSpan().name(spanName); }
/** * 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(); }
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; } }
final Span span; if (kind.equals(Kind.CLIENT)) { span = tracer.nextSpan(); injector.inject(span.context(), invocation.getAttachments()); } else { TraceContextOrSamplingFlags extracted = extractor.extract(invocation.getAttachments()); span = extracted.context() != null ? tracer.joinSpan(extracted.context()) : tracer.nextSpan(extracted); if (!span.isNoop()) { span.kind(kind); String service = invoker.getInterface().getSimpleName(); String method = RpcUtils.getMethodName(invocation); try (Tracer.SpanInScope scope = tracer.withSpanInScope(span)) { Result result = invoker.invoke(invocation); if (result.hasException()) {
span = tracer.nextSpan(kafkaTracing.extractAndClearHeaders(record.headers())); } else { span = tracer.newChild(maybeParent); if (!span.isNoop()) { span.kind(Span.Kind.PRODUCER).name("send"); if (remoteServiceName != null) span.remoteServiceName(remoteServiceName); if (record.key() instanceof String && !"".equals(record.key())) { injector.inject(span.context(), record.headers()); try (Tracer.SpanInScope ws = tracer.withSpanInScope(span)) { return delegate.send(record, TracingCallback.create(callback, span, current)); } catch (RuntimeException | Error e) {
@WebMethod public Collection< Book > getBooks() { final Span span = brave.tracer().nextSpan().name("Get Books").start(); try (SpanInScope scope = brave.tracer().withSpanInScope(span)) { return Arrays.asList( new Book("Apache CXF in Action", UUID.randomUUID().toString()), new Book("Mastering Apache CXF", UUID.randomUUID().toString()) ); } finally { span.finish(); } }
TraceContextOrSamplingFlags extracted = this.extractor.extract(headers); Span consumerSpan = this.tracer.nextSpan(extracted); if (!consumerSpan.isNoop()) { consumerSpan.kind(Span.Kind.CONSUMER).start(); consumerSpan.remoteServiceName(REMOTE_SERVICE_NAME); addTags(message, consumerSpan, channel); this.tracing.propagation().keys()); if (log.isDebugEnabled()) { log.debug("Created a new span in before handle" + consumerSpan);
@Override public void send(Message message) throws JMSException { Span span = createAndStartProducerSpan(null, message); SpanInScope ws = tracer.withSpanInScope(span); // animal-sniffer mistakes this for AutoCloseable try { delegate.send(message); } catch (RuntimeException | JMSException | Error e) { span.error(e); throw e; } finally { ws.close(); span.finish(); } }
@Test public void testThatProvidedSpanIsNotDetachedWhenActiveUsingAsyncClient() throws Exception { final WebClient client = createWebClient("/bookstore/books", braveClientProvider); final Span span = brave.tracer().nextSpan().name("test span").start(); try { try (SpanInScope scope = brave.tracer().withSpanInScope(span)) { final Future<Response> f = client.async().get(); final Response r = f.get(1, TimeUnit.SECONDS); assertEquals(Status.OK.getStatusCode(), r.getStatus()); assertThat(brave.tracer().currentSpan().context().spanId(), equalTo(span.context().spanId())); assertThat(TestSpanReporter.getAllSpans().size(), equalTo(3)); assertThat(TestSpanReporter.getAllSpans().get(0).name(), equalTo("get books")); assertThat(TestSpanReporter.getAllSpans().get(1).name(), equalTo("get /bookstore/books")); assertThat(TestSpanReporter.getAllSpans().get(2).name(), equalTo("get " + client.getCurrentURI())); assertThatTraceHeadersArePresent(r, true); } } finally { span.finish(); } // Await till flush happens, usually a second is enough await().atMost(Duration.ONE_SECOND).until(()-> TestSpanReporter.getAllSpans().size() == 4); assertThat(TestSpanReporter.getAllSpans().size(), equalTo(4)); assertThat(TestSpanReporter.getAllSpans().get(3).name(), equalTo("test span")); }
@Override public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(final ServerCall<ReqT, RespT> call, final Metadata headers, final ServerCallHandler<ReqT, RespT> next) { TraceContextOrSamplingFlags extracted = extractor.extract(headers); Span span = extracted.context() != null ? tracer.joinSpan(extracted.context()) : tracer.nextSpan(extracted); // If grpc propagation is enabled, make sure we refresh the server method if (grpcPropagationFormatEnabled) { Tags tags = span.context().findExtra(Tags.class); if (tags != null) tags.put(RPC_METHOD, call.getMethodDescriptor().getFullMethodName()); } span.kind(Span.Kind.SERVER); parser.onStart(call, headers, span.customizer()); // startCall invokes user interceptors, so we place the span in scope here ServerCall.Listener<ReqT> result; SpanInScope scope = tracer.withSpanInScope(span); try { // retrolambda can't resolve this try/finally result = next.startCall(new TracingServerCall<>(span, call, parser), headers); } catch (RuntimeException | Error e) { span.error(e); span.finish(); throw e; } finally { scope.close(); } // This ensures the server implementation can see the span in scope return new ScopingServerCallListener<>(tracer, span, result, parser); }
@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(); } }
if (records.isEmpty() || tracing.isNoop()) return records; long timestamp = 0L; Map<String, Span> consumerSpansForTopic = new LinkedHashMap<>(); Span span = consumerSpansForTopic.get(topic); if (span == null) { span = tracing.tracer().nextSpan(extracted); if (!span.isNoop()) { setConsumerSpan(topic, span); timestamp = tracing.clock(span.context()).currentTimeMicroseconds(); span.start(timestamp); injector.inject(span.context(), record.headers()); } else { // we extracted request-scoped data, so cannot share a consumer span. Span span = tracing.tracer().nextSpan(extracted); if (!span.isNoop()) { setConsumerSpan(topic, span);
SpanSubscriber(Subscriber<? super T> subscriber, Context ctx, Tracing tracing, String name) { this.subscriber = subscriber; this.tracer = tracing.tracer(); Span root = ctx.getOrDefault(Span.class, this.tracer.currentSpan()); if (log.isTraceEnabled()) { log.trace("Span from context [{}]", root); } this.rootSpan = root; if (log.isTraceEnabled()) { log.trace("Stored context root span [{}]", this.rootSpan); } this.span = root != null ? this.tracer .nextSpan(TraceContextOrSamplingFlags.create(root.context())).name(name) : this.tracer.nextSpan().name(name); if (log.isTraceEnabled()) { log.trace("Created span [{}], with name [{}]", this.span, name); } this.context = ctx.put(Span.class, this.span); }
@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); } } }