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