Java Code Examples for com.nike.internal.util.StringUtils#isNotBlank()

The following examples show how to use com.nike.internal.util.StringUtils#isNotBlank() . 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: ApiExceptionHandlerUtils.java    From backstopper with Apache License 2.0 6 votes vote down vote up
/**
 * @return The distributed trace ID if available in the request or the SLF4J {@link MDC}, or null if it cannot be
 *          found. Will also return null if the distributed trace ID exists but its trimmed length is 0
 *          (i.e. the distributed trace ID must be non-empty and contain something besides whitespace for it to be
 *          used). If you are using a distributed tracing system that uses different keys or where the trace ID is
 *          otherwise unobtainable using the rules defined here, then you can override this method and provide
 *          whatever rules you want.
 */
public String extractDistributedTraceId(RequestInfoForLogging request) {
    String traceIdToUse = null;

    if (distributedTraceIdHeaderKey != null) {
        String dtraceIdFromHeader = request.getHeader(distributedTraceIdHeaderKey);
        Object dtraceIdFromAttribute = request.getAttribute(distributedTraceIdHeaderKey);
        if (StringUtils.isNotBlank(dtraceIdFromHeader))
            traceIdToUse = dtraceIdFromHeader.trim();
        else if (dtraceIdFromAttribute != null && StringUtils.isNotBlank(dtraceIdFromAttribute.toString()))
            traceIdToUse = dtraceIdFromAttribute.toString().trim();
    }

    if (traceIdToUse == null) {
        // As a last resort try to get it from the MDC since some distributed systems (e.g. Wingtips) put the
        //      trace ID there.
        String fromMdc = MDC.get(TRACE_ID_MDC_KEY);
        if (fromMdc != null)
            traceIdToUse = fromMdc.trim();
    }

    return traceIdToUse;
}
 
Example 2
Source File: RiposteHandlerInternalUtil.java    From riposte with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the name that should be used for the span surrounding the request. Defaults to whatever {@link
 * ServerSpanNamingAndTaggingStrategy#getInitialSpanName(RequestInfo)} returns, with a fallback
 * of {@link HttpRequestTracingUtils#getFallbackSpanNameForHttpRequest(String, String)} if the naming strategy
 * returned null or blank string.
 *
 * @param nettyRequest The Netty {@link HttpRequest}.
 * @param riposteRequestInfo The Riposte {@link RequestInfo} (if this is null, then the tag strategy and adapter
 * will be ignored, and we'll fallback to basic behavior using the {@code nettyRequest}).
 * @param namingStrategy The {@link ServerSpanNamingAndTaggingStrategy} being used.
 * @return The name that should be used for the span surrounding the request.
 */
@NotNull String determineOverallRequestSpanName(
    @NotNull HttpRequest nettyRequest,
    @Nullable RequestInfo<?> riposteRequestInfo,
    @NotNull ServerSpanNamingAndTaggingStrategy<Span> namingStrategy
) {
    // Immediately go to the fallback span name if the Riposte RequestInfo is null.
    if (riposteRequestInfo == null) {
        return determineFallbackOverallRequestSpanName(nettyRequest);
    }

    // We have a Riposte RequestInfo. Try the naming strategy first.
    String spanNameFromStrategy = namingStrategy.getInitialSpanName(riposteRequestInfo);

    if (StringUtils.isNotBlank(spanNameFromStrategy)) {
        return spanNameFromStrategy;
    }

    // The naming strategy didn't have anything for us. Fall back to something reasonable.
    return determineFallbackOverallRequestSpanName(nettyRequest);
}
 
Example 3
Source File: AsyncCompletionHandlerWithTracingAndMdcSupport.java    From riposte with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the name that should be used for the subspan surrounding the call. Defaults to whatever {@link
 * SpanNamingAndTaggingStrategy#getInitialSpanName(Object)} returns, with a fallback
 * of {@link HttpRequestTracingUtils#getFallbackSpanNameForHttpRequest(String, String)} if the naming strategy
 * returned null or blank string. You can override this method to return something else if you want different
 * behavior and you don't want to adjust the naming strategy or adapter.
 *
 * @param request        The request that is about to be executed.
 * @param namingStrategy The {@link SpanNamingAndTaggingStrategy} being used.
 * @return The name that should be used for the subspan surrounding the call.
 */
protected @NotNull String getSubspanSpanName(
        @NotNull RequestBuilderWrapper request,
        @NotNull SpanNamingAndTaggingStrategy<RequestBuilderWrapper, ?, ?> namingStrategy
) {
    // Try the naming strategy first.
    String subspanNameFromStrategy = namingStrategy.getInitialSpanName(request);

    if (StringUtils.isNotBlank(subspanNameFromStrategy)) {
        return subspanNameFromStrategy;
    }

    // The naming strategy didn't have anything for us. Fall back to something reasonable.
    return HttpRequestTracingUtils.getFallbackSpanNameForHttpRequest(
            "async_downstream_call", request.httpMethod
    );
}
 
Example 4
Source File: AsyncCompletionHandlerWithTracingAndMdcSupport.java    From riposte with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the name that should be used for the subspan surrounding the call. Defaults to whatever {@link
 * SpanNamingAndTaggingStrategy#getInitialSpanName(Object)} returns, with a fallback
 * of {@link HttpRequestTracingUtils#getFallbackSpanNameForHttpRequest(String, String)} if the naming strategy
 * returned null or blank string. You can override this method to return something else if you want different
 * behavior and you don't want to adjust the naming strategy or adapter.
 *
 * @param request The request that is about to be executed.
 * @param namingStrategy The {@link SpanNamingAndTaggingStrategy} being used.
 * @return The name that should be used for the subspan surrounding the call.
 */
protected @NotNull String getSubspanSpanName(
    @NotNull RequestBuilderWrapper request,
    @NotNull SpanNamingAndTaggingStrategy<RequestBuilderWrapper, ?, ?> namingStrategy
) {
    // Try the naming strategy first.
    String subspanNameFromStrategy = namingStrategy.getInitialSpanName(request);

    if (StringUtils.isNotBlank(subspanNameFromStrategy)) {
        return subspanNameFromStrategy;
    }

    // The naming strategy didn't have anything for us. Fall back to something reasonable.
    return HttpRequestTracingUtils.getFallbackSpanNameForHttpRequest(
        "async_downstream_call", request.httpMethod
    );
}
 
Example 5
Source File: WingtipsSpringWebfluxExchangeFilterFunction.java    From wingtips with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the name that should be used for the subspan surrounding the call. Defaults to whatever {@link
 * HttpTagAndSpanNamingStrategy#getInitialSpanName(Object, HttpTagAndSpanNamingAdapter)} returns, with a fallback
 * of {@link HttpRequestTracingUtils#getFallbackSpanNameForHttpRequest(String, String)} if the naming strategy
 * returned null or blank string. You can override this method to return something else if you want different
 * behavior and you don't want to adjust the naming strategy or adapter.
 *
 * @param request The request that is about to be executed.
 * @param namingStrategy The {@link HttpTagAndSpanNamingStrategy} being used.
 * @param adapter The {@link HttpTagAndSpanNamingAdapter} being used.
 * @return The name that should be used for the subspan surrounding the call.
 */
protected @NotNull String getSubspanSpanName(
    @NotNull ClientRequest request,
    @NotNull HttpTagAndSpanNamingStrategy<ClientRequest, ?> namingStrategy,
    @NotNull HttpTagAndSpanNamingAdapter<ClientRequest, ?> adapter
) {
    // Try the naming strategy first.
    String subspanNameFromStrategy = namingStrategy.getInitialSpanName(request, adapter);

    if (StringUtils.isNotBlank(subspanNameFromStrategy)) {
        return subspanNameFromStrategy;
    }

    // The naming strategy didn't have anything for us. Fall back to something reasonable.
    return HttpRequestTracingUtils.getFallbackSpanNameForHttpRequest(
        "webflux_downstream_call", getRequestMethodAsString(request.method())
    );
}
 
Example 6
Source File: WingtipsSpringWebfluxWebFilter.java    From wingtips with Apache License 2.0 6 votes vote down vote up
/**
 * @param exchange The incoming request.
 * @param namingStrategy The {@link HttpTagAndSpanNamingStrategy} that should be used to try and generate the
 * initial span name - cannot be null.
 * @param adapter The {@link HttpTagAndSpanNamingAdapter} that should be passed to the given {@code namingStrategy}
 * to try and generate the initial span name - cannot be null.
 * @return The human-readable name to be given to a {@link Span} representing this request. By default this method
 * attempts to use {@link HttpTagAndSpanNamingStrategy#getInitialSpanName(Object, HttpTagAndSpanNamingAdapter)}
 * with the given {@code namingStrategy} and {@code adapter} for generating the name, and falls back to
 * {@link HttpRequestTracingUtils#generateSafeSpanName(String, String, Integer)} if the
 * {@link HttpTagAndSpanNamingStrategy} returns null or blank.
 */
protected @NotNull String getInitialSpanName(
    @NotNull ServerWebExchange exchange,
    @NotNull HttpTagAndSpanNamingStrategy<ServerWebExchange, ?> namingStrategy,
    @NotNull HttpTagAndSpanNamingAdapter<ServerWebExchange, ?> adapter
) {
    // Try the naming strategy first.
    String spanNameFromStrategy = namingStrategy.getInitialSpanName(exchange, adapter);

    if (StringUtils.isNotBlank(spanNameFromStrategy)) {
        return spanNameFromStrategy;
    }

    // The naming strategy didn't have anything for us. Fall back to something reasonable.
    String pathTemplate = determineUriPathTemplate(exchange);
    String method = exchange.getRequest().getMethodValue();

    // HttpRequestTracingUtils.generateSafeSpanName() gives us what we want for a fallback, and properly handles
    //      the case where everything passed into it is null.
    return HttpRequestTracingUtils.generateSafeSpanName(method, pathTemplate, (Integer)null);
}
 
Example 7
Source File: WingtipsSpringWebfluxWebFilter.java    From wingtips with Apache License 2.0 6 votes vote down vote up
/**
 * Tries to determine the low-cardinality path template for the given request. First, it looks for a
 * {@link KnownZipkinTags#HTTP_ROUTE} request attribute - if it finds one, then it uses that (this is not expected
 * under normal circumstances, but could be used to override normal behavior). If that attribute is not set, then
 * it falls back to looking for {@link HandlerMapping#BEST_MATCHING_PATTERN_ATTRIBUTE}, which is the usual way
 * Spring tells you what the path template was. If both of those fail, then null will be returned.
 *
 * @param exchange The {@link ServerWebExchange} for the request.
 * @return The low-cardinality path template for the given request, or null if no such path template could be
 * found.
 */
protected static @Nullable String determineUriPathTemplate(@NotNull ServerWebExchange exchange) {
    // Try the Zipkin http.route attribute first. If this exists, then it is definitively what we want.
    String path = getRequestAttributeAsString(exchange, KnownZipkinTags.HTTP_ROUTE);

    if (StringUtils.isNotBlank(path)) {
        // Found http.route. Use it.
        return path;
    }

    // The Zipkin http.route attribute was null or blank, so try the Spring "best matching pattern" attribute.
    path = getRequestAttributeAsString(exchange, HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);

    if (StringUtils.isNotBlank(path)) {
        // Found Spring best matching pattern. Use it.
        return path;
    }

    // At this point we've struck out on finding a path template.
    return null;
}
 
Example 8
Source File: WingtipsClientHttpRequestInterceptor.java    From wingtips with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the name that should be used for the subspan surrounding the call. Defaults to whatever {@link
 * HttpTagAndSpanNamingStrategy#getInitialSpanName(Object, HttpTagAndSpanNamingAdapter)} returns, with a fallback
 * of {@link HttpRequestTracingUtils#getFallbackSpanNameForHttpRequest(String, String)} if the naming strategy
 * returned null or blank string. You can override this method to return something else if you want different
 * behavior and you don't want to adjust the naming strategy or adapter.
 *
 * @param request The request that is about to be executed.
 * @param namingStrategy The {@link HttpTagAndSpanNamingStrategy} being used.
 * @param adapter The {@link HttpTagAndSpanNamingAdapter} being used.
 * @return The name that should be used for the subspan surrounding the call.
 */
protected @NotNull String getSubspanSpanName(
    @NotNull HttpRequest request,
    @NotNull HttpTagAndSpanNamingStrategy<HttpRequest, ?> namingStrategy,
    @NotNull HttpTagAndSpanNamingAdapter<HttpRequest, ?> adapter
) {
    // Try the naming strategy first.
    String subspanNameFromStrategy = namingStrategy.getInitialSpanName(request, adapter);

    if (StringUtils.isNotBlank(subspanNameFromStrategy)) {
        return subspanNameFromStrategy;
    }

    // The naming strategy didn't have anything for us. Fall back to something reasonable.
    return HttpRequestTracingUtils.getFallbackSpanNameForHttpRequest(
        "resttemplate_downstream_call", getRequestMethodAsString(request.getMethod())
    );
}
 
Example 9
Source File: WingtipsAsyncClientHttpRequestInterceptor.java    From wingtips with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the name that should be used for the subspan surrounding the call. Defaults to whatever {@link
 * HttpTagAndSpanNamingStrategy#getInitialSpanName(Object, HttpTagAndSpanNamingAdapter)} returns, with a fallback
 * of {@link HttpRequestTracingUtils#getFallbackSpanNameForHttpRequest(String, String)} if the naming strategy
 * returned null or blank string. You can override this method to return something else if you want different
 * behavior and you don't want to adjust the naming strategy or adapter.
 *
 * @param request The request that is about to be executed.
 * @param namingStrategy The {@link HttpTagAndSpanNamingStrategy} being used.
 * @param adapter The {@link HttpTagAndSpanNamingAdapter} being used.
 * @return The name that should be used for the subspan surrounding the call.
 */
protected @NotNull String getSubspanSpanName(
    @NotNull HttpRequest request,
    @NotNull HttpTagAndSpanNamingStrategy<HttpRequest, ?> namingStrategy,
    @NotNull HttpTagAndSpanNamingAdapter<HttpRequest, ?> adapter
) {
    // Try the naming strategy first.
    String subspanNameFromStrategy = namingStrategy.getInitialSpanName(request, adapter);

    if (StringUtils.isNotBlank(subspanNameFromStrategy)) {
        return subspanNameFromStrategy;
    }

    // The naming strategy didn't have anything for us. Fall back to something reasonable.
    return HttpRequestTracingUtils.getFallbackSpanNameForHttpRequest(
        "asyncresttemplate_downstream_call", getRequestMethodAsString(request.getMethod())
    );
}
 
Example 10
Source File: WingtipsApacheHttpClientInterceptor.java    From wingtips with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the name that should be used for the subspan surrounding the call. Defaults to whatever {@link
 * HttpTagAndSpanNamingStrategy#getInitialSpanName(Object, HttpTagAndSpanNamingAdapter)} returns, with a fallback
 * of {@link WingtipsApacheHttpClientUtil#getFallbackSubspanSpanName(HttpRequest)} if the naming strategy returned
 * null or blank string. You can override this method to return something else if you want different behavior and
 * you don't want to adjust the naming strategy or adapter.
 *
 * @param request The request that is about to be executed.
 * @param namingStrategy The {@link HttpTagAndSpanNamingStrategy} being used.
 * @param adapter The {@link HttpTagAndSpanNamingAdapter} being used.
 * @return The name that should be used for the subspan surrounding the call.
 */
protected @NotNull String getSubspanSpanName(
    @NotNull HttpRequest request,
    @NotNull HttpTagAndSpanNamingStrategy<HttpRequest, ?> namingStrategy,
    @NotNull HttpTagAndSpanNamingAdapter<HttpRequest, ?> adapter
) {
    // Try the naming strategy first.
    String subspanNameFromStrategy = namingStrategy.getInitialSpanName(request, adapter);

    if (StringUtils.isNotBlank(subspanNameFromStrategy)) {
        return subspanNameFromStrategy;
    }

    // The naming strategy didn't have anything for us. Fall back to something reasonable.
    return WingtipsApacheHttpClientUtil.getFallbackSubspanSpanName(request);
}
 
Example 11
Source File: WingtipsHttpClientBuilder.java    From wingtips with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the name that should be used for the subspan surrounding the call. Defaults to whatever {@link
 * HttpTagAndSpanNamingStrategy#getInitialSpanName(Object, HttpTagAndSpanNamingAdapter)} returns, with a fallback
 * of {@link WingtipsApacheHttpClientUtil#getFallbackSubspanSpanName(HttpRequest)} if the naming strategy returned
 * null or blank string. You can override this method to return something else if you want different behavior and
 * you don't want to adjust the naming strategy or adapter.
 *
 * @param request The request that is about to be executed.
 * @param namingStrategy The {@link HttpTagAndSpanNamingStrategy} being used.
 * @param adapter The {@link HttpTagAndSpanNamingAdapter} being used.
 * @return The name that should be used for the subspan surrounding the call.
 */
protected @NotNull String getSubspanSpanName(
    @NotNull HttpRequest request,
    @NotNull HttpTagAndSpanNamingStrategy<HttpRequest, ?> namingStrategy,
    @NotNull HttpTagAndSpanNamingAdapter<HttpRequest, ?> adapter
) {
    // Try the naming strategy first.
    String subspanNameFromStrategy = namingStrategy.getInitialSpanName(request, adapter);

    if (StringUtils.isNotBlank(subspanNameFromStrategy)) {
        return subspanNameFromStrategy;
    }

    // The naming strategy didn't have anything for us. Fall back to something reasonable.
    return WingtipsApacheHttpClientUtil.getFallbackSubspanSpanName(request);
}
 
Example 12
Source File: RequestTracingFilter.java    From wingtips with Apache License 2.0 6 votes vote down vote up
/**
 * @param request The incoming request.
 * @param namingStrategy The {@link HttpTagAndSpanNamingStrategy} that should be used to try and generate the
 * initial span name - cannot be null.
 * @param adapter The {@link HttpTagAndSpanNamingAdapter} that should be passed to the given {@code namingStrategy}
 * to try and generate the initial span name - cannot be null.
 * @return The human-readable name to be given to a {@link Span} representing this request. By default this method
 * attempts to use {@link HttpTagAndSpanNamingStrategy#getInitialSpanName(Object, HttpTagAndSpanNamingAdapter)}
 * with the given {@code namingStrategy} and {@code adapter} for generating the name, and falls back to
 * {@link HttpSpanFactory#getSpanName(HttpServletRequest)} if the {@link HttpTagAndSpanNamingStrategy} returns
 * null or blank.
 */
protected String getInitialSpanName(
    HttpServletRequest request,
    HttpTagAndSpanNamingStrategy<HttpServletRequest, ?> namingStrategy,
    HttpTagAndSpanNamingAdapter<HttpServletRequest, ?> adapter
) {
    // Try the naming strategy first.
    String spanNameFromStrategy = namingStrategy.getInitialSpanName(request, adapter);

    if (StringUtils.isNotBlank(spanNameFromStrategy)) {
        return spanNameFromStrategy;
    }

    // The naming strategy didn't have anything for us. Fall back to something reasonable.
    return HttpSpanFactory.getSpanName(request);
}
 
Example 13
Source File: ServletRequestTagAdapter.java    From wingtips with Apache License 2.0 6 votes vote down vote up
@Override
public @Nullable String getRequestUrl(@Nullable HttpServletRequest request) {
    if (request == null) {
        return null;
    }

    // request.getRequestURL() won't have a query string, so we need to separately look for it and add it
    //      if necessary.
    StringBuffer requestUrl = request.getRequestURL();
    String queryString = request.getQueryString();
    if (StringUtils.isNotBlank(queryString)) {
        requestUrl.append('?').append(queryString);
    }

    return requestUrl.toString();
}
 
Example 14
Source File: OpenTracingHttpTagStrategy.java    From wingtips with Apache License 2.0 6 votes vote down vote up
@Override
protected void doHandleResponseAndErrorTagging(
    @NotNull Span span,
    @Nullable REQ request,
    @Nullable RES response,
    @Nullable Throwable error,
    @NotNull HttpTagAndSpanNamingAdapter<REQ, RES> adapter
) {
    putTagIfValueIsNotBlank(span, KnownOpenTracingTags.HTTP_STATUS, adapter.getResponseHttpStatus(response));

    if (error != null || StringUtils.isNotBlank(adapter.getErrorResponseTagValue(response))) {
        // OpenTracing doesn't expect you to pass messages with the error tag, just error=true.
        //      So we don't need to do anything with the given error Throwable or returned
        //      getErrorResponseTagValue(), other than have them trigger adding the error=true tag.
        span.putTag(KnownOpenTracingTags.ERROR, "true");
    }
}
 
Example 15
Source File: GenericApiExceptionHandlerListener.java    From backstopper with Apache License 2.0 5 votes vote down vote up
@Override
public ApiExceptionHandlerListenerResult shouldHandleException(Throwable ex) {
    // We only care about ApiExceptions.
    if (!(ex instanceof ApiException))
        return ApiExceptionHandlerListenerResult.ignoreResponse();

    ApiException apiException = ((ApiException)ex);

    // Add all the ApiErrors from the exception.
    SortedApiErrorSet errors = new SortedApiErrorSet();
    errors.addAll(apiException.getApiErrors());

    // Add all the extra details for logging from the exception.
    List<Pair<String, String>> messages = new ArrayList<>();
    messages.addAll(apiException.getExtraDetailsForLogging());

    // Add all the extra response headers from the exception.
    List<Pair<String, List<String>>> headers = new ArrayList<>();
    headers.addAll(apiException.getExtraResponseHeaders());

    // Include the ApiException's message as a logged key/value pair.
    if (StringUtils.isNotBlank(ex.getMessage()))
        messages.add(Pair.of("api_exception_message", ex.getMessage()));

    if (ex.getCause() != null) {
        messages.add(Pair.of("exception_cause_class", ex.getCause().getClass().getName()));
        messages.add(Pair.of("exception_cause_message", ex.getCause().getMessage()));
    }

    return ApiExceptionHandlerListenerResult.handleResponse(errors, messages, headers);
}
 
Example 16
Source File: ZipkinHttpTagStrategy.java    From wingtips with Apache License 2.0 5 votes vote down vote up
@Override
protected void doHandleResponseAndErrorTagging(
    @NotNull Span span,
    @Nullable REQ request,
    @Nullable RES response,
    @Nullable Throwable error,
    @NotNull HttpTagAndSpanNamingAdapter<REQ, RES> adapter
) {
    // Now that we have both request and response, we'll re-try to get the route.
    putTagIfValueIsNotBlank(span, KnownZipkinTags.HTTP_ROUTE, adapter.getRequestUriPathTemplate(request, response));

    putTagIfValueIsNotBlank(span, KnownZipkinTags.HTTP_STATUS_CODE, adapter.getResponseHttpStatus(response));

    // For error tagging, we'll defer to the error Throwable if it's not null.
    if (error != null) {
        String message = error.getMessage();
        if (message == null) {
            message = error.getClass().getSimpleName();
        }
        addErrorTagToSpan(span, message);
    }
    else {
        // The error Throwable was null, so we'll see if the adapter thinks this is an error response.
        String errorTagValue = adapter.getErrorResponseTagValue(response);
        if (StringUtils.isNotBlank(errorTagValue)) {
            addErrorTagToSpan(span, errorTagValue);
        }
    }
}
 
Example 17
Source File: StreamingAsyncHttpClient.java    From riposte with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the name that should be used for the span surrounding the downstream call. Defaults to {@link
 * ProxyRouterSpanNamingAndTaggingStrategy#getInitialSpanNameOverride(HttpRequest, RequestInfo, String, String)}
 * if that returns a non-null value, then falls back to whatever {@link
 * ProxyRouterSpanNamingAndTaggingStrategy#getInitialSpanName(HttpRequest)} returns, with a ultimate fallback
 * of {@link HttpRequestTracingUtils#getFallbackSpanNameForHttpRequest(String, String)} if the naming strategy
 * returned null or blank string for both the override and initial span name.
 *
 * @param downstreamRequest The Netty {@link HttpRequest} for the downstream call.
 * @param namingStrategy The {@link ProxyRouterSpanNamingAndTaggingStrategy} being used.
 * @return The name that should be used for the span surrounding the downstream call.
 */
protected @NotNull String getSubspanSpanName(
    @NotNull HttpRequest downstreamRequest,
    @NotNull RequestInfo<?> overallRequest,
    @NotNull ProxyRouterSpanNamingAndTaggingStrategy<Span> namingStrategy
) {
    String spanNameFromStrategy = namingStrategy.getInitialSpanName(downstreamRequest);
    Span overallRequestSpan = Tracer.getInstance().getCurrentSpan();
    String overallRequestSpanName = (overallRequestSpan == null) ? null : overallRequestSpan.getSpanName();

    String spanNameOverride = namingStrategy.getInitialSpanNameOverride(
        downstreamRequest, overallRequest, spanNameFromStrategy, overallRequestSpanName
    );

    if (StringUtils.isNotBlank(spanNameFromStrategy)) {
        // We got a span name from the strategy. See if it should be overridden.
        if (StringUtils.isNotBlank(spanNameOverride)) {
            return spanNameOverride;
        }

        // No override, so just use the name from the strategy.
        return spanNameFromStrategy;
    }

    // The naming strategy didn't have anything for us at all. See if there's an override.
    if (StringUtils.isNotBlank(spanNameOverride)) {
        return spanNameOverride;
    }

    // The naming strategy didn't have anything for us and there was no override. Fall back to something reasonable.
    return getFallbackSpanName(downstreamRequest);
}
 
Example 18
Source File: HttpSpanFactory.java    From wingtips with Apache License 2.0 4 votes vote down vote up
/**
 * Tries to determine the URI path template for this request, and returns null if no such template could be found.
 * i.e. We try to return {@code /foo/:id} (or however your framework shows path template) rather than
 * {@code /foo/12345}.
 *
 * <p>The rules this method follows for trying to find the path template:
 *
 * <ol>
 *     <li>
 *         We look in {@link HttpServletRequest#getAttribute(String)} for a request attribute matching
 *         {@link KnownZipkinTags#HTTP_ROUTE} ("http.route"). If we find a non-empty http.route attribute, then
 *         we'll use it as the path template.
 *     </li>
 *     <li>
 *         If there was no http.route request attribute, then we'll look for Spring's
 *         "best matching pattern" attribute ({@value #SPRING_BEST_MATCHING_PATTERN_REQUEST_ATTRIBUTE_KEY}).
 *     </li>
 * </ol>
 *
 * In particular, note that we're not falling back to {@link HttpServletRequest#getServletPath()}, because while
 * that works in some cases for raw Servlets, many popular Servlet frameworks don't honor the spirit of that
 * method's javadocs and return full URIs. If you're working in a system where you know {@link
 * HttpServletRequest#getServletPath()} is safe to use, then you can call {@code
 * request.setAttribute(KnownZipkinTags.HTTP_ROUTE, request.getServletPath())} and this method will use it.
 * A similar trick of setting http.route as a request attribute can be used anytime by any code to set the path
 * template that will be used.
 *
 * @return The URI path template for this request, or null if no such path template could be determined.
 */
public static @Nullable String determineUriPathTemplate(@Nullable HttpServletRequest request) {
    // Null might be passed in. If so, there's nothing we can do except return null ourselves.
    if (request == null) {
        return null;
    }

    // Try the Zipkin http.route attribute first. If this exists, then it is definitively what we want.
    String path = getRequestAttributeAsString(request, KnownZipkinTags.HTTP_ROUTE);

    if (StringUtils.isNotBlank(path)) {
        // Found http.route. Use it.
        return path;
    }

    // The Zipkin http.route attribute was null or blank, so try the Spring "best matching pattern" attribute.
    path = getRequestAttributeAsString(request, SPRING_BEST_MATCHING_PATTERN_REQUEST_ATTRIBUTE_KEY);

    if (StringUtils.isNotBlank(path)) {
        // Found Spring best matching pattern. Use it.
        return path;
    }

    // At this point we've struck out on finding a path template.
    return null;
}
 
Example 19
Source File: AuditLogsS3TimeBasedRollingPolicy.java    From cerberus with Apache License 2.0 4 votes vote down vote up
private boolean isS3AuditLogCopyingEnabled() {
  return StringUtils.isNotBlank(bucket) && StringUtils.isNotBlank(bucketRegion);
}
 
Example 20
Source File: HttpTagAndSpanNamingStrategy.java    From wingtips with Apache License 2.0 3 votes vote down vote up
/**
 * Determines the final span name that should be used for the given request/response according to this strategy
 * and/or the given adapter, and then calls {@link SpanMutator#changeSpanName(Span, String)} to actually change
 * the span name (if and only if a non-blank final span name is generated).
 *
 * <p>By default this will delegate to the adapter's {@link
 * HttpTagAndSpanNamingAdapter#getFinalSpanName(Object, Object)} for generating the final span name unless you
 * override this method.
 *
 * <p>This method does the actual final-span-naming work for the public-facing {@link
 * #handleResponseTaggingAndFinalSpanName(Span, Object, Object, Throwable, HttpTagAndSpanNamingAdapter)}.
 *
 * @param span The span to name - will never be null.
 * @param request The request object - this may be null.
 * @param response The response object - this may be null.
 * @param error The error that prevented the request/response from completing normally, or null if no such error
 * occurred.
 * @param adapter The adapter to handle the request and response - will never be null.
 */
protected void doDetermineAndSetFinalSpanName(
    @NotNull Span span,
    @Nullable REQ request,
    @Nullable RES response,
    @Nullable Throwable error,
    @NotNull HttpTagAndSpanNamingAdapter<REQ, RES> adapter
) {
    String finalSpanName = adapter.getFinalSpanName(request, response);

    if (StringUtils.isNotBlank(finalSpanName)) {
        SpanMutator.changeSpanName(span, finalSpanName);
    }
}