77import sentry_sdk
88from sentry_sdk .integrations import Integration
99
10- from sentry_sdk import Scope , capture_exception
10+ from sentry_sdk import Hub , capture_exception
1111from 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
1313
1414_ENVVARS_AS_TAGS = frozenset (
1515 [
@@ -59,7 +59,7 @@ def __init__(self, always_report=None):
5959 def setup_once ():
6060 @add_global_event_processor
6161 def procesor (event , hint ):
62- if Scope . get_client () .get_integration (PytestIntegration ) is None :
62+ if Hub . current .get_integration (PytestIntegration ) is None :
6363 return event
6464
6565 for key in _ENVVARS_AS_TAGS :
@@ -82,10 +82,7 @@ def procesor(event, hint):
8282class Client (sentry_sdk .Client ):
8383 def __init__ (self , * args , ** kwargs ):
8484 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 )))
8986 kwargs .setdefault ("_experiments" , {}).setdefault (
9087 "auto_enabling_integrations" , True
9188 )
@@ -97,52 +94,52 @@ def __init__(self, *args, **kwargs):
9794
9895def hookwrapper (itemgetter , ** kwargs ):
9996 """
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
10198 and skips the hook if the integration is disabled.
10299
103100 Assumes the function is a hookwrapper, ie yields once
104101 """
105102
106103 @wrapt .decorator
107- def _with_scope (wrapped , instance , args , kwargs ):
104+ def _with_hub (wrapped , instance , args , kwargs ):
108105 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" ))
110107
111- if scope . client .get_integration (PytestIntegration ) is None :
108+ if hub .get_integration (PytestIntegration ) is None :
112109 yield
113110 else :
114- with use_scope ( scope ) :
111+ with hub :
115112 gen = wrapped (* args , ** kwargs )
116113
117114 while True :
118115 try :
119- with use_scope ( scope ) :
116+ with hub :
120117 chunk = next (gen )
121118
122119 y = yield chunk
123120
124- with use_scope ( scope ) :
121+ with hub :
125122 gen .send (y )
126123
127124 except StopIteration :
128125 break
129126
130127 def inner (f ):
131- return pytest .hookimpl (hookwrapper = True , ** kwargs )(_with_scope (f ))
128+ return pytest .hookimpl (hookwrapper = True , ** kwargs )(_with_hub (f ))
132129
133130 return inner
134131
135132
136133def pytest_load_initial_conftests (early_config , parser , args ):
137134 early_config .addinivalue_line (
138135 "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." ,
140137 )
141138
142139
143140def _start_transaction (** kwargs ):
144141 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
146143 )
147144 transaction .same_process_as_parent = True
148145 return sentry_sdk .start_transaction (transaction )
@@ -157,7 +154,7 @@ def pytest_runtest_protocol(item):
157154 # We use the full name including parameters because then we can identify
158155 # how often a single test has run as part of the same GITHUB_RUN_ID.
159156
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 :
161158 yield
162159
163160 # Purposefully drop transaction to spare quota. We only created it to
@@ -174,16 +171,14 @@ def pytest_runtest_call(item):
174171 # We use the full name including parameters because then we can identify
175172 # how often a single test has run as part of the same GITHUB_RUN_ID.
176173
177- with _start_transaction (op = op , name = "{} {}" .format (op , name )):
174+ with _start_transaction (op = op , name = u "{} {}" .format (op , name )):
178175 yield
179176
180177
181178@hookwrapper (itemgetter = lambda fixturedef , request : request ._pyfuncitem )
182179def pytest_fixture_setup (fixturedef , request ):
183180 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 :
187182 transaction .set_tag ("pytest.fixture.scope" , fixturedef .scope )
188183 yield
189184
@@ -203,31 +198,31 @@ def pytest_runtest_makereport(item, call):
203198 call .excinfo
204199 ]
205200
206- integration = Scope . get_client () .get_integration (PytestIntegration )
201+ integration = Hub . current .get_integration (PytestIntegration )
207202
208203 if (cur_exc_chain and call .excinfo is None ) or integration .always_report :
209204 for exc_info in cur_exc_chain :
210205 capture_exception ((exc_info .type , exc_info .value , exc_info .tb ))
211206
212207
213- DEFAULT_SCOPE = Scope ( client = Client ())
208+ DEFAULT_HUB = Hub ( Client ())
214209
215- _scope_cache = {}
210+ _hub_cache = {}
216211
217212
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 (
221216 marker_value
222217 )
223218 return rv
224219
225- return _scope_cache [id (marker_value )]
220+ return _hub_cache [id (marker_value )]
226221
227222
228- def _resolve_scope_marker_value_uncached (marker_value ):
223+ def _resolve_hub_marker_value_uncached (marker_value ):
229224 if marker_value is None :
230- marker_value = DEFAULT_SCOPE
225+ marker_value = DEFAULT_HUB
231226 else :
232227 marker_value = marker_value .args [0 ]
233228
@@ -236,35 +231,35 @@ def _resolve_scope_marker_value_uncached(marker_value):
236231
237232 if marker_value is None :
238233 # user explicitly disabled reporting
239- return Scope ()
234+ return Hub ()
240235
241236 if isinstance (marker_value , str ):
242- return Scope ( client = Client (marker_value ))
237+ return Hub ( Client (marker_value ))
243238
244239 if isinstance (marker_value , dict ):
245- return Scope ( client = Client (** marker_value ))
240+ return Hub ( Client (** marker_value ))
246241
247242 if isinstance (marker_value , Client ):
248- return Scope ( client = marker_value )
243+ return Hub ( marker_value )
249244
250- if isinstance (marker_value , Scope ):
245+ if isinstance (marker_value , Hub ):
251246 return marker_value
252247
253248 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 (
255250 repr (type (marker_value ))
256251 )
257252 )
258253
259254
260255@pytest .fixture
261- def sentry_test_scope (request ):
256+ def sentry_test_hub (request ):
262257 """
263- Gives back the current scope .
258+ Gives back the current hub .
264259 """
265260
266261 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" ))
268263
269264
270265def _process_stacktrace (stacktrace ):
0 commit comments