use of com.nike.wingtips.Span in project riposte by Nike-Inc.
the class AsyncNettyHelperTest method executeAsyncCall_shouldReturnCompletableFutureUsingCircuitBreaker.
@Test
public void executeAsyncCall_shouldReturnCompletableFutureUsingCircuitBreaker() throws Exception {
// given
String expectedResult = UUID.randomUUID().toString();
ctxMock = TestUtil.mockChannelHandlerContextWithTraceInfo().mockContext;
CircuitBreaker<String> circuitBreaker = spy(new CircuitBreakerImpl<>());
Span parentSpan = Tracer.getInstance().getCurrentSpan();
AtomicReference<Span> runningSpan = new AtomicReference<>();
// when
CompletableFuture<String> circuitBreakerCompletableFuture = AsyncNettyHelper.supplyAsync(() -> {
runningSpan.set(Tracer.getInstance().getCurrentSpan());
return expectedResult;
}, circuitBreaker, executor, ctxMock);
// then
verify(circuitBreaker).executeAsyncCall(any());
assertThat(circuitBreakerCompletableFuture.isCompletedExceptionally()).isFalse();
assertThat(circuitBreakerCompletableFuture.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 AsyncNettyHelperTest method linkTracingAndMdcToCurrentThread_separate_args_works_as_expected.
@DataProvider(value = { "true | true", "false | true", "true | false", "false | false" }, splitBy = "\\|")
@Test
public void linkTracingAndMdcToCurrentThread_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();
resetTracingAndMdc();
Tracer.getInstance().startRequestWithRootSpan("foo-" + UUID.randomUUID().toString());
Pair<Deque<Span>, Map<String, String>> expectedPreCallInfo = Pair.of(Tracer.getInstance().getCurrentSpanStackCopy(), MDC.getCopyOfContextMap());
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(SpanFieldForLoggerMdc.TRACE_ID.mdcKey, expectedSpan.getTraceId());
}
} else {
// Not null MDC. Start with the MDC info for linking.
expectedMdcInfo = new HashMap<>(mdcInfoForLinking);
if (useNullSpanStack) {
// In the case of a null span stack, the trace info would be removed from the MDC.
expectedMdcInfo.remove(SpanFieldForLoggerMdc.TRACE_ID.mdcKey);
}
}
// when
Pair<Deque<Span>, Map<String, String>> preCallInfo = AsyncNettyHelper.linkTracingAndMdcToCurrentThread(spanStackForLinking, mdcInfoForLinking);
Pair<Deque<Span>, Map<String, String>> postCallInfo = Pair.of(Tracer.getInstance().getCurrentSpanStackCopy(), MDC.getCopyOfContextMap());
// then
assertThat(preCallInfo).isEqualTo(expectedPreCallInfo);
assertThat(postCallInfo.getLeft()).isEqualTo(spanStackForLinking);
assertThat(postCallInfo.getRight()).isEqualTo(expectedMdcInfo);
}
use of com.nike.wingtips.Span in project riposte by Nike-Inc.
the class BiConsumerWithTracingAndMdcSupportTest method apply_handles_tracing_and_mdc_info_as_expected.
@DataProvider(value = { "true", "false" })
@Test
public void apply_handles_tracing_and_mdc_info_as_expected(boolean throwException) {
// given
throwExceptionDuringCall = throwException;
Tracer.getInstance().startRequestWithRootSpan("foo");
Deque<Span> spanStack = Tracer.getInstance().getCurrentSpanStackCopy();
Map<String, String> mdcInfo = MDC.getCopyOfContextMap();
BiConsumerWithTracingAndMdcSupport instance = new BiConsumerWithTracingAndMdcSupport(consumerMock, spanStack, mdcInfo);
resetTracingAndMdc();
assertThat(Tracer.getInstance().getCurrentSpanStackCopy()).isNull();
assertThat(MDC.getCopyOfContextMap()).isEmpty();
// when
Throwable ex = catchThrowable(() -> instance.accept(inObj1, inObj2));
// then
verify(consumerMock).accept(inObj1, inObj2);
if (throwException) {
assertThat(ex).isNotNull();
} else {
assertThat(ex).isNull();
}
assertThat(currentSpanStackWhenBiConsumerWasCalled.get(0)).isEqualTo(spanStack);
assertThat(currentMdcInfoWhenBiConsumerWasCalled.get(0)).isEqualTo(mdcInfo);
assertThat(Tracer.getInstance().getCurrentSpanStackCopy()).isNull();
assertThat(MDC.getCopyOfContextMap()).isEmpty();
}
use of com.nike.wingtips.Span in project riposte by Nike-Inc.
the class ConsumerWithTracingAndMdcSupportTest method apply_handles_tracing_and_mdc_info_as_expected.
@DataProvider(value = { "true", "false" })
@Test
public void apply_handles_tracing_and_mdc_info_as_expected(boolean throwException) {
// given
throwExceptionDuringCall = throwException;
Tracer.getInstance().startRequestWithRootSpan("foo");
Deque<Span> spanStack = Tracer.getInstance().getCurrentSpanStackCopy();
Map<String, String> mdcInfo = MDC.getCopyOfContextMap();
ConsumerWithTracingAndMdcSupport instance = new ConsumerWithTracingAndMdcSupport(consumerMock, spanStack, mdcInfo);
resetTracingAndMdc();
assertThat(Tracer.getInstance().getCurrentSpanStackCopy()).isNull();
assertThat(MDC.getCopyOfContextMap()).isEmpty();
// when
Throwable ex = catchThrowable(() -> instance.accept(inObj));
// then
verify(consumerMock).accept(inObj);
if (throwException) {
assertThat(ex).isNotNull();
} else {
assertThat(ex).isNull();
}
assertThat(currentSpanStackWhenConsumerWasCalled.get(0)).isEqualTo(spanStack);
assertThat(currentMdcInfoWhenConsumerWasCalled.get(0)).isEqualTo(mdcInfo);
assertThat(Tracer.getInstance().getCurrentSpanStackCopy()).isNull();
assertThat(MDC.getCopyOfContextMap()).isEmpty();
}
use of com.nike.wingtips.Span in project riposte by Nike-Inc.
the class ProxyRouterProcessingStateTest method beforeMethod.
@Before
public void beforeMethod() {
stateSpy = spy(new ProxyRouterProcessingState());
initialSpanNameFromStrategy = new AtomicReference<>("span-name-from-strategy-" + UUID.randomUUID().toString());
strategyInitialSpanNameMethodCalled = new AtomicBoolean(false);
strategyRequestTaggingMethodCalled = new AtomicBoolean(false);
strategyResponseTaggingAndFinalSpanNameMethodCalled = new AtomicBoolean(false);
strategyInitialSpanNameArgs = new AtomicReference<>(null);
strategyRequestTaggingArgs = new AtomicReference<>(null);
strategyResponseTaggingArgs = new AtomicReference<>(null);
wingtipsStrategy = new ArgCapturingHttpTagAndSpanNamingStrategy<>(initialSpanNameFromStrategy, strategyInitialSpanNameMethodCalled, strategyRequestTaggingMethodCalled, strategyResponseTaggingAndFinalSpanNameMethodCalled, strategyInitialSpanNameArgs, strategyRequestTaggingArgs, strategyResponseTaggingArgs);
wingtipsAdapterMock = mock(HttpTagAndSpanNamingAdapter.class);
proxyTaggingStrategy = new DefaultRiposteProxyRouterSpanNamingAndTaggingStrategy(wingtipsStrategy, wingtipsAdapterMock);
requestMock = mock(HttpRequest.class);
responseMock = mock(HttpResponse.class);
errorMock = mock(Throwable.class);
spanMock = mock(Span.class);
distributedTracingConfigMock = mock(DistributedTracingConfig.class);
doReturn(proxyTaggingStrategy).when(distributedTracingConfigMock).getProxyRouterSpanNamingAndTaggingStrategy();
}
Aggregations