-
Notifications
You must be signed in to change notification settings - Fork 951
Identifying otel http calls #5918
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
Merged
jack-berg
merged 24 commits into
open-telemetry:main
from
LikeTheSalad:identifying-otel-http-calls
Nov 9, 2023
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
c43769d
Created InstrumentationUtil
LikeTheSalad 908e04f
Suppressing automatic instrumentation for OTel exporters using OkHttp
LikeTheSalad f00b5b7
Adding InstrumentationUtilTest validations
LikeTheSalad c10cf0f
Spotless
LikeTheSalad f7c5ef8
Verifying suppress instrumentation key is present in okhttp calls
LikeTheSalad 2bb40e5
Spotless
LikeTheSalad 52b83a0
Suppressing NonFinalStaticField
LikeTheSalad 2e2bed7
Added javadoc
LikeTheSalad 456f71b
Renaming lock by latch
LikeTheSalad faa5f65
Reordering functions
LikeTheSalad adff295
Update exporters/common/src/main/java/io/opentelemetry/exporter/inter…
LikeTheSalad 246ed42
Update exporters/common/src/main/java/io/opentelemetry/exporter/inter…
LikeTheSalad 0b7601b
Update exporters/sender/okhttp/src/test/java/io/opentelemetry/exporte…
LikeTheSalad 33b68cf
Update exporters/sender/okhttp/src/test/java/io/opentelemetry/exporte…
LikeTheSalad 79e616b
Spotless
LikeTheSalad 9a47e53
Renamed ContextKey to suppress_internal_exporter_instrumentation
LikeTheSalad b5f90ac
Renaming test classes
LikeTheSalad 5fe0c06
Updating InstrumentationUtilTest
LikeTheSalad 5011af8
Adding javadoc to DaemonThreadFactory's constructor
LikeTheSalad 2dcdb09
Adding setter for OkHttpUtil.propagateContextForTestingInDispatcher
LikeTheSalad 7ca697e
Spotless
LikeTheSalad 7c2c165
Suppressing NonFinalStaticField warning
LikeTheSalad 180a523
Adding DaemonThreadFactory constructor's summary javadoc to avoid "Su…
LikeTheSalad db23665
Fixing DaemonThreadFactory's constructor javadoc
LikeTheSalad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
exporters/common/src/main/java/io/opentelemetry/exporter/internal/InstrumentationUtil.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| 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. | ||
| */ | ||
| public final class InstrumentationUtil { | ||
| private static final ContextKey<Boolean> SUPPRESS_INSTRUMENTATION_KEY = | ||
| ContextKey.named("suppress_internal_exporter_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); | ||
| } | ||
| } |
27 changes: 27 additions & 0 deletions
27
...ters/common/src/test/java/io/opentelemetry/exporter/internal/InstrumentationUtilTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /* | ||
| * 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 { | ||
| @Test | ||
| 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())); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
.../java/io/opentelemetry/exporter/sender/okhttp/internal/AbstractOkHttpSuppressionTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.exporter.sender.okhttp.internal; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import io.opentelemetry.context.Context; | ||
| import io.opentelemetry.exporter.internal.InstrumentationUtil; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| abstract class AbstractOkHttpSuppressionTest<T> { | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| OkHttpUtil.setPropagateContextForTestingInDispatcher(true); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void tearDown() { | ||
| OkHttpUtil.setPropagateContextForTestingInDispatcher(false); | ||
| } | ||
|
|
||
| @Test | ||
| void testSuppressInstrumentation() throws InterruptedException { | ||
| CountDownLatch latch = new CountDownLatch(1); | ||
| AtomicBoolean suppressInstrumentation = new AtomicBoolean(false); | ||
|
|
||
| Runnable onSuccess = Assertions::fail; | ||
| Runnable onFailure = | ||
| () -> { | ||
| suppressInstrumentation.set( | ||
| InstrumentationUtil.shouldSuppressInstrumentation(Context.current())); | ||
| latch.countDown(); | ||
| }; | ||
|
|
||
| send(getSender(), onSuccess, onFailure); | ||
|
|
||
| latch.await(); | ||
|
|
||
| assertTrue(suppressInstrumentation.get()); | ||
| } | ||
|
|
||
| abstract void send(T sender, Runnable onSuccess, Runnable onFailure); | ||
|
|
||
| private T getSender() { | ||
| return createSender("https://none"); | ||
| } | ||
|
|
||
| abstract T createSender(String endpoint); | ||
| } |
36 changes: 36 additions & 0 deletions
36
...test/java/io/opentelemetry/exporter/sender/okhttp/internal/OkHttpGrpcSuppressionTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.exporter.sender.okhttp.internal; | ||
|
|
||
| import io.opentelemetry.exporter.internal.marshal.MarshalerWithSize; | ||
| import io.opentelemetry.exporter.internal.marshal.Serializer; | ||
| import java.util.Collections; | ||
|
|
||
| class OkHttpGrpcSuppressionTest | ||
| extends AbstractOkHttpSuppressionTest< | ||
| OkHttpGrpcSender<OkHttpGrpcSuppressionTest.DummyMarshaler>> { | ||
|
|
||
| @Override | ||
| void send(OkHttpGrpcSender<DummyMarshaler> sender, Runnable onSuccess, Runnable onFailure) { | ||
| sender.send(new DummyMarshaler(), onSuccess, (grpcResponse, throwable) -> onFailure.run()); | ||
| } | ||
|
|
||
| @Override | ||
| OkHttpGrpcSender<DummyMarshaler> createSender(String endpoint) { | ||
| return new OkHttpGrpcSender<>( | ||
| "https://localhost", false, 10L, Collections.emptyMap(), null, null, null); | ||
| } | ||
|
|
||
| protected static class DummyMarshaler extends MarshalerWithSize { | ||
|
|
||
| protected DummyMarshaler() { | ||
| super(0); | ||
| } | ||
|
|
||
| @Override | ||
| protected void writeTo(Serializer output) {} | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.