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