hub.py 25 KB

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