use of com.nike.wingtips.Span in project riposte by Nike-Inc.
the class ChannelPipelineFinalizerHandlerTest method doChannelInactive_completes_releases_resources_and_completes_tracing_if_necessary.
@DataProvider(value = { "true | true", "false | true", "true | false", "false | false" }, splitBy = "\\|")
@Test
public void doChannelInactive_completes_releases_resources_and_completes_tracing_if_necessary(boolean tracingAlreadyDone, boolean responseSendingCompleted) throws Exception {
// given
Span span = setupTracingForChannelInactive(tracingAlreadyDone);
doReturn(responseSendingCompleted).when(responseInfoMock).isResponseSendingLastChunkSent();
if (responseSendingCompleted) {
state.setResponseWriterFinalChunkChannelFuture(mock(ChannelFuture.class));
}
// when
PipelineContinuationBehavior result = handler.doChannelInactive(ctxMock);
// then
Assertions.assertThat(span.isCompleted()).isEqualTo(!tracingAlreadyDone);
Assertions.assertThat(state.isTraceCompletedOrScheduled()).isTrue();
verify(requestInfoMock).releaseAllResources();
verify(proxyRouterStateMock).cancelRequestStreaming(any(), any());
verify(proxyRouterStateMock).cancelDownstreamRequest(any());
Assertions.assertThat(result).isEqualTo(PipelineContinuationBehavior.CONTINUE);
}
use of com.nike.wingtips.Span in project riposte by Nike-Inc.
the class AsyncNettyHelperTest method executeAsyncCall_shouldReturnCompletableFutureUsingSpanName.
@Test
public void executeAsyncCall_shouldReturnCompletableFutureUsingSpanName() throws Exception {
// given
String expectedResult = UUID.randomUUID().toString();
String expectedSpanName = "nonCircuitBreakerWithSpan";
ctxMock = TestUtil.mockChannelHandlerContextWithTraceInfo().mockContext;
Span parentSpan = Tracer.getInstance().getCurrentSpan();
AtomicReference<Span> runningSpan = new AtomicReference<>();
// when
CompletableFuture<String> completableFuture = AsyncNettyHelper.supplyAsync(expectedSpanName, () -> {
runningSpan.set(Tracer.getInstance().getCurrentSpan());
return expectedResult;
}, executor, ctxMock);
// then
assertThat(completableFuture.isCompletedExceptionally()).isFalse();
assertThat(completableFuture.get()).isEqualTo(expectedResult);
// verify span is as expected
assertThat(runningSpan.get().getParentSpanId()).isEqualTo(parentSpan.getSpanId());
assertThat(runningSpan.get().getSpanName()).isEqualTo(expectedSpanName);
assertThat(runningSpan.get().getTraceId()).isEqualTo(parentSpan.getTraceId());
}
use of com.nike.wingtips.Span in project riposte by Nike-Inc.
the class AsyncNettyHelperTest method unlinkTracingAndMdcFromCurrentThread_separate_args_works_as_expected.
@DataProvider(value = { "true | true", "false | true", "true | false", "false | false" }, splitBy = "\\|")
@Test
public void unlinkTracingAndMdcFromCurrentThread_separate_args_works_as_expected(boolean useNullSpanStack, boolean useNullMdcInfo) {
// given
Pair<Deque<Span>, Map<String, String>> info = setupStateWithTracingAndMdcInfo();
info.getRight().put("fooMdcKey", UUID.randomUUID().toString());
Deque<Span> spanStackForLinking = (useNullSpanStack) ? null : info.getLeft();
Map<String, String> mdcInfoForLinking = (useNullMdcInfo) ? null : info.getRight();
// Setup the current thread with something that is not ultimately what we expect so that our assertions are
// verifying that the unlinkTracingAndMdcFromCurrentThread method actually did something.
resetTracingAndMdc();
Tracer.getInstance().startRequestWithRootSpan("foo-" + UUID.randomUUID().toString());
Map<String, String> expectedMdcInfo;
// The expected MDC info will vary depending on combinations.
if (useNullMdcInfo) {
// MDC may still be populated after the call if the span stack is not empty
if (useNullSpanStack)
expectedMdcInfo = Collections.emptyMap();
else {
// MDC will have been populated with tracing info.
expectedMdcInfo = new HashMap<>();
Span expectedSpan = spanStackForLinking.peek();
expectedMdcInfo.put(Tracer.TRACE_ID_MDC_KEY, expectedSpan.getTraceId());
expectedMdcInfo.put(Tracer.SPAN_JSON_MDC_KEY, expectedSpan.toJSON());
}
} else {
// Not null MDC. Since unlinkTracingAndMdcFromCurrentThread doesn't call registerWithThread when
// the span stack is null we don't need to worry about trace ID and span JSON being removed from MDC.
// Therefore it should match mdcInfoForLinking exactly.
expectedMdcInfo = new HashMap<>(mdcInfoForLinking);
}
// when
AsyncNettyHelper.unlinkTracingAndMdcFromCurrentThread(spanStackForLinking, mdcInfoForLinking);
Pair<Deque<Span>, Map<String, String>> postCallInfo = Pair.of(Tracer.getInstance().getCurrentSpanStackCopy(), MDC.getCopyOfContextMap());
// then
assertThat(postCallInfo.getLeft()).isEqualTo(spanStackForLinking);
assertThat(postCallInfo.getRight()).isEqualTo(expectedMdcInfo);
}
use of com.nike.wingtips.Span in project riposte by Nike-Inc.
the class AsyncNettyHelperTest method executeAsyncCall_shouldReturnCompletableFuture.
@Test
public void executeAsyncCall_shouldReturnCompletableFuture() throws Exception {
// given
String expectedResult = UUID.randomUUID().toString();
ctxMock = TestUtil.mockChannelHandlerContextWithTraceInfo().mockContext;
Span parentSpan = Tracer.getInstance().getCurrentSpan();
AtomicReference<Span> runningSpan = new AtomicReference<>();
// when
CompletableFuture<String> completableFuture = AsyncNettyHelper.supplyAsync(() -> {
runningSpan.set(Tracer.getInstance().getCurrentSpan());
return expectedResult;
}, executor, ctxMock);
// then
assertThat(completableFuture.isCompletedExceptionally()).isFalse();
assertThat(completableFuture.get()).isEqualTo(expectedResult);
// verify new span is not created, but existing span does successfully hop threads
assertThat(runningSpan.get()).isEqualTo(parentSpan);
}
use of com.nike.wingtips.Span in project riposte by Nike-Inc.
the class BaseInboundHandlerWithTracingAndMdcSupportTest method unlinkTracingAndMdcFromCurrentThread_should_reset_tracing_and_mdc_to_originalThreadInfo_if_state_is_null.
@Test
public void unlinkTracingAndMdcFromCurrentThread_should_reset_tracing_and_mdc_to_originalThreadInfo_if_state_is_null() {
// given
doReturn(null).when(stateAttributeMock).get();
MDC.put("foo", "bar");
Tracer.getInstance().startRequestWithRootSpan("blahtrace");
assertThat(MDC.getCopyOfContextMap().isEmpty(), is(false));
assertThat(Tracer.getInstance().getCurrentSpan(), notNullValue());
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);
// when
handler.unlinkTracingAndMdcFromCurrentThread(ctxMock, origThreadInfo);
// then
assertThat(MDC.getCopyOfContextMap(), is(origMdcInfo));
assertThat(Tracer.getInstance().getCurrentSpanStackCopy(), is(origTraceStack));
}
Aggregations