From 6bf48302ed8289a62afcce9c76e364f00b40f677 Mon Sep 17 00:00:00 2001 From: Sylvain Juge <763082+SylvainJuge@users.noreply.github.com> Date: Mon, 1 Jul 2024 17:27:23 +0200 Subject: [PATCH 1/4] move to context + generic context key --- .../context/internal/InstrumentationUtil.java | 35 +++++++++++++++++++ .../internal/InstrumentationUtilTest.java | 11 ++---- .../internal/InstrumentationUtil.java | 14 ++++---- .../okhttp/internal/OkHttpGrpcSender.java | 2 +- .../okhttp/internal/OkHttpHttpSender.java | 2 +- .../AbstractOkHttpSuppressionTest.java | 2 +- 6 files changed, 48 insertions(+), 18 deletions(-) create mode 100644 context/src/main/java/io/opentelemetry/context/internal/InstrumentationUtil.java rename {exporters/common/src/test/java/io/opentelemetry/exporter => context/src/test/java/io/opentelemetry/context}/internal/InstrumentationUtilTest.java (85%) diff --git a/context/src/main/java/io/opentelemetry/context/internal/InstrumentationUtil.java b/context/src/main/java/io/opentelemetry/context/internal/InstrumentationUtil.java new file mode 100644 index 00000000000..b702cadfc2d --- /dev/null +++ b/context/src/main/java/io/opentelemetry/context/internal/InstrumentationUtil.java @@ -0,0 +1,35 @@ +package io.opentelemetry.context.internal; + +import io.opentelemetry.context.Context; +import io.opentelemetry.context.ContextKey; +import java.util.Objects; + +/** + * This class is internal and is hence not for public use. Its APIs are unstable and can change at + * any time. + */ +public final class InstrumentationUtil { + private static final ContextKey SUPPRESS_INSTRUMENTATION_KEY = + ContextKey.named("suppress_instrumentation"); + + private InstrumentationUtil() {} + + /** + * Adds a Context boolean key that will allow to identify HTTP calls coming from OTel exporters. + * The key later be checked by an automatic instrumentation to avoid tracing OTel exporter's + * calls. + */ + public static void suppressInstrumentation(Runnable runnable) { + Context.current().with(SUPPRESS_INSTRUMENTATION_KEY, true).wrap(runnable).run(); + } + + /** + * Checks if an automatic instrumentation should be suppressed with the provided Context. + * + * @return TRUE to suppress the automatic instrumentation, FALSE to continue with the + * instrumentation. + */ + public static boolean shouldSuppressInstrumentation(Context context) { + return Objects.equals(context.get(SUPPRESS_INSTRUMENTATION_KEY), true); + } +} diff --git a/exporters/common/src/test/java/io/opentelemetry/exporter/internal/InstrumentationUtilTest.java b/context/src/test/java/io/opentelemetry/context/internal/InstrumentationUtilTest.java similarity index 85% rename from exporters/common/src/test/java/io/opentelemetry/exporter/internal/InstrumentationUtilTest.java rename to context/src/test/java/io/opentelemetry/context/internal/InstrumentationUtilTest.java index 2c7e9f540c7..ca6abd46205 100644 --- a/exporters/common/src/test/java/io/opentelemetry/exporter/internal/InstrumentationUtilTest.java +++ b/context/src/test/java/io/opentelemetry/context/internal/InstrumentationUtilTest.java @@ -1,16 +1,11 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ +package io.opentelemetry.context.internal; -package io.opentelemetry.exporter.internal; +import io.opentelemetry.context.Context; +import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; -import io.opentelemetry.context.Context; -import org.junit.jupiter.api.Test; - class InstrumentationUtilTest { @Test void verifySuppressInstrumentation() { diff --git a/exporters/common/src/main/java/io/opentelemetry/exporter/internal/InstrumentationUtil.java b/exporters/common/src/main/java/io/opentelemetry/exporter/internal/InstrumentationUtil.java index 4ce9622fe4c..2b4b95cd79f 100644 --- a/exporters/common/src/main/java/io/opentelemetry/exporter/internal/InstrumentationUtil.java +++ b/exporters/common/src/main/java/io/opentelemetry/exporter/internal/InstrumentationUtil.java @@ -6,16 +6,15 @@ package io.opentelemetry.exporter.internal; import io.opentelemetry.context.Context; -import io.opentelemetry.context.ContextKey; -import java.util.Objects; /** * This class is internal and is hence not for public use. Its APIs are unstable and can change at - * any time. + * any time + * + * @deprecated use {@link io.opentelemetry.context.internal.InstrumentationUtil} instead */ +@Deprecated public final class InstrumentationUtil { - private static final ContextKey SUPPRESS_INSTRUMENTATION_KEY = - ContextKey.named("suppress_internal_exporter_instrumentation"); private InstrumentationUtil() {} @@ -25,7 +24,7 @@ private InstrumentationUtil() {} * calls. */ public static void suppressInstrumentation(Runnable runnable) { - Context.current().with(SUPPRESS_INSTRUMENTATION_KEY, true).wrap(runnable).run(); + io.opentelemetry.context.internal.InstrumentationUtil.suppressInstrumentation(runnable); } /** @@ -35,6 +34,7 @@ public static void suppressInstrumentation(Runnable runnable) { * instrumentation. */ public static boolean shouldSuppressInstrumentation(Context context) { - return Objects.equals(context.get(SUPPRESS_INSTRUMENTATION_KEY), true); + return io.opentelemetry.context.internal.InstrumentationUtil.shouldSuppressInstrumentation( + context); } } diff --git a/exporters/sender/okhttp/src/main/java/io/opentelemetry/exporter/sender/okhttp/internal/OkHttpGrpcSender.java b/exporters/sender/okhttp/src/main/java/io/opentelemetry/exporter/sender/okhttp/internal/OkHttpGrpcSender.java index d9ceecb77b7..de4d275c266 100644 --- a/exporters/sender/okhttp/src/main/java/io/opentelemetry/exporter/sender/okhttp/internal/OkHttpGrpcSender.java +++ b/exporters/sender/okhttp/src/main/java/io/opentelemetry/exporter/sender/okhttp/internal/OkHttpGrpcSender.java @@ -23,7 +23,7 @@ package io.opentelemetry.exporter.sender.okhttp.internal; -import io.opentelemetry.exporter.internal.InstrumentationUtil; +import io.opentelemetry.context.internal.InstrumentationUtil; import io.opentelemetry.exporter.internal.RetryUtil; import io.opentelemetry.exporter.internal.compression.Compressor; import io.opentelemetry.exporter.internal.grpc.GrpcExporterUtil; diff --git a/exporters/sender/okhttp/src/main/java/io/opentelemetry/exporter/sender/okhttp/internal/OkHttpHttpSender.java b/exporters/sender/okhttp/src/main/java/io/opentelemetry/exporter/sender/okhttp/internal/OkHttpHttpSender.java index 8b85396ce08..f3659603f0b 100644 --- a/exporters/sender/okhttp/src/main/java/io/opentelemetry/exporter/sender/okhttp/internal/OkHttpHttpSender.java +++ b/exporters/sender/okhttp/src/main/java/io/opentelemetry/exporter/sender/okhttp/internal/OkHttpHttpSender.java @@ -5,7 +5,7 @@ package io.opentelemetry.exporter.sender.okhttp.internal; -import io.opentelemetry.exporter.internal.InstrumentationUtil; +import io.opentelemetry.context.internal.InstrumentationUtil; import io.opentelemetry.exporter.internal.RetryUtil; import io.opentelemetry.exporter.internal.auth.Authenticator; import io.opentelemetry.exporter.internal.compression.Compressor; diff --git a/exporters/sender/okhttp/src/test/java/io/opentelemetry/exporter/sender/okhttp/internal/AbstractOkHttpSuppressionTest.java b/exporters/sender/okhttp/src/test/java/io/opentelemetry/exporter/sender/okhttp/internal/AbstractOkHttpSuppressionTest.java index d1a84a7491d..06e049db5fa 100644 --- a/exporters/sender/okhttp/src/test/java/io/opentelemetry/exporter/sender/okhttp/internal/AbstractOkHttpSuppressionTest.java +++ b/exporters/sender/okhttp/src/test/java/io/opentelemetry/exporter/sender/okhttp/internal/AbstractOkHttpSuppressionTest.java @@ -8,7 +8,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import io.opentelemetry.context.Context; -import io.opentelemetry.exporter.internal.InstrumentationUtil; +import io.opentelemetry.context.internal.InstrumentationUtil; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicBoolean; import org.junit.jupiter.api.AfterEach; From d157adb7b44c3d9564ca20be63c63c6d31ca014d Mon Sep 17 00:00:00 2001 From: Sylvain Juge <763082+SylvainJuge@users.noreply.github.com> Date: Tue, 2 Jul 2024 11:09:22 +0200 Subject: [PATCH 2/4] spotless --- .../context/internal/InstrumentationUtil.java | 5 +++++ .../context/internal/InstrumentationUtilTest.java | 11 ++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/context/src/main/java/io/opentelemetry/context/internal/InstrumentationUtil.java b/context/src/main/java/io/opentelemetry/context/internal/InstrumentationUtil.java index b702cadfc2d..b7a5ab98459 100644 --- a/context/src/main/java/io/opentelemetry/context/internal/InstrumentationUtil.java +++ b/context/src/main/java/io/opentelemetry/context/internal/InstrumentationUtil.java @@ -1,3 +1,8 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + package io.opentelemetry.context.internal; import io.opentelemetry.context.Context; diff --git a/context/src/test/java/io/opentelemetry/context/internal/InstrumentationUtilTest.java b/context/src/test/java/io/opentelemetry/context/internal/InstrumentationUtilTest.java index ca6abd46205..04064d641e0 100644 --- a/context/src/test/java/io/opentelemetry/context/internal/InstrumentationUtilTest.java +++ b/context/src/test/java/io/opentelemetry/context/internal/InstrumentationUtilTest.java @@ -1,11 +1,16 @@ -package io.opentelemetry.context.internal; +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ -import io.opentelemetry.context.Context; -import org.junit.jupiter.api.Test; +package io.opentelemetry.context.internal; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; +import io.opentelemetry.context.Context; +import org.junit.jupiter.api.Test; + class InstrumentationUtilTest { @Test void verifySuppressInstrumentation() { From ae25975ee6f344ca3a556d81649a2881abcbd57d Mon Sep 17 00:00:00 2001 From: Sylvain Juge <763082+SylvainJuge@users.noreply.github.com> Date: Wed, 10 Jul 2024 18:13:41 +0200 Subject: [PATCH 3/4] keep testing old impl for coverage --- .../internal/InstrumentationUtilTest.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 exporters/common/src/test/java/io/opentelemetry/exporter/internal/InstrumentationUtilTest.java diff --git a/exporters/common/src/test/java/io/opentelemetry/exporter/internal/InstrumentationUtilTest.java b/exporters/common/src/test/java/io/opentelemetry/exporter/internal/InstrumentationUtilTest.java new file mode 100644 index 00000000000..5a6a2cfd5a2 --- /dev/null +++ b/exporters/common/src/test/java/io/opentelemetry/exporter/internal/InstrumentationUtilTest.java @@ -0,0 +1,30 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.exporter.internal; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import io.opentelemetry.context.Context; +import org.junit.jupiter.api.Test; + +class InstrumentationUtilTest { + + // testing deprecated implementation until it's removed + @Test + @SuppressWarnings("deprecation") + void verifySuppressInstrumentation() { + // Should be false by default. + assertFalse(InstrumentationUtil.shouldSuppressInstrumentation(Context.current())); + + // Should be true inside the Runnable passed to InstrumentationUtil.suppressInstrumentation. + InstrumentationUtil.suppressInstrumentation( + () -> assertTrue(InstrumentationUtil.shouldSuppressInstrumentation(Context.current()))); + + // Should be false after the runnable finishes. + assertFalse(InstrumentationUtil.shouldSuppressInstrumentation(Context.current())); + } +} From da8ad454e14ae6c53f333459dc1779e668f2989f Mon Sep 17 00:00:00 2001 From: Sylvain Juge <763082+SylvainJuge@users.noreply.github.com> Date: Fri, 12 Jul 2024 11:17:16 +0200 Subject: [PATCH 4/4] move from context to api --- .../opentelemetry/api}/internal/InstrumentationUtil.java | 2 +- .../api}/internal/InstrumentationUtilTest.java | 2 +- .../exporter/internal/InstrumentationUtil.java | 8 ++++---- .../exporter/sender/okhttp/internal/OkHttpGrpcSender.java | 2 +- .../exporter/sender/okhttp/internal/OkHttpHttpSender.java | 2 +- .../okhttp/internal/AbstractOkHttpSuppressionTest.java | 2 +- 6 files changed, 9 insertions(+), 9 deletions(-) rename {context/src/main/java/io/opentelemetry/context => api/all/src/main/java/io/opentelemetry/api}/internal/InstrumentationUtil.java (96%) rename {context/src/test/java/io/opentelemetry/context => api/all/src/test/java/io/opentelemetry/api}/internal/InstrumentationUtilTest.java (95%) diff --git a/context/src/main/java/io/opentelemetry/context/internal/InstrumentationUtil.java b/api/all/src/main/java/io/opentelemetry/api/internal/InstrumentationUtil.java similarity index 96% rename from context/src/main/java/io/opentelemetry/context/internal/InstrumentationUtil.java rename to api/all/src/main/java/io/opentelemetry/api/internal/InstrumentationUtil.java index b7a5ab98459..4f5c1e676bf 100644 --- a/context/src/main/java/io/opentelemetry/context/internal/InstrumentationUtil.java +++ b/api/all/src/main/java/io/opentelemetry/api/internal/InstrumentationUtil.java @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.context.internal; +package io.opentelemetry.api.internal; import io.opentelemetry.context.Context; import io.opentelemetry.context.ContextKey; diff --git a/context/src/test/java/io/opentelemetry/context/internal/InstrumentationUtilTest.java b/api/all/src/test/java/io/opentelemetry/api/internal/InstrumentationUtilTest.java similarity index 95% rename from context/src/test/java/io/opentelemetry/context/internal/InstrumentationUtilTest.java rename to api/all/src/test/java/io/opentelemetry/api/internal/InstrumentationUtilTest.java index 04064d641e0..066c906d875 100644 --- a/context/src/test/java/io/opentelemetry/context/internal/InstrumentationUtilTest.java +++ b/api/all/src/test/java/io/opentelemetry/api/internal/InstrumentationUtilTest.java @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.context.internal; +package io.opentelemetry.api.internal; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; diff --git a/exporters/common/src/main/java/io/opentelemetry/exporter/internal/InstrumentationUtil.java b/exporters/common/src/main/java/io/opentelemetry/exporter/internal/InstrumentationUtil.java index 2b4b95cd79f..5eddab53b76 100644 --- a/exporters/common/src/main/java/io/opentelemetry/exporter/internal/InstrumentationUtil.java +++ b/exporters/common/src/main/java/io/opentelemetry/exporter/internal/InstrumentationUtil.java @@ -11,7 +11,8 @@ * This class is internal and is hence not for public use. Its APIs are unstable and can change at * any time * - * @deprecated use {@link io.opentelemetry.context.internal.InstrumentationUtil} instead + * @deprecated use {@link io.opentelemetry.api.internal.InstrumentationUtil} instead. This class + * should be removed once instrumentation does not refer to it anymore. */ @Deprecated public final class InstrumentationUtil { @@ -24,7 +25,7 @@ private InstrumentationUtil() {} * calls. */ public static void suppressInstrumentation(Runnable runnable) { - io.opentelemetry.context.internal.InstrumentationUtil.suppressInstrumentation(runnable); + io.opentelemetry.api.internal.InstrumentationUtil.suppressInstrumentation(runnable); } /** @@ -34,7 +35,6 @@ public static void suppressInstrumentation(Runnable runnable) { * instrumentation. */ public static boolean shouldSuppressInstrumentation(Context context) { - return io.opentelemetry.context.internal.InstrumentationUtil.shouldSuppressInstrumentation( - context); + return io.opentelemetry.api.internal.InstrumentationUtil.shouldSuppressInstrumentation(context); } } diff --git a/exporters/sender/okhttp/src/main/java/io/opentelemetry/exporter/sender/okhttp/internal/OkHttpGrpcSender.java b/exporters/sender/okhttp/src/main/java/io/opentelemetry/exporter/sender/okhttp/internal/OkHttpGrpcSender.java index de4d275c266..7fb04c4e513 100644 --- a/exporters/sender/okhttp/src/main/java/io/opentelemetry/exporter/sender/okhttp/internal/OkHttpGrpcSender.java +++ b/exporters/sender/okhttp/src/main/java/io/opentelemetry/exporter/sender/okhttp/internal/OkHttpGrpcSender.java @@ -23,7 +23,7 @@ package io.opentelemetry.exporter.sender.okhttp.internal; -import io.opentelemetry.context.internal.InstrumentationUtil; +import io.opentelemetry.api.internal.InstrumentationUtil; import io.opentelemetry.exporter.internal.RetryUtil; import io.opentelemetry.exporter.internal.compression.Compressor; import io.opentelemetry.exporter.internal.grpc.GrpcExporterUtil; diff --git a/exporters/sender/okhttp/src/main/java/io/opentelemetry/exporter/sender/okhttp/internal/OkHttpHttpSender.java b/exporters/sender/okhttp/src/main/java/io/opentelemetry/exporter/sender/okhttp/internal/OkHttpHttpSender.java index f3659603f0b..1a0263a13de 100644 --- a/exporters/sender/okhttp/src/main/java/io/opentelemetry/exporter/sender/okhttp/internal/OkHttpHttpSender.java +++ b/exporters/sender/okhttp/src/main/java/io/opentelemetry/exporter/sender/okhttp/internal/OkHttpHttpSender.java @@ -5,7 +5,7 @@ package io.opentelemetry.exporter.sender.okhttp.internal; -import io.opentelemetry.context.internal.InstrumentationUtil; +import io.opentelemetry.api.internal.InstrumentationUtil; import io.opentelemetry.exporter.internal.RetryUtil; import io.opentelemetry.exporter.internal.auth.Authenticator; import io.opentelemetry.exporter.internal.compression.Compressor; diff --git a/exporters/sender/okhttp/src/test/java/io/opentelemetry/exporter/sender/okhttp/internal/AbstractOkHttpSuppressionTest.java b/exporters/sender/okhttp/src/test/java/io/opentelemetry/exporter/sender/okhttp/internal/AbstractOkHttpSuppressionTest.java index 06e049db5fa..4787bb8a610 100644 --- a/exporters/sender/okhttp/src/test/java/io/opentelemetry/exporter/sender/okhttp/internal/AbstractOkHttpSuppressionTest.java +++ b/exporters/sender/okhttp/src/test/java/io/opentelemetry/exporter/sender/okhttp/internal/AbstractOkHttpSuppressionTest.java @@ -7,8 +7,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue; +import io.opentelemetry.api.internal.InstrumentationUtil; import io.opentelemetry.context.Context; -import io.opentelemetry.context.internal.InstrumentationUtil; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicBoolean; import org.junit.jupiter.api.AfterEach;