Search in sources :

Example 71 with Span

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();
}
Also used : Span(com.nike.wingtips.Span) Test(org.junit.Test)

Example 72 with Span

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();
}
Also used : Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) Span(com.nike.wingtips.Span) Test(org.junit.Test)

Example 73 with Span

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();
}
Also used : Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) Span(com.nike.wingtips.Span) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 74 with Span

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);
}
Also used : Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) Span(com.nike.wingtips.Span) TracingState(com.nike.wingtips.util.TracingState) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider) Test(org.junit.Test)

Example 75 with Span

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());
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) Span(com.nike.wingtips.Span) Test(org.junit.Test)

Aggregations

Span (com.nike.wingtips.Span)103 Test (org.junit.Test)73 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)41 Map (java.util.Map)26 Deque (java.util.Deque)24 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)20 CompletableFuture (java.util.concurrent.CompletableFuture)18 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)13 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)12 HashMap (java.util.HashMap)10 HttpRequest (io.netty.handler.codec.http.HttpRequest)9 Before (org.junit.Before)9 RequestInfo (com.nike.riposte.server.http.RequestInfo)8 Tracer (com.nike.wingtips.Tracer)8 HttpTagAndSpanNamingAdapter (com.nike.wingtips.tags.HttpTagAndSpanNamingAdapter)7 ChannelFuture (io.netty.channel.ChannelFuture)7 ChannelHandler (io.netty.channel.ChannelHandler)7 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)7 HttpResponse (io.netty.handler.codec.http.HttpResponse)7 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)7