Search in sources :

Example 11 with Span

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);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) PipelineContinuationBehavior(com.nike.riposte.server.handler.base.PipelineContinuationBehavior) Span(com.nike.wingtips.Span) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 12 with Span

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

Example 13 with Span

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);
}
Also used : Deque(java.util.Deque) Map(java.util.Map) HashMap(java.util.HashMap) Span(com.nike.wingtips.Span) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 14 with Span

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

Example 15 with Span

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));
}
Also used : HashMap(java.util.HashMap) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Span(com.nike.wingtips.Span) Deque(java.util.Deque) HashMap(java.util.HashMap) Map(java.util.Map) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

Span (com.nike.wingtips.Span)46 Test (org.junit.Test)38 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)21 Map (java.util.Map)14 Deque (java.util.Deque)13 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)9 CompletableFuture (java.util.concurrent.CompletableFuture)7 HashMap (java.util.HashMap)6 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)5 Tracer (com.nike.wingtips.Tracer)4 ChannelFuture (io.netty.channel.ChannelFuture)4 LinkedList (java.util.LinkedList)4 CircuitBreaker (com.nike.fastbreak.CircuitBreaker)3 PipelineContinuationBehavior (com.nike.riposte.server.handler.base.PipelineContinuationBehavior)3 RequestInfo (com.nike.riposte.server.http.RequestInfo)3 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)3 Matchers.anyString (org.mockito.Matchers.anyString)3 WrapperException (com.nike.backstopper.exception.WrapperException)2 Pair (com.nike.internal.util.Pair)2 DownstreamIdleChannelTimeoutHandler (com.nike.riposte.client.asynchttp.netty.downstreampipeline.DownstreamIdleChannelTimeoutHandler)2