Skip to content

Commit 1d7823d

Browse files
Wrap configured ContextStorageProvider if available (#4359)
* Use SPI to search for other ContextStorageProviders and wrap them instead of using SentryOtelThreadLocalStorage * Format code * changelog --------- Co-authored-by: Sentry Github Bot <bot+github-bot@sentry.io>
1 parent 019275e commit 1d7823d

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
## Unreleased
44

5+
### Features
6+
7+
- Wrap configured OpenTelemetry `ContextStorageProvider` if available ([#4359](https://github.com/getsentry/sentry-java/pull/4359))
8+
- This is only relevant if you see `java.lang.IllegalStateException: Found multiple ContextStorageProvider. Set the io.opentelemetry.context.ContextStorageProvider property to the fully qualified class name of the provider to use. Falling back to default ContextStorage. Found providers: ...`
9+
- Set `-Dio.opentelemetry.context.contextStorageProvider=io.sentry.opentelemetry.SentryContextStorageProvider` on your `java` command
10+
- Sentry will then wrap the other `ContextStorageProvider` that has been configured by loading it through SPI
11+
- If no other `ContextStorageProvider` is available or there are problems loading it, we fall back to using `SentryOtelThreadLocalStorage`
12+
513
### Fixes
614

715
- Update profile chunk rate limit and client report ([#4353](https://github.com/getsentry/sentry-java/pull/4353))

sentry-opentelemetry/sentry-opentelemetry-bootstrap/src/main/java/io/sentry/opentelemetry/SentryContextStorageProvider.java

+23-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,32 @@
22

33
import io.opentelemetry.context.ContextStorage;
44
import io.opentelemetry.context.ContextStorageProvider;
5+
import java.util.Iterator;
6+
import java.util.ServiceLoader;
7+
import org.jetbrains.annotations.NotNull;
58

69
public final class SentryContextStorageProvider implements ContextStorageProvider {
710
@Override
811
public ContextStorage get() {
9-
return new SentryContextStorage(new SentryOtelThreadLocalStorage());
12+
return new SentryContextStorage(findStorageToWrap());
13+
}
14+
15+
private @NotNull ContextStorage findStorageToWrap() {
16+
try {
17+
ServiceLoader<ContextStorageProvider> serviceLoader =
18+
ServiceLoader.load(ContextStorageProvider.class);
19+
Iterator<ContextStorageProvider> iterator = serviceLoader.iterator();
20+
while (iterator.hasNext()) {
21+
ContextStorageProvider contextStorageProvider = iterator.next();
22+
if (!(contextStorageProvider instanceof SentryContextStorageProvider)) {
23+
return contextStorageProvider.get();
24+
}
25+
}
26+
} catch (Throwable t) {
27+
// ignore and use fallback
28+
}
29+
30+
// using default / fallback storage
31+
return new SentryOtelThreadLocalStorage();
1032
}
1133
}

0 commit comments

Comments
 (0)