hub.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. import warnings
  2. from contextlib import contextmanager
  3. from typing import TYPE_CHECKING
  4. from sentry_sdk import (
  5. get_client,
  6. get_current_scope,
  7. get_global_scope,
  8. get_isolation_scope,
  9. )
  10. from sentry_sdk._compat import with_metaclass
  11. from sentry_sdk.client import Client
  12. from sentry_sdk.consts import INSTRUMENTER
  13. from sentry_sdk.scope import _ScopeManager
  14. from sentry_sdk.tracing import (
  15. NoOpSpan,
  16. Span,
  17. Transaction,
  18. )
  19. from sentry_sdk.utils import (
  20. ContextVar,
  21. logger,
  22. )
  23. if TYPE_CHECKING:
  24. from typing import (
  25. Any,
  26. Callable,
  27. ContextManager,
  28. Dict,
  29. Generator,
  30. List,
  31. Optional,
  32. Tuple,
  33. Type,
  34. TypeVar,
  35. Union,
  36. overload,
  37. )
  38. from typing_extensions import Unpack
  39. from sentry_sdk._types import (
  40. Breadcrumb,
  41. BreadcrumbHint,
  42. Event,
  43. ExcInfo,
  44. Hint,
  45. LogLevelStr,
  46. SamplingContext,
  47. )
  48. from sentry_sdk.client import BaseClient
  49. from sentry_sdk.integrations import Integration
  50. from sentry_sdk.scope import Scope
  51. from sentry_sdk.tracing import TransactionKwargs
  52. T = TypeVar("T")
  53. else:
  54. def overload(x: "T") -> "T":
  55. return x
  56. class SentryHubDeprecationWarning(DeprecationWarning):
  57. """
  58. A custom deprecation warning to inform users that the Hub is deprecated.
  59. """
  60. _MESSAGE = (
  61. "`sentry_sdk.Hub` is deprecated and will be removed in a future major release. "
  62. "Please consult our 1.x to 2.x migration guide for details on how to migrate "
  63. "`Hub` usage to the new API: "
  64. "https://docs.sentry.io/platforms/python/migration/1.x-to-2.x"
  65. )
  66. def __init__(self, *_: object) -> None:
  67. super().__init__(self._MESSAGE)
  68. @contextmanager
  69. def _suppress_hub_deprecation_warning() -> "Generator[None, None, None]":
  70. """Utility function to suppress deprecation warnings for the Hub."""
  71. with warnings.catch_warnings():
  72. warnings.filterwarnings("ignore", category=SentryHubDeprecationWarning)
  73. yield
  74. _local = ContextVar("sentry_current_hub")
  75. class HubMeta(type):
  76. @property
  77. def current(cls) -> "Hub":
  78. """Returns the current instance of the hub."""
  79. warnings.warn(SentryHubDeprecationWarning(), stacklevel=2)
  80. rv = _local.get(None)
  81. if rv is None:
  82. with _suppress_hub_deprecation_warning():
  83. # This will raise a deprecation warning; suppress it since we already warned above.
  84. rv = Hub(GLOBAL_HUB)
  85. _local.set(rv)
  86. return rv
  87. @property
  88. def main(cls) -> "Hub":
  89. """Returns the main instance of the hub."""
  90. warnings.warn(SentryHubDeprecationWarning(), stacklevel=2)
  91. return GLOBAL_HUB
  92. class Hub(with_metaclass(HubMeta)): # type: ignore
  93. """
  94. .. deprecated:: 2.0.0
  95. The Hub is deprecated. Its functionality will be merged into :py:class:`sentry_sdk.scope.Scope`.
  96. The hub wraps the concurrency management of the SDK. Each thread has
  97. its own hub but the hub might transfer with the flow of execution if
  98. context vars are available.
  99. If the hub is used with a with statement it's temporarily activated.
  100. """
  101. _stack: "List[Tuple[Optional[Client], Scope]]" = None # type: ignore[assignment]
  102. _scope: "Optional[Scope]" = None
  103. # Mypy doesn't pick up on the metaclass.
  104. if TYPE_CHECKING:
  105. current: "Hub" = None # type: ignore[assignment]
  106. main: "Optional[Hub]" = None
  107. def __init__(
  108. self,
  109. client_or_hub: "Optional[Union[Hub, Client]]" = None,
  110. scope: "Optional[Any]" = None,
  111. ) -> None:
  112. warnings.warn(SentryHubDeprecationWarning(), stacklevel=2)
  113. current_scope = None
  114. if isinstance(client_or_hub, Hub):
  115. client = get_client()
  116. if scope is None:
  117. # hub cloning is going on, we use a fork of the current/isolation scope for context manager
  118. scope = get_isolation_scope().fork()
  119. current_scope = get_current_scope().fork()
  120. else:
  121. client = client_or_hub # type: ignore
  122. get_global_scope().set_client(client)
  123. if scope is None: # so there is no Hub cloning going on
  124. # just the current isolation scope is used for context manager
  125. scope = get_isolation_scope()
  126. current_scope = get_current_scope()
  127. if current_scope is None:
  128. # just the current current scope is used for context manager
  129. current_scope = get_current_scope()
  130. self._stack = [(client, scope)] # type: ignore
  131. self._last_event_id: "Optional[str]" = None
  132. self._old_hubs: "List[Hub]" = []
  133. self._old_current_scopes: "List[Scope]" = []
  134. self._old_isolation_scopes: "List[Scope]" = []
  135. self._current_scope: "Scope" = current_scope
  136. self._scope: "Scope" = scope
  137. def __enter__(self) -> "Hub":
  138. self._old_hubs.append(Hub.current)
  139. _local.set(self)
  140. current_scope = get_current_scope()
  141. self._old_current_scopes.append(current_scope)
  142. scope._current_scope.set(self._current_scope)
  143. isolation_scope = get_isolation_scope()
  144. self._old_isolation_scopes.append(isolation_scope)
  145. scope._isolation_scope.set(self._scope)
  146. return self
  147. def __exit__(
  148. self,
  149. exc_type: "Optional[type]",
  150. exc_value: "Optional[BaseException]",
  151. tb: "Optional[Any]",
  152. ) -> None:
  153. old = self._old_hubs.pop()
  154. _local.set(old)
  155. old_current_scope = self._old_current_scopes.pop()
  156. scope._current_scope.set(old_current_scope)
  157. old_isolation_scope = self._old_isolation_scopes.pop()
  158. scope._isolation_scope.set(old_isolation_scope)
  159. def run(
  160. self,
  161. callback: "Callable[[], T]",
  162. ) -> "T":
  163. """
  164. .. deprecated:: 2.0.0
  165. This function is deprecated and will be removed in a future release.
  166. Runs a callback in the context of the hub. Alternatively the
  167. with statement can be used on the hub directly.
  168. """
  169. with self:
  170. return callback()
  171. def get_integration(
  172. self,
  173. name_or_class: "Union[str, Type[Integration]]",
  174. ) -> "Any":
  175. """
  176. .. deprecated:: 2.0.0
  177. This function is deprecated and will be removed in a future release.
  178. Please use :py:meth:`sentry_sdk.client._Client.get_integration` instead.
  179. Returns the integration for this hub by name or class. If there
  180. is no client bound or the client does not have that integration
  181. then `None` is returned.
  182. If the return value is not `None` the hub is guaranteed to have a
  183. client attached.
  184. """
  185. return get_client().get_integration(name_or_class)
  186. @property
  187. def client(self) -> "Optional[BaseClient]":
  188. """
  189. .. deprecated:: 2.0.0
  190. This property is deprecated and will be removed in a future release.
  191. Please use :py:func:`sentry_sdk.api.get_client` instead.
  192. Returns the current client on the hub.
  193. """
  194. client = get_client()
  195. if not client.is_active():
  196. return None
  197. return client
  198. @property
  199. def scope(self) -> "Scope":
  200. """
  201. .. deprecated:: 2.0.0
  202. This property is deprecated and will be removed in a future release.
  203. Returns the current scope on the hub.
  204. """
  205. return get_isolation_scope()
  206. def last_event_id(self) -> "Optional[str]":
  207. """
  208. Returns the last event ID.
  209. .. deprecated:: 1.40.5
  210. 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.
  211. """
  212. logger.warning(
  213. "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."
  214. )
  215. return self._last_event_id
  216. def bind_client(
  217. self,
  218. new: "Optional[BaseClient]",
  219. ) -> None:
  220. """
  221. .. deprecated:: 2.0.0
  222. This function is deprecated and will be removed in a future release.
  223. Please use :py:meth:`sentry_sdk.Scope.set_client` instead.
  224. Binds a new client to the hub.
  225. """
  226. get_global_scope().set_client(new)
  227. def capture_event(
  228. self,
  229. event: "Event",
  230. hint: "Optional[Hint]" = None,
  231. scope: "Optional[Scope]" = None,
  232. **scope_kwargs: "Any",
  233. ) -> "Optional[str]":
  234. """
  235. .. deprecated:: 2.0.0
  236. This function is deprecated and will be removed in a future release.
  237. Please use :py:meth:`sentry_sdk.Scope.capture_event` instead.
  238. Captures an event.
  239. Alias of :py:meth:`sentry_sdk.Scope.capture_event`.
  240. :param event: A ready-made event that can be directly sent to Sentry.
  241. :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.
  242. :param scope: An optional :py:class:`sentry_sdk.Scope` to apply to events.
  243. The `scope` and `scope_kwargs` parameters are mutually exclusive.
  244. :param scope_kwargs: Optional data to apply to event.
  245. For supported `**scope_kwargs` see :py:meth:`sentry_sdk.Scope.update_from_kwargs`.
  246. The `scope` and `scope_kwargs` parameters are mutually exclusive.
  247. """
  248. last_event_id = get_current_scope().capture_event(
  249. event, hint, scope=scope, **scope_kwargs
  250. )
  251. is_transaction = event.get("type") == "transaction"
  252. if last_event_id is not None and not is_transaction:
  253. self._last_event_id = last_event_id
  254. return last_event_id
  255. def capture_message(
  256. self,
  257. message: str,
  258. level: "Optional[LogLevelStr]" = None,
  259. scope: "Optional[Scope]" = None,
  260. **scope_kwargs: "Any",
  261. ) -> "Optional[str]":
  262. """
  263. .. deprecated:: 2.0.0
  264. This function is deprecated and will be removed in a future release.
  265. Please use :py:meth:`sentry_sdk.Scope.capture_message` instead.
  266. Captures a message.
  267. Alias of :py:meth:`sentry_sdk.Scope.capture_message`.
  268. :param message: The string to send as the message to Sentry.
  269. :param level: If no level is provided, the default level is `info`.
  270. :param scope: An optional :py:class:`sentry_sdk.Scope` to apply to events.
  271. The `scope` and `scope_kwargs` parameters are mutually exclusive.
  272. :param scope_kwargs: Optional data to apply to event.
  273. For supported `**scope_kwargs` see :py:meth:`sentry_sdk.Scope.update_from_kwargs`.
  274. The `scope` and `scope_kwargs` parameters are mutually exclusive.
  275. :returns: An `event_id` if the SDK decided to send the event (see :py:meth:`sentry_sdk.client._Client.capture_event`).
  276. """
  277. last_event_id = get_current_scope().capture_message(
  278. message, level=level, scope=scope, **scope_kwargs
  279. )
  280. if last_event_id is not None:
  281. self._last_event_id = last_event_id
  282. return last_event_id
  283. def capture_exception(
  284. self,
  285. error: "Optional[Union[BaseException, ExcInfo]]" = None,
  286. scope: "Optional[Scope]" = None,
  287. **scope_kwargs: "Any",
  288. ) -> "Optional[str]":
  289. """
  290. .. deprecated:: 2.0.0
  291. This function is deprecated and will be removed in a future release.
  292. Please use :py:meth:`sentry_sdk.Scope.capture_exception` instead.
  293. Captures an exception.
  294. Alias of :py:meth:`sentry_sdk.Scope.capture_exception`.
  295. :param error: An exception to capture. If `None`, `sys.exc_info()` will be used.
  296. :param scope: An optional :py:class:`sentry_sdk.Scope` to apply to events.
  297. The `scope` and `scope_kwargs` parameters are mutually exclusive.
  298. :param scope_kwargs: Optional data to apply to event.
  299. For supported `**scope_kwargs` see :py:meth:`sentry_sdk.Scope.update_from_kwargs`.
  300. The `scope` and `scope_kwargs` parameters are mutually exclusive.
  301. :returns: An `event_id` if the SDK decided to send the event (see :py:meth:`sentry_sdk.client._Client.capture_event`).
  302. """
  303. last_event_id = get_current_scope().capture_exception(
  304. error, scope=scope, **scope_kwargs
  305. )
  306. if last_event_id is not None:
  307. self._last_event_id = last_event_id
  308. return last_event_id
  309. def add_breadcrumb(
  310. self,
  311. crumb: "Optional[Breadcrumb]" = None,
  312. hint: "Optional[BreadcrumbHint]" = None,
  313. **kwargs: "Any",
  314. ) -> None:
  315. """
  316. .. deprecated:: 2.0.0
  317. This function is deprecated and will be removed in a future release.
  318. Please use :py:meth:`sentry_sdk.Scope.add_breadcrumb` instead.
  319. Adds a breadcrumb.
  320. :param crumb: Dictionary with the data as the sentry v7/v8 protocol expects.
  321. :param hint: An optional value that can be used by `before_breadcrumb`
  322. to customize the breadcrumbs that are emitted.
  323. """
  324. get_isolation_scope().add_breadcrumb(crumb, hint, **kwargs)
  325. def start_span(
  326. self, instrumenter: str = INSTRUMENTER.SENTRY, **kwargs: "Any"
  327. ) -> "Span":
  328. """
  329. .. deprecated:: 2.0.0
  330. This function is deprecated and will be removed in a future release.
  331. Please use :py:meth:`sentry_sdk.Scope.start_span` instead.
  332. Start a span whose parent is the currently active span or transaction, if any.
  333. The return value is a :py:class:`sentry_sdk.tracing.Span` instance,
  334. typically used as a context manager to start and stop timing in a `with`
  335. block.
  336. Only spans contained in a transaction are sent to Sentry. Most
  337. integrations start a transaction at the appropriate time, for example
  338. for every incoming HTTP request. Use
  339. :py:meth:`sentry_sdk.start_transaction` to start a new transaction when
  340. one is not already in progress.
  341. For supported `**kwargs` see :py:class:`sentry_sdk.tracing.Span`.
  342. """
  343. scope = get_current_scope()
  344. return scope.start_span(instrumenter=instrumenter, **kwargs)
  345. def start_transaction(
  346. self,
  347. transaction: "Optional[Transaction]" = None,
  348. instrumenter: str = INSTRUMENTER.SENTRY,
  349. custom_sampling_context: "Optional[SamplingContext]" = None,
  350. **kwargs: "Unpack[TransactionKwargs]",
  351. ) -> "Union[Transaction, NoOpSpan]":
  352. """
  353. .. deprecated:: 2.0.0
  354. This function is deprecated and will be removed in a future release.
  355. Please use :py:meth:`sentry_sdk.Scope.start_transaction` instead.
  356. Start and return a transaction.
  357. Start an existing transaction if given, otherwise create and start a new
  358. transaction with kwargs.
  359. This is the entry point to manual tracing instrumentation.
  360. A tree structure can be built by adding child spans to the transaction,
  361. and child spans to other spans. To start a new child span within the
  362. transaction or any span, call the respective `.start_child()` method.
  363. Every child span must be finished before the transaction is finished,
  364. otherwise the unfinished spans are discarded.
  365. When used as context managers, spans and transactions are automatically
  366. finished at the end of the `with` block. If not using context managers,
  367. call the `.finish()` method.
  368. When the transaction is finished, it will be sent to Sentry with all its
  369. finished child spans.
  370. For supported `**kwargs` see :py:class:`sentry_sdk.tracing.Transaction`.
  371. """
  372. scope = get_current_scope()
  373. # For backwards compatibility, we allow passing the scope as the hub.
  374. # We need a major release to make this nice. (if someone searches the code: deprecated)
  375. # Type checking disabled for this line because deprecated keys are not allowed in the type signature.
  376. kwargs["hub"] = scope # type: ignore
  377. return scope.start_transaction(
  378. transaction, instrumenter, custom_sampling_context, **kwargs
  379. )
  380. def continue_trace(
  381. self,
  382. environ_or_headers: "Dict[str, Any]",
  383. op: "Optional[str]" = None,
  384. name: "Optional[str]" = None,
  385. source: "Optional[str]" = None,
  386. ) -> "Transaction":
  387. """
  388. .. deprecated:: 2.0.0
  389. This function is deprecated and will be removed in a future release.
  390. Please use :py:meth:`sentry_sdk.Scope.continue_trace` instead.
  391. Sets the propagation context from environment or headers and returns a transaction.
  392. """
  393. return get_isolation_scope().continue_trace(
  394. environ_or_headers=environ_or_headers, op=op, name=name, source=source
  395. )
  396. @overload
  397. def push_scope(
  398. self,
  399. callback: "Optional[None]" = None,
  400. ) -> "ContextManager[Scope]":
  401. pass
  402. @overload
  403. def push_scope( # noqa: F811
  404. self,
  405. callback: "Callable[[Scope], None]",
  406. ) -> None:
  407. pass
  408. def push_scope( # noqa
  409. self,
  410. callback: "Optional[Callable[[Scope], None]]" = None,
  411. continue_trace: bool = True,
  412. ) -> "Optional[ContextManager[Scope]]":
  413. """
  414. .. deprecated:: 2.0.0
  415. This function is deprecated and will be removed in a future release.
  416. Pushes a new layer on the scope stack.
  417. :param callback: If provided, this method pushes a scope, calls
  418. `callback`, and pops the scope again.
  419. :returns: If no `callback` is provided, a context manager that should
  420. be used to pop the scope again.
  421. """
  422. if callback is not None:
  423. with self.push_scope() as scope:
  424. callback(scope)
  425. return None
  426. return _ScopeManager(self)
  427. def pop_scope_unsafe(self) -> "Tuple[Optional[Client], Scope]":
  428. """
  429. .. deprecated:: 2.0.0
  430. This function is deprecated and will be removed in a future release.
  431. Pops a scope layer from the stack.
  432. Try to use the context manager :py:meth:`push_scope` instead.
  433. """
  434. rv = self._stack.pop()
  435. assert self._stack, "stack must have at least one layer"
  436. return rv
  437. @overload
  438. def configure_scope(
  439. self,
  440. callback: "Optional[None]" = None,
  441. ) -> "ContextManager[Scope]":
  442. pass
  443. @overload
  444. def configure_scope( # noqa: F811
  445. self,
  446. callback: "Callable[[Scope], None]",
  447. ) -> None:
  448. pass
  449. def configure_scope( # noqa
  450. self,
  451. callback: "Optional[Callable[[Scope], None]]" = None,
  452. continue_trace: bool = True,
  453. ) -> "Optional[ContextManager[Scope]]":
  454. """
  455. .. deprecated:: 2.0.0
  456. This function is deprecated and will be removed in a future release.
  457. Reconfigures the scope.
  458. :param callback: If provided, call the callback with the current scope.
  459. :returns: If no callback is provided, returns a context manager that returns the scope.
  460. """
  461. scope = get_isolation_scope()
  462. if continue_trace:
  463. scope.generate_propagation_context()
  464. if callback is not None:
  465. # TODO: used to return None when client is None. Check if this changes behavior.
  466. callback(scope)
  467. return None
  468. @contextmanager
  469. def inner() -> "Generator[Scope, None, None]":
  470. yield scope
  471. return inner()
  472. def start_session(
  473. self,
  474. session_mode: str = "application",
  475. ) -> None:
  476. """
  477. .. deprecated:: 2.0.0
  478. This function is deprecated and will be removed in a future release.
  479. Please use :py:meth:`sentry_sdk.Scope.start_session` instead.
  480. Starts a new session.
  481. """
  482. get_isolation_scope().start_session(
  483. session_mode=session_mode,
  484. )
  485. def end_session(self) -> None:
  486. """
  487. .. deprecated:: 2.0.0
  488. This function is deprecated and will be removed in a future release.
  489. Please use :py:meth:`sentry_sdk.Scope.end_session` instead.
  490. Ends the current session if there is one.
  491. """
  492. get_isolation_scope().end_session()
  493. def stop_auto_session_tracking(self) -> None:
  494. """
  495. .. deprecated:: 2.0.0
  496. This function is deprecated and will be removed in a future release.
  497. Please use :py:meth:`sentry_sdk.Scope.stop_auto_session_tracking` instead.
  498. Stops automatic session tracking.
  499. This temporarily session tracking for the current scope when called.
  500. To resume session tracking call `resume_auto_session_tracking`.
  501. """
  502. get_isolation_scope().stop_auto_session_tracking()
  503. def resume_auto_session_tracking(self) -> None:
  504. """
  505. .. deprecated:: 2.0.0
  506. This function is deprecated and will be removed in a future release.
  507. Please use :py:meth:`sentry_sdk.Scope.resume_auto_session_tracking` instead.
  508. Resumes automatic session tracking for the current scope if
  509. disabled earlier. This requires that generally automatic session
  510. tracking is enabled.
  511. """
  512. get_isolation_scope().resume_auto_session_tracking()
  513. def flush(
  514. self,
  515. timeout: "Optional[float]" = None,
  516. callback: "Optional[Callable[[int, float], None]]" = None,
  517. ) -> None:
  518. """
  519. .. deprecated:: 2.0.0
  520. This function is deprecated and will be removed in a future release.
  521. Please use :py:meth:`sentry_sdk.client._Client.flush` instead.
  522. Alias for :py:meth:`sentry_sdk.client._Client.flush`
  523. """
  524. return get_client().flush(timeout=timeout, callback=callback)
  525. def get_traceparent(self) -> "Optional[str]":
  526. """
  527. .. deprecated:: 2.0.0
  528. This function is deprecated and will be removed in a future release.
  529. Please use :py:meth:`sentry_sdk.Scope.get_traceparent` instead.
  530. Returns the traceparent either from the active span or from the scope.
  531. """
  532. current_scope = get_current_scope()
  533. traceparent = current_scope.get_traceparent()
  534. if traceparent is None:
  535. isolation_scope = get_isolation_scope()
  536. traceparent = isolation_scope.get_traceparent()
  537. return traceparent
  538. def get_baggage(self) -> "Optional[str]":
  539. """
  540. .. deprecated:: 2.0.0
  541. This function is deprecated and will be removed in a future release.
  542. Please use :py:meth:`sentry_sdk.Scope.get_baggage` instead.
  543. Returns Baggage either from the active span or from the scope.
  544. """
  545. current_scope = get_current_scope()
  546. baggage = current_scope.get_baggage()
  547. if baggage is None:
  548. isolation_scope = get_isolation_scope()
  549. baggage = isolation_scope.get_baggage()
  550. if baggage is not None:
  551. return baggage.serialize()
  552. return None
  553. def iter_trace_propagation_headers(
  554. self, span: "Optional[Span]" = None
  555. ) -> "Generator[Tuple[str, str], None, None]":
  556. """
  557. .. deprecated:: 2.0.0
  558. This function is deprecated and will be removed in a future release.
  559. Please use :py:meth:`sentry_sdk.Scope.iter_trace_propagation_headers` instead.
  560. Return HTTP headers which allow propagation of trace data. Data taken
  561. from the span representing the request, if available, or the current
  562. span on the scope if not.
  563. """
  564. return get_current_scope().iter_trace_propagation_headers(
  565. span=span,
  566. )
  567. def trace_propagation_meta(self, span: "Optional[Span]" = None) -> str:
  568. """
  569. .. deprecated:: 2.0.0
  570. This function is deprecated and will be removed in a future release.
  571. Please use :py:meth:`sentry_sdk.Scope.trace_propagation_meta` instead.
  572. Return meta tags which should be injected into HTML templates
  573. to allow propagation of trace information.
  574. """
  575. if span is not None:
  576. logger.warning(
  577. "The parameter `span` in trace_propagation_meta() is deprecated and will be removed in the future."
  578. )
  579. return get_current_scope().trace_propagation_meta(
  580. span=span,
  581. )
  582. with _suppress_hub_deprecation_warning():
  583. # Suppress deprecation warning for the Hub here, since we still always
  584. # import this module.
  585. GLOBAL_HUB = Hub()
  586. _local.set(GLOBAL_HUB)
  587. # Circular imports
  588. from sentry_sdk import scope