/** * Opens a new {@link SpanType#LOCAL LOCAL} span for this thread's call trace, labeled with the provided operation. */ public static CloseableTracer startSpan(String operation) { Tracer.startSpan(operation); return INSTANCE; }
/** * Opens a new {@link SpanType#LOCAL LOCAL} span for this thread's call trace, labeled with the provided operation. */ public static CloseableTracer startSpan(String operation) { Tracer.startSpan(operation); return INSTANCE; }
@Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); String spanName = request.method(); String httpRemotingPath = request.header(PATH_TEMPLATE_HEADER); if (httpRemotingPath != null) { spanName = httpRemotingPath; request = request.newBuilder().removeHeader(PATH_TEMPLATE_HEADER).build(); } OpenSpan span = Tracer.startSpan(spanName, SpanType.CLIENT_OUTGOING); Request.Builder tracedRequest = request.newBuilder() .addHeader(TraceHttpHeaders.TRACE_ID, Tracer.getTraceId()) .addHeader(TraceHttpHeaders.SPAN_ID, span.getSpanId()) .addHeader(TraceHttpHeaders.IS_SAMPLED, Tracer.isTraceObservable() ? "1" : "0"); if (span.getParentSpanId().isPresent()) { tracedRequest.header(TraceHttpHeaders.PARENT_SPAN_ID, span.getParentSpanId().get()); } Response response; try { response = chain.proceed(tracedRequest.build()); } finally { Tracer.completeSpan(); } return response; }
@Override public void filter(ContainerRequestContext requestContext) throws IOException { String path = Optional.ofNullable(uriInfo) .map(ExtendedUriInfo::getMatchedModelResource) .map(Resource::getPath) .orElse("(unknown)"); String operation = requestContext.getMethod() + " " + path; // The following strings are all nullable String traceId = requestContext.getHeaderString(TraceHttpHeaders.TRACE_ID); String spanId = requestContext.getHeaderString(TraceHttpHeaders.SPAN_ID); // Set up thread-local span that inherits state from HTTP headers if (Strings.isNullOrEmpty(traceId)) { // HTTP request did not indicate a trace; initialize trace state and create a span. Tracer.initTrace(hasSampledHeader(requestContext), Tracers.randomId()); Tracer.startSpan(operation, SpanType.SERVER_INCOMING); } else { Tracer.initTrace(hasSampledHeader(requestContext), traceId); if (spanId == null) { Tracer.startSpan(operation, SpanType.SERVER_INCOMING); } else { // caller's span is this span's parent. Tracer.startSpan(operation, spanId, SpanType.SERVER_INCOMING); } } // Give asynchronous downstream handlers access to the trace id requestContext.setProperty("com.palantir.conjure.java.traceId", Tracer.getTraceId()); }