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));
}
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);
}
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);
}
}
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);
}
}
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);
}
Aggregations