Move ContextPropagationDebug to internal package
And use it in the Instrumenter API
MODIFY
instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/ServerInstrumenter.java
instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/ServerInstrumenter.java
@@ -9,6 +9,7 @@ import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.context.propagation.TextMapGetter;
+import io.opentelemetry.instrumentation.api.internal.ContextPropagationDebug;
import java.util.List;
final class ServerInstrumenter<REQUEST, RESPONSE> extends Instrumenter<REQUEST, RESPONSE> {
@@ -40,6 +41,8 @@ final class ServerInstrumenter<REQUEST, RESPONSE> extends Instrumenter<REQUEST,
@Override
public Context start(Context parentContext, REQUEST request) {
+ ContextPropagationDebug.debugContextLeakIfEnabled();
+
Context extracted = propagators.getTextMapPropagator().extract(parentContext, request, getter);
return super.start(extracted, request);
}

RENAME
instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/context/ContextPropagationDebug.java
instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/internal/ContextPropagationDebug.java
* SPDX-License-Identifier: Apache-2.0
*/
-package io.opentelemetry.instrumentation.api.context;
+package io.opentelemetry.instrumentation.api.internal;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
@@ -11,6 +11,7 @@ import io.opentelemetry.context.ContextKey;
import io.opentelemetry.instrumentation.api.config.Config;
import java.util.Iterator;
import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -35,12 +36,14 @@ public final class ContextPropagationDebug {
return THREAD_PROPAGATION_DEBUGGER;
}
- public static List<StackTraceElement[]> getLocations(Context context) {
- return context.get(THREAD_PROPAGATION_LOCATIONS);
+ public static Context appendLocations(Context context, StackTraceElement[] locations) {
+ List<StackTraceElement[]> currentLocations = ContextPropagationDebug.getLocations(context);
+ if (currentLocations == null) {
+ currentLocations = new CopyOnWriteArrayList<>();
+ context = context.with(THREAD_PROPAGATION_LOCATIONS, currentLocations);
}
-
- public static Context withLocations(List<StackTraceElement[]> locations, Context context) {
- return context.with(THREAD_PROPAGATION_LOCATIONS, locations);
+ currentLocations.add(0, locations);
+ return context;
}
public static void debugContextLeakIfEnabled() {
@@ -64,8 +67,12 @@ public final class ContextPropagationDebug {
}
}
+ private static List<StackTraceElement[]> getLocations(Context context) {
+ return context.get(THREAD_PROPAGATION_LOCATIONS);
+ }
+
private static void debugContextPropagation(Context context) {
- List<StackTraceElement[]> locations = ContextPropagationDebug.getLocations(context);
+ List<StackTraceElement[]> locations = getLocations(context);
if (locations != null) {
StringBuilder sb = new StringBuilder();
Iterator<StackTraceElement[]> i = locations.iterator();

MODIFY
instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/tracer/BaseTracer.java
instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/tracer/BaseTracer.java
@@ -18,7 +18,7 @@ import io.opentelemetry.context.propagation.TextMapGetter;
import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.context.propagation.TextMapSetter;
import io.opentelemetry.instrumentation.api.InstrumentationVersion;
-import io.opentelemetry.instrumentation.api.context.ContextPropagationDebug;
+import io.opentelemetry.instrumentation.api.internal.ContextPropagationDebug;
import io.opentelemetry.instrumentation.api.internal.SupportabilityMetrics;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

MODIFY
javaagent-api/src/main/java/io/opentelemetry/javaagent/instrumentation/api/concurrent/ExecutorInstrumentationUtils.java
javaagent-api/src/main/java/io/opentelemetry/javaagent/instrumentation/api/concurrent/ExecutorInstrumentationUtils.java
package io.opentelemetry.javaagent.instrumentation.api.concurrent;
import io.opentelemetry.context.Context;
-import io.opentelemetry.instrumentation.api.context.ContextPropagationDebug;
+import io.opentelemetry.instrumentation.api.internal.ContextPropagationDebug;
import io.opentelemetry.javaagent.instrumentation.api.ContextStore;
-import java.util.List;
-import java.util.concurrent.CopyOnWriteArrayList;
/** Utils for concurrent instrumentations. */
public class ExecutorInstrumentationUtils {
@@ -130,12 +128,7 @@ public class ExecutorInstrumentationUtils {
public static <T> State setupState(ContextStore<T, State> contextStore, T task, Context context) {
State state = contextStore.putIfAbsent(task, State.FACTORY);
if (ContextPropagationDebug.isThreadPropagationDebuggerEnabled()) {
- List<StackTraceElement[]> locations = ContextPropagationDebug.getLocations(context);
- if (locations == null) {
- locations = new CopyOnWriteArrayList<>();
- context = ContextPropagationDebug.withLocations(locations, context);
- }
- locations.add(0, new Exception().getStackTrace());
+ context = ContextPropagationDebug.appendLocations(context, new Exception().getStackTrace());
}
state.setParentContext(context);
return state;

Move ContextPropagationDebug to internal package (#2804)

And use it in the Instrumenter API