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
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 6
6
package io .opentelemetry .exporter .internal ;
7
7
8
8
import io .opentelemetry .context .Context ;
9
- import io .opentelemetry .context .ContextKey ;
10
- import java .util .Objects ;
11
9
12
10
/**
13
11
* 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.
15
16
*/
17
+ @ Deprecated
16
18
public final class InstrumentationUtil {
17
- private static final ContextKey <Boolean > SUPPRESS_INSTRUMENTATION_KEY =
18
- ContextKey .named ("suppress_internal_exporter_instrumentation" );
19
19
20
20
private InstrumentationUtil () {}
21
21
@@ -25,7 +25,7 @@ private InstrumentationUtil() {}
25
25
* calls.
26
26
*/
27
27
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 );
29
29
}
30
30
31
31
/**
@@ -35,6 +35,6 @@ public static void suppressInstrumentation(Runnable runnable) {
35
35
* instrumentation.
36
36
*/
37
37
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 );
39
39
}
40
40
}
Original file line number Diff line number Diff line change 12
12
import org .junit .jupiter .api .Test ;
13
13
14
14
class InstrumentationUtilTest {
15
+
16
+ // testing deprecated implementation until it's removed
15
17
@ Test
18
+ @ SuppressWarnings ("deprecation" )
16
19
void verifySuppressInstrumentation () {
17
20
// Should be false by default.
18
21
assertFalse (InstrumentationUtil .shouldSuppressInstrumentation (Context .current ()));
Original file line number Diff line number Diff line change 23
23
24
24
package io .opentelemetry .exporter .sender .okhttp .internal ;
25
25
26
- import io .opentelemetry .exporter .internal .InstrumentationUtil ;
26
+ import io .opentelemetry .api .internal .InstrumentationUtil ;
27
27
import io .opentelemetry .exporter .internal .RetryUtil ;
28
28
import io .opentelemetry .exporter .internal .compression .Compressor ;
29
29
import io .opentelemetry .exporter .internal .grpc .GrpcExporterUtil ;
Original file line number Diff line number Diff line change 5
5
6
6
package io .opentelemetry .exporter .sender .okhttp .internal ;
7
7
8
- import io .opentelemetry .exporter .internal .InstrumentationUtil ;
8
+ import io .opentelemetry .api .internal .InstrumentationUtil ;
9
9
import io .opentelemetry .exporter .internal .RetryUtil ;
10
10
import io .opentelemetry .exporter .internal .auth .Authenticator ;
11
11
import io .opentelemetry .exporter .internal .compression .Compressor ;
Original file line number Diff line number Diff line change 7
7
8
8
import static org .junit .jupiter .api .Assertions .assertTrue ;
9
9
10
+ import io .opentelemetry .api .internal .InstrumentationUtil ;
10
11
import io .opentelemetry .context .Context ;
11
- import io .opentelemetry .exporter .internal .InstrumentationUtil ;
12
12
import java .util .concurrent .CountDownLatch ;
13
13
import java .util .concurrent .atomic .AtomicBoolean ;
14
14
import org .junit .jupiter .api .AfterEach ;
You can’t perform that action at this time.
0 commit comments