Search in sources :

Example 76 with Span

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

Example 77 with Span

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);
}
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 78 with Span

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();
}
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 79 with Span

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();
}
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 80 with Span

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();
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DistributedTracingConfig(com.nike.riposte.server.config.distributedtracing.DistributedTracingConfig) DefaultRiposteProxyRouterSpanNamingAndTaggingStrategy(com.nike.riposte.server.config.distributedtracing.DefaultRiposteProxyRouterSpanNamingAndTaggingStrategy) HttpResponse(io.netty.handler.codec.http.HttpResponse) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) HttpTagAndSpanNamingAdapter(com.nike.wingtips.tags.HttpTagAndSpanNamingAdapter) Span(com.nike.wingtips.Span) Before(org.junit.Before)

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