Search in sources :

Example 31 with InterceptorScopeInvocation

use of com.navercorp.pinpoint.bootstrap.interceptor.scope.InterceptorScopeInvocation in project pinpoint by naver.

the class HttpEngineSendRequestMethodInterceptor method after.

@Override
public void after(Object target, Object[] args, Object result, Throwable throwable) {
    if (isDebug) {
        logger.afterInterceptor(target, args);
    }
    final Trace trace = traceContext.currentTraceObject();
    if (trace == null) {
        return;
    }
    if (!validate(target)) {
        return;
    }
    try {
        SpanEventRecorder recorder = trace.currentSpanEventRecorder();
        recorder.recordApi(methodDescriptor);
        recorder.recordException(throwable);
        // typeCheck validate();
        Request request = ((UserRequestGetter) target)._$PINPOINT$_getUserRequest();
        if (request != null) {
            try {
                recorder.recordAttribute(AnnotationKey.HTTP_URL, InterceptorUtils.getHttpUrl(request.urlString(), param));
                final String endpoint = getDestinationId(request.url());
                recorder.recordDestinationId(endpoint);
            } catch (Exception ignored) {
                logger.warn("Failed to invoke of request.url(). {}", ignored.getMessage());
            }
            recordRequest(trace, request, throwable);
        }
        // clear attachment.
        InterceptorScopeInvocation invocation = interceptorScope.getCurrentInvocation();
        if (invocation != null && invocation.getAttachment() != null) {
            invocation.removeAttachment();
        }
    } finally {
        trace.traceBlockEnd();
    }
}
Also used : InterceptorScopeInvocation(com.navercorp.pinpoint.bootstrap.interceptor.scope.InterceptorScopeInvocation) Request(com.squareup.okhttp.Request)

Example 32 with InterceptorScopeInvocation

use of com.navercorp.pinpoint.bootstrap.interceptor.scope.InterceptorScopeInvocation in project pinpoint by naver.

the class RequestBuilderBuildMethodBackwardCompatibilityInterceptor method before.

@Override
public void before(Object target, Object[] args) {
    if (isDebug) {
        logger.beforeInterceptor(target, args);
    }
    final Trace trace = traceContext.currentRawTraceObject();
    if (trace == null) {
        return;
    }
    try {
        if (!(target instanceof Request.Builder)) {
            return;
        }
        final Request.Builder builder = ((Request.Builder) target);
        if (!trace.canSampled()) {
            if (isDebug) {
                logger.debug("set Sampling flag=false");
            }
            ((Request.Builder) target).header(Header.HTTP_SAMPLED.toString(), SamplingFlagUtils.SAMPLING_RATE_FALSE);
            return;
        }
        final InterceptorScopeInvocation invocation = interceptorScope.getCurrentInvocation();
        if (invocation == null || invocation.getAttachment() == null || !(invocation.getAttachment() instanceof TraceId)) {
            logger.debug("Invalid interceptor scope invocation. {}", invocation);
            return;
        }
        final TraceId nextId = (TraceId) invocation.getAttachment();
        builder.header(Header.HTTP_TRACE_ID.toString(), nextId.getTransactionId());
        builder.header(Header.HTTP_SPAN_ID.toString(), String.valueOf(nextId.getSpanId()));
        builder.header(Header.HTTP_PARENT_SPAN_ID.toString(), String.valueOf(nextId.getParentSpanId()));
        builder.header(Header.HTTP_FLAGS.toString(), String.valueOf(nextId.getFlags()));
        builder.header(Header.HTTP_PARENT_APPLICATION_NAME.toString(), traceContext.getApplicationName());
        builder.header(Header.HTTP_PARENT_APPLICATION_TYPE.toString(), Short.toString(traceContext.getServerTypeCode()));
        if (target instanceof UrlGetter) {
            final URL url = ((UrlGetter) target)._$PINPOINT$_getUrl();
            if (url != null) {
                final String endpoint = getDestinationId(url);
                logger.debug("Set HTTP_HOST {}", endpoint);
                builder.header(Header.HTTP_HOST.toString(), endpoint);
            }
        }
    } catch (Throwable t) {
        logger.warn("Failed to BEFORE process. {}", t.getMessage(), t);
    }
}
Also used : InterceptorScopeInvocation(com.navercorp.pinpoint.bootstrap.interceptor.scope.InterceptorScopeInvocation) UrlGetter(com.navercorp.pinpoint.plugin.okhttp.UrlGetter) Request(com.squareup.okhttp.Request) URL(java.net.URL)

Example 33 with InterceptorScopeInvocation

use of com.navercorp.pinpoint.bootstrap.interceptor.scope.InterceptorScopeInvocation in project pinpoint by naver.

the class RequestBuilderBuildMethodInterceptor method before.

@Override
public void before(Object target, Object[] args) {
    if (isDebug) {
        logger.beforeInterceptor(target, args);
    }
    final Trace trace = traceContext.currentRawTraceObject();
    if (trace == null) {
        return;
    }
    try {
        if (!(target instanceof Request.Builder)) {
            return;
        }
        final Request.Builder builder = ((Request.Builder) target);
        if (!trace.canSampled()) {
            if (isDebug) {
                logger.debug("set Sampling flag=false");
            }
            ((Request.Builder) target).header(Header.HTTP_SAMPLED.toString(), SamplingFlagUtils.SAMPLING_RATE_FALSE);
            return;
        }
        final InterceptorScopeInvocation invocation = interceptorScope.getCurrentInvocation();
        if (invocation == null || invocation.getAttachment() == null || !(invocation.getAttachment() instanceof TraceId)) {
            logger.debug("Invalid interceptor scope invocation. {}", invocation);
            return;
        }
        final TraceId nextId = (TraceId) invocation.getAttachment();
        builder.header(Header.HTTP_TRACE_ID.toString(), nextId.getTransactionId());
        builder.header(Header.HTTP_SPAN_ID.toString(), String.valueOf(nextId.getSpanId()));
        builder.header(Header.HTTP_PARENT_SPAN_ID.toString(), String.valueOf(nextId.getParentSpanId()));
        builder.header(Header.HTTP_FLAGS.toString(), String.valueOf(nextId.getFlags()));
        builder.header(Header.HTTP_PARENT_APPLICATION_NAME.toString(), traceContext.getApplicationName());
        builder.header(Header.HTTP_PARENT_APPLICATION_TYPE.toString(), Short.toString(traceContext.getServerTypeCode()));
        if (target instanceof HttpUrlGetter) {
            final HttpUrl url = ((HttpUrlGetter) target)._$PINPOINT$_getHttpUrl();
            if (url != null) {
                final String endpoint = getDestinationId(url);
                logger.debug("Set HTTP_HOST {}", endpoint);
                builder.header(Header.HTTP_HOST.toString(), endpoint);
            }
        }
    } catch (Throwable t) {
        logger.warn("Failed to BEFORE process. {}", t.getMessage(), t);
    }
}
Also used : InterceptorScopeInvocation(com.navercorp.pinpoint.bootstrap.interceptor.scope.InterceptorScopeInvocation) Request(com.squareup.okhttp.Request) HttpUrl(com.squareup.okhttp.HttpUrl)

Example 34 with InterceptorScopeInvocation

use of com.navercorp.pinpoint.bootstrap.interceptor.scope.InterceptorScopeInvocation in project pinpoint by naver.

the class TBaseProcessorProcessInterceptor method getMethodUri.

private String getMethodUri(Object target) {
    String methodUri = ThriftConstants.UNKNOWN_METHOD_URI;
    InterceptorScopeInvocation currentTransaction = this.scope.getCurrentInvocation();
    Object attachment = currentTransaction.getAttachment();
    if (attachment instanceof ThriftClientCallContext && target instanceof TBaseProcessor) {
        ThriftClientCallContext clientCallContext = (ThriftClientCallContext) attachment;
        String methodName = clientCallContext.getMethodName();
        methodUri = ThriftUtils.getProcessorNameAsUri((TBaseProcessor<?>) target);
        StringBuilder sb = new StringBuilder(methodUri);
        if (!methodUri.endsWith("/")) {
            sb.append("/");
        }
        sb.append(methodName);
        methodUri = sb.toString();
    }
    return methodUri;
}
Also used : InterceptorScopeInvocation(com.navercorp.pinpoint.bootstrap.interceptor.scope.InterceptorScopeInvocation) ThriftClientCallContext(com.navercorp.pinpoint.plugin.thrift.ThriftClientCallContext) TBaseProcessor(org.apache.thrift.TBaseProcessor)

Example 35 with InterceptorScopeInvocation

use of com.navercorp.pinpoint.bootstrap.interceptor.scope.InterceptorScopeInvocation in project pinpoint by naver.

the class TProtocolWriteFieldStopInterceptor method appendParentTraceInfo.

private void appendParentTraceInfo(TProtocol oprot) throws TException {
    InterceptorScopeInvocation currentTransaction = this.scope.getCurrentInvocation();
    ThriftRequestProperty parentTraceInfo = (ThriftRequestProperty) currentTransaction.getAttachment();
    if (parentTraceInfo == null) {
        return;
    }
    boolean shouldSample = parentTraceInfo.shouldSample(true);
    if (!shouldSample) {
        parentTraceInfo.writeTraceHeader(ThriftHeader.THRFIT_SAMPLED, oprot);
        return;
    }
    parentTraceInfo.writeTraceHeader(ThriftHeader.THRIFT_TRACE_ID, oprot);
    parentTraceInfo.writeTraceHeader(ThriftHeader.THRIFT_SPAN_ID, oprot);
    parentTraceInfo.writeTraceHeader(ThriftHeader.THRIFT_PARENT_SPAN_ID, oprot);
    parentTraceInfo.writeTraceHeader(ThriftHeader.THRIFT_FLAGS, oprot);
    parentTraceInfo.writeTraceHeader(ThriftHeader.THRIFT_PARENT_APPLICATION_NAME, oprot);
    parentTraceInfo.writeTraceHeader(ThriftHeader.THRIFT_PARENT_APPLICATION_TYPE, oprot);
    parentTraceInfo.writeTraceHeader(ThriftHeader.THRIFT_HOST, oprot);
}
Also used : InterceptorScopeInvocation(com.navercorp.pinpoint.bootstrap.interceptor.scope.InterceptorScopeInvocation) ThriftRequestProperty(com.navercorp.pinpoint.plugin.thrift.ThriftRequestProperty)

Aggregations

InterceptorScopeInvocation (com.navercorp.pinpoint.bootstrap.interceptor.scope.InterceptorScopeInvocation)53 Test (org.junit.Test)20 Trace (com.navercorp.pinpoint.bootstrap.context.Trace)15 DefaultInterceptorScopeInvocation (com.navercorp.pinpoint.profiler.interceptor.scope.DefaultInterceptorScopeInvocation)15 SpanEventRecorder (com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder)8 ThriftClientCallContext (com.navercorp.pinpoint.plugin.thrift.ThriftClientCallContext)7 DefaultInterceptorScopeDefinition (com.navercorp.pinpoint.bootstrap.instrument.DefaultInterceptorScopeDefinition)6 HttpCallContext (com.navercorp.pinpoint.plugin.httpclient4.HttpCallContext)6 ThriftRequestProperty (com.navercorp.pinpoint.plugin.thrift.ThriftRequestProperty)5 CommandContext (com.navercorp.pinpoint.plugin.redis.CommandContext)4 ServerMarkerFlagFieldAccessor (com.navercorp.pinpoint.plugin.thrift.field.accessor.ServerMarkerFlagFieldAccessor)4 AsyncTraceId (com.navercorp.pinpoint.bootstrap.context.AsyncTraceId)3 TraceId (com.navercorp.pinpoint.bootstrap.context.TraceId)3 Request (com.squareup.okhttp.Request)3 AttachmentFactory (com.navercorp.pinpoint.bootstrap.interceptor.scope.AttachmentFactory)2 HttpClient3CallContext (com.navercorp.pinpoint.plugin.httpclient3.HttpClient3CallContext)2 EndPointAccessor (com.navercorp.pinpoint.plugin.redis.EndPointAccessor)2 HttpRequest (org.apache.http.HttpRequest)2 AsyncTraceIdAccessor (com.navercorp.pinpoint.bootstrap.async.AsyncTraceIdAccessor)1 UrlGetter (com.navercorp.pinpoint.plugin.okhttp.UrlGetter)1