Skip to content

Commit 8f3460a

Browse files
authored
suppress instrumentation: move to api + generic context key (#6546)
1 parent 958b59b commit 8f3460a

File tree

7 files changed

+80
-10
lines changed

7 files changed

+80
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.api.internal;
7+
8+
import io.opentelemetry.context.Context;
9+
import io.opentelemetry.context.ContextKey;
10+
import java.util.Objects;
11+
12+
/**
13+
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
14+
* any time.
15+
*/
16+
public final class InstrumentationUtil {
17+
private static final ContextKey<Boolean> SUPPRESS_INSTRUMENTATION_KEY =
18+
ContextKey.named("suppress_instrumentation");
19+
20+
private InstrumentationUtil() {}
21+
22+
/**
23+
* Adds a Context boolean key that will allow to identify HTTP calls coming from OTel exporters.
24+
* The key later be checked by an automatic instrumentation to avoid tracing OTel exporter's
25+
* calls.
26+
*/
27+
public static void suppressInstrumentation(Runnable runnable) {
28+
Context.current().with(SUPPRESS_INSTRUMENTATION_KEY, true).wrap(runnable).run();
29+
}
30+
31+
/**
32+
* Checks if an automatic instrumentation should be suppressed with the provided Context.
33+
*
34+
* @return TRUE to suppress the automatic instrumentation, FALSE to continue with the
35+
* instrumentation.
36+
*/
37+
public static boolean shouldSuppressInstrumentation(Context context) {
38+
return Objects.equals(context.get(SUPPRESS_INSTRUMENTATION_KEY), true);
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.api.internal;
7+
8+
import static org.junit.jupiter.api.Assertions.assertFalse;
9+
import static org.junit.jupiter.api.Assertions.assertTrue;
10+
11+
import io.opentelemetry.context.Context;
12+
import org.junit.jupiter.api.Test;
13+
14+
class InstrumentationUtilTest {
15+
@Test
16+
void verifySuppressInstrumentation() {
17+
// Should be false by default.
18+
assertFalse(InstrumentationUtil.shouldSuppressInstrumentation(Context.current()));
19+
20+
// Should be true inside the Runnable passed to InstrumentationUtil.suppressInstrumentation.
21+
InstrumentationUtil.suppressInstrumentation(
22+
() -> assertTrue(InstrumentationUtil.shouldSuppressInstrumentation(Context.current())));
23+
24+
// Should be false after the runnable finishes.
25+
assertFalse(InstrumentationUtil.shouldSuppressInstrumentation(Context.current()));
26+
}
27+
}

exporters/common/src/main/java/io/opentelemetry/exporter/internal/InstrumentationUtil.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
package io.opentelemetry.exporter.internal;
77

88
import io.opentelemetry.context.Context;
9-
import io.opentelemetry.context.ContextKey;
10-
import java.util.Objects;
119

1210
/**
1311
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
14-
* any time.
12+
* any time
13+
*
14+
* @deprecated use {@link io.opentelemetry.api.internal.InstrumentationUtil} instead. This class
15+
* should be removed once instrumentation does not refer to it anymore.
1516
*/
17+
@Deprecated
1618
public final class InstrumentationUtil {
17-
private static final ContextKey<Boolean> SUPPRESS_INSTRUMENTATION_KEY =
18-
ContextKey.named("suppress_internal_exporter_instrumentation");
1919

2020
private InstrumentationUtil() {}
2121

@@ -25,7 +25,7 @@ private InstrumentationUtil() {}
2525
* calls.
2626
*/
2727
public static void suppressInstrumentation(Runnable runnable) {
28-
Context.current().with(SUPPRESS_INSTRUMENTATION_KEY, true).wrap(runnable).run();
28+
io.opentelemetry.api.internal.InstrumentationUtil.suppressInstrumentation(runnable);
2929
}
3030

3131
/**
@@ -35,6 +35,6 @@ public static void suppressInstrumentation(Runnable runnable) {
3535
* instrumentation.
3636
*/
3737
public static boolean shouldSuppressInstrumentation(Context context) {
38-
return Objects.equals(context.get(SUPPRESS_INSTRUMENTATION_KEY), true);
38+
return io.opentelemetry.api.internal.InstrumentationUtil.shouldSuppressInstrumentation(context);
3939
}
4040
}

exporters/common/src/test/java/io/opentelemetry/exporter/internal/InstrumentationUtilTest.java

+3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
import org.junit.jupiter.api.Test;
1313

1414
class InstrumentationUtilTest {
15+
16+
// testing deprecated implementation until it's removed
1517
@Test
18+
@SuppressWarnings("deprecation")
1619
void verifySuppressInstrumentation() {
1720
// Should be false by default.
1821
assertFalse(InstrumentationUtil.shouldSuppressInstrumentation(Context.current()));

exporters/sender/okhttp/src/main/java/io/opentelemetry/exporter/sender/okhttp/internal/OkHttpGrpcSender.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
package io.opentelemetry.exporter.sender.okhttp.internal;
2525

26-
import io.opentelemetry.exporter.internal.InstrumentationUtil;
26+
import io.opentelemetry.api.internal.InstrumentationUtil;
2727
import io.opentelemetry.exporter.internal.RetryUtil;
2828
import io.opentelemetry.exporter.internal.compression.Compressor;
2929
import io.opentelemetry.exporter.internal.grpc.GrpcExporterUtil;

exporters/sender/okhttp/src/main/java/io/opentelemetry/exporter/sender/okhttp/internal/OkHttpHttpSender.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
package io.opentelemetry.exporter.sender.okhttp.internal;
77

8-
import io.opentelemetry.exporter.internal.InstrumentationUtil;
8+
import io.opentelemetry.api.internal.InstrumentationUtil;
99
import io.opentelemetry.exporter.internal.RetryUtil;
1010
import io.opentelemetry.exporter.internal.auth.Authenticator;
1111
import io.opentelemetry.exporter.internal.compression.Compressor;

exporters/sender/okhttp/src/test/java/io/opentelemetry/exporter/sender/okhttp/internal/AbstractOkHttpSuppressionTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
import static org.junit.jupiter.api.Assertions.assertTrue;
99

10+
import io.opentelemetry.api.internal.InstrumentationUtil;
1011
import io.opentelemetry.context.Context;
11-
import io.opentelemetry.exporter.internal.InstrumentationUtil;
1212
import java.util.concurrent.CountDownLatch;
1313
import java.util.concurrent.atomic.AtomicBoolean;
1414
import org.junit.jupiter.api.AfterEach;

0 commit comments

Comments
 (0)