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
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 @@ -9,8 +9,10 @@
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.ContextKey;
import io.opentelemetry.instrumentation.api.internal.SpanKey;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

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

static class ByContextKey implements SpanSuppressor {
private final SpanSuppressor delegate;
static final ContextKey<Boolean> SUPPRESS_INSTRUMENTATION =
ContextKey.named(
"suppress_instrumentation"); // todo must use the key defined in the Java SDK.
trask marked this conversation as resolved.
Show resolved Hide resolved

ByContextKey(SpanSuppressor delegate) {
this.delegate = delegate;
}

@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 (Objects.equals(parentContext.get(SUPPRESS_INSTRUMENTATION), true)) {
return true;
}
return delegate.shouldSuppress(parentContext, spanKind);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package io.opentelemetry.instrumentation.api.instrumenter;

import static io.opentelemetry.instrumentation.api.instrumenter.SpanSuppressors.ByContextKey.SUPPRESS_INSTRUMENTATION;
import static java.util.Arrays.asList;
import static java.util.Collections.emptySet;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -157,4 +158,31 @@ void semconv_shouldNotSuppressContextWithPartiallyDifferentSpanKeys() {

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

@Test
void context_shouldSuppressWhenKeyIsAvailableAndTrue() {
Context context = Context.current().with(SUPPRESS_INSTRUMENTATION, true);
SpanSuppressor suppressor =
new SpanSuppressors.ByContextKey(SpanSuppressionStrategy.NONE.create(emptySet()));

assertTrue(suppressor.shouldSuppress(context, SpanKind.CLIENT));
}

@Test
void context_shouldNotSuppressWhenKeyIsAvailableAndFalse() {
Context context = Context.current().with(SUPPRESS_INSTRUMENTATION, false);
SpanSuppressor suppressor =
new SpanSuppressors.ByContextKey(SpanSuppressionStrategy.NONE.create(emptySet()));

assertFalse(suppressor.shouldSuppress(context, 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));
}
}