Search in sources :

Example 16 with Span

use of com.nike.wingtips.Span in project riposte by Nike-Inc.

the class BaseInboundHandlerWithTracingAndMdcSupportTest method unlinkTracingAndMdcFromCurrentThread_should_populate_state_and_reset_tracing_and_mdc_to_originalThreadInfo_if_state_is_not_null.

@Test
public void unlinkTracingAndMdcFromCurrentThread_should_populate_state_and_reset_tracing_and_mdc_to_originalThreadInfo_if_state_is_not_null() {
    // given
    // Orig thread info
    Deque<Span> origTraceStack = new LinkedList<>();
    Span origSpan = Span.newBuilder(UUID.randomUUID().toString(), LOCAL_ONLY).withTraceId(UUID.randomUUID().toString()).build();
    origTraceStack.add(origSpan);
    Map<String, String> origMdcInfo = new HashMap<>();
    origMdcInfo.put(UUID.randomUUID().toString(), UUID.randomUUID().toString());
    origMdcInfo.put(Tracer.TRACE_ID_MDC_KEY, origSpan.getTraceId());
    origMdcInfo.put(Tracer.SPAN_JSON_MDC_KEY, origSpan.toJSON());
    Pair<Deque<Span>, Map<String, String>> origThreadInfo = Pair.of(origTraceStack, origMdcInfo);
    // "Current" state
    MDC.put("foo", "bar");
    Tracer.getInstance().startRequestWithRootSpan(UUID.randomUUID().toString());
    Deque<Span> currentTraceStackBeforeUnlinkCall = Tracer.getInstance().getCurrentSpanStackCopy();
    Map<String, String> currentMdcInfoBeforeUnlinkCall = MDC.getCopyOfContextMap();
    // Verify we've set up our "given" section correctly
    assertThat(MDC.getCopyOfContextMap(), is(currentMdcInfoBeforeUnlinkCall));
    assertThat(MDC.get("foo"), is("bar"));
    assertThat(Tracer.getInstance().getCurrentSpanStackCopy(), is(currentTraceStackBeforeUnlinkCall));
    assertThat(state.getLoggerMdcContextMap(), nullValue());
    assertThat(state.getDistributedTraceStack(), nullValue());
    assertThat(origMdcInfo, not(currentMdcInfoBeforeUnlinkCall));
    assertThat(origTraceStack, not(currentTraceStackBeforeUnlinkCall));
    // when
    handler.unlinkTracingAndMdcFromCurrentThread(ctxMock, origThreadInfo);
    // then
    // The state should have the expected values from the time when the unlink method was called
    assertThat(state.getLoggerMdcContextMap(), is(currentMdcInfoBeforeUnlinkCall));
    assertThat(state.getDistributedTraceStack(), is(currentTraceStackBeforeUnlinkCall));
    // The "current" thread state after the unlink call should match the original thread info
    assertThat(MDC.getCopyOfContextMap(), is(origMdcInfo));
    assertThat(Tracer.getInstance().getCurrentSpanStackCopy(), is(origTraceStack));
}
Also used : HashMap(java.util.HashMap) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Span(com.nike.wingtips.Span) Deque(java.util.Deque) HashMap(java.util.HashMap) Map(java.util.Map) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 17 with Span

use of com.nike.wingtips.Span in project riposte by Nike-Inc.

the class BaseInboundHandlerWithTracingAndMdcSupport method unlinkTracingAndMdcFromCurrentThread.

protected void unlinkTracingAndMdcFromCurrentThread(ChannelHandlerContext ctx, Pair<Deque<Span>, Map<String, String>> origThreadInfo) {
    // Update the state (if we have any) with the current values of the MDC and tracer data
    HttpProcessingState state = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get();
    if (state != null) {
        // Get references to the *current* MDC and tracer data for storing in our ctx
        // and set them on the state object
        Map<String, String> currentMdcContextMapForState = MDC.getCopyOfContextMap();
        Deque<Span> currentTraceStackForState = Tracer.getInstance().unregisterFromThread();
        state.setLoggerMdcContextMap(currentMdcContextMapForState);
        state.setDistributedTraceStack(currentTraceStackForState);
    }
    // Reset the thread to the way it was before linkTracingAndMdcToCurrentThread was called
    AsyncNettyHelper.unlinkTracingAndMdcFromCurrentThread(origThreadInfo);
}
Also used : HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) Span(com.nike.wingtips.Span)

Example 18 with Span

use of com.nike.wingtips.Span in project riposte by Nike-Inc.

the class VerifyProxyRouterTracingBehaviorComponentTest method proxy_endpoint_tracing_behavior_should_work_as_desired_when_orig_request_does_not_have_tracing_headers.

@DataProvider(value = { "HEADERS_ON_SUBSPAN_ON", "HEADERS_OFF_SUBSPAN_ON", "HEADERS_ON_SUBSPAN_OFF", "HEADERS_OFF_SUBSPAN_OFF" })
@Test
public void proxy_endpoint_tracing_behavior_should_work_as_desired_when_orig_request_does_not_have_tracing_headers(TracingBehaviorScenario scenario) {
    ExtractableResponse response = given().baseUri("http://127.0.0.1").port(proxyServerConfig.endpointsPort()).basePath(RouterEndpoint.MATCHING_PATH).header(SET_TRACING_HEADERS_HEADER_KEY, scenario.tracingHeadersOn).header(PERFORM_SUBSPAN_HEADER_KEY, scenario.subspanOn).log().all().when().get().then().log().headers().extract();
    assertThat(response.statusCode()).isEqualTo(200);
    assertThat(response.asString()).isEqualTo(DownstreamEndpoint.RESPONSE_PAYLOAD);
    // Verify that the proxy honored the subspan option, and get a handle on the span that surrounded the proxy
    // downstream call.
    Span spanForDownstreamCall = verifyCompletedSpansAndReturnSpanForDownstreamCall(scenario.subspanOn);
    if (scenario.tracingHeadersOn) {
        // The downstream endpoint should have received tracing headers based on the span surrounding the proxy
        // downstream call.
        verifyExpectedTracingHeadersReceivedDownstream(response, spanForDownstreamCall);
    } else {
        // Proxy had the "set tracing headers" option off, and we didn't send any in our original request, so
        // the downstream endpoint should not have received *any* tracing headers.
        verifyExpectedTracingHeadersReceivedDownstream(response, null, null, null, null);
    }
}
Also used : ExtractableResponse(io.restassured.response.ExtractableResponse) Span(com.nike.wingtips.Span) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 19 with Span

use of com.nike.wingtips.Span in project riposte by Nike-Inc.

the class VerifyProxyRouterTracingBehaviorComponentTest method proxy_endpoint_tracing_behavior_should_work_as_desired_when_orig_request_does_have_tracing_headers.

@DataProvider(value = { "HEADERS_ON_SUBSPAN_ON      |   true", "HEADERS_ON_SUBSPAN_ON      |   false", "HEADERS_OFF_SUBSPAN_ON     |   true", "HEADERS_OFF_SUBSPAN_ON     |   false", "HEADERS_ON_SUBSPAN_OFF     |   true", "HEADERS_ON_SUBSPAN_OFF     |   false", "HEADERS_OFF_SUBSPAN_OFF    |   true", "HEADERS_OFF_SUBSPAN_OFF    |   false" }, splitBy = "\\|")
@Test
public void proxy_endpoint_tracing_behavior_should_work_as_desired_when_orig_request_does_have_tracing_headers(TracingBehaviorScenario scenario, boolean sampled) {
    Span origCallSpan = Span.newBuilder("origCall", Span.SpanPurpose.CLIENT).withSampleable(sampled).build();
    Map<String, String> tracingHeaders = new HashMap<>();
    HttpRequestTracingUtils.propagateTracingHeaders(tracingHeaders::put, origCallSpan);
    ExtractableResponse response = given().baseUri("http://127.0.0.1").port(proxyServerConfig.endpointsPort()).basePath(RouterEndpoint.MATCHING_PATH).header(SET_TRACING_HEADERS_HEADER_KEY, scenario.tracingHeadersOn).header(PERFORM_SUBSPAN_HEADER_KEY, scenario.subspanOn).headers(tracingHeaders).log().all().when().get().then().log().headers().extract();
    assertThat(response.statusCode()).isEqualTo(200);
    assertThat(response.asString()).isEqualTo(DownstreamEndpoint.RESPONSE_PAYLOAD);
    // Verify that the proxy honored the subspan option, and get a handle on the span that surrounded the proxy
    // downstream call.
    Span spanForDownstreamCall = verifyCompletedSpansAndReturnSpanForDownstreamCall(scenario.subspanOn);
    // Sanity checking - spanForDownstreamCall should be a child of origCallSpan (same trace ID and sampleable
    // value but different span IDs.
    assertThat(spanForDownstreamCall.getTraceId()).isEqualTo(origCallSpan.getTraceId());
    assertThat(spanForDownstreamCall.isSampleable()).isEqualTo(origCallSpan.isSampleable());
    assertThat(spanForDownstreamCall.getSpanId()).isNotEqualTo(origCallSpan.getSpanId());
    if (scenario.tracingHeadersOn) {
        // The downstream endpoint should have received tracing headers based on the span surrounding the proxy
        // downstream call.
        verifyExpectedTracingHeadersReceivedDownstream(response, spanForDownstreamCall);
    } else {
        // The ProxyRouterEndpoint is configured to *not* set tracing headers, so we should see the values
        // from origCallSpan show up on the other end since we sent them with the original call - the proxy
        // should not have messed with them.
        verifyExpectedTracingHeadersReceivedDownstream(response, origCallSpan);
    }
}
Also used : HashMap(java.util.HashMap) ExtractableResponse(io.restassured.response.ExtractableResponse) Span(com.nike.wingtips.Span) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 20 with Span

use of com.nike.wingtips.Span in project riposte by Nike-Inc.

the class ChannelPipelineFinalizerHandlerTest method doChannelInactive_does_not_try_to_recomplete_span_if_already_completed.

@Test
public void doChannelInactive_does_not_try_to_recomplete_span_if_already_completed() throws Exception {
    // given
    Span span = setupTracingForChannelInactive(false);
    Deque<Span> deque = new LinkedList<>();
    deque.add(span);
    Tracer.getInstance().registerWithThread(deque);
    Tracer.getInstance().completeRequestSpan();
    Assertions.assertThat(span.isCompleted()).isTrue();
    long durationNanosBefore = span.getDurationNanos();
    // when
    PipelineContinuationBehavior result = handler.doChannelInactive(ctxMock);
    // then
    // no change
    Assertions.assertThat(span.isCompleted()).isTrue();
    Assertions.assertThat(span.getDurationNanos()).isEqualTo(durationNanosBefore);
    verify(requestInfoMock).releaseAllResources();
    verify(proxyRouterStateMock).cancelRequestStreaming(any(), any());
    verify(proxyRouterStateMock).cancelDownstreamRequest(any());
    Assertions.assertThat(result).isEqualTo(PipelineContinuationBehavior.CONTINUE);
}
Also used : PipelineContinuationBehavior(com.nike.riposte.server.handler.base.PipelineContinuationBehavior) Span(com.nike.wingtips.Span) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

Span (com.nike.wingtips.Span)46 Test (org.junit.Test)38 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)21 Map (java.util.Map)14 Deque (java.util.Deque)13 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)9 CompletableFuture (java.util.concurrent.CompletableFuture)7 HashMap (java.util.HashMap)6 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)5 Tracer (com.nike.wingtips.Tracer)4 ChannelFuture (io.netty.channel.ChannelFuture)4 LinkedList (java.util.LinkedList)4 CircuitBreaker (com.nike.fastbreak.CircuitBreaker)3 PipelineContinuationBehavior (com.nike.riposte.server.handler.base.PipelineContinuationBehavior)3 RequestInfo (com.nike.riposte.server.http.RequestInfo)3 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)3 Matchers.anyString (org.mockito.Matchers.anyString)3 WrapperException (com.nike.backstopper.exception.WrapperException)2 Pair (com.nike.internal.util.Pair)2 DownstreamIdleChannelTimeoutHandler (com.nike.riposte.client.asynchttp.netty.downstreampipeline.DownstreamIdleChannelTimeoutHandler)2