-
Notifications
You must be signed in to change notification settings - Fork 858
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
suppress instrumentation: move to api + generic context key #6546
Merged
jack-berg
merged 7 commits into
open-telemetry:main
from
SylvainJuge:suppress-instrumentation
Jul 17, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6bf4830
move to context + generic context key
SylvainJuge d157adb
spotless
SylvainJuge 5789c27
Merge branch 'main' of github.com:open-telemetry/opentelemetry-java i…
SylvainJuge ae25975
keep testing old impl for coverage
SylvainJuge 81066c2
Merge branch 'main' of github.com:open-telemetry/opentelemetry-java i…
SylvainJuge da8ad45
move from context to api
SylvainJuge 5b82090
Merge branch 'main' of github.com:open-telemetry/opentelemetry-java i…
SylvainJuge 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
api/all/src/main/java/io/opentelemetry/api/internal/InstrumentationUtil.java
This file contains 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.api.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_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
api/all/src/test/java/io/opentelemetry/api/internal/InstrumentationUtilTest.java
This file contains 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.api.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 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 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 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 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[for reviewer] keeping this class allows to keep instrumentation agent compatible as it currently relies on it, switching to the new class location is the next step in the instrumentation agent once this has been merged and released.