77import sentry_sdk
88from sentry_sdk .integrations import Integration
99
10- from sentry_sdk import Hub , capture_exception
10+ from sentry_sdk import Scope , capture_exception
1111from 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
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 Hub . current .get_integration (PytestIntegration ) is None :
62+ if Scope . get_client () .get_integration (PytestIntegration ) is None :
6363 return event
6464
6565 for key in _ENVVARS_AS_TAGS :
@@ -82,7 +82,10 @@ 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 ("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+ )
8689 kwargs .setdefault ("_experiments" , {}).setdefault (
8790 "auto_enabling_integrations" , True
8891 )
@@ -94,52 +97,52 @@ def __init__(self, *args, **kwargs):
9497
9598def hookwrapper (itemgetter , ** kwargs ):
9699 """
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
98101 and skips the hook if the integration is disabled.
99102
100103 Assumes the function is a hookwrapper, ie yields once
101104 """
102105
103106 @wrapt .decorator
104- def _with_hub (wrapped , instance , args , kwargs ):
107+ def _with_scope (wrapped , instance , args , kwargs ):
105108 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" ))
107110
108- if hub .get_integration (PytestIntegration ) is None :
111+ if scope . client .get_integration (PytestIntegration ) is None :
109112 yield
110113 else :
111- with hub :
114+ with use_scope ( scope ) :
112115 gen = wrapped (* args , ** kwargs )
113116
114117 while True :
115118 try :
116- with hub :
119+ with use_scope ( scope ) :
117120 chunk = next (gen )
118121
119122 y = yield chunk
120123
121- with hub :
124+ with use_scope ( scope ) :
122125 gen .send (y )
123126
124127 except StopIteration :
125128 break
126129
127130 def inner (f ):
128- return pytest .hookimpl (hookwrapper = True , ** kwargs )(_with_hub (f ))
131+ return pytest .hookimpl (hookwrapper = True , ** kwargs )(_with_scope (f ))
129132
130133 return inner
131134
132135
133136def pytest_load_initial_conftests (early_config , parser , args ):
134137 early_config .addinivalue_line (
135138 "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." ,
137140 )
138141
139142
140143def _start_transaction (** kwargs ):
141144 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
143146 )
144147 transaction .same_process_as_parent = True
145148 return sentry_sdk .start_transaction (transaction )
@@ -154,7 +157,7 @@ def pytest_runtest_protocol(item):
154157 # We use the full name including parameters because then we can identify
155158 # how often a single test has run as part of the same GITHUB_RUN_ID.
156159
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 :
158161 yield
159162
160163 # Purposefully drop transaction to spare quota. We only created it to
@@ -171,14 +174,16 @@ def pytest_runtest_call(item):
171174 # We use the full name including parameters because then we can identify
172175 # how often a single test has run as part of the same GITHUB_RUN_ID.
173176
174- with _start_transaction (op = op , name = u "{} {}" .format (op , name )):
177+ with _start_transaction (op = op , name = "{} {}" .format (op , name )):
175178 yield
176179
177180
178181@hookwrapper (itemgetter = lambda fixturedef , request : request ._pyfuncitem )
179182def pytest_fixture_setup (fixturedef , request ):
180183 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 :
182187 transaction .set_tag ("pytest.fixture.scope" , fixturedef .scope )
183188 yield
184189
@@ -198,31 +203,31 @@ def pytest_runtest_makereport(item, call):
198203 call .excinfo
199204 ]
200205
201- integration = Hub . current .get_integration (PytestIntegration )
206+ integration = Scope . get_client () .get_integration (PytestIntegration )
202207
203208 if (cur_exc_chain and call .excinfo is None ) or integration .always_report :
204209 for exc_info in cur_exc_chain :
205210 capture_exception ((exc_info .type , exc_info .value , exc_info .tb ))
206211
207212
208- DEFAULT_HUB = Hub ( Client ())
213+ DEFAULT_SCOPE = Scope ( client = Client ())
209214
210- _hub_cache = {}
215+ _scope_cache = {}
211216
212217
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 (
216221 marker_value
217222 )
218223 return rv
219224
220- return _hub_cache [id (marker_value )]
225+ return _scope_cache [id (marker_value )]
221226
222227
223- def _resolve_hub_marker_value_uncached (marker_value ):
228+ def _resolve_scope_marker_value_uncached (marker_value ):
224229 if marker_value is None :
225- marker_value = DEFAULT_HUB
230+ marker_value = DEFAULT_SCOPE
226231 else :
227232 marker_value = marker_value .args [0 ]
228233
@@ -231,35 +236,35 @@ def _resolve_hub_marker_value_uncached(marker_value):
231236
232237 if marker_value is None :
233238 # user explicitly disabled reporting
234- return Hub ()
239+ return Scope ()
235240
236241 if isinstance (marker_value , str ):
237- return Hub ( Client (marker_value ))
242+ return Scope ( client = Client (marker_value ))
238243
239244 if isinstance (marker_value , dict ):
240- return Hub ( Client (** marker_value ))
245+ return Scope ( client = Client (** marker_value ))
241246
242247 if isinstance (marker_value , Client ):
243- return Hub ( marker_value )
248+ return Scope ( client = marker_value )
244249
245- if isinstance (marker_value , Hub ):
250+ if isinstance (marker_value , Scope ):
246251 return marker_value
247252
248253 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 (
250255 repr (type (marker_value ))
251256 )
252257 )
253258
254259
255260@pytest .fixture
256- def sentry_test_hub (request ):
261+ def sentry_test_scope (request ):
257262 """
258- Gives back the current hub .
263+ Gives back the current scope .
259264 """
260265
261266 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" ))
263268
264269
265270def _process_stacktrace (stacktrace ):
0 commit comments