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));
}
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();
}
}
}
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);
}
}
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();
}
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());
}
Aggregations