use of com.nike.wingtips.Span in project riposte by Nike-Inc.
the class HttpProcessingStateTest method handleTracingResponseTaggingAndFinalSpanNameIfNotAlreadyDone_works_as_expected_happy_path.
@Test
public void handleTracingResponseTaggingAndFinalSpanNameIfNotAlreadyDone_works_as_expected_happy_path() {
// given
stateSpy.setDistributedTracingConfig(distributedTracingConfigMock);
Span overallRequestSpanMock = mock(Span.class);
doReturn(overallRequestSpanMock).when(stateSpy).getOverallRequestSpan();
stateSpy.setRequestInfo(requestMock);
stateSpy.setResponseInfo(responseMock, errorMock);
assertThat(stateSpy.isTracingResponseTaggingAndFinalSpanNameCompleted()).isFalse();
// when
stateSpy.handleTracingResponseTaggingAndFinalSpanNameIfNotAlreadyDone();
// then
assertThat(stateSpy.isTracingResponseTaggingAndFinalSpanNameCompleted()).isTrue();
strategyResponseTaggingArgs.get().verifyArgs(overallRequestSpanMock, requestMock, responseMock, errorMock, wingtipsAdapterMock);
// and when
// Verify it only works once.
strategyResponseTaggingArgs.set(null);
stateSpy.handleTracingResponseTaggingAndFinalSpanNameIfNotAlreadyDone();
// then
assertThat(strategyResponseTaggingArgs.get()).isNull();
}
use of com.nike.wingtips.Span in project riposte by Nike-Inc.
the class HttpProcessingStateTest method handleTracingResponseTaggingAndFinalSpanNameIfNotAlreadyDone_does_not_propagate_unexpected_exception.
@Test
public void handleTracingResponseTaggingAndFinalSpanNameIfNotAlreadyDone_does_not_propagate_unexpected_exception() {
// given
doThrow(new RuntimeException("intentional exception")).when(distributedTracingConfigMock).getServerSpanNamingAndTaggingStrategy();
stateSpy.setDistributedTracingConfig(distributedTracingConfigMock);
Span overallRequestSpanMock = mock(Span.class);
doReturn(overallRequestSpanMock).when(stateSpy).getOverallRequestSpan();
stateSpy.setRequestInfo(requestMock);
stateSpy.setResponseInfo(responseMock, errorMock);
assertThat(stateSpy.isTracingResponseTaggingAndFinalSpanNameCompleted()).isFalse();
// when
Throwable ex = catchThrowable(() -> stateSpy.handleTracingResponseTaggingAndFinalSpanNameIfNotAlreadyDone());
// then
assertThat(ex).isNull();
verify(distributedTracingConfigMock).getServerSpanNamingAndTaggingStrategy();
assertThat(stateSpy.isTracingResponseTaggingAndFinalSpanNameCompleted()).isTrue();
}
use of com.nike.wingtips.Span in project riposte by Nike-Inc.
the class BiFunctionWithTracingAndMdcSupportTest 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();
BiFunctionWithTracingAndMdcSupport instance = new BiFunctionWithTracingAndMdcSupport(biFunctionMock, spanStack, mdcInfo);
resetTracingAndMdc();
assertThat(Tracer.getInstance().getCurrentSpanStackCopy()).isNull();
assertThat(MDC.getCopyOfContextMap()).isEmpty();
// when
Throwable ex = null;
Object result = null;
try {
result = instance.apply(inObj1, inObj2);
} catch (Throwable t) {
ex = t;
}
// then
verify(biFunctionMock).apply(inObj1, inObj2);
if (throwException) {
assertThat(ex).isNotNull();
assertThat(result).isNull();
} else {
assertThat(ex).isNull();
assertThat(result).isSameAs(outObj);
}
assertThat(currentSpanStackWhenFunctionWasCalled.get(0)).isEqualTo(spanStack);
assertThat(currentMdcInfoWhenFunctionWasCalled.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 ResponseSenderHandlerTest method sendResponse_adds_error_tag_to_current_span_when_response_sending_has_already_started_but_only_if_error_tag_does_not_already_exist.
@DataProvider(value = { "true | true", "true | false", "false | true", "false | false" }, splitBy = "\\|")
@Test
public void sendResponse_adds_error_tag_to_current_span_when_response_sending_has_already_started_but_only_if_error_tag_does_not_already_exist(boolean errorTagAlreadyExists, boolean exceptionHasMessage) throws JsonProcessingException {
// given
Object msg = new Object();
RuntimeException expectedExceptionFromDoSendResponse = (exceptionHasMessage) ? new RuntimeException("intentional test exception") : new RuntimeException();
doThrow(expectedExceptionFromDoSendResponse).when(handlerSpy).doSendResponse(any(), any());
doReturn(true).when(stateMock).isResponseSendingStarted();
Span currentSpan = Tracer.getInstance().startRequestWithRootSpan("fooSpan");
TracingState tracingState = TracingState.getCurrentThreadTracingState();
doReturn(tracingState.spanStack).when(stateMock).getDistributedTraceStack();
doReturn(tracingState.mdcInfo).when(stateMock).getLoggerMdcContextMap();
String addedErrorTagValue = (exceptionHasMessage) ? "intentional test exception" : expectedExceptionFromDoSendResponse.getClass().getSimpleName();
String preexistingErrorTagValue = UUID.randomUUID().toString();
if (errorTagAlreadyExists) {
currentSpan.putTag(KnownZipkinTags.ERROR, preexistingErrorTagValue);
}
String expectedErrorTagValue = (errorTagAlreadyExists) ? preexistingErrorTagValue : addedErrorTagValue;
// when
Throwable propagatedEx = catchThrowable(() -> handlerSpy.sendResponse(ctxMock, msg, false));
// then
// Standard assertions for this method call.
assertThat(propagatedEx).isSameAs(expectedExceptionFromDoSendResponse);
verify(handlerSpy, times(1)).doSendResponse(any(), any());
verify(stateMock).isResponseSendingStarted();
verify(proxyStateMock).cancelRequestStreaming(expectedExceptionFromDoSendResponse, ctxMock);
verify(proxyStateMock).cancelDownstreamRequest(expectedExceptionFromDoSendResponse);
verify(channelMock).close();
// What we're actually testing in this test.
assertThat(currentSpan.getTags().get(KnownZipkinTags.ERROR)).isEqualTo(expectedErrorTagValue);
}
use of com.nike.wingtips.Span in project riposte by Nike-Inc.
the class AsyncNettyHelperTest method executeAsyncCall_shouldReturnCompletableFutureUsingCircuitBreakerWithSpanName.
@Test
public void executeAsyncCall_shouldReturnCompletableFutureUsingCircuitBreakerWithSpanName() throws Exception {
// given
String expectedResult = UUID.randomUUID().toString();
String expectedSpanName = "circuitBreakerWithSpan";
ctxMock = TestUtil.mockChannelHandlerContextWithTraceInfo().mockContext;
Span parentSpan = Tracer.getInstance().getCurrentSpan();
CircuitBreaker<String> circuitBreaker = spy(new CircuitBreakerImpl<>());
AtomicReference<Span> runningSpan = new AtomicReference<>();
// when
CompletableFuture<String> circuitBreakerFuture = AsyncNettyHelper.supplyAsync(expectedSpanName, () -> {
runningSpan.set(Tracer.getInstance().getCurrentSpan());
return expectedResult;
}, circuitBreaker, executor, ctxMock);
// then
assertThat(circuitBreakerFuture.isCompletedExceptionally()).isFalse();
assertThat(circuitBreakerFuture.get()).isEqualTo(expectedResult);
verify(circuitBreaker).executeAsyncCall(any());
// 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());
}
Aggregations