use of com.navercorp.pinpoint.bootstrap.context.TraceId in project pinpoint by naver.
the class DefaultTraceIdFactory method newTraceId.
@Override
public TraceId newTraceId() {
final long localTransactionId = idGenerator.nextTransactionId();
final TraceId traceId = new DefaultTraceId(agentId, agentStartTime, localTransactionId);
return traceId;
}
use of com.navercorp.pinpoint.bootstrap.context.TraceId in project pinpoint by naver.
the class DefaultBaseTraceFactory method continueAsyncTraceObject.
// internal async trace.
@Override
public Trace continueAsyncTraceObject(AsyncTraceId traceId, int asyncId, long startTime) {
final TraceId parentTraceId = traceId.getParentTraceId();
final Storage storage = storageFactory.createStorage();
final Storage asyncStorage = new AsyncStorage(storage);
final Trace trace = new DefaultTrace(callStackFactory, asyncStorage, parentTraceId, AtomicIdGenerator.UNTRACKED_ID, asyncIdGenerator, true, spanFactory, recorderFactory);
final AsyncTrace asyncTrace = new AsyncTrace(trace, asyncId, traceId.nextAsyncSequence(), startTime);
return asyncTrace;
}
use of com.navercorp.pinpoint.bootstrap.context.TraceId in project pinpoint by naver.
the class TServiceClientSendBaseInterceptor method before.
@Override
public void before(Object target, Object[] args) {
if (isDebug) {
logger.beforeInterceptor(target, args);
}
if (target instanceof TServiceClient) {
TServiceClient client = (TServiceClient) target;
TProtocol oprot = client.getOutputProtocol();
TTransport transport = oprot.getTransport();
final Trace trace = traceContext.currentRawTraceObject();
if (trace == null) {
return;
}
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();
recorder.recordServiceType(ThriftConstants.THRIFT_CLIENT);
// retrieve connection information
String remoteAddress = ThriftConstants.UNKNOWN_ADDRESS;
if (transport instanceof SocketFieldAccessor) {
Socket socket = ((SocketFieldAccessor) transport)._$PINPOINT$_getSocket();
if (socket != null) {
remoteAddress = ThriftUtils.getHostPort(socket.getRemoteSocketAddress());
}
} else {
if (isDebug) {
logger.debug("Invalid target object. Need field accessor({}).", SocketFieldAccessor.class.getName());
}
}
recorder.recordDestinationId(remoteAddress);
String methodName = ThriftConstants.UNKNOWN_METHOD_NAME;
if (args[0] instanceof String) {
methodName = (String) args[0];
}
String serviceName = ThriftUtils.getClientServiceName(client);
String thriftUrl = getServiceUrl(remoteAddress, serviceName, methodName);
recorder.recordAttribute(ThriftConstants.THRIFT_URL, thriftUrl);
TraceId nextId = trace.getTraceId().getNextTraceId();
recorder.recordNextSpanId(nextId.getSpanId());
parentTraceInfo.setTraceId(nextId.getTransactionId());
parentTraceInfo.setSpanId(nextId.getSpanId());
parentTraceInfo.setParentSpanId(nextId.getParentSpanId());
parentTraceInfo.setFlags(nextId.getFlags());
parentTraceInfo.setParentApplicationName(traceContext.getApplicationName());
parentTraceInfo.setParentApplicationType(traceContext.getServerTypeCode());
parentTraceInfo.setAcceptorHost(remoteAddress);
}
InterceptorScopeInvocation currentTransaction = this.scope.getCurrentInvocation();
currentTransaction.setAttachment(parentTraceInfo);
}
}
use of com.navercorp.pinpoint.bootstrap.context.TraceId in project pinpoint by naver.
the class ServletInvocationInterceptor method createTrace.
private Trace createTrace(Object target, Object[] args) {
final HttpServletRequest request = (HttpServletRequest) args[0];
if (isAsynchronousProcess(request)) {
// servlet 3.0
final Trace trace = getTraceMetadata(request);
if (trace != null) {
// change api
SpanRecorder recorder = trace.getSpanRecorder();
recorder.recordApi(SERVLET_ASYNCHRONOUS_API_TAG);
// attach current thread local.
traceContext.continueTraceObject(trace);
return trace;
}
}
final Trace currentRawTraceObject = traceContext.currentRawTraceObject();
if (currentRawTraceObject != null) {
return currentRawTraceObject;
}
final String requestURI = request.getRequestURI();
if (excludeUrlFilter.filter(requestURI)) {
if (isDebug) {
logger.debug("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()) {
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()) {
SpanRecorder recorder = trace.getSpanRecorder();
recordRootSpan(recorder, request);
setTraceMetadata(request, trace);
if (isDebug) {
logger.debug("TraceID not exist. start new trace. requestUrl:{}, remoteAddr:{} , traceId:{}", request.getRequestURI(), request.getRemoteAddr(), trace.getTraceId());
}
} else {
if (isDebug) {
logger.debug("TraceID not exist. camSampled is false. skip trace. requestUrl:{}, remoteAddr:{}", request.getRequestURI(), request.getRemoteAddr());
}
}
return trace;
}
}
use of com.navercorp.pinpoint.bootstrap.context.TraceId in project pinpoint by naver.
the class ServletInvocationInterceptor method populateTraceIdFromRequest.
/**
* Populate source trace from HTTP Header.
*
* @param request
* @return TraceId when it is possible to get a transactionId from Http header. if not possible return null
*/
private TraceId populateTraceIdFromRequest(HttpServletRequest request) {
String transactionId = request.getHeader(Header.HTTP_TRACE_ID.toString());
if (transactionId != null) {
long parentSpanID = NumberUtils.parseLong(request.getHeader(Header.HTTP_PARENT_SPAN_ID.toString()), SpanId.NULL);
long spanID = NumberUtils.parseLong(request.getHeader(Header.HTTP_SPAN_ID.toString()), SpanId.NULL);
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;
}
}
Aggregations