Search in sources :

Example 61 with Span

use of com.nike.wingtips.Span in project riposte by Nike-Inc.

the class DefaultAsyncHttpClientHelperSpanNamingAndTaggingStrategyTest method beforeMethod.

@Before
public void beforeMethod() {
    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);
    impl = new DefaultAsyncHttpClientHelperSpanNamingAndTaggingStrategy(wingtipsStrategy, wingtipsAdapterMock);
    requestMock = mock(RequestBuilderWrapper.class);
    responseMock = mock(Response.class);
    errorMock = mock(Throwable.class);
    spanMock = mock(Span.class);
}
Also used : Response(org.asynchttpclient.Response) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) HttpTagAndSpanNamingAdapter(com.nike.wingtips.tags.HttpTagAndSpanNamingAdapter) Span(com.nike.wingtips.Span) Before(org.junit.Before)

Example 62 with Span

use of com.nike.wingtips.Span in project riposte by Nike-Inc.

the class AsyncCompletionHandlerWithTracingAndMdcSupportTest method onCompleted_deals_with_trace_info_as_expected.

@DataProvider(value = { "null", "false", "true" })
@Test
public void onCompleted_deals_with_trace_info_as_expected(Boolean setupForSubspan) throws Throwable {
    // given
    Pair<Deque<Span>, Map<String, String>> traceInfo = generateTraceInfo(setupForSubspan);
    Whitebox.setInternalState(handlerSpy, "distributedTraceStackToUse", traceInfo.getLeft());
    Whitebox.setInternalState(handlerSpy, "mdcContextToUse", traceInfo.getRight());
    Whitebox.setInternalState(handlerSpy, "performSubSpanAroundDownstreamCalls", TRUE.equals(setupForSubspan));
    Span expectedSpanBeforeCompletion = (traceInfo.getLeft() == null) ? null : traceInfo.getLeft().peek();
    Span expectedSpanAfterCompletion = (setupForSubspan == null) ? null : // one there).
    traceInfo.getLeft().peekLast();
    Pair<ObjectHolder<Span>, ObjectHolder<Span>> actualBeforeAndAfterSpanHolders = setupBeforeAndAfterSpanCaptureForOnCompleted();
    // when
    handlerSpy.onCompleted(responseMock);
    // then
    verify(circuitBreakerManualTaskMock).handleEvent(responseMock);
    verify(responseHandlerFunctionMock).handleResponse(responseMock);
    assertThat(actualBeforeAndAfterSpanHolders.getLeft().objSet).isTrue();
    assertThat(actualBeforeAndAfterSpanHolders.getRight().objSet).isTrue();
    assertThat(actualBeforeAndAfterSpanHolders.getLeft().obj).isEqualTo(expectedSpanBeforeCompletion);
    assertThat(actualBeforeAndAfterSpanHolders.getRight().obj).isEqualTo(expectedSpanAfterCompletion);
    if (TRUE.equals(setupForSubspan)) {
        assertThat(strategyResponseTaggingAndFinalSpanNameMethodCalled.get()).isTrue();
        strategyResponseTaggingArgs.get().verifyArgs(expectedSpanBeforeCompletion, handlerSpy.rbwCopyWithHttpMethodAndUrlOnly, responseMock, null, wingtipsTagAndNamingAdapterMock);
    } else {
        assertThat(strategyResponseTaggingAndFinalSpanNameMethodCalled.get()).isFalse();
    }
}
Also used : Deque(java.util.Deque) HashMap(java.util.HashMap) Map(java.util.Map) Span(com.nike.wingtips.Span) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 63 with Span

use of com.nike.wingtips.Span in project riposte by Nike-Inc.

the class AsyncCompletionHandlerWithTracingAndMdcSupportTest method getTraceForCall_works_as_expected.

@DataProvider(value = { "NULL", "EMPTY", "HAS_EXISTING_SPAN" }, splitBy = "\\|")
@Test
public void getTraceForCall_works_as_expected(ExistingSpanStackState existingSpanStackState) {
    // given
    Deque<Span> spanStack;
    Span expectedResult;
    switch(existingSpanStackState) {
        case NULL:
            spanStack = null;
            expectedResult = null;
            break;
        case EMPTY:
            spanStack = new LinkedList<>();
            expectedResult = null;
            break;
        case HAS_EXISTING_SPAN:
            spanStack = handlerSpy.distributedTraceStackToUse;
            assertThat(spanStack).isNotEmpty();
            expectedResult = spanStack.peek();
            break;
        default:
            throw new IllegalArgumentException("Unhandled state: " + existingSpanStackState.name());
    }
    Whitebox.setInternalState(handlerSpy, "distributedTraceStackToUse", spanStack);
    // when
    Span spanForCall = handlerSpy.getSpanForCall();
    // then
    assertThat(spanForCall).isEqualTo(expectedResult);
}
Also used : Span(com.nike.wingtips.Span) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 64 with Span

use of com.nike.wingtips.Span in project riposte by Nike-Inc.

the class AsyncCompletionHandlerWithTracingAndMdcSupportTest method constructor_sets_values_exactly_as_given_when_subtracing_is_off.

@Test
public void constructor_sets_values_exactly_as_given_when_subtracing_is_off() {
    // given
    CompletableFuture cfResponse = mock(CompletableFuture.class);
    AsyncResponseHandler responseHandlerFunc = mock(AsyncResponseHandler.class);
    RequestBuilderWrapper rbwMock = mock(RequestBuilderWrapper.class);
    Optional<CircuitBreaker<Response>> circuitBreaker = Optional.of(mock(CircuitBreaker.class));
    Deque<Span> spanStack = mock(Deque.class);
    Map<String, String> mdcInfo = mock(Map.class);
    Deque<Span> spanStackBeforeCall = Tracer.getInstance().getCurrentSpanStackCopy();
    Map<String, String> mdcInfoBeforeCall = MDC.getCopyOfContextMap();
    // when
    AsyncCompletionHandlerWithTracingAndMdcSupport instance = new AsyncCompletionHandlerWithTracingAndMdcSupport(cfResponse, responseHandlerFunc, false, rbwMock, circuitBreaker, spanStack, mdcInfo, tagAndNamingStrategy);
    // then
    assertThat(instance.completableFutureResponse).isSameAs(cfResponse);
    assertThat(instance.responseHandlerFunction).isSameAs(responseHandlerFunc);
    assertThat(instance.performSubSpanAroundDownstreamCalls).isEqualTo(false);
    assertThat(instance.circuitBreakerManualTask).isSameAs(circuitBreaker);
    assertThat(instance.distributedTraceStackToUse).isSameAs(spanStack);
    assertThat(instance.mdcContextToUse).isSameAs(mdcInfo);
    assertThat(Tracer.getInstance().getCurrentSpanStackCopy()).isEqualTo(spanStackBeforeCall);
    assertThat(MDC.getCopyOfContextMap()).isEqualTo(mdcInfoBeforeCall);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) CircuitBreaker(com.nike.fastbreak.CircuitBreaker) Span(com.nike.wingtips.Span) Test(org.junit.Test)

Example 65 with Span

use of com.nike.wingtips.Span in project riposte by Nike-Inc.

the class AsyncCompletionHandlerWithTracingAndMdcSupportTest method onThrowable_deals_with_trace_info_as_expected.

@DataProvider(value = { "null", "false", "true" })
@Test
public void onThrowable_deals_with_trace_info_as_expected(Boolean setupForSubspan) throws Throwable {
    // given
    Exception ex = new Exception("kaboom");
    CompletableFuture<String> cfMock = mock(CompletableFuture.class);
    Whitebox.setInternalState(handlerSpy, "completableFutureResponse", cfMock);
    doReturn(false).when(cfMock).isDone();
    Pair<Deque<Span>, Map<String, String>> traceInfo = generateTraceInfo(setupForSubspan);
    Whitebox.setInternalState(handlerSpy, "distributedTraceStackToUse", traceInfo.getLeft());
    Whitebox.setInternalState(handlerSpy, "mdcContextToUse", traceInfo.getRight());
    Whitebox.setInternalState(handlerSpy, "performSubSpanAroundDownstreamCalls", TRUE.equals(setupForSubspan));
    Span expectedSpanBeforeCompletion = (traceInfo.getLeft() == null) ? null : traceInfo.getLeft().peek();
    Span expectedSpanAfterCompletion = (setupForSubspan == null) ? null : // one there).
    traceInfo.getLeft().peekLast();
    Pair<ObjectHolder<Span>, ObjectHolder<Span>> actualBeforeAndAfterSpanHolders = setupBeforeAndAfterSpanCaptureForOnThrowable(cfMock);
    // when
    handlerSpy.onThrowable(ex);
    // then
    verify(circuitBreakerManualTaskMock).handleException(ex);
    verify(cfMock).completeExceptionally(ex);
    assertThat(actualBeforeAndAfterSpanHolders.getLeft().objSet).isTrue();
    assertThat(actualBeforeAndAfterSpanHolders.getRight().objSet).isTrue();
    assertThat(actualBeforeAndAfterSpanHolders.getLeft().obj).isEqualTo(expectedSpanBeforeCompletion);
    assertThat(actualBeforeAndAfterSpanHolders.getRight().obj).isEqualTo(expectedSpanAfterCompletion);
    if (TRUE.equals(setupForSubspan)) {
        assertThat(strategyResponseTaggingAndFinalSpanNameMethodCalled.get()).isTrue();
        strategyResponseTaggingArgs.get().verifyArgs(expectedSpanBeforeCompletion, handlerSpy.rbwCopyWithHttpMethodAndUrlOnly, null, ex, wingtipsTagAndNamingAdapterMock);
    } else {
        assertThat(strategyResponseTaggingAndFinalSpanNameMethodCalled.get()).isFalse();
    }
}
Also used : Deque(java.util.Deque) HashMap(java.util.HashMap) Map(java.util.Map) Span(com.nike.wingtips.Span) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) 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