File tree Expand file tree Collapse file tree 7 files changed +80
-10
lines changed
main/java/io/opentelemetry/api/internal
test/java/io/opentelemetry/api/internal
main/java/io/opentelemetry/exporter/internal
test/java/io/opentelemetry/exporter/internal
main/java/io/opentelemetry/exporter/sender/okhttp/internal
test/java/io/opentelemetry/exporter/sender/okhttp/internal Expand file tree Collapse file tree 7 files changed +80
-10
lines changed Original file line number Diff line number Diff line change 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 number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 66package io .opentelemetry .exporter .internal ;
77
88import 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
1618public 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}
Original file line number Diff line number Diff line change 1212import org .junit .jupiter .api .Test ;
1313
1414class 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 ()));
Original file line number Diff line number Diff line change 2323
2424package io .opentelemetry .exporter .sender .okhttp .internal ;
2525
26- import io .opentelemetry .exporter .internal .InstrumentationUtil ;
26+ import io .opentelemetry .api .internal .InstrumentationUtil ;
2727import io .opentelemetry .exporter .internal .RetryUtil ;
2828import io .opentelemetry .exporter .internal .compression .Compressor ;
2929import io .opentelemetry .exporter .internal .grpc .GrpcExporterUtil ;
Original file line number Diff line number Diff line change 55
66package io .opentelemetry .exporter .sender .okhttp .internal ;
77
8- import io .opentelemetry .exporter .internal .InstrumentationUtil ;
8+ import io .opentelemetry .api .internal .InstrumentationUtil ;
99import io .opentelemetry .exporter .internal .RetryUtil ;
1010import io .opentelemetry .exporter .internal .auth .Authenticator ;
1111import io .opentelemetry .exporter .internal .compression .Compressor ;
Original file line number Diff line number Diff line change 77
88import static org .junit .jupiter .api .Assertions .assertTrue ;
99
10+ import io .opentelemetry .api .internal .InstrumentationUtil ;
1011import io .opentelemetry .context .Context ;
11- import io .opentelemetry .exporter .internal .InstrumentationUtil ;
1212import java .util .concurrent .CountDownLatch ;
1313import java .util .concurrent .atomic .AtomicBoolean ;
1414import org .junit .jupiter .api .AfterEach ;
You can’t perform that action at this time.
0 commit comments