@Override public void sendMessage(RespT message) { super.sendMessage(message); parser.onMessageSent(message, span.customizer()); }
@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 void parseRequest(Req request, Span span) { span.kind(Span.Kind.SERVER); adapter.parseClientIpAndPort(request, span); parser.request(adapter, request, span.customizer()); }
@Override void parseRequest(Req request, Span span) { span.kind(Span.Kind.CLIENT); if (serverName != null) span.remoteServiceName(serverName); parser.request(adapter, request, span.customizer()); }
@Override public void close(Status status, Metadata trailers) { try { super.close(status, trailers); parser.onClose(status, trailers, span.customizer()); } catch (RuntimeException | Error e) { span.error(e); throw e; } finally { span.finish(); } } }
@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; } }
@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 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 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(); } }
request.setAttribute(SpanCustomizer.class.getName(), span.customizer()); request.setAttribute(TraceContext.class.getName(), span.context());
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); } }
@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 sendMessage(RespT message) { super.sendMessage(message); parser.onMessageSent(message, span.customizer()); }
@Override public void close(Status status, Metadata trailers) { try { super.close(status, trailers); parser.onClose(status, trailers, span.customizer()); } catch (RuntimeException | Error e) { span.error(e); throw e; } finally { span.finish(); } } }
@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(); } }
private void serverResponse(Tracing brave, String serviceName, Exchange exchange) { Span span = null; ZipkinState state = exchange.getProperty(ZipkinState.KEY, ZipkinState.class); if (state != null) { // only process if it was a zipkin server event span = state.popServerSpan(); } if (span != null) { ZipkinServerResponseAdapter parser = new ZipkinServerResponseAdapter(this, exchange); parser.onResponse(exchange, span.customizer()); span.finish(); TraceContext context = span.context(); String traceId = "" + context.traceIdString(); String spanId = "" + context.spanId(); String parentId = context.parentId() != null ? "" + context.parentId() : null; if (camelContext.isUseMDCLogging()) { MDC.put("traceId", traceId); MDC.put("spanId", spanId); MDC.put("parentId", parentId); } if (LOG.isDebugEnabled()) { if (parentId != null) { LOG.debug(String.format("serverResponse[service=%s, traceId=%20s, spanId=%20s, parentId=%20s]", serviceName, traceId, spanId, parentId)); } else { LOG.debug(String.format("serverResponse[service=%s, traceId=%20s, spanId=%20s]", serviceName, traceId, spanId)); } } } }
@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(); } }
@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(); } } }
private void clientResponse(Tracing brave, String serviceName, ExchangeSentEvent event) { Span span = null; ZipkinState state = event.getExchange().getProperty(ZipkinState.KEY, ZipkinState.class); if (state != null) { // only process if it was a zipkin client event span = state.popClientSpan(); } if (span != null) { ZipkinClientResponseAdaptor parser = new ZipkinClientResponseAdaptor(this, event.getEndpoint()); parser.onResponse(event.getExchange(), span.customizer()); span.finish(); TraceContext context = span.context(); String traceId = "" + context.traceIdString(); String spanId = "" + context.spanId(); String parentId = context.parentId() != null ? "" + context.parentId() : null; if (camelContext.isUseMDCLogging()) { MDC.put("traceId", traceId); MDC.put("spanId", spanId); MDC.put("parentId", parentId); } if (LOG.isDebugEnabled()) { if (parentId != null) { LOG.debug(String.format("clientResponse[service=%s, traceId=%20s, spanId=%20s, parentId=%20s]", serviceName, traceId, spanId, parentId)); } else { LOG.debug(String.format("clientResponse[service=%s, traceId=%20s, spanId=%20s]", serviceName, traceId, spanId)); } } } }
@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); }