7
7
import sentry_sdk
8
8
from sentry_sdk .integrations import Integration
9
9
10
- from sentry_sdk import Hub , capture_exception
10
+ from sentry_sdk import Scope , capture_exception
11
11
from sentry_sdk .tracing import Transaction
12
- from sentry_sdk .scope import add_global_event_processor
12
+ from sentry_sdk .scope import add_global_event_processor , use_scope
13
13
14
14
_ENVVARS_AS_TAGS = frozenset (
15
15
[
@@ -59,7 +59,7 @@ def __init__(self, always_report=None):
59
59
def setup_once ():
60
60
@add_global_event_processor
61
61
def procesor (event , hint ):
62
- if Hub . current .get_integration (PytestIntegration ) is None :
62
+ if Scope . get_client () .get_integration (PytestIntegration ) is None :
63
63
return event
64
64
65
65
for key in _ENVVARS_AS_TAGS :
@@ -82,7 +82,10 @@ def procesor(event, hint):
82
82
class Client (sentry_sdk .Client ):
83
83
def __init__ (self , * args , ** kwargs ):
84
84
kwargs .setdefault ("dsn" , os .environ .get ("PYTEST_SENTRY_DSN" , None ))
85
- kwargs .setdefault ("traces_sample_rate" , float (os .environ .get ("PYTEST_SENTRY_TRACES_SAMPLE_RATE" , 1.0 )))
85
+ kwargs .setdefault (
86
+ "traces_sample_rate" ,
87
+ float (os .environ .get ("PYTEST_SENTRY_TRACES_SAMPLE_RATE" , 1.0 )),
88
+ )
86
89
kwargs .setdefault ("_experiments" , {}).setdefault (
87
90
"auto_enabling_integrations" , True
88
91
)
@@ -94,52 +97,52 @@ def __init__(self, *args, **kwargs):
94
97
95
98
def hookwrapper (itemgetter , ** kwargs ):
96
99
"""
97
- A version of pytest.hookimpl that sets the current hub to the correct one
100
+ A version of pytest.hookimpl that sets the current scope to the correct one
98
101
and skips the hook if the integration is disabled.
99
102
100
103
Assumes the function is a hookwrapper, ie yields once
101
104
"""
102
105
103
106
@wrapt .decorator
104
- def _with_hub (wrapped , instance , args , kwargs ):
107
+ def _with_scope (wrapped , instance , args , kwargs ):
105
108
item = itemgetter (* args , ** kwargs )
106
- hub = _resolve_hub_marker_value (item .get_closest_marker ("sentry_client" ))
109
+ scope = _resolve_scope_marker_value (item .get_closest_marker ("sentry_client" ))
107
110
108
- if hub .get_integration (PytestIntegration ) is None :
111
+ if scope . client .get_integration (PytestIntegration ) is None :
109
112
yield
110
113
else :
111
- with hub :
114
+ with use_scope ( scope ) :
112
115
gen = wrapped (* args , ** kwargs )
113
116
114
117
while True :
115
118
try :
116
- with hub :
119
+ with use_scope ( scope ) :
117
120
chunk = next (gen )
118
121
119
122
y = yield chunk
120
123
121
- with hub :
124
+ with use_scope ( scope ) :
122
125
gen .send (y )
123
126
124
127
except StopIteration :
125
128
break
126
129
127
130
def inner (f ):
128
- return pytest .hookimpl (hookwrapper = True , ** kwargs )(_with_hub (f ))
131
+ return pytest .hookimpl (hookwrapper = True , ** kwargs )(_with_scope (f ))
129
132
130
133
return inner
131
134
132
135
133
136
def pytest_load_initial_conftests (early_config , parser , args ):
134
137
early_config .addinivalue_line (
135
138
"markers" ,
136
- "sentry_client(client=None): Use this client instance for reporting tests. You can also pass a DSN string directly, or a `Hub ` if you need it." ,
139
+ "sentry_client(client=None): Use this client instance for reporting tests. You can also pass a DSN string directly, or a `Scope ` if you need it." ,
137
140
)
138
141
139
142
140
143
def _start_transaction (** kwargs ):
141
144
transaction = Transaction .continue_from_headers (
142
- dict (Hub . current .iter_trace_propagation_headers ()), ** kwargs
145
+ dict (Scope . get_current_scope () .iter_trace_propagation_headers ()), ** kwargs
143
146
)
144
147
transaction .same_process_as_parent = True
145
148
return sentry_sdk .start_transaction (transaction )
@@ -154,7 +157,7 @@ def pytest_runtest_protocol(item):
154
157
# We use the full name including parameters because then we can identify
155
158
# how often a single test has run as part of the same GITHUB_RUN_ID.
156
159
157
- with _start_transaction (op = op , name = u "{} {}" .format (op , name )) as tx :
160
+ with _start_transaction (op = op , name = "{} {}" .format (op , name )) as tx :
158
161
yield
159
162
160
163
# Purposefully drop transaction to spare quota. We only created it to
@@ -171,14 +174,16 @@ def pytest_runtest_call(item):
171
174
# We use the full name including parameters because then we can identify
172
175
# how often a single test has run as part of the same GITHUB_RUN_ID.
173
176
174
- with _start_transaction (op = op , name = u "{} {}" .format (op , name )):
177
+ with _start_transaction (op = op , name = "{} {}" .format (op , name )):
175
178
yield
176
179
177
180
178
181
@hookwrapper (itemgetter = lambda fixturedef , request : request ._pyfuncitem )
179
182
def pytest_fixture_setup (fixturedef , request ):
180
183
op = "pytest.fixture.setup"
181
- with _start_transaction (op = op , name = u"{} {}" .format (op , fixturedef .argname )) as transaction :
184
+ with _start_transaction (
185
+ op = op , name = "{} {}" .format (op , fixturedef .argname )
186
+ ) as transaction :
182
187
transaction .set_tag ("pytest.fixture.scope" , fixturedef .scope )
183
188
yield
184
189
@@ -198,31 +203,31 @@ def pytest_runtest_makereport(item, call):
198
203
call .excinfo
199
204
]
200
205
201
- integration = Hub . current .get_integration (PytestIntegration )
206
+ integration = Scope . get_client () .get_integration (PytestIntegration )
202
207
203
208
if (cur_exc_chain and call .excinfo is None ) or integration .always_report :
204
209
for exc_info in cur_exc_chain :
205
210
capture_exception ((exc_info .type , exc_info .value , exc_info .tb ))
206
211
207
212
208
- DEFAULT_HUB = Hub ( Client ())
213
+ DEFAULT_SCOPE = Scope ( client = Client ())
209
214
210
- _hub_cache = {}
215
+ _scope_cache = {}
211
216
212
217
213
- def _resolve_hub_marker_value (marker_value ):
214
- if id (marker_value ) not in _hub_cache :
215
- _hub_cache [id (marker_value )] = rv = _resolve_hub_marker_value_uncached (
218
+ def _resolve_scope_marker_value (marker_value ):
219
+ if id (marker_value ) not in _scope_cache :
220
+ _scope_cache [id (marker_value )] = rv = _resolve_scope_marker_value_uncached (
216
221
marker_value
217
222
)
218
223
return rv
219
224
220
- return _hub_cache [id (marker_value )]
225
+ return _scope_cache [id (marker_value )]
221
226
222
227
223
- def _resolve_hub_marker_value_uncached (marker_value ):
228
+ def _resolve_scope_marker_value_uncached (marker_value ):
224
229
if marker_value is None :
225
- marker_value = DEFAULT_HUB
230
+ marker_value = DEFAULT_SCOPE
226
231
else :
227
232
marker_value = marker_value .args [0 ]
228
233
@@ -231,35 +236,35 @@ def _resolve_hub_marker_value_uncached(marker_value):
231
236
232
237
if marker_value is None :
233
238
# user explicitly disabled reporting
234
- return Hub ()
239
+ return Scope ()
235
240
236
241
if isinstance (marker_value , str ):
237
- return Hub ( Client (marker_value ))
242
+ return Scope ( client = Client (marker_value ))
238
243
239
244
if isinstance (marker_value , dict ):
240
- return Hub ( Client (** marker_value ))
245
+ return Scope ( client = Client (** marker_value ))
241
246
242
247
if isinstance (marker_value , Client ):
243
- return Hub ( marker_value )
248
+ return Scope ( client = marker_value )
244
249
245
- if isinstance (marker_value , Hub ):
250
+ if isinstance (marker_value , Scope ):
246
251
return marker_value
247
252
248
253
raise RuntimeError (
249
- "The `sentry_client` value must be a client, hub or string, not {}" .format (
254
+ "The `sentry_client` value must be a client, scope or string, not {}" .format (
250
255
repr (type (marker_value ))
251
256
)
252
257
)
253
258
254
259
255
260
@pytest .fixture
256
- def sentry_test_hub (request ):
261
+ def sentry_test_scope (request ):
257
262
"""
258
- Gives back the current hub .
263
+ Gives back the current scope .
259
264
"""
260
265
261
266
item = request .node
262
- return _resolve_hub_marker_value (item .get_closest_marker ("sentry_client" ))
267
+ return _resolve_scope_marker_value (item .get_closest_marker ("sentry_client" ))
263
268
264
269
265
270
def _process_stacktrace (stacktrace ):
0 commit comments