sentry-sdk 2.26.1__py2.py3-none-any.whl → 3.0.0a1__py2.py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of sentry-sdk might be problematic. Click here for more details.

Files changed (114) hide show
  1. sentry_sdk/__init__.py +4 -8
  2. sentry_sdk/_compat.py +0 -1
  3. sentry_sdk/_init_implementation.py +6 -44
  4. sentry_sdk/_log_batcher.py +47 -28
  5. sentry_sdk/_types.py +8 -64
  6. sentry_sdk/ai/monitoring.py +14 -10
  7. sentry_sdk/ai/utils.py +1 -1
  8. sentry_sdk/api.py +69 -163
  9. sentry_sdk/client.py +25 -72
  10. sentry_sdk/consts.py +42 -23
  11. sentry_sdk/debug.py +0 -10
  12. sentry_sdk/envelope.py +2 -10
  13. sentry_sdk/feature_flags.py +5 -1
  14. sentry_sdk/integrations/__init__.py +5 -2
  15. sentry_sdk/integrations/_asgi_common.py +3 -3
  16. sentry_sdk/integrations/_wsgi_common.py +11 -40
  17. sentry_sdk/integrations/aiohttp.py +104 -57
  18. sentry_sdk/integrations/anthropic.py +10 -7
  19. sentry_sdk/integrations/arq.py +24 -13
  20. sentry_sdk/integrations/asgi.py +103 -83
  21. sentry_sdk/integrations/asyncio.py +1 -0
  22. sentry_sdk/integrations/asyncpg.py +45 -30
  23. sentry_sdk/integrations/aws_lambda.py +109 -92
  24. sentry_sdk/integrations/boto3.py +38 -9
  25. sentry_sdk/integrations/bottle.py +1 -1
  26. sentry_sdk/integrations/celery/__init__.py +48 -38
  27. sentry_sdk/integrations/clickhouse_driver.py +59 -28
  28. sentry_sdk/integrations/cohere.py +2 -0
  29. sentry_sdk/integrations/django/__init__.py +25 -46
  30. sentry_sdk/integrations/django/asgi.py +6 -2
  31. sentry_sdk/integrations/django/caching.py +13 -22
  32. sentry_sdk/integrations/django/middleware.py +1 -0
  33. sentry_sdk/integrations/django/signals_handlers.py +3 -1
  34. sentry_sdk/integrations/django/templates.py +8 -12
  35. sentry_sdk/integrations/django/transactions.py +1 -6
  36. sentry_sdk/integrations/django/views.py +5 -2
  37. sentry_sdk/integrations/falcon.py +7 -25
  38. sentry_sdk/integrations/fastapi.py +3 -3
  39. sentry_sdk/integrations/flask.py +1 -1
  40. sentry_sdk/integrations/gcp.py +63 -38
  41. sentry_sdk/integrations/graphene.py +6 -13
  42. sentry_sdk/integrations/grpc/aio/client.py +14 -8
  43. sentry_sdk/integrations/grpc/aio/server.py +19 -21
  44. sentry_sdk/integrations/grpc/client.py +8 -6
  45. sentry_sdk/integrations/grpc/server.py +12 -14
  46. sentry_sdk/integrations/httpx.py +47 -12
  47. sentry_sdk/integrations/huey.py +26 -22
  48. sentry_sdk/integrations/huggingface_hub.py +1 -0
  49. sentry_sdk/integrations/langchain.py +22 -15
  50. sentry_sdk/integrations/launchdarkly.py +3 -3
  51. sentry_sdk/integrations/litestar.py +4 -2
  52. sentry_sdk/integrations/logging.py +12 -3
  53. sentry_sdk/integrations/openai.py +2 -0
  54. sentry_sdk/integrations/openfeature.py +3 -5
  55. sentry_sdk/integrations/pymongo.py +18 -25
  56. sentry_sdk/integrations/pyramid.py +1 -1
  57. sentry_sdk/integrations/quart.py +3 -3
  58. sentry_sdk/integrations/ray.py +23 -17
  59. sentry_sdk/integrations/redis/_async_common.py +30 -18
  60. sentry_sdk/integrations/redis/_sync_common.py +28 -18
  61. sentry_sdk/integrations/redis/modules/caches.py +13 -10
  62. sentry_sdk/integrations/redis/modules/queries.py +14 -11
  63. sentry_sdk/integrations/redis/rb.py +4 -4
  64. sentry_sdk/integrations/redis/redis.py +6 -6
  65. sentry_sdk/integrations/redis/redis_cluster.py +18 -16
  66. sentry_sdk/integrations/redis/redis_py_cluster_legacy.py +4 -4
  67. sentry_sdk/integrations/redis/utils.py +63 -19
  68. sentry_sdk/integrations/rq.py +68 -23
  69. sentry_sdk/integrations/rust_tracing.py +28 -43
  70. sentry_sdk/integrations/sanic.py +23 -13
  71. sentry_sdk/integrations/socket.py +9 -5
  72. sentry_sdk/integrations/sqlalchemy.py +8 -8
  73. sentry_sdk/integrations/starlette.py +11 -31
  74. sentry_sdk/integrations/starlite.py +4 -2
  75. sentry_sdk/integrations/stdlib.py +56 -9
  76. sentry_sdk/integrations/strawberry.py +40 -59
  77. sentry_sdk/integrations/threading.py +10 -26
  78. sentry_sdk/integrations/tornado.py +57 -18
  79. sentry_sdk/integrations/trytond.py +4 -1
  80. sentry_sdk/integrations/unleash.py +2 -3
  81. sentry_sdk/integrations/wsgi.py +84 -38
  82. sentry_sdk/opentelemetry/__init__.py +9 -0
  83. sentry_sdk/opentelemetry/consts.py +33 -0
  84. sentry_sdk/opentelemetry/contextvars_context.py +73 -0
  85. sentry_sdk/{integrations/opentelemetry → opentelemetry}/propagator.py +19 -28
  86. sentry_sdk/opentelemetry/sampler.py +326 -0
  87. sentry_sdk/opentelemetry/scope.py +218 -0
  88. sentry_sdk/opentelemetry/span_processor.py +329 -0
  89. sentry_sdk/opentelemetry/tracing.py +35 -0
  90. sentry_sdk/opentelemetry/utils.py +476 -0
  91. sentry_sdk/profiler/__init__.py +0 -40
  92. sentry_sdk/profiler/continuous_profiler.py +1 -30
  93. sentry_sdk/profiler/transaction_profiler.py +5 -56
  94. sentry_sdk/scope.py +107 -351
  95. sentry_sdk/sessions.py +0 -87
  96. sentry_sdk/tracing.py +418 -1134
  97. sentry_sdk/tracing_utils.py +134 -169
  98. sentry_sdk/transport.py +4 -104
  99. sentry_sdk/types.py +26 -2
  100. sentry_sdk/utils.py +169 -152
  101. {sentry_sdk-2.26.1.dist-info → sentry_sdk-3.0.0a1.dist-info}/METADATA +3 -5
  102. sentry_sdk-3.0.0a1.dist-info/RECORD +154 -0
  103. {sentry_sdk-2.26.1.dist-info → sentry_sdk-3.0.0a1.dist-info}/WHEEL +1 -1
  104. sentry_sdk-3.0.0a1.dist-info/entry_points.txt +2 -0
  105. sentry_sdk/hub.py +0 -739
  106. sentry_sdk/integrations/opentelemetry/__init__.py +0 -7
  107. sentry_sdk/integrations/opentelemetry/consts.py +0 -5
  108. sentry_sdk/integrations/opentelemetry/integration.py +0 -58
  109. sentry_sdk/integrations/opentelemetry/span_processor.py +0 -391
  110. sentry_sdk/metrics.py +0 -965
  111. sentry_sdk-2.26.1.dist-info/RECORD +0 -152
  112. sentry_sdk-2.26.1.dist-info/entry_points.txt +0 -2
  113. {sentry_sdk-2.26.1.dist-info → sentry_sdk-3.0.0a1.dist-info}/licenses/LICENSE +0 -0
  114. {sentry_sdk-2.26.1.dist-info → sentry_sdk-3.0.0a1.dist-info}/top_level.txt +0 -0
sentry_sdk/hub.py DELETED
@@ -1,739 +0,0 @@
1
- import warnings
2
- from contextlib import contextmanager
3
-
4
- from sentry_sdk import (
5
- get_client,
6
- get_global_scope,
7
- get_isolation_scope,
8
- get_current_scope,
9
- )
10
- from sentry_sdk._compat import with_metaclass
11
- from sentry_sdk.consts import INSTRUMENTER
12
- from sentry_sdk.scope import _ScopeManager
13
- from sentry_sdk.client import Client
14
- from sentry_sdk.tracing import (
15
- NoOpSpan,
16
- Span,
17
- Transaction,
18
- )
19
-
20
- from sentry_sdk.utils import (
21
- logger,
22
- ContextVar,
23
- )
24
-
25
- from typing import TYPE_CHECKING
26
-
27
- if TYPE_CHECKING:
28
- from typing import Any
29
- from typing import Callable
30
- from typing import ContextManager
31
- from typing import Dict
32
- from typing import Generator
33
- from typing import List
34
- from typing import Optional
35
- from typing import overload
36
- from typing import Tuple
37
- from typing import Type
38
- from typing import TypeVar
39
- from typing import Union
40
-
41
- from typing_extensions import Unpack
42
-
43
- from sentry_sdk.scope import Scope
44
- from sentry_sdk.client import BaseClient
45
- from sentry_sdk.integrations import Integration
46
- from sentry_sdk._types import (
47
- Event,
48
- Hint,
49
- Breadcrumb,
50
- BreadcrumbHint,
51
- ExcInfo,
52
- LogLevelStr,
53
- SamplingContext,
54
- )
55
- from sentry_sdk.tracing import TransactionKwargs
56
-
57
- T = TypeVar("T")
58
-
59
- else:
60
-
61
- def overload(x):
62
- # type: (T) -> T
63
- return x
64
-
65
-
66
- class SentryHubDeprecationWarning(DeprecationWarning):
67
- """
68
- A custom deprecation warning to inform users that the Hub is deprecated.
69
- """
70
-
71
- _MESSAGE = (
72
- "`sentry_sdk.Hub` is deprecated and will be removed in a future major release. "
73
- "Please consult our 1.x to 2.x migration guide for details on how to migrate "
74
- "`Hub` usage to the new API: "
75
- "https://docs.sentry.io/platforms/python/migration/1.x-to-2.x"
76
- )
77
-
78
- def __init__(self, *_):
79
- # type: (*object) -> None
80
- super().__init__(self._MESSAGE)
81
-
82
-
83
- @contextmanager
84
- def _suppress_hub_deprecation_warning():
85
- # type: () -> Generator[None, None, None]
86
- """Utility function to suppress deprecation warnings for the Hub."""
87
- with warnings.catch_warnings():
88
- warnings.filterwarnings("ignore", category=SentryHubDeprecationWarning)
89
- yield
90
-
91
-
92
- _local = ContextVar("sentry_current_hub")
93
-
94
-
95
- class HubMeta(type):
96
- @property
97
- def current(cls):
98
- # type: () -> Hub
99
- """Returns the current instance of the hub."""
100
- warnings.warn(SentryHubDeprecationWarning(), stacklevel=2)
101
- rv = _local.get(None)
102
- if rv is None:
103
- with _suppress_hub_deprecation_warning():
104
- # This will raise a deprecation warning; suppress it since we already warned above.
105
- rv = Hub(GLOBAL_HUB)
106
- _local.set(rv)
107
- return rv
108
-
109
- @property
110
- def main(cls):
111
- # type: () -> Hub
112
- """Returns the main instance of the hub."""
113
- warnings.warn(SentryHubDeprecationWarning(), stacklevel=2)
114
- return GLOBAL_HUB
115
-
116
-
117
- class Hub(with_metaclass(HubMeta)): # type: ignore
118
- """
119
- .. deprecated:: 2.0.0
120
- The Hub is deprecated. Its functionality will be merged into :py:class:`sentry_sdk.scope.Scope`.
121
-
122
- The hub wraps the concurrency management of the SDK. Each thread has
123
- its own hub but the hub might transfer with the flow of execution if
124
- context vars are available.
125
-
126
- If the hub is used with a with statement it's temporarily activated.
127
- """
128
-
129
- _stack = None # type: List[Tuple[Optional[Client], Scope]]
130
- _scope = None # type: Optional[Scope]
131
-
132
- # Mypy doesn't pick up on the metaclass.
133
-
134
- if TYPE_CHECKING:
135
- current = None # type: Hub
136
- main = None # type: Hub
137
-
138
- def __init__(
139
- self,
140
- client_or_hub=None, # type: Optional[Union[Hub, Client]]
141
- scope=None, # type: Optional[Any]
142
- ):
143
- # type: (...) -> None
144
- warnings.warn(SentryHubDeprecationWarning(), stacklevel=2)
145
-
146
- current_scope = None
147
-
148
- if isinstance(client_or_hub, Hub):
149
- client = get_client()
150
- if scope is None:
151
- # hub cloning is going on, we use a fork of the current/isolation scope for context manager
152
- scope = get_isolation_scope().fork()
153
- current_scope = get_current_scope().fork()
154
- else:
155
- client = client_or_hub # type: ignore
156
- get_global_scope().set_client(client)
157
-
158
- if scope is None: # so there is no Hub cloning going on
159
- # just the current isolation scope is used for context manager
160
- scope = get_isolation_scope()
161
- current_scope = get_current_scope()
162
-
163
- if current_scope is None:
164
- # just the current current scope is used for context manager
165
- current_scope = get_current_scope()
166
-
167
- self._stack = [(client, scope)] # type: ignore
168
- self._last_event_id = None # type: Optional[str]
169
- self._old_hubs = [] # type: List[Hub]
170
-
171
- self._old_current_scopes = [] # type: List[Scope]
172
- self._old_isolation_scopes = [] # type: List[Scope]
173
- self._current_scope = current_scope # type: Scope
174
- self._scope = scope # type: Scope
175
-
176
- def __enter__(self):
177
- # type: () -> Hub
178
- self._old_hubs.append(Hub.current)
179
- _local.set(self)
180
-
181
- current_scope = get_current_scope()
182
- self._old_current_scopes.append(current_scope)
183
- scope._current_scope.set(self._current_scope)
184
-
185
- isolation_scope = get_isolation_scope()
186
- self._old_isolation_scopes.append(isolation_scope)
187
- scope._isolation_scope.set(self._scope)
188
-
189
- return self
190
-
191
- def __exit__(
192
- self,
193
- exc_type, # type: Optional[type]
194
- exc_value, # type: Optional[BaseException]
195
- tb, # type: Optional[Any]
196
- ):
197
- # type: (...) -> None
198
- old = self._old_hubs.pop()
199
- _local.set(old)
200
-
201
- old_current_scope = self._old_current_scopes.pop()
202
- scope._current_scope.set(old_current_scope)
203
-
204
- old_isolation_scope = self._old_isolation_scopes.pop()
205
- scope._isolation_scope.set(old_isolation_scope)
206
-
207
- def run(
208
- self, callback # type: Callable[[], T]
209
- ):
210
- # type: (...) -> T
211
- """
212
- .. deprecated:: 2.0.0
213
- This function is deprecated and will be removed in a future release.
214
-
215
- Runs a callback in the context of the hub. Alternatively the
216
- with statement can be used on the hub directly.
217
- """
218
- with self:
219
- return callback()
220
-
221
- def get_integration(
222
- self, name_or_class # type: Union[str, Type[Integration]]
223
- ):
224
- # type: (...) -> Any
225
- """
226
- .. deprecated:: 2.0.0
227
- This function is deprecated and will be removed in a future release.
228
- Please use :py:meth:`sentry_sdk.client._Client.get_integration` instead.
229
-
230
- Returns the integration for this hub by name or class. If there
231
- is no client bound or the client does not have that integration
232
- then `None` is returned.
233
-
234
- If the return value is not `None` the hub is guaranteed to have a
235
- client attached.
236
- """
237
- return get_client().get_integration(name_or_class)
238
-
239
- @property
240
- def client(self):
241
- # type: () -> Optional[BaseClient]
242
- """
243
- .. deprecated:: 2.0.0
244
- This property is deprecated and will be removed in a future release.
245
- Please use :py:func:`sentry_sdk.api.get_client` instead.
246
-
247
- Returns the current client on the hub.
248
- """
249
- client = get_client()
250
-
251
- if not client.is_active():
252
- return None
253
-
254
- return client
255
-
256
- @property
257
- def scope(self):
258
- # type: () -> Scope
259
- """
260
- .. deprecated:: 2.0.0
261
- This property is deprecated and will be removed in a future release.
262
- Returns the current scope on the hub.
263
- """
264
- return get_isolation_scope()
265
-
266
- def last_event_id(self):
267
- # type: () -> Optional[str]
268
- """
269
- Returns the last event ID.
270
-
271
- .. deprecated:: 1.40.5
272
- This function is deprecated and will be removed in a future release. The functions `capture_event`, `capture_message`, and `capture_exception` return the event ID directly.
273
- """
274
- logger.warning(
275
- "Deprecated: last_event_id is deprecated. This will be removed in the future. The functions `capture_event`, `capture_message`, and `capture_exception` return the event ID directly."
276
- )
277
- return self._last_event_id
278
-
279
- def bind_client(
280
- self, new # type: Optional[BaseClient]
281
- ):
282
- # type: (...) -> None
283
- """
284
- .. deprecated:: 2.0.0
285
- This function is deprecated and will be removed in a future release.
286
- Please use :py:meth:`sentry_sdk.Scope.set_client` instead.
287
-
288
- Binds a new client to the hub.
289
- """
290
- get_global_scope().set_client(new)
291
-
292
- def capture_event(self, event, hint=None, scope=None, **scope_kwargs):
293
- # type: (Event, Optional[Hint], Optional[Scope], Any) -> Optional[str]
294
- """
295
- .. deprecated:: 2.0.0
296
- This function is deprecated and will be removed in a future release.
297
- Please use :py:meth:`sentry_sdk.Scope.capture_event` instead.
298
-
299
- Captures an event.
300
-
301
- Alias of :py:meth:`sentry_sdk.Scope.capture_event`.
302
-
303
- :param event: A ready-made event that can be directly sent to Sentry.
304
-
305
- :param hint: Contains metadata about the event that can be read from `before_send`, such as the original exception object or a HTTP request object.
306
-
307
- :param scope: An optional :py:class:`sentry_sdk.Scope` to apply to events.
308
- The `scope` and `scope_kwargs` parameters are mutually exclusive.
309
-
310
- :param scope_kwargs: Optional data to apply to event.
311
- For supported `**scope_kwargs` see :py:meth:`sentry_sdk.Scope.update_from_kwargs`.
312
- The `scope` and `scope_kwargs` parameters are mutually exclusive.
313
- """
314
- last_event_id = get_current_scope().capture_event(
315
- event, hint, scope=scope, **scope_kwargs
316
- )
317
-
318
- is_transaction = event.get("type") == "transaction"
319
- if last_event_id is not None and not is_transaction:
320
- self._last_event_id = last_event_id
321
-
322
- return last_event_id
323
-
324
- def capture_message(self, message, level=None, scope=None, **scope_kwargs):
325
- # type: (str, Optional[LogLevelStr], Optional[Scope], Any) -> Optional[str]
326
- """
327
- .. deprecated:: 2.0.0
328
- This function is deprecated and will be removed in a future release.
329
- Please use :py:meth:`sentry_sdk.Scope.capture_message` instead.
330
-
331
- Captures a message.
332
-
333
- Alias of :py:meth:`sentry_sdk.Scope.capture_message`.
334
-
335
- :param message: The string to send as the message to Sentry.
336
-
337
- :param level: If no level is provided, the default level is `info`.
338
-
339
- :param scope: An optional :py:class:`sentry_sdk.Scope` to apply to events.
340
- The `scope` and `scope_kwargs` parameters are mutually exclusive.
341
-
342
- :param scope_kwargs: Optional data to apply to event.
343
- For supported `**scope_kwargs` see :py:meth:`sentry_sdk.Scope.update_from_kwargs`.
344
- The `scope` and `scope_kwargs` parameters are mutually exclusive.
345
-
346
- :returns: An `event_id` if the SDK decided to send the event (see :py:meth:`sentry_sdk.client._Client.capture_event`).
347
- """
348
- last_event_id = get_current_scope().capture_message(
349
- message, level=level, scope=scope, **scope_kwargs
350
- )
351
-
352
- if last_event_id is not None:
353
- self._last_event_id = last_event_id
354
-
355
- return last_event_id
356
-
357
- def capture_exception(self, error=None, scope=None, **scope_kwargs):
358
- # type: (Optional[Union[BaseException, ExcInfo]], Optional[Scope], Any) -> Optional[str]
359
- """
360
- .. deprecated:: 2.0.0
361
- This function is deprecated and will be removed in a future release.
362
- Please use :py:meth:`sentry_sdk.Scope.capture_exception` instead.
363
-
364
- Captures an exception.
365
-
366
- Alias of :py:meth:`sentry_sdk.Scope.capture_exception`.
367
-
368
- :param error: An exception to capture. If `None`, `sys.exc_info()` will be used.
369
-
370
- :param scope: An optional :py:class:`sentry_sdk.Scope` to apply to events.
371
- The `scope` and `scope_kwargs` parameters are mutually exclusive.
372
-
373
- :param scope_kwargs: Optional data to apply to event.
374
- For supported `**scope_kwargs` see :py:meth:`sentry_sdk.Scope.update_from_kwargs`.
375
- The `scope` and `scope_kwargs` parameters are mutually exclusive.
376
-
377
- :returns: An `event_id` if the SDK decided to send the event (see :py:meth:`sentry_sdk.client._Client.capture_event`).
378
- """
379
- last_event_id = get_current_scope().capture_exception(
380
- error, scope=scope, **scope_kwargs
381
- )
382
-
383
- if last_event_id is not None:
384
- self._last_event_id = last_event_id
385
-
386
- return last_event_id
387
-
388
- def add_breadcrumb(self, crumb=None, hint=None, **kwargs):
389
- # type: (Optional[Breadcrumb], Optional[BreadcrumbHint], Any) -> None
390
- """
391
- .. deprecated:: 2.0.0
392
- This function is deprecated and will be removed in a future release.
393
- Please use :py:meth:`sentry_sdk.Scope.add_breadcrumb` instead.
394
-
395
- Adds a breadcrumb.
396
-
397
- :param crumb: Dictionary with the data as the sentry v7/v8 protocol expects.
398
-
399
- :param hint: An optional value that can be used by `before_breadcrumb`
400
- to customize the breadcrumbs that are emitted.
401
- """
402
- get_isolation_scope().add_breadcrumb(crumb, hint, **kwargs)
403
-
404
- def start_span(self, instrumenter=INSTRUMENTER.SENTRY, **kwargs):
405
- # type: (str, Any) -> Span
406
- """
407
- .. deprecated:: 2.0.0
408
- This function is deprecated and will be removed in a future release.
409
- Please use :py:meth:`sentry_sdk.Scope.start_span` instead.
410
-
411
- Start a span whose parent is the currently active span or transaction, if any.
412
-
413
- The return value is a :py:class:`sentry_sdk.tracing.Span` instance,
414
- typically used as a context manager to start and stop timing in a `with`
415
- block.
416
-
417
- Only spans contained in a transaction are sent to Sentry. Most
418
- integrations start a transaction at the appropriate time, for example
419
- for every incoming HTTP request. Use
420
- :py:meth:`sentry_sdk.start_transaction` to start a new transaction when
421
- one is not already in progress.
422
-
423
- For supported `**kwargs` see :py:class:`sentry_sdk.tracing.Span`.
424
- """
425
- scope = get_current_scope()
426
- return scope.start_span(instrumenter=instrumenter, **kwargs)
427
-
428
- def start_transaction(
429
- self,
430
- transaction=None,
431
- instrumenter=INSTRUMENTER.SENTRY,
432
- custom_sampling_context=None,
433
- **kwargs
434
- ):
435
- # type: (Optional[Transaction], str, Optional[SamplingContext], Unpack[TransactionKwargs]) -> Union[Transaction, NoOpSpan]
436
- """
437
- .. deprecated:: 2.0.0
438
- This function is deprecated and will be removed in a future release.
439
- Please use :py:meth:`sentry_sdk.Scope.start_transaction` instead.
440
-
441
- Start and return a transaction.
442
-
443
- Start an existing transaction if given, otherwise create and start a new
444
- transaction with kwargs.
445
-
446
- This is the entry point to manual tracing instrumentation.
447
-
448
- A tree structure can be built by adding child spans to the transaction,
449
- and child spans to other spans. To start a new child span within the
450
- transaction or any span, call the respective `.start_child()` method.
451
-
452
- Every child span must be finished before the transaction is finished,
453
- otherwise the unfinished spans are discarded.
454
-
455
- When used as context managers, spans and transactions are automatically
456
- finished at the end of the `with` block. If not using context managers,
457
- call the `.finish()` method.
458
-
459
- When the transaction is finished, it will be sent to Sentry with all its
460
- finished child spans.
461
-
462
- For supported `**kwargs` see :py:class:`sentry_sdk.tracing.Transaction`.
463
- """
464
- scope = get_current_scope()
465
-
466
- # For backwards compatibility, we allow passing the scope as the hub.
467
- # We need a major release to make this nice. (if someone searches the code: deprecated)
468
- # Type checking disabled for this line because deprecated keys are not allowed in the type signature.
469
- kwargs["hub"] = scope # type: ignore
470
-
471
- return scope.start_transaction(
472
- transaction, instrumenter, custom_sampling_context, **kwargs
473
- )
474
-
475
- def continue_trace(self, environ_or_headers, op=None, name=None, source=None):
476
- # type: (Dict[str, Any], Optional[str], Optional[str], Optional[str]) -> Transaction
477
- """
478
- .. deprecated:: 2.0.0
479
- This function is deprecated and will be removed in a future release.
480
- Please use :py:meth:`sentry_sdk.Scope.continue_trace` instead.
481
-
482
- Sets the propagation context from environment or headers and returns a transaction.
483
- """
484
- return get_isolation_scope().continue_trace(
485
- environ_or_headers=environ_or_headers, op=op, name=name, source=source
486
- )
487
-
488
- @overload
489
- def push_scope(
490
- self, callback=None # type: Optional[None]
491
- ):
492
- # type: (...) -> ContextManager[Scope]
493
- pass
494
-
495
- @overload
496
- def push_scope( # noqa: F811
497
- self, callback # type: Callable[[Scope], None]
498
- ):
499
- # type: (...) -> None
500
- pass
501
-
502
- def push_scope( # noqa
503
- self,
504
- callback=None, # type: Optional[Callable[[Scope], None]]
505
- continue_trace=True, # type: bool
506
- ):
507
- # type: (...) -> Optional[ContextManager[Scope]]
508
- """
509
- .. deprecated:: 2.0.0
510
- This function is deprecated and will be removed in a future release.
511
-
512
- Pushes a new layer on the scope stack.
513
-
514
- :param callback: If provided, this method pushes a scope, calls
515
- `callback`, and pops the scope again.
516
-
517
- :returns: If no `callback` is provided, a context manager that should
518
- be used to pop the scope again.
519
- """
520
- if callback is not None:
521
- with self.push_scope() as scope:
522
- callback(scope)
523
- return None
524
-
525
- return _ScopeManager(self)
526
-
527
- def pop_scope_unsafe(self):
528
- # type: () -> Tuple[Optional[Client], Scope]
529
- """
530
- .. deprecated:: 2.0.0
531
- This function is deprecated and will be removed in a future release.
532
-
533
- Pops a scope layer from the stack.
534
-
535
- Try to use the context manager :py:meth:`push_scope` instead.
536
- """
537
- rv = self._stack.pop()
538
- assert self._stack, "stack must have at least one layer"
539
- return rv
540
-
541
- @overload
542
- def configure_scope(
543
- self, callback=None # type: Optional[None]
544
- ):
545
- # type: (...) -> ContextManager[Scope]
546
- pass
547
-
548
- @overload
549
- def configure_scope( # noqa: F811
550
- self, callback # type: Callable[[Scope], None]
551
- ):
552
- # type: (...) -> None
553
- pass
554
-
555
- def configure_scope( # noqa
556
- self,
557
- callback=None, # type: Optional[Callable[[Scope], None]]
558
- continue_trace=True, # type: bool
559
- ):
560
- # type: (...) -> Optional[ContextManager[Scope]]
561
- """
562
- .. deprecated:: 2.0.0
563
- This function is deprecated and will be removed in a future release.
564
-
565
- Reconfigures the scope.
566
-
567
- :param callback: If provided, call the callback with the current scope.
568
-
569
- :returns: If no callback is provided, returns a context manager that returns the scope.
570
- """
571
- scope = get_isolation_scope()
572
-
573
- if continue_trace:
574
- scope.generate_propagation_context()
575
-
576
- if callback is not None:
577
- # TODO: used to return None when client is None. Check if this changes behavior.
578
- callback(scope)
579
-
580
- return None
581
-
582
- @contextmanager
583
- def inner():
584
- # type: () -> Generator[Scope, None, None]
585
- yield scope
586
-
587
- return inner()
588
-
589
- def start_session(
590
- self, session_mode="application" # type: str
591
- ):
592
- # type: (...) -> None
593
- """
594
- .. deprecated:: 2.0.0
595
- This function is deprecated and will be removed in a future release.
596
- Please use :py:meth:`sentry_sdk.Scope.start_session` instead.
597
-
598
- Starts a new session.
599
- """
600
- get_isolation_scope().start_session(
601
- session_mode=session_mode,
602
- )
603
-
604
- def end_session(self):
605
- # type: (...) -> None
606
- """
607
- .. deprecated:: 2.0.0
608
- This function is deprecated and will be removed in a future release.
609
- Please use :py:meth:`sentry_sdk.Scope.end_session` instead.
610
-
611
- Ends the current session if there is one.
612
- """
613
- get_isolation_scope().end_session()
614
-
615
- def stop_auto_session_tracking(self):
616
- # type: (...) -> None
617
- """
618
- .. deprecated:: 2.0.0
619
- This function is deprecated and will be removed in a future release.
620
- Please use :py:meth:`sentry_sdk.Scope.stop_auto_session_tracking` instead.
621
-
622
- Stops automatic session tracking.
623
-
624
- This temporarily session tracking for the current scope when called.
625
- To resume session tracking call `resume_auto_session_tracking`.
626
- """
627
- get_isolation_scope().stop_auto_session_tracking()
628
-
629
- def resume_auto_session_tracking(self):
630
- # type: (...) -> None
631
- """
632
- .. deprecated:: 2.0.0
633
- This function is deprecated and will be removed in a future release.
634
- Please use :py:meth:`sentry_sdk.Scope.resume_auto_session_tracking` instead.
635
-
636
- Resumes automatic session tracking for the current scope if
637
- disabled earlier. This requires that generally automatic session
638
- tracking is enabled.
639
- """
640
- get_isolation_scope().resume_auto_session_tracking()
641
-
642
- def flush(
643
- self,
644
- timeout=None, # type: Optional[float]
645
- callback=None, # type: Optional[Callable[[int, float], None]]
646
- ):
647
- # type: (...) -> None
648
- """
649
- .. deprecated:: 2.0.0
650
- This function is deprecated and will be removed in a future release.
651
- Please use :py:meth:`sentry_sdk.client._Client.flush` instead.
652
-
653
- Alias for :py:meth:`sentry_sdk.client._Client.flush`
654
- """
655
- return get_client().flush(timeout=timeout, callback=callback)
656
-
657
- def get_traceparent(self):
658
- # type: () -> Optional[str]
659
- """
660
- .. deprecated:: 2.0.0
661
- This function is deprecated and will be removed in a future release.
662
- Please use :py:meth:`sentry_sdk.Scope.get_traceparent` instead.
663
-
664
- Returns the traceparent either from the active span or from the scope.
665
- """
666
- current_scope = get_current_scope()
667
- traceparent = current_scope.get_traceparent()
668
-
669
- if traceparent is None:
670
- isolation_scope = get_isolation_scope()
671
- traceparent = isolation_scope.get_traceparent()
672
-
673
- return traceparent
674
-
675
- def get_baggage(self):
676
- # type: () -> Optional[str]
677
- """
678
- .. deprecated:: 2.0.0
679
- This function is deprecated and will be removed in a future release.
680
- Please use :py:meth:`sentry_sdk.Scope.get_baggage` instead.
681
-
682
- Returns Baggage either from the active span or from the scope.
683
- """
684
- current_scope = get_current_scope()
685
- baggage = current_scope.get_baggage()
686
-
687
- if baggage is None:
688
- isolation_scope = get_isolation_scope()
689
- baggage = isolation_scope.get_baggage()
690
-
691
- if baggage is not None:
692
- return baggage.serialize()
693
-
694
- return None
695
-
696
- def iter_trace_propagation_headers(self, span=None):
697
- # type: (Optional[Span]) -> Generator[Tuple[str, str], None, None]
698
- """
699
- .. deprecated:: 2.0.0
700
- This function is deprecated and will be removed in a future release.
701
- Please use :py:meth:`sentry_sdk.Scope.iter_trace_propagation_headers` instead.
702
-
703
- Return HTTP headers which allow propagation of trace data. Data taken
704
- from the span representing the request, if available, or the current
705
- span on the scope if not.
706
- """
707
- return get_current_scope().iter_trace_propagation_headers(
708
- span=span,
709
- )
710
-
711
- def trace_propagation_meta(self, span=None):
712
- # type: (Optional[Span]) -> str
713
- """
714
- .. deprecated:: 2.0.0
715
- This function is deprecated and will be removed in a future release.
716
- Please use :py:meth:`sentry_sdk.Scope.trace_propagation_meta` instead.
717
-
718
- Return meta tags which should be injected into HTML templates
719
- to allow propagation of trace information.
720
- """
721
- if span is not None:
722
- logger.warning(
723
- "The parameter `span` in trace_propagation_meta() is deprecated and will be removed in the future."
724
- )
725
-
726
- return get_current_scope().trace_propagation_meta(
727
- span=span,
728
- )
729
-
730
-
731
- with _suppress_hub_deprecation_warning():
732
- # Suppress deprecation warning for the Hub here, since we still always
733
- # import this module.
734
- GLOBAL_HUB = Hub()
735
- _local.set(GLOBAL_HUB)
736
-
737
-
738
- # Circular imports
739
- from sentry_sdk import scope