public HttpServerTracingHandler newServerHandler() { if (!enabled()) { return null; } HttpServerTracingDispatch state = new HttpServerTracingDispatch(name, tracer); return new HttpServerTracingHandler(state); }
@Override public void channelRead0(ChannelHandlerContext ctx, Request request) throws Exception { if (request.startOfMessage()) { state.onRequest(ctx, request); } ctx.fireChannelRead(request); }
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { if (!(msg instanceof Response)) { ctx.write(msg, promise); return; } Response response = (Response) msg; if (response.endOfMessage()) { ctx.write(msg, promise) .addListener(future -> state.onResponse(ctx, response, future.cause())); } else { ctx.write(msg, promise); } } }
public void onRequest(ChannelHandlerContext ctx, Request request) { String requestorIpAddress = remoteIp(ctx); if (request.headers().get("x-remote-ip") != null) { requestorIpAddress = request.headers().get("x-remote-ip"); .withTag("http.request.type", httpType) .withTag("http.request.streamId", request.streamId()) .withTag("http.request.source-ip-address", localIp(ctx)) .withTag("http.request.originating-ip-address", requestorIpAddress); setSpan(ctx, request.streamId(), scope.span()); request.httpTraceInfo().setSpan(scope.span());
public void onResponse(ChannelHandlerContext ctx, Response response, Throwable error) { popSpan(ctx, response.streamId()) .ifPresent( span -> { int responseCode = response.status().code(); span.setTag(Tags.HTTP_STATUS.getKey(), responseCode); span.setTag("http.response.streamId", response.streamId()); StreamSupport.stream(response.headers().spliterator(), false) .forEach( (entry) -> { span.setTag( "http.response.headers." + entry.getKey().toString(), entry.getValue().toString()); }); span.finish(); response.httpTraceInfo().setSpan(span); }); } }
@Before public void setup() throws Exception { httpTracing = HttpTracing.create(tracingBuilder(Sampler.ALWAYS_SAMPLE).build()); BraveTracer braveTracer = BraveTracer.create(httpTracing.tracing()); httpTracingState = new HttpServerTracingDispatch("test", braveTracer); }