|
@@ -30,10 +30,10 @@ import org.elasticsearch.common.component.AbstractLifecycleComponent;
|
|
|
import org.elasticsearch.common.settings.Settings;
|
|
|
import org.elasticsearch.common.util.Maps;
|
|
|
import org.elasticsearch.common.util.concurrent.ConcurrentCollections;
|
|
|
-import org.elasticsearch.common.util.concurrent.ThreadContext;
|
|
|
import org.elasticsearch.core.Nullable;
|
|
|
import org.elasticsearch.core.Releasable;
|
|
|
import org.elasticsearch.tasks.Task;
|
|
|
+import org.elasticsearch.telemetry.tracing.TraceContext;
|
|
|
import org.elasticsearch.telemetry.tracing.Traceable;
|
|
|
|
|
|
import java.security.AccessController;
|
|
@@ -160,8 +160,8 @@ public class APMTracer extends AbstractLifecycleComponent implements org.elastic
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void startTrace(ThreadContext threadContext, Traceable traceable, String spanName, @Nullable Map<String, Object> attributes) {
|
|
|
- assert threadContext != null;
|
|
|
+ public void startTrace(TraceContext traceContext, Traceable traceable, String spanName, @Nullable Map<String, Object> attributes) {
|
|
|
+ assert traceContext != null;
|
|
|
String spanId = traceable.getSpanId();
|
|
|
assert spanId != null;
|
|
|
assert spanName != null;
|
|
@@ -183,21 +183,21 @@ public class APMTracer extends AbstractLifecycleComponent implements org.elastic
|
|
|
|
|
|
// A span can have a parent span, which here is modelled though a parent span context.
|
|
|
// Setting this is important for seeing a complete trace in the APM UI.
|
|
|
- final Context parentContext = getParentContext(threadContext);
|
|
|
+ final Context parentContext = getParentContext(traceContext);
|
|
|
if (parentContext != null) {
|
|
|
spanBuilder.setParent(parentContext);
|
|
|
}
|
|
|
|
|
|
- setSpanAttributes(threadContext, attributes, spanBuilder);
|
|
|
+ setSpanAttributes(traceContext, attributes, spanBuilder);
|
|
|
|
|
|
- Instant startTime = threadContext.getTransient(Task.TRACE_START_TIME);
|
|
|
+ Instant startTime = traceContext.getTransient(Task.TRACE_START_TIME);
|
|
|
if (startTime != null) {
|
|
|
spanBuilder.setStartTimestamp(startTime);
|
|
|
}
|
|
|
final Span span = spanBuilder.startSpan();
|
|
|
final Context contextForNewSpan = Context.current().with(span);
|
|
|
|
|
|
- updateThreadContext(threadContext, services, contextForNewSpan);
|
|
|
+ updateThreadContext(traceContext, services, contextForNewSpan);
|
|
|
|
|
|
return contextForNewSpan;
|
|
|
}));
|
|
@@ -222,29 +222,29 @@ public class APMTracer extends AbstractLifecycleComponent implements org.elastic
|
|
|
spanBuilder.startSpan();
|
|
|
}
|
|
|
|
|
|
- private static void updateThreadContext(ThreadContext threadContext, APMServices services, Context context) {
|
|
|
+ private static void updateThreadContext(TraceContext traceContext, APMServices services, Context context) {
|
|
|
// The new span context can be used as the parent context directly within the same Java process...
|
|
|
- threadContext.putTransient(Task.APM_TRACE_CONTEXT, context);
|
|
|
+ traceContext.putTransient(Task.APM_TRACE_CONTEXT, context);
|
|
|
|
|
|
- // ...whereas for tasks sent to other ES nodes, we need to put trace HTTP headers into the threadContext so
|
|
|
+ // ...whereas for tasks sent to other ES nodes, we need to put trace HTTP headers into the traceContext so
|
|
|
// that they can be propagated.
|
|
|
- services.openTelemetry.getPropagators().getTextMapPropagator().inject(context, threadContext, (tc, key, value) -> {
|
|
|
+ services.openTelemetry.getPropagators().getTextMapPropagator().inject(context, traceContext, (tc, key, value) -> {
|
|
|
if (isSupportedContextKey(key)) {
|
|
|
tc.putHeader(key, value);
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- private Context getParentContext(ThreadContext threadContext) {
|
|
|
+ private Context getParentContext(TraceContext traceContext) {
|
|
|
// https://github.com/open-telemetry/opentelemetry-java/discussions/2884#discussioncomment-381870
|
|
|
// If you just want to propagate across threads within the same process, you don't need context propagators (extract/inject).
|
|
|
// You can just pass the Context object directly to another thread (it is immutable and thus thread-safe).
|
|
|
|
|
|
// Attempt to fetch a local parent context first, otherwise look for a remote parent
|
|
|
- Context parentContext = threadContext.getTransient("parent_" + Task.APM_TRACE_CONTEXT);
|
|
|
+ Context parentContext = traceContext.getTransient("parent_" + Task.APM_TRACE_CONTEXT);
|
|
|
if (parentContext == null) {
|
|
|
- final String traceParentHeader = threadContext.getTransient("parent_" + Task.TRACE_PARENT_HTTP_HEADER);
|
|
|
- final String traceStateHeader = threadContext.getTransient("parent_" + Task.TRACE_STATE);
|
|
|
+ final String traceParentHeader = traceContext.getTransient("parent_" + Task.TRACE_PARENT_HTTP_HEADER);
|
|
|
+ final String traceStateHeader = traceContext.getTransient("parent_" + Task.TRACE_STATE);
|
|
|
|
|
|
if (traceParentHeader != null) {
|
|
|
final Map<String, String> traceContextMap = Maps.newMapWithExpectedSize(2);
|
|
@@ -328,10 +328,10 @@ public class APMTracer extends AbstractLifecycleComponent implements org.elastic
|
|
|
spanBuilder.setAttribute(org.elasticsearch.telemetry.tracing.Tracer.AttributeKeys.CLUSTER_NAME, clusterName);
|
|
|
}
|
|
|
|
|
|
- private void setSpanAttributes(ThreadContext threadContext, @Nullable Map<String, Object> spanAttributes, SpanBuilder spanBuilder) {
|
|
|
+ private void setSpanAttributes(TraceContext traceContext, @Nullable Map<String, Object> spanAttributes, SpanBuilder spanBuilder) {
|
|
|
setSpanAttributes(spanAttributes, spanBuilder);
|
|
|
|
|
|
- final String xOpaqueId = threadContext.getHeader(Task.X_OPAQUE_ID_HTTP_HEADER);
|
|
|
+ final String xOpaqueId = traceContext.getHeader(Task.X_OPAQUE_ID_HTTP_HEADER);
|
|
|
if (xOpaqueId != null) {
|
|
|
spanBuilder.setAttribute("es.x-opaque-id", xOpaqueId);
|
|
|
}
|