use of com.navercorp.pinpoint.bootstrap.context.TraceId in project pinpoint by naver.
the class ExecuteRequestInterceptor 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;
}
if (args.length == 0 || !(args[0] instanceof com.ning.http.client.Request)) {
return;
}
final com.ning.http.client.Request httpRequest = (com.ning.http.client.Request) args[0];
final boolean sampling = trace.canSampled();
if (!sampling) {
if (isDebug) {
logger.debug("set Sampling flag=false");
}
if (httpRequest != null) {
final FluentCaseInsensitiveStringsMap httpRequestHeaders = httpRequest.getHeaders();
httpRequestHeaders.add(Header.HTTP_SAMPLED.toString(), SamplingFlagUtils.SAMPLING_RATE_FALSE);
}
return;
}
trace.traceBlockBegin();
SpanEventRecorder recorder = trace.currentSpanEventRecorder();
TraceId nextId = trace.getTraceId().getNextTraceId();
recorder.recordNextSpanId(nextId.getSpanId());
recorder.recordServiceType(NingAsyncHttpClientPlugin.ASYNC_HTTP_CLIENT);
if (httpRequest != null) {
final FluentCaseInsensitiveStringsMap httpRequestHeaders = httpRequest.getHeaders();
putHeader(httpRequestHeaders, Header.HTTP_TRACE_ID.toString(), nextId.getTransactionId());
putHeader(httpRequestHeaders, Header.HTTP_SPAN_ID.toString(), String.valueOf(nextId.getSpanId()));
putHeader(httpRequestHeaders, Header.HTTP_PARENT_SPAN_ID.toString(), String.valueOf(nextId.getParentSpanId()));
putHeader(httpRequestHeaders, Header.HTTP_FLAGS.toString(), String.valueOf(nextId.getFlags()));
putHeader(httpRequestHeaders, Header.HTTP_PARENT_APPLICATION_NAME.toString(), traceContext.getApplicationName());
putHeader(httpRequestHeaders, Header.HTTP_PARENT_APPLICATION_TYPE.toString(), Short.toString(traceContext.getServerTypeCode()));
final String hostString = getEndpoint(httpRequest.getURI().getHost(), httpRequest.getURI().getPort());
if (hostString != null) {
putHeader(httpRequestHeaders, Header.HTTP_HOST.toString(), hostString);
}
}
}
use of com.navercorp.pinpoint.bootstrap.context.TraceId in project pinpoint by naver.
the class InvokeMethodInterceptorTest method testInvalidHeaderExists.
/**
* Test invalid header exists.
*/
@SuppressWarnings("unchecked")
@Test
public void testInvalidHeaderExists() {
when(request.getRequestURI()).thenReturn("/hellotest.nhn");
when(request.getRemoteAddr()).thenReturn("10.0.0.1");
when(request.getHeader(Header.HTTP_TRACE_ID.toString())).thenReturn("TRACEID");
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, never()).newTraceObject();
verify(traceContext, never()).disableSampling();
verify(traceContext, never()).continueTraceObject(any(TraceId.class));
interceptor.before("target", new Object[] { request, response });
interceptor.after("target", new Object[] { request, response }, new Object(), null);
verify(traceContext, never()).newTraceObject();
verify(traceContext, never()).disableSampling();
verify(traceContext, never()).continueTraceObject(any(TraceId.class));
}
use of com.navercorp.pinpoint.bootstrap.context.TraceId 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.TraceId in project pinpoint by naver.
the class TAsyncClientManagerCallInterceptor method before.
@Override
public void before(Object target, Object[] args) {
if (isDebug) {
logger.beforeInterceptor(target, args);
}
if (!validate(target, args)) {
return;
}
final Trace trace = this.traceContext.currentRawTraceObject();
if (trace == null) {
return;
}
try {
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();
Object asyncMethodCallObj = args[0];
// inject async trace info to AsyncMethodCall object
final AsyncTraceId asyncTraceId = injectAsyncTraceId(asyncMethodCallObj, trace);
// retrieve connection information
String remoteAddress = getRemoteAddress(asyncMethodCallObj);
final TraceId nextId = asyncTraceId.getNextTraceId();
// Inject nextSpanId as the actual sending of data will be handled asynchronously.
final long nextSpanId = nextId.getSpanId();
parentTraceInfo.setSpanId(nextSpanId);
parentTraceInfo.setTraceId(nextId.getTransactionId());
parentTraceInfo.setParentSpanId(nextId.getParentSpanId());
parentTraceInfo.setFlags(nextId.getFlags());
parentTraceInfo.setParentApplicationName(this.traceContext.getApplicationName());
parentTraceInfo.setParentApplicationType(this.traceContext.getServerTypeCode());
parentTraceInfo.setAcceptorHost(remoteAddress);
recorder.recordServiceType(ThriftConstants.THRIFT_CLIENT);
recorder.recordNextSpanId(nextSpanId);
recorder.recordDestinationId(remoteAddress);
String methodUri = ThriftUtils.getAsyncMethodCallName((TAsyncMethodCall<?>) asyncMethodCallObj);
String thriftUrl = remoteAddress + "/" + methodUri;
recorder.recordAttribute(ThriftConstants.THRIFT_URL, thriftUrl);
}
InterceptorScopeInvocation currentTransaction = this.scope.getCurrentInvocation();
currentTransaction.setAttachment(parentTraceInfo);
} catch (Throwable t) {
logger.warn("BEFORE error. Caused:{}", t.getMessage(), t);
}
}
use of com.navercorp.pinpoint.bootstrap.context.TraceId in project pinpoint by naver.
the class DefaultBaseTraceFactory method newAsyncTraceObject.
// entry point async trace.
@InterfaceAudience.LimitedPrivate("vert.x")
@Override
public Trace newAsyncTraceObject() {
final boolean sampling = sampler.isSampling();
if (sampling) {
final Storage storage = storageFactory.createStorage();
final TraceId traceId = traceIdFactory.newTraceId();
final long localTransactionId = traceId.getTransactionSequence();
final DefaultTrace trace = new DefaultTrace(callStackFactory, storage, traceId, localTransactionId, asyncIdGenerator, true, spanFactory, recorderFactory);
final SpanAsyncStateListener asyncStateListener = new SpanAsyncStateListener(trace.getSpan(), storageFactory.createStorage());
final AsyncState closer = new ListenableAsyncState(asyncStateListener);
final AsyncTrace asyncTrace = new AsyncTrace(trace, closer);
return asyncTrace;
} else {
return newDisableTrace();
}
}
Aggregations