Java Code Examples for com.nike.wingtips.Tracer#startRequestWithRootSpan()

The following examples show how to use com.nike.wingtips.Tracer#startRequestWithRootSpan() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RequestTracingFilter.java    From wingtips with Apache License 2.0 6 votes vote down vote up
/**
 * @param request The incoming request.
 * @return A new {@link Span} for the overall request. This inspects the incoming request's headers to determine
 * if it should continue an existing trace with a child span, or whether a brand new trace needs to be started.
 * {@link #getInitialSpanName(HttpServletRequest, HttpTagAndSpanNamingStrategy, HttpTagAndSpanNamingAdapter)}
 * is used to generate the initial span name.
 */
protected Span createNewSpanForRequest(HttpServletRequest request) {
    // See if there's trace info in the incoming request's headers. If so it becomes the parent trace.
    Tracer tracer = Tracer.getInstance();
    final Span parentSpan = HttpSpanFactory.fromHttpServletRequest(request, getUserIdHeaderKeys());
    Span newSpan;

    if (parentSpan != null) {
        logger.debug("Found parent Span {}", parentSpan);
        newSpan = tracer.startRequestWithChildSpan(
            parentSpan,
            getInitialSpanName(request, tagAndNamingStrategy, tagAndNamingAdapter)
        );
    }
    else {
        newSpan = tracer.startRequestWithRootSpan(
            getInitialSpanName(request, tagAndNamingStrategy, tagAndNamingAdapter),
            HttpSpanFactory.getUserIdFromHttpServletRequest(request, getUserIdHeaderKeys())
        );
        logger.debug("Parent span not found, starting a new span {}", newSpan);
    }
    return newSpan;
}
 
Example 2
Source File: TracingStateTest.java    From wingtips with Apache License 2.0 6 votes vote down vote up
@Test
public void getCurrentThreadTracingState_works_as_expected() {
    // given
    Tracer tracer = Tracer.getInstance();
    tracer.startRequestWithRootSpan("request-" + UUID.randomUUID().toString());
    Deque<Span> currentSpanStack = tracer.getCurrentSpanStackCopy();
    Map<String, String> currentMdcInfo = MDC.getCopyOfContextMap();

    assertThat(currentSpanStack.size()).isGreaterThanOrEqualTo(1);
    assertThat(currentMdcInfo).isNotEmpty();

    // when
    TracingState currentTracingState = TracingState.getCurrentThreadTracingState();

    // then
    assertThat(currentTracingState.spanStack).isEqualTo(currentSpanStack);
    assertThat(currentTracingState.mdcInfo).isEqualTo(currentMdcInfo);
}
 
Example 3
Source File: WingtipsSpringWebfluxWebFilter.java    From wingtips with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new {@link Span} for the overall request. This inspects the incoming request's headers to determine
 * if it should continue an existing trace with a child span, or whether a brand new trace needs to be started.
 * {@link #getInitialSpanName(ServerWebExchange, HttpTagAndSpanNamingStrategy, HttpTagAndSpanNamingAdapter)}
 * is used to generate the initial span name.
 *
 * @param exchange The incoming request.
 * @return A new {@link Span} for the overall request.
 */
protected @NotNull Span createNewSpanForRequest(@NotNull ServerWebExchange exchange) {
    // See if there's trace info in the incoming request's headers. If so it becomes the parent trace.
    Tracer tracer = Tracer.getInstance();
    RequestWithHeadersServerWebExchangeAdapter requestWithHeadersAdapter =
        new RequestWithHeadersServerWebExchangeAdapter(exchange);

    final Span parentSpan = HttpRequestTracingUtils.fromRequestWithHeaders(
        requestWithHeadersAdapter, userIdHeaderKeys
    );

    Span newSpan;

    if (parentSpan == null) {
        newSpan = tracer.startRequestWithRootSpan(
            getInitialSpanName(exchange, tagAndNamingStrategy, tagAndNamingAdapter),
            HttpRequestTracingUtils.getUserIdFromRequestWithHeaders(requestWithHeadersAdapter, userIdHeaderKeys)
        );
        logger.debug("Parent span not found, starting a new span {}", newSpan);
    }
    else {
        logger.debug("Found parent Span {}", parentSpan);
        newSpan = tracer.startRequestWithChildSpan(
            parentSpan,
            getInitialSpanName(exchange, tagAndNamingStrategy, tagAndNamingAdapter)
        );
    }
    
    return newSpan;
}
 
Example 4
Source File: DTraceStartHandler.java    From riposte with Apache License 2.0 4 votes vote down vote up
protected void startTrace(HttpRequest nettyRequest, ChannelHandlerContext ctx) {
    Tracer tracer = Tracer.getInstance();

    // Start the distributed trace.
    RequestWithHeaders requestWrapper = new RequestWithHeadersNettyAdapter(nettyRequest);
    final Span parentSpan = HttpRequestTracingUtils.fromRequestWithHeaders(requestWrapper, userIdHeaderKeys);

    HttpProcessingState httpProcessingState = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get();

    // Create the new trace or child span, depending on what info came through via the Netty HttpRequest.
    //      We'll use the fallback span name to start with, because it's possible to throw an exception when
    //      creating the Riposte RequestInfo from the HttpRequest (which can happen when we call
    //      handlerUtils.createRequestInfoFromNettyHttpRequestAndHandleStateSetupIfNecessary(...) for certain
    //      bad requests), and we want the log message for *that* to include trace ID.
    Span newSpan;
    if (parentSpan != null) {
        newSpan = tracer.startRequestWithChildSpan(
            parentSpan,
            handlerUtils.determineFallbackOverallRequestSpanName(nettyRequest)
        );
        logger.debug("Found Parent Span {}", parentSpan);
    }
    else {
        newSpan = tracer.startRequestWithRootSpan(
            handlerUtils.determineFallbackOverallRequestSpanName(nettyRequest),
            HttpRequestTracingUtils.getUserIdFromRequestWithHeaders(requestWrapper, userIdHeaderKeys)
        );
        logger.debug("Parent Span not found, starting a new trace with root span {}", newSpan);
    }

    // Add the "we received the first bytes of the request on the wire" annotation to the span if desired.
    if (spanNamingAndTaggingStrategy.shouldAddWireReceiveStartAnnotation()) {
        newSpan.addTimestampedAnnotationForCurrentTime(
            spanNamingAndTaggingStrategy.wireReceiveStartAnnotationName()
        );
    }

    // Get the RequestInfo (generating it from the given Netty HttpRequest if necessary), so that
    //      getSpanName() can use it to come up with a better initial span name.
    RequestInfo<?> riposteRequestInfo =
        (httpProcessingState == null)
        ? null
        : handlerUtils.createRequestInfoFromNettyHttpRequestAndHandleStateSetupIfNecessary(
            nettyRequest, httpProcessingState
        );

    if (riposteRequestInfo != null) {
        // Change the span name based on what the tag strategy wants now that we have a Riposte RequestInfo.
        spanNamingAndTaggingStrategy.changeSpanName(
            newSpan,
            handlerUtils.determineOverallRequestSpanName(
                nettyRequest, riposteRequestInfo, spanNamingAndTaggingStrategy
            )
        );

        // Add request tagging.
        spanNamingAndTaggingStrategy.handleRequestTagging(newSpan, riposteRequestInfo);
    }
}