All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.catchpoint.trace.instrument.integrations.spring.web.advice.SpringHttpClientAdviceHelper Maven / Gradle / Ivy

There is a newer version: 0.0.20
Show newest version
package com.catchpoint.trace.instrument.integrations.spring.web.advice;

import com.catchpoint.trace.common.logger.LoggerFactory;
import com.catchpoint.trace.common.util.PropertyUtils;
import com.catchpoint.trace.core.codec.CatchpointSpanContextKeys;
import com.catchpoint.trace.core.scope.CatchpointScope;
import com.catchpoint.trace.core.span.CommonTags;
import com.catchpoint.trace.core.span.CatchpointSpan;
import com.catchpoint.trace.core.CatchpointTracer;
import com.catchpoint.trace.integrations.http.HttpTags;
import com.catchpoint.trace.integrations.http.HttpTraceHelper;
import org.slf4j.Logger;
import org.springframework.http.HttpMethod;

import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * @author serkan
 */
public abstract class SpringHttpClientAdviceHelper {

    private static final Logger LOGGER = LoggerFactory.getLogger(SpringHttpClientAdviceHelper.class);

    public static final String HTTP_DOMAIN_NAME = "API";
    public static final String HTTP_CLASS_NAME = "HTTP";
    public static final String AWS_API_GW_ID_HEADER_NAME = "x-amz-apigw-id";
    public static final String AWS_API_GW_CLASS_NAME = "AWS-APIGateway";
    public static final String AWS_TRACE_ID_HEADER_NAME = "X-Amzn-Trace-Id";
    public static final String CATCHPOINT_RESOURCE_HEADER_NAME = CatchpointSpanContextKeys.RESOURCE_NAME.getName();

    public static final boolean MASK_BODY =
            PropertyUtils.getBooleanProperty("catchpoint.trace.integrations.http.body.mask", false);
    public static final int MAX_BODY_SIZE =
            PropertyUtils.getIntegerProperty("catchpoint.trace.integrations.http.body.size.max", 10 * 1024);
    public static final boolean MASK_RESPONSE_BODY =
            PropertyUtils.getBooleanProperty("catchpoint.trace.integrations.http.response.body.mask", true);
    public static final int MAX_RESPONSE_BODY_SIZE =
            PropertyUtils.getIntegerProperty("catchpoint.trace.integrations.http.response.body.size.max", 10 * 1024);
    public static final boolean DISABLE_SPAN_CONTEXT_INJECTION =
            PropertyUtils.getBooleanProperty(
                    "catchpoint.trace.integrations.http.spancontext.disable",
                    false);

    protected SpringHttpClientAdviceHelper() {
    }

    public static CatchpointScope createAndStartSpan(CatchpointTracer tracer, URI uri, HttpMethod method) {
        CatchpointScope scope = createSpan(tracer);
        CatchpointSpan span = scope.span();

        updateSpan(span, uri, method);

        return scope;
    }

    public static CatchpointScope createSpan(CatchpointTracer tracer) {
        return tracer.buildSpan("/").
                        withDomainName(HTTP_DOMAIN_NAME).
                        withClassName(HTTP_CLASS_NAME).
                        startActive(false);
    }

    public static void updateSpan(CatchpointSpan span, URI uri, HttpMethod method) {
        String hostname = uri.getHost();
        String path = uri.getPath();
        String url = hostname + path;
        String normalizedUrl = HttpTraceHelper.getNormalizedUrl(uri.toString());
        String operationName = normalizedUrl;

        span.setOperationName(operationName);

        span.setTag(HttpTags.HOST, hostname);
        span.setTag(HttpTags.METHOD, method.name());
        span.setTag(HttpTags.URL, url);
        span.setTag(HttpTags.PATH, path);
        span.setTag(HttpTags.QUERY_PARAMS, uri.getQuery() != null ? uri.getQuery() : "");

        span.setTag(CommonTags.OPERATION_TYPE, method.name());
        span.setTag(CommonTags.TOPOLOGY_VERTEX, true);
        span.setTag(CommonTags.TRACE_LINKS, Arrays.asList(span.getId()));

        span.initCompleted();
    }

    public static void finishSpan(CatchpointScope scope, Throwable error) {
        CatchpointSpan span = scope.span();
        if (error != null) {
            span.tagError(error);
        }
        span.finish();
    }

    public static void finishScope(CatchpointScope scope, Throwable error) {
        try {
            finishSpan(scope, error);
        } finally {
            scope.close();
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy