-
-
Notifications
You must be signed in to change notification settings - Fork 461
POTEL 26 - Customize OpenTelemetry Scope.close behaviour
#3517
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
adinauer
merged 7 commits into
8.x.x
from
feat/potel-26-customize-otel-thread-local-storage-behaviour
Jun 26, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1ad8685
improve changelog
adinauer 2ab67ff
bump otel versions
adinauer 9ce1e9c
merge fix; pr comments
adinauer a4a07a9
workaround for agent non auto init
adinauer e4e60d2
Customize OTel ThreadLocal storage behaviour
adinauer 88b8457
fix changelog
adinauer 202d89f
Merge branch '8.x.x' into feat/potel-26-customize-otel-thread-local-s…
adinauer 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
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
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
85 changes: 85 additions & 0 deletions
85
...lemetry-bootstrap/src/main/java/io/sentry/opentelemetry/SentryOtelThreadLocalStorage.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,85 @@ | ||
| /* | ||
| * Adapted from https://github.com/open-telemetry/opentelemetry-java/blob/0aacc55d1e3f5cc6dbb4f8fa26bcb657b01a7bc9/context/src/main/java/io/opentelemetry/context/ThreadLocalContextStorage.java | ||
| * | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.sentry.opentelemetry; | ||
|
|
||
| import io.opentelemetry.context.Context; | ||
| import io.opentelemetry.context.ContextStorage; | ||
| import io.opentelemetry.context.Scope; | ||
| import java.util.logging.Level; | ||
| import java.util.logging.Logger; | ||
| import org.jetbrains.annotations.ApiStatus; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| /** | ||
| * Workaround to make OpenTelemetry context storage work for Sentry since Sentry sometimes forks | ||
| * Context without cleaning up. We are not yet sure if this is something we can easliy fix, since | ||
| * Sentry static API makes heavy use of getCurrentScopes and there is no easy way of knowing when to | ||
| * restore previous Context. | ||
| */ | ||
| @ApiStatus.Experimental | ||
| @ApiStatus.Internal | ||
| public final class SentryOtelThreadLocalStorage implements ContextStorage { | ||
| private static final Logger logger = | ||
| Logger.getLogger(SentryOtelThreadLocalStorage.class.getName()); | ||
|
|
||
| private static final ThreadLocal<Context> THREAD_LOCAL_STORAGE = new ThreadLocal<>(); | ||
|
|
||
| @Override | ||
| public Scope attach(Context toAttach) { | ||
| if (toAttach == null) { | ||
| // Null context not allowed so ignore it. | ||
| return NoopScope.INSTANCE; | ||
| } | ||
|
|
||
| Context beforeAttach = current(); | ||
| if (toAttach == beforeAttach) { | ||
| return NoopScope.INSTANCE; | ||
| } | ||
|
|
||
| THREAD_LOCAL_STORAGE.set(toAttach); | ||
|
|
||
| return new SentryScopeImpl(beforeAttach); | ||
| } | ||
|
|
||
| private static class SentryScopeImpl implements Scope { | ||
| @Nullable private final Context beforeAttach; | ||
| private boolean closed; | ||
|
|
||
| private SentryScopeImpl(@Nullable Context beforeAttach) { | ||
| this.beforeAttach = beforeAttach; | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| // if (!closed && current() == toAttach) { | ||
| // Used to make OTel thread local storage compatible with Sentry where cleanup isn't always | ||
| // performed correctly | ||
| if (!closed) { | ||
| closed = true; | ||
| THREAD_LOCAL_STORAGE.set(beforeAttach); | ||
| } else { | ||
| logger.log( | ||
| Level.FINE, | ||
| " Trying to close scope which does not represent current context. Ignoring the call."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| @Nullable | ||
| public Context current() { | ||
| return THREAD_LOCAL_STORAGE.get(); | ||
| } | ||
|
|
||
| enum NoopScope implements Scope { | ||
| INSTANCE; | ||
|
|
||
| @Override | ||
| public void close() {} | ||
| } | ||
| } | ||
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.