@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(); } }
@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 channelRead(ChannelHandlerContext ctx, Object msg) { if (!(msg instanceof HttpRequest)) { ctx.fireChannelRead(msg); // superclass does not throw return; } HttpRequest request = (HttpRequest) msg; Span span = nextSpan(extractor.extract(request.headers()), request).kind(Span.Kind.SERVER); ctx.channel().attr(NettyHttpTracing.SPAN_ATTRIBUTE).set(span); SpanInScope spanInScope = tracer.withSpanInScope(span); ctx.channel().attr(NettyHttpTracing.SPAN_IN_SCOPE_ATTRIBUTE).set(spanInScope); // Place the span in scope so that downstream code can read trace IDs try { if (!span.isNoop()) { parseChannelAddress(ctx, request, span); parser.request(adapter, request, span.customizer()); span.start(); } ctx.fireChannelRead(msg); spanInScope.close(); } catch (RuntimeException | Error e) { spanInScope.close(); span.error(e).finish(); // the request abended, so finish the span; throw e; } }
final MethodDescriptor<ReqT, RespT> method, final CallOptions callOptions, final Channel next) { Span span = tracer.nextSpan(); SpanInScope scope = tracer.withSpanInScope(span); try { span.error(e).finish(); throw e; } finally { scope.close();
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise prm) { Span span = ctx.channel().attr(NettyHttpTracing.SPAN_ATTRIBUTE).get(); if (span == null || !(msg instanceof HttpResponse)) { ctx.write(msg, prm); return; } HttpResponse response = (HttpResponse) msg; // Guard re-scoping the same span SpanInScope spanInScope = ctx.channel().attr(NettyHttpTracing.SPAN_IN_SCOPE_ATTRIBUTE).get(); if (spanInScope == null) spanInScope = tracer.withSpanInScope(span); try { ctx.write(msg, prm); parser.response(adapter, response, null, span.customizer()); } catch (RuntimeException | Error e) { span.error(e); throw e; } finally { spanInScope.close(); // clear scope before reporting span.finish(); } } }
@Override public void onClose(Status status, Metadata trailers) { SpanInScope scope = tracer.withSpanInScope(span); try { super.onClose(status, trailers); parser.onClose(status, trailers, span.customizer()); } finally { scope.close(); span.finish(); } } }
@Override public void onCancel() { SpanInScope scope = tracer.withSpanInScope(span); try { // retrolambda can't resolve this try/finally delegate().onCancel(); } finally { scope.close(); } }
@Override public void onMessage(ReqT message) { SpanInScope scope = tracer.withSpanInScope(span); try { // retrolambda can't resolve this try/finally parser.onMessageReceived(message, span.customizer()); delegate().onMessage(message); } finally { scope.close(); } }
@Override public <T> T continueSpan(final Traceable<T> traceable) throws Exception { SpanInScope scope = null; if (tracer.currentSpan() == null && continuationSpan != null) { scope = tracer.withSpanInScope(continuationSpan); } try { return traceable.call(new BraveTracerContext(brave)); } finally { if (continuationSpan != null && scope != null) { scope.close(); } } }
@Override public void filter(ClientRequestContext request, ClientResponseContext response) { Span span = tracer.currentSpan(); if (span == null) return; ((Tracer.SpanInScope) request.getProperty(Tracer.SpanInScope.class.getName())).close(); handler.handleReceive(response, null, span); }
return new SpanInScope(currentTraceContext.newScope(span != null ? span.context() : null));
void handleReceive(Span clientSpan, Tracer.SpanInScope ws, ClientResponse clientResponse, Throwable throwable) { this.handler.handleReceive(clientResponse, throwable, clientSpan); ws.close(); }
void handleReceive(Span clientSpan, Tracer.SpanInScope ws, ClientResponse clientResponse, Throwable throwable) { this.handler.handleReceive(clientResponse, throwable, clientSpan); ws.close(); }
@Override public void close() { if (finishSpanOnClose) { wrapped.finish(); } scope.close(); source.deregister(this); }
@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); }
final MethodDescriptor<ReqT, RespT> method, final CallOptions callOptions, final Channel next) { Span span = tracer.nextSpan(); SpanInScope scope = tracer.withSpanInScope(span); try { span.error(e).finish(); throw e; } finally { scope.close();
@Override public void onClose(Status status, Metadata trailers) { SpanInScope scope = tracer.withSpanInScope(span); try { super.onClose(status, trailers); parser.onClose(status, trailers, span.customizer()); } finally { scope.close(); span.finish(); } } }
@Override public void onComplete() { SpanInScope scope = tracer.withSpanInScope(span); try { // retrolambda can't resolve this try/finally delegate().onComplete(); } finally { scope.close(); } }
@Override public void onMessage(RespT message) { SpanInScope scope = tracer.withSpanInScope(span); try { // retrolambda can't resolve this try/finally parser.onMessageReceived(message, span.customizer()); delegate().onMessage(message); } finally { scope.close(); } }