-
-
Notifications
You must be signed in to change notification settings - Fork 474
Expand file tree
/
Copy pathSentryContextWrapper.java
More file actions
97 lines (77 loc) · 3.15 KB
/
Copy pathSentryContextWrapper.java
File metadata and controls
97 lines (77 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package io.sentry.opentelemetry;
import static io.sentry.opentelemetry.SentryOtelKeys.SENTRY_SCOPES_KEY;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.ContextKey;
import io.sentry.IScopes;
import io.sentry.Sentry;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ApiStatus.Internal
public final class SentryContextWrapper implements Context {
private final @NotNull Context delegate;
private SentryContextWrapper(final @NotNull Context delegate) {
this.delegate = delegate;
}
@Override
public <V> V get(final @NotNull ContextKey<V> contextKey) {
return delegate.get(contextKey);
}
@Override
public <V> Context with(final @NotNull ContextKey<V> contextKey, V v) {
final @NotNull Context modifiedContext = delegate.with(contextKey, v);
if (isOpentelemetrySpan(contextKey)) {
return forkCurrentScope(modifiedContext);
} else {
return new SentryContextWrapper(modifiedContext);
}
}
private <V> boolean isOpentelemetrySpan(final @NotNull ContextKey<V> contextKey) {
return "opentelemetry-trace-span-key".equals(contextKey.toString());
}
private static @NotNull Context forkCurrentScope(final @NotNull Context context) {
final @Nullable IOtelSpanWrapper sentrySpan = getCurrentSpanFromGlobalStorage(context);
final @Nullable IScopes spanScopes = sentrySpan == null ? null : sentrySpan.getScopes();
final @NotNull IScopes forkedScopes = forkCurrentScopeInternal(context, spanScopes);
if (sentrySpan != null) {
forkedScopes.setActiveSpan(sentrySpan);
}
return context.with(SENTRY_SCOPES_KEY, forkedScopes);
}
private static @NotNull IScopes forkCurrentScopeInternal(
final @NotNull Context context, final @Nullable IScopes spanScopes) {
final @Nullable IScopes scopesInContext = context.get(SENTRY_SCOPES_KEY);
if (scopesInContext != null && spanScopes != null) {
if (scopesInContext.isAncestorOf(spanScopes)) {
return spanScopes.forkedCurrentScope("contextwrapper.spanancestor");
}
}
if (scopesInContext != null) {
return scopesInContext.forkedCurrentScope("contextwrapper.scopeincontext");
}
if (spanScopes != null) {
return spanScopes.forkedCurrentScope("contextwrapper.spanscope");
}
return Sentry.forkedRootScopes("contextwrapper.fallback");
}
private static @Nullable IOtelSpanWrapper getCurrentSpanFromGlobalStorage(
final @NotNull Context context) {
@Nullable final Span span = Span.fromContextOrNull(context);
if (span != null) {
final @Nullable IOtelSpanWrapper sentrySpan =
SentryWeakSpanStorage.getInstance().getSentrySpan(span.getSpanContext());
return sentrySpan;
}
return null;
}
public static @NotNull SentryContextWrapper wrap(final @NotNull Context context) {
// we have to fork here because the first time we get to wrap a context it may already have a
// span and a scope
return new SentryContextWrapper(forkCurrentScope(context));
}
@Override
public String toString() {
return delegate.toString();
}
}