-
Notifications
You must be signed in to change notification settings - Fork 665
Expand file tree
/
Copy pathscope.py
More file actions
218 lines (181 loc) · 6.37 KB
/
Copy pathscope.py
File metadata and controls
218 lines (181 loc) · 6.37 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
from typing import cast
from contextlib import contextmanager
import warnings
from opentelemetry.context import (
get_value,
set_value,
attach,
detach,
get_current,
)
from opentelemetry.trace import (
SpanContext,
NonRecordingSpan,
TraceFlags,
TraceState,
use_span,
)
from sentry_sdk.opentelemetry.consts import (
SENTRY_SCOPES_KEY,
SENTRY_FORK_ISOLATION_SCOPE_KEY,
SENTRY_USE_CURRENT_SCOPE_KEY,
SENTRY_USE_ISOLATION_SCOPE_KEY,
TRACESTATE_SAMPLED_KEY,
)
from sentry_sdk.opentelemetry.contextvars_context import (
SentryContextVarsRuntimeContext,
)
from sentry_sdk.opentelemetry.utils import trace_state_from_baggage
from sentry_sdk.scope import Scope, ScopeType
from sentry_sdk.tracing import Span
from sentry_sdk._types import TYPE_CHECKING
if TYPE_CHECKING:
from typing import Tuple, Optional, Generator, Dict, Any
class PotelScope(Scope):
@classmethod
def _get_scopes(cls):
# type: () -> Optional[Tuple[PotelScope, PotelScope]]
"""
Returns the current scopes tuple on the otel context. Internal use only.
"""
return cast(
"Optional[Tuple[PotelScope, PotelScope]]", get_value(SENTRY_SCOPES_KEY)
)
@classmethod
def get_current_scope(cls):
# type: () -> PotelScope
"""
Returns the current scope.
"""
return cls._get_current_scope() or _INITIAL_CURRENT_SCOPE
@classmethod
def _get_current_scope(cls):
# type: () -> Optional[PotelScope]
"""
Returns the current scope without creating a new one. Internal use only.
"""
scopes = cls._get_scopes()
return scopes[0] if scopes else None
@classmethod
def get_isolation_scope(cls):
# type: () -> PotelScope
"""
Returns the isolation scope.
"""
return cls._get_isolation_scope() or _INITIAL_ISOLATION_SCOPE
@classmethod
def _get_isolation_scope(cls):
# type: () -> Optional[PotelScope]
"""
Returns the isolation scope without creating a new one. Internal use only.
"""
scopes = cls._get_scopes()
return scopes[1] if scopes else None
@contextmanager
def continue_trace(self, environ_or_headers):
# type: (Dict[str, Any]) -> Generator[None, None, None]
"""
Sets the propagation context from environment or headers to continue an incoming trace.
Any span started within this context manager will use the same trace_id, parent_span_id
and inherit the sampling decision from the incoming trace.
"""
self.generate_propagation_context(environ_or_headers)
span_context = self._incoming_otel_span_context()
if span_context is None:
yield
else:
with use_span(NonRecordingSpan(span_context)):
yield
def _incoming_otel_span_context(self):
# type: () -> Optional[SpanContext]
if self._propagation_context is None:
return None
# If sentry-trace extraction didn't have a parent_span_id, we don't have an upstream header
if self._propagation_context.parent_span_id is None:
return None
trace_flags = TraceFlags(
TraceFlags.SAMPLED
if self._propagation_context.parent_sampled
else TraceFlags.DEFAULT
)
if self._propagation_context.baggage:
trace_state = trace_state_from_baggage(self._propagation_context.baggage)
else:
trace_state = TraceState()
# for twp to work, we also need to consider deferred sampling when the sampling
# flag is not present, so the above TraceFlags are not sufficient
if self._propagation_context.parent_sampled is None:
trace_state = trace_state.update(TRACESTATE_SAMPLED_KEY, "deferred")
span_context = SpanContext(
trace_id=int(self._propagation_context.trace_id, 16),
span_id=int(self._propagation_context.parent_span_id, 16),
is_remote=True,
trace_flags=trace_flags,
trace_state=trace_state,
)
return span_context
def start_transaction(self, **kwargs):
# type: (Any) -> Span
"""
.. deprecated:: 3.0.0
This function is deprecated and will be removed in a future release.
Use :py:meth:`sentry_sdk.start_span` instead.
"""
warnings.warn(
"The `start_transaction` method is deprecated, please use `sentry_sdk.start_span instead.`",
DeprecationWarning,
stacklevel=2,
)
return self.start_span(**kwargs)
def start_span(self, **kwargs):
# type: (Any) -> Span
return Span(**kwargs)
_INITIAL_CURRENT_SCOPE = PotelScope(ty=ScopeType.CURRENT)
_INITIAL_ISOLATION_SCOPE = PotelScope(ty=ScopeType.ISOLATION)
def setup_initial_scopes():
# type: () -> None
global _INITIAL_CURRENT_SCOPE, _INITIAL_ISOLATION_SCOPE
_INITIAL_CURRENT_SCOPE = PotelScope(ty=ScopeType.CURRENT)
_INITIAL_ISOLATION_SCOPE = PotelScope(ty=ScopeType.ISOLATION)
scopes = (_INITIAL_CURRENT_SCOPE, _INITIAL_ISOLATION_SCOPE)
attach(set_value(SENTRY_SCOPES_KEY, scopes))
def setup_scope_context_management():
# type: () -> None
import opentelemetry.context
opentelemetry.context._RUNTIME_CONTEXT = SentryContextVarsRuntimeContext()
setup_initial_scopes()
@contextmanager
def isolation_scope():
# type: () -> Generator[Scope, None, None]
context = set_value(SENTRY_FORK_ISOLATION_SCOPE_KEY, True)
token = attach(context)
try:
yield PotelScope.get_isolation_scope()
finally:
detach(token)
@contextmanager
def new_scope():
# type: () -> Generator[Scope, None, None]
token = attach(get_current())
try:
yield PotelScope.get_current_scope()
finally:
detach(token)
@contextmanager
def use_scope(scope):
# type: (Scope) -> Generator[Scope, None, None]
context = set_value(SENTRY_USE_CURRENT_SCOPE_KEY, scope)
token = attach(context)
try:
yield scope
finally:
detach(token)
@contextmanager
def use_isolation_scope(isolation_scope):
# type: (Scope) -> Generator[Scope, None, None]
context = set_value(SENTRY_USE_ISOLATION_SCOPE_KEY, isolation_scope)
token = attach(context)
try:
yield isolation_scope
finally:
detach(token)