Search in sources :

Example 1 with TraceId

use of com.navercorp.pinpoint.bootstrap.context.TraceId in project pinpoint by naver.

the class DefaultTraceIdFactory method newTraceId.

@Override
public TraceId newTraceId() {
    final long localTransactionId = idGenerator.nextTransactionId();
    final TraceId traceId = new DefaultTraceId(agentId, agentStartTime, localTransactionId);
    return traceId;
}
Also used : TraceId(com.navercorp.pinpoint.bootstrap.context.TraceId)

Example 2 with TraceId

use of com.navercorp.pinpoint.bootstrap.context.TraceId in project pinpoint by naver.

the class DefaultBaseTraceFactory method continueAsyncTraceObject.

// internal async trace.
@Override
public Trace continueAsyncTraceObject(AsyncTraceId traceId, int asyncId, long startTime) {
    final TraceId parentTraceId = traceId.getParentTraceId();
    final Storage storage = storageFactory.createStorage();
    final Storage asyncStorage = new AsyncStorage(storage);
    final Trace trace = new DefaultTrace(callStackFactory, asyncStorage, parentTraceId, AtomicIdGenerator.UNTRACKED_ID, asyncIdGenerator, true, spanFactory, recorderFactory);
    final AsyncTrace asyncTrace = new AsyncTrace(trace, asyncId, traceId.nextAsyncSequence(), startTime);
    return asyncTrace;
}
Also used : Trace(com.navercorp.pinpoint.bootstrap.context.Trace) AsyncStorage(com.navercorp.pinpoint.profiler.context.storage.AsyncStorage) Storage(com.navercorp.pinpoint.profiler.context.storage.Storage) TraceId(com.navercorp.pinpoint.bootstrap.context.TraceId) AsyncTraceId(com.navercorp.pinpoint.bootstrap.context.AsyncTraceId) AsyncStorage(com.navercorp.pinpoint.profiler.context.storage.AsyncStorage)

Example 3 with TraceId

use of com.navercorp.pinpoint.bootstrap.context.TraceId in project pinpoint by naver.

the class TServiceClientSendBaseInterceptor method before.

@Override
public void before(Object target, Object[] args) {
    if (isDebug) {
        logger.beforeInterceptor(target, args);
    }
    if (target instanceof TServiceClient) {
        TServiceClient client = (TServiceClient) target;
        TProtocol oprot = client.getOutputProtocol();
        TTransport transport = oprot.getTransport();
        final Trace trace = traceContext.currentRawTraceObject();
        if (trace == null) {
            return;
        }
        ThriftRequestProperty parentTraceInfo = new ThriftRequestProperty();
        final boolean shouldSample = trace.canSampled();
        if (!shouldSample) {
            if (isDebug) {
                logger.debug("set Sampling flag=false");
            }
            parentTraceInfo.setShouldSample(shouldSample);
        } else {
            SpanEventRecorder recorder = trace.traceBlockBegin();
            recorder.recordServiceType(ThriftConstants.THRIFT_CLIENT);
            // retrieve connection information
            String remoteAddress = ThriftConstants.UNKNOWN_ADDRESS;
            if (transport instanceof SocketFieldAccessor) {
                Socket socket = ((SocketFieldAccessor) transport)._$PINPOINT$_getSocket();
                if (socket != null) {
                    remoteAddress = ThriftUtils.getHostPort(socket.getRemoteSocketAddress());
                }
            } else {
                if (isDebug) {
                    logger.debug("Invalid target object. Need field accessor({}).", SocketFieldAccessor.class.getName());
                }
            }
            recorder.recordDestinationId(remoteAddress);
            String methodName = ThriftConstants.UNKNOWN_METHOD_NAME;
            if (args[0] instanceof String) {
                methodName = (String) args[0];
            }
            String serviceName = ThriftUtils.getClientServiceName(client);
            String thriftUrl = getServiceUrl(remoteAddress, serviceName, methodName);
            recorder.recordAttribute(ThriftConstants.THRIFT_URL, thriftUrl);
            TraceId nextId = trace.getTraceId().getNextTraceId();
            recorder.recordNextSpanId(nextId.getSpanId());
            parentTraceInfo.setTraceId(nextId.getTransactionId());
            parentTraceInfo.setSpanId(nextId.getSpanId());
            parentTraceInfo.setParentSpanId(nextId.getParentSpanId());
            parentTraceInfo.setFlags(nextId.getFlags());
            parentTraceInfo.setParentApplicationName(traceContext.getApplicationName());
            parentTraceInfo.setParentApplicationType(traceContext.getServerTypeCode());
            parentTraceInfo.setAcceptorHost(remoteAddress);
        }
        InterceptorScopeInvocation currentTransaction = this.scope.getCurrentInvocation();
        currentTransaction.setAttachment(parentTraceInfo);
    }
}
Also used : TServiceClient(org.apache.thrift.TServiceClient) Trace(com.navercorp.pinpoint.bootstrap.context.Trace) InterceptorScopeInvocation(com.navercorp.pinpoint.bootstrap.interceptor.scope.InterceptorScopeInvocation) TProtocol(org.apache.thrift.protocol.TProtocol) ThriftRequestProperty(com.navercorp.pinpoint.plugin.thrift.ThriftRequestProperty) SpanEventRecorder(com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder) TraceId(com.navercorp.pinpoint.bootstrap.context.TraceId) SocketFieldAccessor(com.navercorp.pinpoint.plugin.thrift.field.accessor.SocketFieldAccessor) TTransport(org.apache.thrift.transport.TTransport) Socket(java.net.Socket)

Example 4 with TraceId

use of com.navercorp.pinpoint.bootstrap.context.TraceId in project pinpoint by naver.

the class ServletInvocationInterceptor method createTrace.

private Trace createTrace(Object target, Object[] args) {
    final HttpServletRequest request = (HttpServletRequest) args[0];
    if (isAsynchronousProcess(request)) {
        // servlet 3.0
        final Trace trace = getTraceMetadata(request);
        if (trace != null) {
            // change api
            SpanRecorder recorder = trace.getSpanRecorder();
            recorder.recordApi(SERVLET_ASYNCHRONOUS_API_TAG);
            // attach current thread local.
            traceContext.continueTraceObject(trace);
            return trace;
        }
    }
    final Trace currentRawTraceObject = traceContext.currentRawTraceObject();
    if (currentRawTraceObject != null) {
        return currentRawTraceObject;
    }
    final String requestURI = request.getRequestURI();
    if (excludeUrlFilter.filter(requestURI)) {
        if (isDebug) {
            logger.debug("filter requestURI:{}", requestURI);
        }
        return null;
    }
    // check sampling flag from client. If the flag is false, do not sample this request.
    final boolean sampling = samplingEnable(request);
    if (!sampling) {
        // Even if this transaction is not a sampling target, we have to create Trace object to mark 'not sampling'.
        // For example, if this transaction invokes rpc call, we can add parameter to tell remote node 'don't sample this transaction'
        final Trace trace = traceContext.disableSampling();
        if (isDebug) {
            logger.debug("remotecall sampling flag found. skip trace requestUrl:{}, remoteAddr:{}", request.getRequestURI(), request.getRemoteAddr());
        }
        return trace;
    }
    final TraceId traceId = populateTraceIdFromRequest(request);
    if (traceId != null) {
        // TODO Maybe we should decide to trace or not even if the sampling  flag is true to prevent too many requests are traced.
        final Trace trace = traceContext.continueTraceObject(traceId);
        if (trace.canSampled()) {
            SpanRecorder recorder = trace.getSpanRecorder();
            recordRootSpan(recorder, request);
            setTraceMetadata(request, trace);
            if (isDebug) {
                logger.debug("TraceID exist. continue trace. traceId:{}, requestUrl:{}, remoteAddr:{}", traceId, request.getRequestURI(), request.getRemoteAddr());
            }
        } else {
            if (isDebug) {
                logger.debug("TraceID exist. camSampled is false. skip trace. traceId:{}, requestUrl:{}, remoteAddr:{}", traceId, request.getRequestURI(), request.getRemoteAddr());
            }
        }
        return trace;
    } else {
        final Trace trace = traceContext.newTraceObject();
        if (trace.canSampled()) {
            SpanRecorder recorder = trace.getSpanRecorder();
            recordRootSpan(recorder, request);
            setTraceMetadata(request, trace);
            if (isDebug) {
                logger.debug("TraceID not exist. start new trace. requestUrl:{}, remoteAddr:{} , traceId:{}", request.getRequestURI(), request.getRemoteAddr(), trace.getTraceId());
            }
        } else {
            if (isDebug) {
                logger.debug("TraceID not exist. camSampled is false. skip trace. requestUrl:{}, remoteAddr:{}", request.getRequestURI(), request.getRemoteAddr());
            }
        }
        return trace;
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Trace(com.navercorp.pinpoint.bootstrap.context.Trace) SpanRecorder(com.navercorp.pinpoint.bootstrap.context.SpanRecorder) TraceId(com.navercorp.pinpoint.bootstrap.context.TraceId)

Example 5 with TraceId

use of com.navercorp.pinpoint.bootstrap.context.TraceId in project pinpoint by naver.

the class ServletInvocationInterceptor method populateTraceIdFromRequest.

/**
     * Populate source trace from HTTP Header.
     *
     * @param request
     * @return TraceId when it is possible to get a transactionId from Http header. if not possible return null
     */
private TraceId populateTraceIdFromRequest(HttpServletRequest request) {
    String transactionId = request.getHeader(Header.HTTP_TRACE_ID.toString());
    if (transactionId != null) {
        long parentSpanID = NumberUtils.parseLong(request.getHeader(Header.HTTP_PARENT_SPAN_ID.toString()), SpanId.NULL);
        long spanID = NumberUtils.parseLong(request.getHeader(Header.HTTP_SPAN_ID.toString()), SpanId.NULL);
        short flags = NumberUtils.parseShort(request.getHeader(Header.HTTP_FLAGS.toString()), (short) 0);
        final TraceId id = traceContext.createTraceId(transactionId, parentSpanID, spanID, flags);
        if (isDebug) {
            logger.debug("TraceID exist. continue trace. {}", id);
        }
        return id;
    } else {
        return null;
    }
}
Also used : TraceId(com.navercorp.pinpoint.bootstrap.context.TraceId)

Aggregations

TraceId (com.navercorp.pinpoint.bootstrap.context.TraceId)28 Trace (com.navercorp.pinpoint.bootstrap.context.Trace)17 SpanEventRecorder (com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder)9 DefaultTraceId (com.navercorp.pinpoint.profiler.context.id.DefaultTraceId)9 Test (org.junit.Test)8 AsyncTraceId (com.navercorp.pinpoint.bootstrap.context.AsyncTraceId)6 TraceContext (com.navercorp.pinpoint.bootstrap.context.TraceContext)4 InterceptorScopeInvocation (com.navercorp.pinpoint.bootstrap.interceptor.scope.InterceptorScopeInvocation)3 AsyncIdGenerator (com.navercorp.pinpoint.profiler.context.id.AsyncIdGenerator)3 DefaultRecorderFactory (com.navercorp.pinpoint.profiler.context.recorder.DefaultRecorderFactory)3 RecorderFactory (com.navercorp.pinpoint.profiler.context.recorder.RecorderFactory)3 AsyncStorage (com.navercorp.pinpoint.profiler.context.storage.AsyncStorage)3 SpanStorage (com.navercorp.pinpoint.profiler.context.storage.SpanStorage)3 Storage (com.navercorp.pinpoint.profiler.context.storage.Storage)3 SqlMetaDataService (com.navercorp.pinpoint.profiler.metadata.SqlMetaDataService)3 StringMetaDataService (com.navercorp.pinpoint.profiler.metadata.StringMetaDataService)3 SpanRecorder (com.navercorp.pinpoint.bootstrap.context.SpanRecorder)2 StandardHostValveInvokeInterceptor (com.navercorp.pinpoint.plugin.jboss.interceptor.StandardHostValveInvokeInterceptor)2 ThriftRequestProperty (com.navercorp.pinpoint.plugin.thrift.ThriftRequestProperty)2 StandardHostValveInvokeInterceptor (com.navercorp.pinpoint.plugin.tomcat.interceptor.StandardHostValveInvokeInterceptor)2