private HttpEntity getHttpEntityWithUserIdHeader() { HttpHeaders headers = new HttpHeaders(); String userId = Tracer.getInstance().getCurrentSpan().getUserId(); if (userIdHeaderKeys == null || userIdHeaderKeys.isEmpty() || userId == null) { return new HttpEntity(headers); } headers.set(userIdHeaderKeys.get(0), userId); return new HttpEntity(headers); }
@Test public void linkTracingAndMdcToCurrentThread_should_clear_mdc_if_state_is_available_but_state_mdc_info_is_null() { // given MDC.put("foo", "bar"); Tracer.getInstance().startRequestWithRootSpan("blahtrace"); assertThat(MDC.getCopyOfContextMap().isEmpty(), is(false)); assertThat(Tracer.getInstance().getCurrentSpan(), notNullValue()); // when handler.linkTracingAndMdcToCurrentThread(ctxMock); // then assertThat(MDC.getCopyOfContextMap().isEmpty(), is(true)); assertThat(Tracer.getInstance().getCurrentSpanStackCopy(), nullValue()); }
private HttpEntity getHttpEntityWithUserIdHeader() { HttpHeaders headers = new HttpHeaders(); String userId = Tracer.getInstance().getCurrentSpan().getUserId(); if (userIdHeaderKeys.isEmpty() || userId == null) { return new HttpEntity(headers); } headers.set(userIdHeaderKeys.get(0), userId); return new HttpEntity(headers); }
public static ChannelHandlerContextMocks mockChannelHandlerContextWithTraceInfo(String userId) { if (Tracer.getInstance().getCurrentSpan() == null) { Tracer.getInstance().startRequestWithRootSpan("mockChannelHandlerContext", userId); } ChannelHandlerContextMocks channelHandlerMocks = mockChannelHandlerContext(); when(channelHandlerMocks.mockHttpProcessingState.getLoggerMdcContextMap()).thenReturn(MDC.getCopyOfContextMap()); when(channelHandlerMocks.mockHttpProcessingState.getDistributedTraceStack()).thenReturn(Tracer.getInstance().getCurrentSpanStackCopy()); return channelHandlerMocks; }
@GetMapping(path = SPAN_INFO_CALL_PATH) @SuppressWarnings("unused") public EndpointSpanInfoDto getSpanInfoCall(HttpServletRequest request) { logger.info("Span info endpoint hit. Sleeping..."); sleepThread(SLEEP_TIME_MILLIS); return new EndpointSpanInfoDto(request, Tracer.getInstance().getCurrentSpan(), userIdHeaderKeys); }
@GetMapping(path = SPAN_INFO_CALL_PATH) @SuppressWarnings("unused") public EndpointSpanInfoDto getSpanInfoCall(HttpServletRequest request) { logger.info("Span info endpoint hit. Sleeping..."); sleepThread(SLEEP_TIME_MILLIS); return new EndpointSpanInfoDto(request, Tracer.getInstance().getCurrentSpan(), userIdHeaderKeys); }
protected String extractDistributedTraceId(RequestInfo requestInfo, ChannelHandlerContext ctx) { String traceId = (requestInfo == null) ? null : requestInfo.getHeaders().get(TraceHeaders.TRACE_ID); if (traceId == null) { traceId = supplierWithTracingAndMdc( () -> { Span currentSpanFromTracer = Tracer.getInstance().getCurrentSpan(); if (currentSpanFromTracer != null) return currentSpanFromTracer.getTraceId(); return null; }, ctx ).get(); } return traceId; }
protected String extractDistributedTraceId(RequestInfo requestInfo, ChannelHandlerContext ctx) { String traceId = (requestInfo == null) ? null : requestInfo.getHeaders().get(TraceHeaders.TRACE_ID); if (traceId == null) { traceId = supplierWithTracingAndMdc( () -> { Span currentSpanFromTracer = Tracer.getInstance().getCurrentSpan(); if (currentSpanFromTracer != null) return currentSpanFromTracer.getTraceId(); return null; }, ctx ).get(); } return traceId; }
protected CloseableHttpResponse propagateHeadersAndExecute( HttpRoute route, HttpRequestWrapper request, HttpClientContext clientContext, HttpExecutionAware execAware ) throws IOException, HttpException { propagateTracingHeaders(request, Tracer.getInstance().getCurrentSpan()); return protocolExec.execute(route, request, clientContext, execAware); } };
@Override public CloseableHttpResponse execute(HttpRoute route, HttpRequestWrapper request, HttpClientContext clientContext, HttpExecutionAware execAware) throws IOException, HttpException { capturedSpan = Tracer.getInstance().getCurrentSpan(); doReturn(statusLineMock).when(response).getStatusLine(); if (exceptionToThrow != null) { throw exceptionToThrow; } // Return a graceful 500 from the response doReturn(500).when(statusLineMock).getStatusCode(); return response; } }
/** * Calls {@link WingtipsSpringUtil#propagateTracingHeaders(HttpMessage, Span)} to propagate the current span's * tracing state on the given request's headers, then returns * {@link ClientHttpRequestExecution#execute(HttpRequest, byte[])} to execute the request. * * @return The result of calling {@link ClientHttpRequestExecution#execute(HttpRequest, byte[])}. */ protected ClientHttpResponse propagateTracingHeadersAndExecuteRequest( HttpRequestWrapperWithModifiableHeaders wrapperRequest, byte[] body, ClientHttpRequestExecution execution ) throws IOException { propagateTracingHeaders(wrapperRequest, Tracer.getInstance().getCurrentSpan()); // Execute the request/interceptor chain. return execution.execute(wrapperRequest, body); }
/** * Calls {@link WingtipsSpringUtil#propagateTracingHeaders(HttpMessage, Span)} to propagate the current span's * tracing state on the given request's headers, then returns * {@link AsyncClientHttpRequestExecution#executeAsync(HttpRequest, byte[])} to execute the request. * * @return The result of calling {@link AsyncClientHttpRequestExecution#executeAsync(HttpRequest, byte[])}. */ protected ListenableFuture<ClientHttpResponse> propagateTracingHeadersAndExecute( HttpRequestWrapperWithModifiableHeaders wrapperRequest, byte[] body, AsyncClientHttpRequestExecution execution ) throws IOException { propagateTracingHeaders(wrapperRequest, Tracer.getInstance().getCurrentSpan()); // Execute the request/interceptor chain. return execution.executeAsync(wrapperRequest, body); }
@Test public void getCurrentSpan_should_return_current_span() throws Exception { // given Tracer tracer = Tracer.getInstance(); tracer.startRequestWithRootSpan("test-span"); // when Span span = tracer.getCurrentSpan(); // then assertThat(span).isNotNull(); assertThat(span.getSpanName()).isEqualTo("test-span"); }
@Test public void close_completes_the_span_as_expected_overall_request_span() { // given Span overallSpan = Tracer.getInstance().startRequestWithRootSpan("root"); assertThat(Tracer.getInstance().getCurrentSpan()).isSameAs(overallSpan); assertThat(overallSpan.isCompleted()).isFalse(); // when overallSpan.close(); // then assertThat(overallSpan.isCompleted()).isTrue(); assertThat(Tracer.getInstance().getCurrentSpan()).isNull(); }
@Test public void handleSpanCloseMethod_completes_the_span_as_expected_overall_request_span() { // given Span overallSpan = Tracer.getInstance().startRequestWithRootSpan("root"); assertThat(Tracer.getInstance().getCurrentSpan()).isSameAs(overallSpan); assertThat(overallSpan.isCompleted()).isFalse(); // when Tracer.getInstance().handleSpanCloseMethod(overallSpan); // then assertThat(overallSpan.isCompleted()).isTrue(); assertThat(Tracer.getInstance().getCurrentSpan()).isNull(); }
@Test public void completeSubSpan_should_do_nothing_if_the_span_stack_is_null() { // given: a null span stack assertThat(Tracer.getInstance().getCurrentSpan()).isNull(); assertThat(getSpanStackSize()).isEqualTo(0); assertThat(getSpanStackFromTracer()).isNull(); // when: completeSubSpan() is called Tracer.getInstance().completeSubSpan(); // then: nothing should be done because the stack is null assertThat(Tracer.getInstance().getCurrentSpan()).isNull(); assertThat(getSpanStackSize()).isEqualTo(0); assertThat(getSpanStackFromTracer()).isNull(); }
@Override public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { capturedSpan = Tracer.getInstance().getCurrentSpan(); captureSpanCopyAtTimeOfDoFilter = Span.newBuilder(capturedSpan).build(); } }
@Test public void close_completes_the_span_as_expected_subspan() { // given Span parentSpan = Tracer.getInstance().startRequestWithRootSpan("root"); Span subspan = Tracer.getInstance().startSubSpan("subspan", SpanPurpose.LOCAL_ONLY); assertThat(Tracer.getInstance().getCurrentSpan()).isSameAs(subspan); assertThat(subspan.isCompleted()).isFalse(); // when subspan.close(); // then assertThat(subspan.isCompleted()).isTrue(); assertThat(Tracer.getInstance().getCurrentSpan()).isSameAs(parentSpan); }
@Test public void startRequestWithRootSpan_wipes_out_any_existing_spans_on_the_stack() { // given: Tracer already has some Spans on the stack Tracer.getInstance().startRequestWithRootSpan("span1"); Tracer.getInstance().startSubSpan("span2", SpanPurpose.LOCAL_ONLY); assertThat(getSpanStackSize()).isEqualTo(2); // when: Tracer.startRequestWithRootSpan(String) is called to start a span without a parent Tracer.getInstance().startRequestWithRootSpan("noparent"); // then: a new span is started for it, and the other spans on the stack are removed assertThat(getSpanStackSize()).isEqualTo(1); Span span = Tracer.getInstance().getCurrentSpan(); assertThat(span).isNotNull(); assertThat(span.getSpanName()).isEqualTo("noparent"); }
@Test public void handleSpanCloseMethod_completes_the_span_as_expected_subspan() { // given Span parentSpan = Tracer.getInstance().startRequestWithRootSpan("root"); Span subspan = Tracer.getInstance().startSubSpan("subspan", SpanPurpose.LOCAL_ONLY); assertThat(Tracer.getInstance().getCurrentSpan()).isSameAs(subspan); assertThat(subspan.isCompleted()).isFalse(); // when Tracer.getInstance().handleSpanCloseMethod(subspan); // then assertThat(subspan.isCompleted()).isTrue(); assertThat(Tracer.getInstance().getCurrentSpan()).isSameAs(parentSpan); }