Search in sources :

Example 11 with TraceId

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

the class DefaultTraceTest method testPushPop.

@Test
public void testPushPop() {
    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);
    long localTransactionId = 1;
    TraceId traceId = new DefaultTraceId("agentId", System.currentTimeMillis(), localTransactionId);
    Trace trace = new DefaultTrace(callStackFactory, storage, traceId, localTransactionId, asyncIdGenerator, true, spanFactory, recorderFactory);
    trace.traceBlockBegin();
    trace.traceBlockBegin();
    trace.traceBlockEnd();
    trace.traceBlockEnd();
    trace.close();
}
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)

Example 12 with TraceId

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

the class StandardHostValveInvokeInterceptor method createTrace.

/**
     * Creates the trace.
     *
     * @param target the target
     * @param args the args
     * @return the trace
     */
private Trace createTrace(final Object target, final Object[] args) {
    final HttpServletRequest request = (HttpServletRequest) args[0];
    if (isAsynchronousProcess(request)) {
        // servlet 3.0
        final Trace trace = getTraceMetadata(request);
        if (trace != null) {
            // change api
            final SpanRecorder recorder = trace.getSpanRecorder();
            recorder.recordApi(SERVLET_ASYNCHRONOUS_API_TAG);
            // attach current thread local.
            traceContext.continueTraceObject(trace);
            return trace;
        }
    }
    final String requestURI = request.getRequestURI();
    if (excludeUrlFilter.filter(requestURI)) {
        if (isTrace) {
            logger.trace("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()) {
            final 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()) {
            final SpanRecorder recorder = trace.getSpanRecorder();
            recordRootSpan(recorder, request);
            setTraceMetadata(request, trace);
            if (isDebug) {
                logger.debug("TraceID not exist. start new trace. requestUrl:{}, remoteAddr:{}", request.getRequestURI(), request.getRemoteAddr());
            }
        } 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 13 with TraceId

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

the class StandardHostValveInvokeInterceptor method populateTraceIdFromRequest.

/**
     * Populate source trace from HTTP Header.
     *
     * @param request the request
     * @return TraceId when it is possible to get a transactionId from Http header. if not possible return null
     */
private TraceId populateTraceIdFromRequest(final HttpServletRequest request) {
    final String transactionId = request.getHeader(Header.HTTP_TRACE_ID.toString());
    if (transactionId != null) {
        final long parentSpanID = NumberUtils.parseLong(request.getHeader(Header.HTTP_PARENT_SPAN_ID.toString()), SpanId.NULL);
        final long spanID = NumberUtils.parseLong(request.getHeader(Header.HTTP_SPAN_ID.toString()), SpanId.NULL);
        final 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)

Example 14 with TraceId

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

the class HttpURLConnectionInterceptor method before.

@Override
public void before(Object target, Object[] args) {
    if (isDebug) {
        logger.beforeInterceptor(target, args);
    }
    Trace trace = traceContext.currentRawTraceObject();
    if (trace == null) {
        return;
    }
    final HttpURLConnection request = (HttpURLConnection) target;
    boolean connected = false;
    if (target instanceof ConnectedGetter) {
        connected = ((ConnectedGetter) target)._$PINPOINT$_isConnected();
    }
    boolean connecting = false;
    if (target instanceof ConnectingGetter) {
        connecting = ((ConnectingGetter) target)._$PINPOINT$_isConnecting();
    }
    if (connected || connecting) {
        return;
    }
    final boolean sampling = trace.canSampled();
    if (!sampling) {
        request.setRequestProperty(Header.HTTP_SAMPLED.toString(), SamplingFlagUtils.SAMPLING_RATE_FALSE);
        return;
    }
    scope.getCurrentInvocation().setAttachment(TRACE_BLOCK_BEGIN_MARKER);
    SpanEventRecorder recorder = trace.traceBlockBegin();
    TraceId nextId = trace.getTraceId().getNextTraceId();
    recorder.recordNextSpanId(nextId.getSpanId());
    final URL url = request.getURL();
    final String host = url.getHost();
    final int port = url.getPort();
    // TODO How to represent protocol?
    String endpoint = getEndpoint(host, port);
    request.setRequestProperty(Header.HTTP_TRACE_ID.toString(), nextId.getTransactionId());
    request.setRequestProperty(Header.HTTP_SPAN_ID.toString(), String.valueOf(nextId.getSpanId()));
    request.setRequestProperty(Header.HTTP_PARENT_SPAN_ID.toString(), String.valueOf(nextId.getParentSpanId()));
    request.setRequestProperty(Header.HTTP_FLAGS.toString(), String.valueOf(nextId.getFlags()));
    request.setRequestProperty(Header.HTTP_PARENT_APPLICATION_NAME.toString(), traceContext.getApplicationName());
    request.setRequestProperty(Header.HTTP_PARENT_APPLICATION_TYPE.toString(), Short.toString(traceContext.getServerTypeCode()));
    if (host != null) {
        request.setRequestProperty(Header.HTTP_HOST.toString(), endpoint);
    }
    recorder.recordServiceType(JdkHttpConstants.SERVICE_TYPE);
    // Don't record end point because it's same with destination id.
    recorder.recordDestinationId(endpoint);
    recorder.recordAttribute(AnnotationKey.HTTP_URL, InterceptorUtils.getHttpUrl(url.toString(), param));
}
Also used : Trace(com.navercorp.pinpoint.bootstrap.context.Trace) HttpURLConnection(java.net.HttpURLConnection) ConnectedGetter(com.navercorp.pinpoint.plugin.jdk.http.ConnectedGetter) SpanEventRecorder(com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder) ConnectingGetter(com.navercorp.pinpoint.plugin.jdk.http.ConnectingGetter) TraceId(com.navercorp.pinpoint.bootstrap.context.TraceId) URL(java.net.URL)

Example 15 with TraceId

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

the class InvokeMethodInterceptorTest method testValidHeaderExists.

@Test
public void testValidHeaderExists() {
    when(request.getRequestURI()).thenReturn("/hellotest.nhn");
    when(request.getRemoteAddr()).thenReturn("10.0.0.1");
    TraceId traceId = new DefaultTraceId("agentTest", System.currentTimeMillis(), 1);
    when(request.getHeader(Header.HTTP_TRACE_ID.toString())).thenReturn(traceId.getTransactionId());
    when(request.getHeader(Header.HTTP_PARENT_SPAN_ID.toString())).thenReturn("PARENTSPANID");
    when(request.getHeader(Header.HTTP_SPAN_ID.toString())).thenReturn("SPANID");
    when(request.getHeader(Header.HTTP_SAMPLED.toString())).thenReturn("false");
    when(request.getHeader(Header.HTTP_FLAGS.toString())).thenReturn("0");
    Enumeration<?> enumeration = mock(Enumeration.class);
    when(request.getParameterNames()).thenReturn(enumeration);
    TraceContext traceContext = spyTraceContext();
    StandardHostValveInvokeInterceptor interceptor = new StandardHostValveInvokeInterceptor(traceContext, descriptor);
    interceptor.before("target", new Object[] { request, response });
    interceptor.after("target", new Object[] { request, response }, new Object(), null);
    verify(traceContext, times(1)).continueTraceObject(any(TraceId.class));
    interceptor.before("target", new Object[] { request, response });
    interceptor.after("target", new Object[] { request, response }, new Object(), null);
    verify(traceContext, times(2)).continueTraceObject(any(TraceId.class));
}
Also used : DefaultTraceId(com.navercorp.pinpoint.profiler.context.id.DefaultTraceId) TraceId(com.navercorp.pinpoint.bootstrap.context.TraceId) DefaultTraceId(com.navercorp.pinpoint.profiler.context.id.DefaultTraceId) TraceContext(com.navercorp.pinpoint.bootstrap.context.TraceContext) StandardHostValveInvokeInterceptor(com.navercorp.pinpoint.plugin.tomcat.interceptor.StandardHostValveInvokeInterceptor) 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