Search in sources :

Example 21 with TraceContext

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

the class InvokeMethodInterceptorTest method testValidHeaderExists.

/**
     * Test valid header exists.
     */
@SuppressWarnings("unchecked")
@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");
    final Enumeration<?> enumeration = mock(Enumeration.class);
    when(request.getParameterNames()).thenReturn((Enumeration<String>) enumeration);
    TraceContext traceContext = spyTraceContext();
    final 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.jboss.interceptor.StandardHostValveInvokeInterceptor) Test(org.junit.Test)

Example 22 with TraceContext

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

the class PinpointJUnit4ClassRunner method endTracing.

private void endTracing(FrameworkMethod method, RunNotifier notifier) {
    if (shouldCreateNewTraceObject(method)) {
        TraceContext traceContext = getTraceContext();
        try {
            Trace trace = traceContext.currentRawTraceObject();
            if (trace == null) {
                // Trace is already detached from the ThreadLocal storage.
                // Happens when root trace method is tested without @IsRootSpan.
                EachTestNotifier testMethodNotifier = new EachTestNotifier(notifier, super.describeChild(method));
                String traceObjectAlreadyDetachedMessage = "Trace object already detached. If you're testing a trace root, please add @IsRootSpan to the test method";
                testMethodNotifier.addFailure(new IllegalStateException(traceObjectAlreadyDetachedMessage));
            } else {
                trace.close();
            }
        } finally {
            traceContext.removeTraceObject();
        }
    }
}
Also used : Trace(com.navercorp.pinpoint.bootstrap.context.Trace) EachTestNotifier(org.junit.internal.runners.model.EachTestNotifier) TraceContext(com.navercorp.pinpoint.bootstrap.context.TraceContext)

Example 23 with TraceContext

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

the class PinpointJUnit4ClassRunner method beginTracing.

private void beginTracing(FrameworkMethod method) {
    if (shouldCreateNewTraceObject(method)) {
        TraceContext traceContext = getTraceContext();
        Trace trace = traceContext.newTraceObject();
        SpanRecorder recorder = trace.getSpanRecorder();
        recorder.recordServiceType(ServiceType.TEST);
    }
}
Also used : Trace(com.navercorp.pinpoint.bootstrap.context.Trace) SpanRecorder(com.navercorp.pinpoint.bootstrap.context.SpanRecorder) TraceContext(com.navercorp.pinpoint.bootstrap.context.TraceContext)

Example 24 with TraceContext

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

the class InvokeMethodInterceptorTest method testHeaderNOTExists.

@Test
public void testHeaderNOTExists() {
    when(request.getRequestURI()).thenReturn("/hellotest.nhn");
    when(request.getRemoteAddr()).thenReturn("10.0.0.1");
    when(request.getHeader(Header.HTTP_TRACE_ID.toString())).thenReturn(null);
    when(request.getHeader(Header.HTTP_PARENT_SPAN_ID.toString())).thenReturn(null);
    when(request.getHeader(Header.HTTP_SPAN_ID.toString())).thenReturn(null);
    when(request.getHeader(Header.HTTP_SAMPLED.toString())).thenReturn(null);
    when(request.getHeader(Header.HTTP_FLAGS.toString())).thenReturn(null);
    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)).newTraceObject();
    interceptor.before("target", new Object[] { request, response });
    interceptor.after("target", new Object[] { request, response }, new Object(), null);
    verify(traceContext, times(2)).newTraceObject();
}
Also used : TraceContext(com.navercorp.pinpoint.bootstrap.context.TraceContext) StandardHostValveInvokeInterceptor(com.navercorp.pinpoint.plugin.tomcat.interceptor.StandardHostValveInvokeInterceptor) Test(org.junit.Test)

Example 25 with TraceContext

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

the class DefaultTraceContextTest method transactionCountTest.

@Test
public void transactionCountTest() {
    final int samplingRate = 5;
    final ProfilerConfig profilerConfig = Mockito.mock(ProfilerConfig.class);
    Mockito.when(profilerConfig.isTraceAgentActiveThread()).thenReturn(true);
    Mockito.when((profilerConfig.getSamplingRate())).thenReturn(samplingRate);
    Mockito.when((profilerConfig.isSamplingEnable())).thenReturn(true);
    MockTraceContextFactory mockTraceContextFactory = MockTraceContextFactory.newTestTraceContextFactory(profilerConfig);
    final TraceContext traceContext = mockTraceContextFactory.getTraceContext();
    final TransactionCounter transactionCounter = new DefaultTransactionCounter(mockTraceContextFactory.getIdGenerator());
    final long newTransactionCount = 22L;
    @SuppressWarnings("unused") final long expectedSampledNewCount = newTransactionCount / samplingRate + (newTransactionCount % samplingRate > 0 ? 1 : 0);
    final long expectedUnsampledNewCount = newTransactionCount - expectedSampledNewCount;
    for (int i = 0; i < newTransactionCount; ++i) {
        traceContext.newTraceObject();
        traceContext.removeTraceObject();
    }
    final long expectedSampledContinuationCount = 5L;
    for (int i = 0; i < expectedSampledContinuationCount; ++i) {
        traceContext.continueTraceObject(new DefaultTraceId("agentId", 0L, i));
        traceContext.removeTraceObject();
    }
    final long expectedUnsampledContinuationCount = 10L;
    for (int i = 0; i < expectedUnsampledContinuationCount; ++i) {
        traceContext.disableSampling();
        traceContext.removeTraceObject();
    }
    final long expectedTotalTransactionCount = expectedSampledNewCount + expectedUnsampledNewCount + expectedSampledContinuationCount + expectedUnsampledContinuationCount;
    Assert.assertEquals(expectedSampledNewCount, transactionCounter.getSampledNewCount());
    Assert.assertEquals(expectedUnsampledNewCount, transactionCounter.getUnSampledNewCount());
    Assert.assertEquals(expectedSampledContinuationCount, transactionCounter.getSampledContinuationCount());
    Assert.assertEquals(expectedUnsampledContinuationCount, transactionCounter.getUnSampledContinuationCount());
    Assert.assertEquals(expectedTotalTransactionCount, transactionCounter.getTotalTransactionCount());
}
Also used : DefaultTransactionCounter(com.navercorp.pinpoint.profiler.context.id.DefaultTransactionCounter) DefaultTraceId(com.navercorp.pinpoint.profiler.context.id.DefaultTraceId) ProfilerConfig(com.navercorp.pinpoint.bootstrap.config.ProfilerConfig) DefaultProfilerConfig(com.navercorp.pinpoint.bootstrap.config.DefaultProfilerConfig) TraceContext(com.navercorp.pinpoint.bootstrap.context.TraceContext) DefaultTransactionCounter(com.navercorp.pinpoint.profiler.context.id.DefaultTransactionCounter) TransactionCounter(com.navercorp.pinpoint.profiler.context.id.TransactionCounter) Test(org.junit.Test)

Aggregations

TraceContext (com.navercorp.pinpoint.bootstrap.context.TraceContext)25 Test (org.junit.Test)17 Trace (com.navercorp.pinpoint.bootstrap.context.Trace)8 DefaultProfilerConfig (com.navercorp.pinpoint.bootstrap.config.DefaultProfilerConfig)5 ProfilerConfig (com.navercorp.pinpoint.bootstrap.config.ProfilerConfig)5 DefaultTraceId (com.navercorp.pinpoint.profiler.context.id.DefaultTraceId)5 SpanRecorder (com.navercorp.pinpoint.bootstrap.context.SpanRecorder)4 TraceId (com.navercorp.pinpoint.bootstrap.context.TraceId)4 MethodDescriptor (com.navercorp.pinpoint.bootstrap.context.MethodDescriptor)3 StandardHostValveInvokeInterceptor (com.navercorp.pinpoint.plugin.jboss.interceptor.StandardHostValveInvokeInterceptor)3 StandardHostValveInvokeInterceptor (com.navercorp.pinpoint.plugin.tomcat.interceptor.StandardHostValveInvokeInterceptor)3 SpanEventRecorder (com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder)2 JdbcUrlParser (com.navercorp.pinpoint.bootstrap.plugin.jdbc.JdbcUrlParser)2 Driver (java.sql.Driver)2 Matchers.anyString (org.mockito.Matchers.anyString)2 InstrumentClass (com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass)1 InstrumentContext (com.navercorp.pinpoint.bootstrap.instrument.InstrumentContext)1 InstrumentMethod (com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod)1 DatabaseInfoAccessor (com.navercorp.pinpoint.bootstrap.plugin.jdbc.DatabaseInfoAccessor)1 DefaultTransactionCounter (com.navercorp.pinpoint.profiler.context.id.DefaultTransactionCounter)1