TraceHttpRequestInterceptor { public TraceContext process(HttpRequest request) { Labels.Builder labels = Labels.builder(); TraceInterceptorUtil .annotateIfNotEmpty(labels, HttpLabels.HTTP_METHOD, request.getURI().toString()); labels.add(HttpLabels.HTTP_METHOD, request.getMethod()); TraceInterceptorUtil .annotateIfNotEmpty(labels, HttpLabels.HTTP_URL, request.getURI().toString()); TraceInterceptorUtil.annotateIfNotEmpty(labels, HttpLabels.HTTP_CLIENT_PROTOCOL, request.getProtocol()); TraceInterceptorUtil.annotateIfNotEmpty(labels, HttpLabels.HTTP_USER_AGENT, request.getHeader(HttpHeaders.USER_AGENT)); TraceInterceptorUtil.annotateIfNotEmpty(labels, HttpLabels.REQUEST_SIZE, request.getHeader(HttpHeaders.CONTENT_LENGTH)); TraceContext traceContext = tracer.startSpan(request.getURI().getPath()); tracer.annotateSpan(traceContext, labels.build()); return traceContext; } TraceHttpRequestInterceptor();  TraceHttpRequestInterceptor(Tracer tracer); TraceContext process(HttpRequest request);  }
@Test public void testProcess_WithNoHeaders() throws Exception { TraceContext traceContext = requestInterceptor.process(requestWithNoHeaders); assertThat(tracer.startSpanEvents).hasSize(1); StartSpanEvent startEvent = tracer.startSpanEvents.get(0); assertThat(startEvent.getName()).isEqualTo("/"); assertThat(traceContext).isEqualTo(startEvent.getTraceContext()); assertThat(tracer.annotateEvents).hasSize(1); AnnotateEvent annotateEvent = tracer.annotateEvents.get(0); assertThat(annotateEvent.getLabels().getLabels()).containsAllOf( new Label("/http/method", "GET"), new Label("/http/client_protocol", "HTTP") ); assertThat(annotateEvent.getTraceContext()).isEqualTo(startEvent.getTraceContext()); }
@Test public void testProcess_WithHeaders() throws Exception { TraceContext traceContext = requestInterceptor.process(requestWithHeaders); assertThat(tracer.startSpanEvents).hasSize(1); StartSpanEvent startEvent = tracer.startSpanEvents.get(0); assertThat(startEvent.getName()).isEqualTo("/foo/bar"); assertThat(traceContext).isEqualTo(startEvent.getTraceContext()); assertThat(tracer.annotateEvents).hasSize(1); AnnotateEvent annotateEvent = tracer.annotateEvents.get(0); assertThat(annotateEvent.getLabels().getLabels()).containsAllOf( new Label("/http/user_agent", "test-user-agent"), new Label("/request/size", "123456"), new Label("/http/method", "GET"), new Label("/http/client_protocol", "HTTP") ); assertThat(annotateEvent.getTraceContext()).isEqualTo(startEvent.getTraceContext()); }