Search in sources :

Example 91 with Span

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

the class ChannelPipelineFinalizerHandlerTest method setupTracingForChannelInactive.

private Span setupTracingForChannelInactive(boolean traceCompletedOrScheduled) {
    state.setTraceCompletedOrScheduled(traceCompletedOrScheduled);
    Span span = Span.newBuilder("fooSpan", Span.SpanPurpose.SERVER).build();
    Assertions.assertThat(span.isCompleted()).isFalse();
    Deque<Span> spanStack = new LinkedList<>();
    spanStack.add(span);
    state.setDistributedTraceStack(spanStack);
    return span;
}
Also used : Span(com.nike.wingtips.Span) LinkedList(java.util.LinkedList)

Example 92 with Span

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

the class ChannelPipelineFinalizerHandlerTest method doChannelInactive_does_not_try_to_recomplete_span_if_already_completed.

@Test
public void doChannelInactive_does_not_try_to_recomplete_span_if_already_completed() throws Exception {
    // given
    Span span = setupTracingForChannelInactive(false);
    Deque<Span> deque = new LinkedList<>();
    deque.add(span);
    Tracer.getInstance().registerWithThread(deque);
    Tracer.getInstance().completeRequestSpan();
    Assertions.assertThat(span.isCompleted()).isTrue();
    long durationNanosBefore = span.getDurationNanos();
    // when
    PipelineContinuationBehavior result = handler.doChannelInactive(ctxMock);
    // then
    // no change
    Assertions.assertThat(span.isCompleted()).isTrue();
    Assertions.assertThat(span.getDurationNanos()).isEqualTo(durationNanosBefore);
    verify(requestInfoMock).releaseAllResources();
    verify(proxyRouterStateMock).cancelRequestStreaming(any(), any());
    verify(proxyRouterStateMock).cancelDownstreamRequest(any());
    Assertions.assertThat(result).isEqualTo(PipelineContinuationBehavior.CONTINUE);
}
Also used : PipelineContinuationBehavior(com.nike.riposte.server.handler.base.PipelineContinuationBehavior) Span(com.nike.wingtips.Span) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 93 with Span

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

the class VerifyProxyRouterTracingBehaviorComponentTest method proxy_endpoint_tracing_behavior_should_work_as_desired_when_orig_request_does_have_tracing_headers.

@DataProvider(value = { "HEADERS_ON_SUBSPAN_ON      |   true", "HEADERS_ON_SUBSPAN_ON      |   false", "HEADERS_OFF_SUBSPAN_ON     |   true", "HEADERS_OFF_SUBSPAN_ON     |   false", "HEADERS_ON_SUBSPAN_OFF     |   true", "HEADERS_ON_SUBSPAN_OFF     |   false", "HEADERS_OFF_SUBSPAN_OFF    |   true", "HEADERS_OFF_SUBSPAN_OFF    |   false" }, splitBy = "\\|")
@Test
public void proxy_endpoint_tracing_behavior_should_work_as_desired_when_orig_request_does_have_tracing_headers(TracingBehaviorScenario scenario, boolean sampled) {
    Span origCallSpan = Span.newBuilder("origCall", Span.SpanPurpose.CLIENT).withSampleable(sampled).build();
    Map<String, String> tracingHeaders = new HashMap<>();
    HttpRequestTracingUtils.propagateTracingHeaders(tracingHeaders::put, origCallSpan);
    ExtractableResponse response = given().baseUri("http://127.0.0.1").port(proxyServerConfig.endpointsPort()).basePath(RouterEndpoint.MATCHING_PATH).header(SET_TRACING_HEADERS_HEADER_KEY, scenario.tracingHeadersOn).header(PERFORM_SUBSPAN_HEADER_KEY, scenario.subspanOn).headers(tracingHeaders).log().all().when().get().then().log().headers().extract();
    assertThat(response.statusCode()).isEqualTo(200);
    assertThat(response.asString()).isEqualTo(DownstreamEndpoint.RESPONSE_PAYLOAD);
    // Verify that the proxy honored the subspan option, and get a handle on the span that surrounded the proxy
    // downstream call.
    Span spanForDownstreamCall = verifyCompletedSpansAndReturnSpanForDownstreamCall(scenario.subspanOn);
    // Sanity checking - spanForDownstreamCall should be a child of origCallSpan (same trace ID and sampleable
    // value but different span IDs.
    assertThat(spanForDownstreamCall.getTraceId()).isEqualTo(origCallSpan.getTraceId());
    assertThat(spanForDownstreamCall.isSampleable()).isEqualTo(origCallSpan.isSampleable());
    assertThat(spanForDownstreamCall.getSpanId()).isNotEqualTo(origCallSpan.getSpanId());
    if (scenario.tracingHeadersOn) {
        // The downstream endpoint should have received tracing headers based on the span surrounding the proxy
        // downstream call.
        verifyExpectedTracingHeadersReceivedDownstream(response, spanForDownstreamCall);
    } else {
        // The ProxyRouterEndpoint is configured to *not* set tracing headers, so we should see the values
        // from origCallSpan show up on the other end since we sent them with the original call - the proxy
        // should not have messed with them.
        verifyExpectedTracingHeadersReceivedDownstream(response, origCallSpan);
    }
}
Also used : HashMap(java.util.HashMap) ExtractableResponse(io.restassured.response.ExtractableResponse) Span(com.nike.wingtips.Span) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 94 with Span

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

the class DTraceStartHandlerTest method startTrace_creates_trace_from_parent_if_available_and_sets_expected_span_name_and_tags_and_annotations.

@Test
public void startTrace_creates_trace_from_parent_if_available_and_sets_expected_span_name_and_tags_and_annotations() {
    // given
    String parentTraceId = UUID.randomUUID().toString();
    String parentParentSpanId = UUID.randomUUID().toString();
    String parentSpanId = UUID.randomUUID().toString();
    String parentSpanName = UUID.randomUUID().toString();
    String parentTraceEnabled = "true";
    String parentUserId = UUID.randomUUID().toString();
    httpRequest.headers().set(TraceHeaders.TRACE_ID, parentTraceId);
    httpRequest.headers().set(TraceHeaders.PARENT_SPAN_ID, parentParentSpanId);
    httpRequest.headers().set(TraceHeaders.SPAN_ID, parentSpanId);
    httpRequest.headers().set(TraceHeaders.SPAN_NAME, parentSpanName);
    httpRequest.headers().set(TraceHeaders.TRACE_SAMPLED, parentTraceEnabled);
    httpRequest.headers().set(USER_ID_HEADER_KEY, parentUserId);
    assertThat(Tracer.getInstance().getCurrentSpan(), nullValue());
    // when
    long nanosBefore = System.nanoTime();
    handler.startTrace(httpRequest, ctxMock);
    long nanosAfter = System.nanoTime();
    // then
    Span span = Tracer.getInstance().getCurrentSpan();
    assertThat(span.getTraceId(), is(parentTraceId));
    assertThat(span.getParentSpanId(), is(parentSpanId));
    assertThat(span.getSpanId(), notNullValue());
    assertThat(span.getSpanId(), not(parentSpanId));
    assertThat(span.getSpanName(), is(initialSpanNameFromStrategy.get()));
    assertThat(span.getUserId(), is(parentUserId));
    assertThat(span.isSampleable(), is(Boolean.valueOf(parentTraceEnabled)));
    strategyInitialSpanNameArgs.get().verifyArgs(requestInfoMock, tagAndNamingAdapterMock);
    strategyRequestTaggingArgs.get().verifyArgs(span, requestInfoMock, tagAndNamingAdapterMock);
    long expectedMaxWireReceiveStartTimestamp = span.getSpanStartTimeEpochMicros() + TimeUnit.NANOSECONDS.toMicros(nanosAfter - nanosBefore);
    TimestampedAnnotation wireReceiveStartAnnotation = findAnnotationInSpan(span, "wr.start");
    Assertions.assertThat(wireReceiveStartAnnotation.getTimestampEpochMicros()).isBetween(span.getSpanStartTimeEpochMicros(), expectedMaxWireReceiveStartTimestamp);
}
Also used : TimestampedAnnotation(com.nike.wingtips.Span.TimestampedAnnotation) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Span(com.nike.wingtips.Span) Test(org.junit.Test)

Example 95 with Span

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

the class DTraceStartHandlerTest method doChannelRead_adds_wire_receive_finish_annotation_whem_msg_is_LastHttpContent_if_desired.

@DataProvider(value = { "true   |   true    |   true", "true   |   false   |   false", "false  |   true    |   false", "false  |   false   |   false" }, splitBy = "\\|")
@Test
public void doChannelRead_adds_wire_receive_finish_annotation_whem_msg_is_LastHttpContent_if_desired(boolean addWireReceiveFinishAnnotationConfigValue, boolean overallRequestSpanExists, boolean expectAnnotation) {
    // given
    DTraceStartHandler handlerSpy = spy(handler);
    HttpProcessingState stateSpy = spy(state);
    doReturn(stateSpy).when(stateAttributeMock).get();
    Span spanMock = (overallRequestSpanExists) ? mock(Span.class) : null;
    doReturn(spanMock).when(stateSpy).getOverallRequestSpan();
    shouldAddWireReceiveFinishAnnotation = addWireReceiveFinishAnnotationConfigValue;
    // when
    PipelineContinuationBehavior result = handlerSpy.doChannelRead(ctxMock, new DefaultLastHttpContent());
    // then
    if (expectAnnotation) {
        verify(spanMock).addTimestampedAnnotationForCurrentTime(distributedTracingConfig.getServerSpanNamingAndTaggingStrategy().wireReceiveFinishAnnotationName());
        verifyNoMoreInteractions(spanMock);
    } else if (spanMock != null) {
        verify(spanMock, never()).addTimestampedAnnotationForCurrentTime(anyString());
    }
    Assertions.assertThat(result).isEqualTo(PipelineContinuationBehavior.CONTINUE);
}
Also used : PipelineContinuationBehavior(com.nike.riposte.server.handler.base.PipelineContinuationBehavior) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) 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