Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppress instrumentation based on suppress Context key #9739

Merged
merged 8 commits into from
Nov 17, 2023
1 change: 1 addition & 0 deletions instrumentation-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dependencies {

testImplementation(project(":testing-common"))
testImplementation("io.opentelemetry:opentelemetry-sdk-testing")
testImplementation("io.opentelemetry:opentelemetry-exporter-common")
testImplementation("org.junit-pioneer:junit-pioneer")

jmhImplementation(project(":instrumentation-api-semconv"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,8 @@ private String getSchemaUrl() {
}

SpanSuppressor buildSpanSuppressor() {
return spanSuppressionStrategy.create(getSpanKeysFromAttributesExtractors());
return new SpanSuppressors.ByContextKey(
spanSuppressionStrategy.create(getSpanKeysFromAttributesExtractors()));
}

private Set<SpanKey> getSpanKeysFromAttributesExtractors() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
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;

Expand Down Expand Up @@ -85,4 +87,48 @@ public boolean shouldSuppress(Context parentContext, SpanKind spanKind) {
return true;
}
}

static class ByContextKey implements SpanSuppressor {
private final SpanSuppressor delegate;
private final Method shouldSuppressInstrumentation;

ByContextKey(SpanSuppressor delegate) {
this.delegate = delegate;
Method shouldSuppressInstrumentation;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too bad Android desugaring doesn't support MethodHandles 😢

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ikr 😩

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;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.context.Context;
import io.opentelemetry.exporter.internal.InstrumentationUtil;
import io.opentelemetry.instrumentation.api.internal.SpanKey;
import java.util.HashSet;
import java.util.Set;
Expand Down Expand Up @@ -157,4 +158,24 @@ void semconv_shouldNotSuppressContextWithPartiallyDifferentSpanKeys() {

assertFalse(suppressor.shouldSuppress(context, SpanKind.SERVER));
}

@Test
void context_shouldSuppressWhenKeyIsAvailableAndTrue() {
InstrumentationUtil.suppressInstrumentation(
() -> {
SpanSuppressor suppressor =
new SpanSuppressors.ByContextKey(SpanSuppressionStrategy.NONE.create(emptySet()));

assertTrue(suppressor.shouldSuppress(Context.current(), SpanKind.CLIENT));
});
}

@Test
void context_shouldNotSuppressWhenKeyIsNotAvailable() {
Context context = Context.current();
SpanSuppressor suppressor =
new SpanSuppressors.ByContextKey(SpanSuppressionStrategy.NONE.create(emptySet()));

assertFalse(suppressor.shouldSuppress(context, SpanKind.CLIENT));
}
}
Loading