Search in sources :

Example 6 with TraceId

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

the class TProtocolReadMessageEndInterceptor method createTrace.

private Trace createTrace(Object target, ThriftRequestProperty parentTraceInfo, String methodName) {
    // Check if parent trace info is set.
    // If it is, then make a continued trace object (from parent application)
    // If not, make a new trace object (from user cloud)
    // Check sampling flag from client. If the flag is false, do not sample this request.
    final boolean shouldSample = checkSamplingFlag(parentTraceInfo);
    if (!shouldSample) {
        // For example, if this transaction invokes rpc call, we can add parameter to tell remote node 'don't sample this transaction'
        if (isDebug) {
            logger.debug("Disable sampling flag given from remote. Skipping trace for method:{}", methodName);
        }
        return this.traceContext.disableSampling();
    }
    final TraceId traceId = populateTraceIdThriftHeader(parentTraceInfo);
    if (traceId != null) {
        // Parent trace info given
        // 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 = this.traceContext.continueTraceObject(traceId);
        if (trace.canSampled()) {
            recordRootSpan(trace, parentTraceInfo, target);
            if (isDebug) {
                logger.debug("TraceId exists - continue trace. TraceId:{}, method:{}", traceId, methodName);
            }
        } else {
            if (isDebug) {
                logger.debug("TraceId exists, canSampled is false - skip trace. TraceId:{}, method:{}", traceId, methodName);
            }
        }
        return trace;
    } else {
        // No parent trace info, start new trace
        final Trace trace = traceContext.newTraceObject();
        if (trace.canSampled()) {
            recordRootSpan(trace, parentTraceInfo, target);
            if (isDebug) {
                logger.debug("TraceId does not exist - start new trace. Method:{}", methodName);
            }
        } else {
            if (isDebug) {
                logger.debug("TraceId does not exist, canSampled is false - skip trace. Method:{}", methodName);
            }
        }
        return trace;
    }
}
Also used : Trace(com.navercorp.pinpoint.bootstrap.context.Trace) TraceId(com.navercorp.pinpoint.bootstrap.context.TraceId)

Example 7 with TraceId

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

the class HttpRequestExecutorExecuteMethodInterceptor 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;
    }
    final HttpRequest httpRequest = getHttpRequest(args);
    final boolean sampling = trace.canSampled();
    if (!sampling) {
        if (isDebug) {
            logger.debug("set Sampling flag=false");
        }
        if (httpRequest != null) {
            httpRequest.setHeader(Header.HTTP_SAMPLED.toString(), SamplingFlagUtils.SAMPLING_RATE_FALSE);
        }
        return;
    }
    final SpanEventRecorder recorder = trace.traceBlockBegin();
    TraceId nextId = trace.getTraceId().getNextTraceId();
    recorder.recordNextSpanId(nextId.getSpanId());
    recorder.recordServiceType(HttpClient4Constants.HTTP_CLIENT_4);
    if (httpRequest != null) {
        httpRequest.setHeader(Header.HTTP_TRACE_ID.toString(), nextId.getTransactionId());
        httpRequest.setHeader(Header.HTTP_SPAN_ID.toString(), String.valueOf(nextId.getSpanId()));
        httpRequest.setHeader(Header.HTTP_PARENT_SPAN_ID.toString(), String.valueOf(nextId.getParentSpanId()));
        httpRequest.setHeader(Header.HTTP_FLAGS.toString(), String.valueOf(nextId.getFlags()));
        httpRequest.setHeader(Header.HTTP_PARENT_APPLICATION_NAME.toString(), traceContext.getApplicationName());
        httpRequest.setHeader(Header.HTTP_PARENT_APPLICATION_TYPE.toString(), Short.toString(traceContext.getServerTypeCode()));
        final NameIntValuePair<String> host = getHost();
        if (host != null) {
            final String endpoint = getEndpoint(host.getName(), host.getValue());
            logger.debug("Get host {}", endpoint);
            httpRequest.setHeader(Header.HTTP_HOST.toString(), endpoint);
        }
    }
    InterceptorScopeInvocation invocation = interceptorScope.getCurrentInvocation();
    if (invocation != null) {
        invocation.getOrCreateAttachment(HttpCallContextFactory.HTTPCALL_CONTEXT_FACTORY);
    }
}
Also used : Trace(com.navercorp.pinpoint.bootstrap.context.Trace) HttpRequest(org.apache.http.HttpRequest) InterceptorScopeInvocation(com.navercorp.pinpoint.bootstrap.interceptor.scope.InterceptorScopeInvocation) SpanEventRecorder(com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder) TraceId(com.navercorp.pinpoint.bootstrap.context.TraceId)

Example 8 with TraceId

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

the class DefaultClientExchangeHandlerImplStartMethodInterceptor method before.

@Override
public void before(Object target, Object[] args) {
    if (isDebug) {
        logger.beforeInterceptor(target, "", methodDescriptor.getMethodName(), "", args);
    }
    final Trace trace = traceContext.currentRawTraceObject();
    if (trace == null) {
        return;
    }
    final HttpRequest httpRequest = getHttpRequest(target);
    final boolean sampling = trace.canSampled();
    if (!sampling) {
        if (isDebug) {
            logger.debug("set Sampling flag=false");
        }
        if (httpRequest != null) {
            httpRequest.setHeader(Header.HTTP_SAMPLED.toString(), SamplingFlagUtils.SAMPLING_RATE_FALSE);
        }
        return;
    }
    SpanEventRecorder recorder = trace.traceBlockBegin();
    // set remote trace
    final TraceId nextId = trace.getTraceId().getNextTraceId();
    recorder.recordNextSpanId(nextId.getSpanId());
    recorder.recordServiceType(HttpClient4Constants.HTTP_CLIENT_4);
    if (httpRequest != null) {
        httpRequest.setHeader(Header.HTTP_TRACE_ID.toString(), nextId.getTransactionId());
        httpRequest.setHeader(Header.HTTP_SPAN_ID.toString(), String.valueOf(nextId.getSpanId()));
        httpRequest.setHeader(Header.HTTP_PARENT_SPAN_ID.toString(), String.valueOf(nextId.getParentSpanId()));
        httpRequest.setHeader(Header.HTTP_FLAGS.toString(), String.valueOf(nextId.getFlags()));
        httpRequest.setHeader(Header.HTTP_PARENT_APPLICATION_NAME.toString(), traceContext.getApplicationName());
        httpRequest.setHeader(Header.HTTP_PARENT_APPLICATION_TYPE.toString(), Short.toString(traceContext.getServerTypeCode()));
        final NameIntValuePair<String> host = getHost(target);
        if (host != null) {
            final String endpoint = getEndpoint(host.getName(), host.getValue());
            logger.debug("Get host {}", endpoint);
            httpRequest.setHeader(Header.HTTP_HOST.toString(), endpoint);
        }
    }
    try {
        if (isAsynchronousInvocation(target, args)) {
            // set asynchronous trace
            final AsyncTraceId asyncTraceId = trace.getAsyncTraceId();
            recorder.recordNextAsyncId(asyncTraceId.getAsyncId());
            // check type isAsynchronousInvocation()
            ((AsyncTraceIdAccessor) ((ResultFutureGetter) target)._$PINPOINT$_getResultFuture())._$PINPOINT$_setAsyncTraceId(asyncTraceId);
            if (isDebug) {
                logger.debug("Set asyncTraceId metadata {}", asyncTraceId);
            }
        }
    } catch (Throwable t) {
        logger.warn("Failed to BEFORE process. {}", t.getMessage(), t);
    }
}
Also used : Trace(com.navercorp.pinpoint.bootstrap.context.Trace) HttpRequest(org.apache.http.HttpRequest) SpanEventRecorder(com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder) AsyncTraceId(com.navercorp.pinpoint.bootstrap.context.AsyncTraceId) AsyncTraceId(com.navercorp.pinpoint.bootstrap.context.AsyncTraceId) TraceId(com.navercorp.pinpoint.bootstrap.context.TraceId) AsyncTraceIdAccessor(com.navercorp.pinpoint.bootstrap.async.AsyncTraceIdAccessor)

Example 9 with TraceId

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

the class TraceTest method trace.

@Test
public void trace() {
    TraceId traceId = new DefaultTraceId("agent", 0, 1);
    CallStackFactory callStackFactory = new DefaultCallStackFactory(64);
    SpanFactory spanFactory = new DefaultSpanFactory("appName", "agentId", 0, ServiceType.STAND_ALONE);
    StringMetaDataService stringMetaDataService = mock(StringMetaDataService.class);
    SqlMetaDataService sqlMetaDataService = mock(SqlMetaDataService.class);
    RecorderFactory recorderFactory = new DefaultRecorderFactory(stringMetaDataService, sqlMetaDataService);
    AsyncIdGenerator asyncIdGenerator = mock(AsyncIdGenerator.class);
    SpanStorage storage = new SpanStorage(LoggingDataSender.DEFAULT_LOGGING_DATA_SENDER);
    Trace trace = new DefaultTrace(callStackFactory, storage, traceId, 0L, asyncIdGenerator, true, spanFactory, recorderFactory);
    trace.traceBlockBegin();
    // get data form db
    getDataFromDB(trace);
    // response to client
    trace.traceBlockEnd();
}
Also used : DefaultRecorderFactory(com.navercorp.pinpoint.profiler.context.recorder.DefaultRecorderFactory) RecorderFactory(com.navercorp.pinpoint.profiler.context.recorder.RecorderFactory) DefaultTraceId(com.navercorp.pinpoint.profiler.context.id.DefaultTraceId) DefaultRecorderFactory(com.navercorp.pinpoint.profiler.context.recorder.DefaultRecorderFactory) SpanStorage(com.navercorp.pinpoint.profiler.context.storage.SpanStorage) Trace(com.navercorp.pinpoint.bootstrap.context.Trace) StringMetaDataService(com.navercorp.pinpoint.profiler.metadata.StringMetaDataService) TraceId(com.navercorp.pinpoint.bootstrap.context.TraceId) DefaultTraceId(com.navercorp.pinpoint.profiler.context.id.DefaultTraceId) SqlMetaDataService(com.navercorp.pinpoint.profiler.metadata.SqlMetaDataService) AsyncIdGenerator(com.navercorp.pinpoint.profiler.context.id.AsyncIdGenerator) Test(org.junit.Test)

Example 10 with TraceId

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

the class TraceTest method popEventTest.

@Test
public void popEventTest() {
    TraceId traceId = new DefaultTraceId("agent", 0, 1);
    CallStackFactory callStackFactory = new DefaultCallStackFactory(64);
    SpanFactory spanFactory = new DefaultSpanFactory("appName", "agentId", 0, ServiceType.STAND_ALONE);
    StringMetaDataService stringMetaDataService = mock(StringMetaDataService.class);
    SqlMetaDataService sqlMetaDataService = mock(SqlMetaDataService.class);
    RecorderFactory recorderFactory = new DefaultRecorderFactory(stringMetaDataService, sqlMetaDataService);
    AsyncIdGenerator asyncIdGenerator = mock(AsyncIdGenerator.class);
    TestDataSender dataSender = new TestDataSender();
    SpanStorage storage = new SpanStorage(LoggingDataSender.DEFAULT_LOGGING_DATA_SENDER);
    Trace trace = new DefaultTrace(callStackFactory, storage, traceId, 0L, asyncIdGenerator, true, spanFactory, recorderFactory);
    trace.close();
    logger.debug(String.valueOf(dataSender.event));
}
Also used : DefaultRecorderFactory(com.navercorp.pinpoint.profiler.context.recorder.DefaultRecorderFactory) RecorderFactory(com.navercorp.pinpoint.profiler.context.recorder.RecorderFactory) DefaultTraceId(com.navercorp.pinpoint.profiler.context.id.DefaultTraceId) DefaultRecorderFactory(com.navercorp.pinpoint.profiler.context.recorder.DefaultRecorderFactory) SpanStorage(com.navercorp.pinpoint.profiler.context.storage.SpanStorage) Trace(com.navercorp.pinpoint.bootstrap.context.Trace) StringMetaDataService(com.navercorp.pinpoint.profiler.metadata.StringMetaDataService) TraceId(com.navercorp.pinpoint.bootstrap.context.TraceId) DefaultTraceId(com.navercorp.pinpoint.profiler.context.id.DefaultTraceId) SqlMetaDataService(com.navercorp.pinpoint.profiler.metadata.SqlMetaDataService) AsyncIdGenerator(com.navercorp.pinpoint.profiler.context.id.AsyncIdGenerator) Test(org.junit.Test)

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