|
@@ -10,6 +10,8 @@ import io.opentelemetry.api.trace.Span;
|
|
|
import io.opentelemetry.api.trace.SpanKind;
|
|
|
import io.opentelemetry.context.Context;
|
|
|
import io.opentelemetry.instrumentation.api.internal.SpanKey;
|
|
|
+import java.lang.reflect.InvocationTargetException;
|
|
|
+import java.lang.reflect.Method;
|
|
|
import java.util.Map;
|
|
|
import java.util.Set;
|
|
|
|
|
@@ -85,4 +87,48 @@ final class SpanSuppressors {
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ static class ByContextKey implements SpanSuppressor {
|
|
|
+ private final SpanSuppressor delegate;
|
|
|
+ private final Method shouldSuppressInstrumentation;
|
|
|
+
|
|
|
+ ByContextKey(SpanSuppressor delegate) {
|
|
|
+ this.delegate = delegate;
|
|
|
+ Method shouldSuppressInstrumentation;
|
|
|
+ try {
|
|
|
+ Class<?> instrumentationUtil =
|
|
|
+ Class.forName("io.opentelemetry.exporter.internal.InstrumentationUtil");
|
|
|
+ shouldSuppressInstrumentation =
|
|
|
+ instrumentationUtil.getDeclaredMethod("shouldSuppressInstrumentation", Context.class);
|
|
|
+ } catch (ClassNotFoundException | NoSuchMethodException e) {
|
|
|
+ shouldSuppressInstrumentation = null;
|
|
|
+ }
|
|
|
+ this.shouldSuppressInstrumentation = shouldSuppressInstrumentation;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Context storeInContext(Context context, SpanKind spanKind, Span span) {
|
|
|
+ return delegate.storeInContext(context, spanKind, span);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean shouldSuppress(Context parentContext, SpanKind spanKind) {
|
|
|
+ if (suppressByContextKey(parentContext)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return delegate.shouldSuppress(parentContext, spanKind);
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean suppressByContextKey(Context context) {
|
|
|
+ if (shouldSuppressInstrumentation == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ return (boolean) shouldSuppressInstrumentation.invoke(null, context);
|
|
|
+ } catch (IllegalAccessException | InvocationTargetException e) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|