Search in sources :

Example 81 with Span

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

the class SupplierWithTracingAndMdcSupportTest 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();
    SupplierWithTracingAndMdcSupport instance = new SupplierWithTracingAndMdcSupport(supplierMock, spanStack, mdcInfo);
    resetTracingAndMdc();
    assertThat(Tracer.getInstance().getCurrentSpanStackCopy()).isNull();
    assertThat(MDC.getCopyOfContextMap()).isEmpty();
    // when
    Throwable ex = null;
    Object result = null;
    try {
        result = instance.get();
    } catch (Throwable t) {
        ex = t;
    }
    // then
    verify(supplierMock).get();
    if (throwException) {
        assertThat(ex).isNotNull();
        assertThat(result).isNull();
    } else {
        assertThat(ex).isNull();
        assertThat(result).isSameAs(outObj);
    }
    assertThat(currentSpanStackWhenSupplierWasCalled.get(0)).isEqualTo(spanStack);
    assertThat(currentMdcInfoWhenSupplierWasCalled.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 82 with Span

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

the class RunnableWithTracingAndMdcSupportTest method run_handles_tracing_and_mdc_info_as_expected.

@DataProvider(value = { "true", "false" })
@Test
public void run_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();
    RunnableWithTracingAndMdcSupport instance = new RunnableWithTracingAndMdcSupport(runnableMock, spanStack, mdcInfo);
    resetTracingAndMdc();
    assertThat(Tracer.getInstance().getCurrentSpanStackCopy()).isNull();
    assertThat(MDC.getCopyOfContextMap()).isEmpty();
    // when
    Throwable ex = catchThrowable(() -> instance.run());
    // then
    verify(runnableMock).run();
    if (throwException)
        assertThat(ex).isNotNull();
    else
        assertThat(ex).isNull();
    assertThat(currentSpanStackWhenRunnableWasCalled.get(0)).isEqualTo(spanStack);
    assertThat(currentMdcInfoWhenRunnableWasCalled.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 83 with Span

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

the class DefaultRiposteServerSpanNamingAndTaggingStrategyTest method doChangeSpanName_changes_span_name_as_expected.

@DataProvider(value = { "null           |   false", "               |   false", "[whitespace]   |   false", "fooNewName     |   true" }, splitBy = "\\|")
@Test
public void doChangeSpanName_changes_span_name_as_expected(String newName, boolean expectNameToBeChanged) {
    // given
    if ("[whitespace]".equals(newName)) {
        newName = "  \r\n\t  ";
    }
    String initialSpanName = UUID.randomUUID().toString();
    Span span = Span.newBuilder(initialSpanName, Span.SpanPurpose.CLIENT).build();
    String expectedSpanName = (expectNameToBeChanged) ? newName : initialSpanName;
    // when
    impl.doChangeSpanName(span, newName);
    // then
    assertThat(span.getSpanName()).isEqualTo(expectedSpanName);
}
Also used : Span(com.nike.wingtips.Span) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 84 with Span

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

the class DTraceEndHandlerTest method endDtrace_completes_the_trace_using_ChannelFutureListener_if_state_is_not_null_and_isResponseSendingLastChunkSent_returns_true.

@Test
public void endDtrace_completes_the_trace_using_ChannelFutureListener_if_state_is_not_null_and_isResponseSendingLastChunkSent_returns_true() throws Exception {
    // given
    assertThat(state.isTraceCompletedOrScheduled(), is(false));
    assertThat(state.isResponseSendingLastChunkSent(), is(true));
    assertThat(state.getDistributedTraceStack(), nullValue());
    Pair<Deque<Span>, Map<String, String>> expectedDtraceInfo = setupStateWithNewSpan("blahTrace");
    assertThat(state.getDistributedTraceStack(), notNullValue());
    assertThat(state.getDistributedTraceStack(), is(expectedDtraceInfo.getLeft()));
    assertThat(state.getDistributedTraceStack().size(), is(1));
    assertThat(state.isTracingResponseTaggingAndFinalSpanNameCompleted(), is(false));
    Span expectedSpan = expectedDtraceInfo.getLeft().peek();
    // when
    handlerSpy.endDtrace(ctxMock);
    // then
    // completeCurrentSpan() not immediately called, but scheduled
    verify(handlerSpy, never()).completeCurrentSpan();
    assertThat(state.isTraceCompletedOrScheduled(), is(true));
    // Response tagging was done.
    assertThat(state.isTracingResponseTaggingAndFinalSpanNameCompleted(), is(true));
    // Extract the listener that was attached to the last chunk future.
    GenericFutureListener lastChunkListener = extractChannelFutureListenerAddedToLastChunkFuture();
    assertThat(lastChunkListener, notNullValue());
    assertThat(lastChunkListener, instanceOf(ChannelFutureListenerWithTracingAndMdc.class));
    assertThat(Whitebox.getInternalState(lastChunkListener, "distributedTraceStackForExecution"), is(expectedDtraceInfo.getLeft()));
    assertThat(Whitebox.getInternalState(lastChunkListener, "mdcContextMapForExecution"), is(expectedDtraceInfo.getRight()));
    Consumer<ChannelFuture> embeddedListenerConsumer = (Consumer<ChannelFuture>) Whitebox.getInternalState(lastChunkListener, "postCompleteOperation");
    // Execute the embedded listener so we can validate what it does. Note that we can't verify using mockito spy verify(),
    // because the method call goes through the internal handler, not the spy impl. But we can still verify by
    // setting up the Tracer state to what we expect, execute the embedded listener, and verify subsequent Tracer state.
    AsyncNettyHelper.linkTracingAndMdcToCurrentThread(expectedDtraceInfo);
    assertThat(Tracer.getInstance().getCurrentSpan(), is(expectedSpan));
    embeddedListenerConsumer.accept(null);
    assertThat(Tracer.getInstance().getCurrentSpan(), nullValue());
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Consumer(java.util.function.Consumer) ChannelFutureListenerWithTracingAndMdc(com.nike.riposte.util.asynchelperwrapper.ChannelFutureListenerWithTracingAndMdc) Deque(java.util.Deque) Map(java.util.Map) Span(com.nike.wingtips.Span) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener) Test(org.junit.Test)

Example 85 with Span

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

the class DTraceEndHandlerTest method endDtrace_completes_the_trace_immediately_if_state_is_not_null_but_isResponseSendingLastChunkSent_returns_false.

@Test
public void endDtrace_completes_the_trace_immediately_if_state_is_not_null_but_isResponseSendingLastChunkSent_returns_false() {
    // given
    assertThat(state.isTraceCompletedOrScheduled(), is(false));
    state.setResponseWriterFinalChunkChannelFuture(null);
    assertThat(state.isResponseSendingLastChunkSent(), is(false));
    assertThat(state.getDistributedTraceStack(), nullValue());
    Pair<Deque<Span>, Map<String, String>> expectedDtraceInfo = setupStateWithNewSpan("blahTrace");
    assertThat(state.getDistributedTraceStack(), notNullValue());
    assertThat(state.getDistributedTraceStack(), is(expectedDtraceInfo.getLeft()));
    assertThat(state.getDistributedTraceStack().size(), is(1));
    assertThat(state.isTracingResponseTaggingAndFinalSpanNameCompleted(), is(false));
    Span expectedSpan = expectedDtraceInfo.getLeft().peek();
    // when
    handlerSpy.endDtrace(ctxMock);
    // then
    verify(handlerSpy).completeCurrentSpan();
    assertThat(currentSpanWhenCompleteCurrentSpanWasCalled, is(expectedSpan));
    assertThat(currentSpanAfterCompleteCurrentSpanWasCalled, nullValue());
    assertThat(state.isTraceCompletedOrScheduled(), is(true));
    assertThat(state.isTracingResponseTaggingAndFinalSpanNameCompleted(), is(true));
}
Also used : Deque(java.util.Deque) Map(java.util.Map) 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