tracing.py 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  1. import uuid
  2. import warnings
  3. from datetime import datetime, timedelta, timezone
  4. from enum import Enum
  5. from typing import TYPE_CHECKING
  6. import sentry_sdk
  7. from sentry_sdk.consts import INSTRUMENTER, SPANDATA, SPANSTATUS, SPANTEMPLATE
  8. from sentry_sdk.profiler.continuous_profiler import get_profiler_id
  9. from sentry_sdk.utils import (
  10. capture_internal_exceptions,
  11. get_current_thread_meta,
  12. is_valid_sample_rate,
  13. logger,
  14. nanosecond_time,
  15. should_be_treated_as_error,
  16. )
  17. if TYPE_CHECKING:
  18. from collections.abc import Callable, Mapping, MutableMapping
  19. from typing import (
  20. Any,
  21. Dict,
  22. Iterator,
  23. List,
  24. Optional,
  25. ParamSpec,
  26. Set,
  27. Tuple,
  28. TypeVar,
  29. Union,
  30. overload,
  31. )
  32. from typing_extensions import TypedDict, Unpack
  33. P = ParamSpec("P")
  34. R = TypeVar("R")
  35. from sentry_sdk._types import (
  36. Event,
  37. MeasurementUnit,
  38. MeasurementValue,
  39. SamplingContext,
  40. )
  41. from sentry_sdk.profiler.continuous_profiler import ContinuousProfile
  42. from sentry_sdk.profiler.transaction_profiler import Profile
  43. class SpanKwargs(TypedDict, total=False):
  44. trace_id: str
  45. """
  46. The trace ID of the root span. If this new span is to be the root span,
  47. omit this parameter, and a new trace ID will be generated.
  48. """
  49. span_id: str
  50. """The span ID of this span. If omitted, a new span ID will be generated."""
  51. parent_span_id: str
  52. """The span ID of the parent span, if applicable."""
  53. same_process_as_parent: bool
  54. """Whether this span is in the same process as the parent span."""
  55. sampled: bool
  56. """
  57. Whether the span should be sampled. Overrides the default sampling decision
  58. for this span when provided.
  59. """
  60. op: str
  61. """
  62. The span's operation. A list of recommended values is available here:
  63. https://develop.sentry.dev/sdk/performance/span-operations/
  64. """
  65. description: str
  66. """A description of what operation is being performed within the span. This argument is DEPRECATED. Please use the `name` parameter, instead."""
  67. hub: "Optional[sentry_sdk.Hub]"
  68. """The hub to use for this span. This argument is DEPRECATED. Please use the `scope` parameter, instead."""
  69. status: str
  70. """The span's status. Possible values are listed at https://develop.sentry.dev/sdk/event-payloads/span/"""
  71. containing_transaction: "Optional[Transaction]"
  72. """The transaction that this span belongs to."""
  73. start_timestamp: "Optional[Union[datetime, float]]"
  74. """
  75. The timestamp when the span started. If omitted, the current time
  76. will be used.
  77. """
  78. scope: "sentry_sdk.Scope"
  79. """The scope to use for this span. If not provided, we use the current scope."""
  80. origin: str
  81. """
  82. The origin of the span.
  83. See https://develop.sentry.dev/sdk/performance/trace-origin/
  84. Default "manual".
  85. """
  86. name: str
  87. """A string describing what operation is being performed within the span/transaction."""
  88. class TransactionKwargs(SpanKwargs, total=False):
  89. source: str
  90. """
  91. A string describing the source of the transaction name. This will be used to determine the transaction's type.
  92. See https://develop.sentry.dev/sdk/event-payloads/transaction/#transaction-annotations for more information.
  93. Default "custom".
  94. """
  95. parent_sampled: bool
  96. """Whether the parent transaction was sampled. If True this transaction will be kept, if False it will be discarded."""
  97. baggage: "Baggage"
  98. """The W3C baggage header value. (see https://www.w3.org/TR/baggage/)"""
  99. ProfileContext = TypedDict(
  100. "ProfileContext",
  101. {
  102. "profiler_id": str,
  103. },
  104. )
  105. BAGGAGE_HEADER_NAME = "baggage"
  106. SENTRY_TRACE_HEADER_NAME = "sentry-trace"
  107. # Transaction source
  108. # see https://develop.sentry.dev/sdk/event-payloads/transaction/#transaction-annotations
  109. class TransactionSource(str, Enum):
  110. COMPONENT = "component"
  111. CUSTOM = "custom"
  112. ROUTE = "route"
  113. TASK = "task"
  114. URL = "url"
  115. VIEW = "view"
  116. def __str__(self) -> str:
  117. return self.value
  118. # These are typically high cardinality and the server hates them
  119. LOW_QUALITY_TRANSACTION_SOURCES = [
  120. TransactionSource.URL,
  121. ]
  122. SOURCE_FOR_STYLE = {
  123. "endpoint": TransactionSource.COMPONENT,
  124. "function_name": TransactionSource.COMPONENT,
  125. "handler_name": TransactionSource.COMPONENT,
  126. "method_and_path_pattern": TransactionSource.ROUTE,
  127. "path": TransactionSource.URL,
  128. "route_name": TransactionSource.COMPONENT,
  129. "route_pattern": TransactionSource.ROUTE,
  130. "uri_template": TransactionSource.ROUTE,
  131. "url": TransactionSource.ROUTE,
  132. }
  133. def get_span_status_from_http_code(http_status_code: int) -> str:
  134. """
  135. Returns the Sentry status corresponding to the given HTTP status code.
  136. See: https://develop.sentry.dev/sdk/event-payloads/contexts/#trace-context
  137. """
  138. if http_status_code < 400:
  139. return SPANSTATUS.OK
  140. elif 400 <= http_status_code < 500:
  141. if http_status_code == 403:
  142. return SPANSTATUS.PERMISSION_DENIED
  143. elif http_status_code == 404:
  144. return SPANSTATUS.NOT_FOUND
  145. elif http_status_code == 429:
  146. return SPANSTATUS.RESOURCE_EXHAUSTED
  147. elif http_status_code == 413:
  148. return SPANSTATUS.FAILED_PRECONDITION
  149. elif http_status_code == 401:
  150. return SPANSTATUS.UNAUTHENTICATED
  151. elif http_status_code == 409:
  152. return SPANSTATUS.ALREADY_EXISTS
  153. else:
  154. return SPANSTATUS.INVALID_ARGUMENT
  155. elif 500 <= http_status_code < 600:
  156. if http_status_code == 504:
  157. return SPANSTATUS.DEADLINE_EXCEEDED
  158. elif http_status_code == 501:
  159. return SPANSTATUS.UNIMPLEMENTED
  160. elif http_status_code == 503:
  161. return SPANSTATUS.UNAVAILABLE
  162. else:
  163. return SPANSTATUS.INTERNAL_ERROR
  164. return SPANSTATUS.UNKNOWN_ERROR
  165. class _SpanRecorder:
  166. """Limits the number of spans recorded in a transaction."""
  167. __slots__ = ("maxlen", "spans", "dropped_spans")
  168. def __init__(self, maxlen: int) -> None:
  169. # FIXME: this is `maxlen - 1` only to preserve historical behavior
  170. # enforced by tests.
  171. # Either this should be changed to `maxlen` or the JS SDK implementation
  172. # should be changed to match a consistent interpretation of what maxlen
  173. # limits: either transaction+spans or only child spans.
  174. self.maxlen = maxlen - 1
  175. self.spans: "List[Span]" = []
  176. self.dropped_spans: int = 0
  177. def add(self, span: "Span") -> None:
  178. if len(self.spans) > self.maxlen:
  179. span._span_recorder = None
  180. self.dropped_spans += 1
  181. else:
  182. self.spans.append(span)
  183. class Span:
  184. """A span holds timing information of a block of code.
  185. Spans can have multiple child spans thus forming a span tree.
  186. :param trace_id: The trace ID of the root span. If this new span is to be the root span,
  187. omit this parameter, and a new trace ID will be generated.
  188. :param span_id: The span ID of this span. If omitted, a new span ID will be generated.
  189. :param parent_span_id: The span ID of the parent span, if applicable.
  190. :param same_process_as_parent: Whether this span is in the same process as the parent span.
  191. :param sampled: Whether the span should be sampled. Overrides the default sampling decision
  192. for this span when provided.
  193. :param op: The span's operation. A list of recommended values is available here:
  194. https://develop.sentry.dev/sdk/performance/span-operations/
  195. :param description: A description of what operation is being performed within the span.
  196. .. deprecated:: 2.15.0
  197. Please use the `name` parameter, instead.
  198. :param name: A string describing what operation is being performed within the span.
  199. :param hub: The hub to use for this span.
  200. .. deprecated:: 2.0.0
  201. Please use the `scope` parameter, instead.
  202. :param status: The span's status. Possible values are listed at
  203. https://develop.sentry.dev/sdk/event-payloads/span/
  204. :param containing_transaction: The transaction that this span belongs to.
  205. :param start_timestamp: The timestamp when the span started. If omitted, the current time
  206. will be used.
  207. :param scope: The scope to use for this span. If not provided, we use the current scope.
  208. """
  209. __slots__ = (
  210. "_trace_id",
  211. "_span_id",
  212. "parent_span_id",
  213. "same_process_as_parent",
  214. "sampled",
  215. "op",
  216. "description",
  217. "_measurements",
  218. "start_timestamp",
  219. "_start_timestamp_monotonic_ns",
  220. "status",
  221. "timestamp",
  222. "_tags",
  223. "_data",
  224. "_span_recorder",
  225. "hub",
  226. "_context_manager_state",
  227. "_containing_transaction",
  228. "scope",
  229. "origin",
  230. "name",
  231. "_flags",
  232. "_flags_capacity",
  233. )
  234. def __init__(
  235. self,
  236. trace_id: "Optional[str]" = None,
  237. span_id: "Optional[str]" = None,
  238. parent_span_id: "Optional[str]" = None,
  239. same_process_as_parent: bool = True,
  240. sampled: "Optional[bool]" = None,
  241. op: "Optional[str]" = None,
  242. description: "Optional[str]" = None,
  243. hub: "Optional[sentry_sdk.Hub]" = None, # deprecated
  244. status: "Optional[str]" = None,
  245. containing_transaction: "Optional[Transaction]" = None,
  246. start_timestamp: "Optional[Union[datetime, float]]" = None,
  247. scope: "Optional[sentry_sdk.Scope]" = None,
  248. origin: str = "manual",
  249. name: "Optional[str]" = None,
  250. ) -> None:
  251. self._trace_id = trace_id
  252. self._span_id = span_id
  253. self.parent_span_id = parent_span_id
  254. self.same_process_as_parent = same_process_as_parent
  255. self.sampled = sampled
  256. self.op = op
  257. self.description = name or description
  258. self.status = status
  259. self.hub = hub # backwards compatibility
  260. self.scope = scope
  261. self.origin = origin
  262. self._measurements: "Dict[str, MeasurementValue]" = {}
  263. self._tags: "MutableMapping[str, str]" = {}
  264. self._data: "Dict[str, Any]" = {}
  265. self._containing_transaction = containing_transaction
  266. self._flags: "Dict[str, bool]" = {}
  267. self._flags_capacity = 10
  268. if hub is not None:
  269. warnings.warn(
  270. "The `hub` parameter is deprecated. Please use `scope` instead.",
  271. DeprecationWarning,
  272. stacklevel=2,
  273. )
  274. self.scope = self.scope or hub.scope
  275. if start_timestamp is None:
  276. start_timestamp = datetime.now(timezone.utc)
  277. elif isinstance(start_timestamp, float):
  278. start_timestamp = datetime.fromtimestamp(start_timestamp, timezone.utc)
  279. self.start_timestamp = start_timestamp
  280. try:
  281. # profiling depends on this value and requires that
  282. # it is measured in nanoseconds
  283. self._start_timestamp_monotonic_ns = nanosecond_time()
  284. except AttributeError:
  285. pass
  286. #: End timestamp of span
  287. self.timestamp: "Optional[datetime]" = None
  288. self._span_recorder: "Optional[_SpanRecorder]" = None
  289. self.update_active_thread()
  290. self.set_profiler_id(get_profiler_id())
  291. # TODO this should really live on the Transaction class rather than the Span
  292. # class
  293. def init_span_recorder(self, maxlen: int) -> None:
  294. if self._span_recorder is None:
  295. self._span_recorder = _SpanRecorder(maxlen)
  296. @property
  297. def trace_id(self) -> str:
  298. if not self._trace_id:
  299. self._trace_id = uuid.uuid4().hex
  300. return self._trace_id
  301. @trace_id.setter
  302. def trace_id(self, value: str) -> None:
  303. self._trace_id = value
  304. @property
  305. def span_id(self) -> str:
  306. if not self._span_id:
  307. self._span_id = uuid.uuid4().hex[16:]
  308. return self._span_id
  309. @span_id.setter
  310. def span_id(self, value: str) -> None:
  311. self._span_id = value
  312. def __repr__(self) -> str:
  313. return (
  314. "<%s(op=%r, description:%r, trace_id=%r, span_id=%r, parent_span_id=%r, sampled=%r, origin=%r)>"
  315. % (
  316. self.__class__.__name__,
  317. self.op,
  318. self.description,
  319. self.trace_id,
  320. self.span_id,
  321. self.parent_span_id,
  322. self.sampled,
  323. self.origin,
  324. )
  325. )
  326. def __enter__(self) -> "Span":
  327. scope = self.scope or sentry_sdk.get_current_scope()
  328. old_span = scope.span
  329. scope.span = self
  330. self._context_manager_state = (scope, old_span)
  331. return self
  332. def __exit__(
  333. self, ty: "Optional[Any]", value: "Optional[Any]", tb: "Optional[Any]"
  334. ) -> None:
  335. if value is not None and should_be_treated_as_error(ty, value):
  336. self.set_status(SPANSTATUS.INTERNAL_ERROR)
  337. with capture_internal_exceptions():
  338. scope, old_span = self._context_manager_state
  339. del self._context_manager_state
  340. self.finish(scope)
  341. scope.span = old_span
  342. @property
  343. def containing_transaction(self) -> "Optional[Transaction]":
  344. """The ``Transaction`` that this span belongs to.
  345. The ``Transaction`` is the root of the span tree,
  346. so one could also think of this ``Transaction`` as the "root span"."""
  347. # this is a getter rather than a regular attribute so that transactions
  348. # can return `self` here instead (as a way to prevent them circularly
  349. # referencing themselves)
  350. return self._containing_transaction
  351. def start_child(
  352. self, instrumenter: str = INSTRUMENTER.SENTRY, **kwargs: "Any"
  353. ) -> "Span":
  354. """
  355. Start a sub-span from the current span or transaction.
  356. Takes the same arguments as the initializer of :py:class:`Span`. The
  357. trace id, sampling decision, transaction pointer, and span recorder are
  358. inherited from the current span/transaction.
  359. The instrumenter parameter is deprecated for user code, and it will
  360. be removed in the next major version. Going forward, it should only
  361. be used by the SDK itself.
  362. """
  363. if kwargs.get("description") is not None:
  364. warnings.warn(
  365. "The `description` parameter is deprecated. Please use `name` instead.",
  366. DeprecationWarning,
  367. stacklevel=2,
  368. )
  369. configuration_instrumenter = sentry_sdk.get_client().options["instrumenter"]
  370. if instrumenter != configuration_instrumenter:
  371. return NoOpSpan()
  372. kwargs.setdefault("sampled", self.sampled)
  373. child = Span(
  374. trace_id=self.trace_id,
  375. parent_span_id=self.span_id,
  376. containing_transaction=self.containing_transaction,
  377. **kwargs,
  378. )
  379. span_recorder = (
  380. self.containing_transaction and self.containing_transaction._span_recorder
  381. )
  382. if span_recorder:
  383. span_recorder.add(child)
  384. return child
  385. @classmethod
  386. def continue_from_environ(
  387. cls,
  388. environ: "Mapping[str, str]",
  389. **kwargs: "Any",
  390. ) -> "Transaction":
  391. """
  392. DEPRECATED: Use :py:meth:`sentry_sdk.continue_trace`.
  393. Create a Transaction with the given params, then add in data pulled from
  394. the ``sentry-trace`` and ``baggage`` headers from the environ (if any)
  395. before returning the Transaction.
  396. This is different from :py:meth:`~sentry_sdk.tracing.Span.continue_from_headers`
  397. in that it assumes header names in the form ``HTTP_HEADER_NAME`` -
  398. such as you would get from a WSGI/ASGI environ -
  399. rather than the form ``header-name``.
  400. :param environ: The ASGI/WSGI environ to pull information from.
  401. """
  402. return Transaction.continue_from_headers(EnvironHeaders(environ), **kwargs)
  403. @classmethod
  404. def continue_from_headers(
  405. cls,
  406. headers: "Mapping[str, str]",
  407. *,
  408. _sample_rand: "Optional[str]" = None,
  409. **kwargs: "Any",
  410. ) -> "Transaction":
  411. """
  412. DEPRECATED: Use :py:meth:`sentry_sdk.continue_trace`.
  413. Create a transaction with the given params (including any data pulled from
  414. the ``sentry-trace`` and ``baggage`` headers).
  415. :param headers: The dictionary with the HTTP headers to pull information from.
  416. :param _sample_rand: If provided, we override the sample_rand value from the
  417. incoming headers with this value. (internal use only)
  418. """
  419. logger.warning("Deprecated: use sentry_sdk.continue_trace instead.")
  420. # TODO-neel move away from this kwargs stuff, it's confusing and opaque
  421. # make more explicit
  422. baggage = Baggage.from_incoming_header(
  423. headers.get(BAGGAGE_HEADER_NAME), _sample_rand=_sample_rand
  424. )
  425. kwargs.update({BAGGAGE_HEADER_NAME: baggage})
  426. sentrytrace_kwargs = extract_sentrytrace_data(
  427. headers.get(SENTRY_TRACE_HEADER_NAME)
  428. )
  429. if sentrytrace_kwargs is not None:
  430. kwargs.update(sentrytrace_kwargs)
  431. # If there's an incoming sentry-trace but no incoming baggage header,
  432. # for instance in traces coming from older SDKs,
  433. # baggage will be empty and immutable and won't be populated as head SDK.
  434. baggage.freeze()
  435. transaction = Transaction(**kwargs)
  436. transaction.same_process_as_parent = False
  437. return transaction
  438. def iter_headers(self) -> "Iterator[Tuple[str, str]]":
  439. """
  440. Creates a generator which returns the span's ``sentry-trace`` and ``baggage`` headers.
  441. If the span's containing transaction doesn't yet have a ``baggage`` value,
  442. this will cause one to be generated and stored.
  443. """
  444. if not self.containing_transaction:
  445. # Do not propagate headers if there is no containing transaction. Otherwise, this
  446. # span ends up being the root span of a new trace, and since it does not get sent
  447. # to Sentry, the trace will be missing a root transaction. The dynamic sampling
  448. # context will also be missing, breaking dynamic sampling & traces.
  449. return
  450. yield SENTRY_TRACE_HEADER_NAME, self.to_traceparent()
  451. baggage = self.containing_transaction.get_baggage().serialize()
  452. if baggage:
  453. yield BAGGAGE_HEADER_NAME, baggage
  454. @classmethod
  455. def from_traceparent(
  456. cls,
  457. traceparent: "Optional[str]",
  458. **kwargs: "Any",
  459. ) -> "Optional[Transaction]":
  460. """
  461. DEPRECATED: Use :py:meth:`sentry_sdk.continue_trace`.
  462. Create a ``Transaction`` with the given params, then add in data pulled from
  463. the given ``sentry-trace`` header value before returning the ``Transaction``.
  464. """
  465. if not traceparent:
  466. return None
  467. return cls.continue_from_headers(
  468. {SENTRY_TRACE_HEADER_NAME: traceparent}, **kwargs
  469. )
  470. def to_traceparent(self) -> str:
  471. if self.sampled is True:
  472. sampled = "1"
  473. elif self.sampled is False:
  474. sampled = "0"
  475. else:
  476. sampled = None
  477. traceparent = "%s-%s" % (self.trace_id, self.span_id)
  478. if sampled is not None:
  479. traceparent += "-%s" % (sampled,)
  480. return traceparent
  481. def to_baggage(self) -> "Optional[Baggage]":
  482. """Returns the :py:class:`~sentry_sdk.tracing_utils.Baggage`
  483. associated with this ``Span``, if any. (Taken from the root of the span tree.)
  484. """
  485. if self.containing_transaction:
  486. return self.containing_transaction.get_baggage()
  487. return None
  488. def set_tag(self, key: str, value: "Any") -> None:
  489. self._tags[key] = value
  490. def set_data(self, key: str, value: "Any") -> None:
  491. self._data[key] = value
  492. def update_data(self, data: "Dict[str, Any]") -> None:
  493. self._data.update(data)
  494. def set_flag(self, flag: str, result: bool) -> None:
  495. if len(self._flags) < self._flags_capacity:
  496. self._flags[flag] = result
  497. def set_status(self, value: str) -> None:
  498. self.status = value
  499. def set_measurement(
  500. self, name: str, value: float, unit: "MeasurementUnit" = ""
  501. ) -> None:
  502. """
  503. .. deprecated:: 2.28.0
  504. This function is deprecated and will be removed in the next major release.
  505. """
  506. warnings.warn(
  507. "`set_measurement()` is deprecated and will be removed in the next major version. Please use `set_data()` instead.",
  508. DeprecationWarning,
  509. stacklevel=2,
  510. )
  511. self._measurements[name] = {"value": value, "unit": unit}
  512. def set_thread(
  513. self, thread_id: "Optional[int]", thread_name: "Optional[str]"
  514. ) -> None:
  515. if thread_id is not None:
  516. self.set_data(SPANDATA.THREAD_ID, str(thread_id))
  517. if thread_name is not None:
  518. self.set_data(SPANDATA.THREAD_NAME, thread_name)
  519. def set_profiler_id(self, profiler_id: "Optional[str]") -> None:
  520. if profiler_id is not None:
  521. self.set_data(SPANDATA.PROFILER_ID, profiler_id)
  522. def set_http_status(self, http_status: int) -> None:
  523. self.set_tag(
  524. "http.status_code", str(http_status)
  525. ) # TODO-neel remove in major, we keep this for backwards compatibility
  526. self.set_data(SPANDATA.HTTP_STATUS_CODE, http_status)
  527. self.set_status(get_span_status_from_http_code(http_status))
  528. def is_success(self) -> bool:
  529. return self.status == "ok"
  530. def finish(
  531. self,
  532. scope: "Optional[sentry_sdk.Scope]" = None,
  533. end_timestamp: "Optional[Union[float, datetime]]" = None,
  534. ) -> "Optional[str]":
  535. """
  536. Sets the end timestamp of the span.
  537. Additionally it also creates a breadcrumb from the span,
  538. if the span represents a database or HTTP request.
  539. :param scope: The scope to use for this transaction.
  540. If not provided, the current scope will be used.
  541. :param end_timestamp: Optional timestamp that should
  542. be used as timestamp instead of the current time.
  543. :return: Always ``None``. The type is ``Optional[str]`` to match
  544. the return value of :py:meth:`sentry_sdk.tracing.Transaction.finish`.
  545. """
  546. if self.timestamp is not None:
  547. # This span is already finished, ignore.
  548. return None
  549. try:
  550. if end_timestamp:
  551. if isinstance(end_timestamp, float):
  552. end_timestamp = datetime.fromtimestamp(end_timestamp, timezone.utc)
  553. self.timestamp = end_timestamp
  554. else:
  555. elapsed = nanosecond_time() - self._start_timestamp_monotonic_ns
  556. self.timestamp = self.start_timestamp + timedelta(
  557. microseconds=elapsed / 1000
  558. )
  559. except AttributeError:
  560. self.timestamp = datetime.now(timezone.utc)
  561. scope = scope or sentry_sdk.get_current_scope()
  562. maybe_create_breadcrumbs_from_span(scope, self)
  563. return None
  564. def to_json(self) -> "Dict[str, Any]":
  565. """Returns a JSON-compatible representation of the span."""
  566. rv: "Dict[str, Any]" = {
  567. "trace_id": self.trace_id,
  568. "span_id": self.span_id,
  569. "parent_span_id": self.parent_span_id,
  570. "same_process_as_parent": self.same_process_as_parent,
  571. "op": self.op,
  572. "description": self.description,
  573. "start_timestamp": self.start_timestamp,
  574. "timestamp": self.timestamp,
  575. "origin": self.origin,
  576. }
  577. if self.status:
  578. rv["status"] = self.status
  579. # TODO-neel remove redundant tag in major
  580. self._tags["status"] = self.status
  581. if len(self._measurements) > 0:
  582. rv["measurements"] = self._measurements
  583. tags = self._tags
  584. if tags:
  585. rv["tags"] = tags
  586. data = {}
  587. data.update(self._flags)
  588. data.update(self._data)
  589. if data:
  590. rv["data"] = data
  591. return rv
  592. def get_trace_context(self) -> "Any":
  593. rv: "Dict[str, Any]" = {
  594. "trace_id": self.trace_id,
  595. "span_id": self.span_id,
  596. "parent_span_id": self.parent_span_id,
  597. "op": self.op,
  598. "description": self.description,
  599. "origin": self.origin,
  600. }
  601. if self.status:
  602. rv["status"] = self.status
  603. if self.containing_transaction:
  604. rv["dynamic_sampling_context"] = (
  605. self.containing_transaction.get_baggage().dynamic_sampling_context()
  606. )
  607. data = {}
  608. thread_id = self._data.get(SPANDATA.THREAD_ID)
  609. if thread_id is not None:
  610. data["thread.id"] = thread_id
  611. thread_name = self._data.get(SPANDATA.THREAD_NAME)
  612. if thread_name is not None:
  613. data["thread.name"] = thread_name
  614. if data:
  615. rv["data"] = data
  616. return rv
  617. def get_profile_context(self) -> "Optional[ProfileContext]":
  618. profiler_id = self._data.get(SPANDATA.PROFILER_ID)
  619. if profiler_id is None:
  620. return None
  621. return {
  622. "profiler_id": profiler_id,
  623. }
  624. def update_active_thread(self) -> None:
  625. thread_id, thread_name = get_current_thread_meta()
  626. self.set_thread(thread_id, thread_name)
  627. class Transaction(Span):
  628. """The Transaction is the root element that holds all the spans
  629. for Sentry performance instrumentation.
  630. :param name: Identifier of the transaction.
  631. Will show up in the Sentry UI.
  632. :param parent_sampled: Whether the parent transaction was sampled.
  633. If True this transaction will be kept, if False it will be discarded.
  634. :param baggage: The W3C baggage header value.
  635. (see https://www.w3.org/TR/baggage/)
  636. :param source: A string describing the source of the transaction name.
  637. This will be used to determine the transaction's type.
  638. See https://develop.sentry.dev/sdk/event-payloads/transaction/#transaction-annotations
  639. for more information. Default "custom".
  640. :param kwargs: Additional arguments to be passed to the Span constructor.
  641. See :py:class:`sentry_sdk.tracing.Span` for available arguments.
  642. """
  643. __slots__ = (
  644. "name",
  645. "source",
  646. "parent_sampled",
  647. # used to create baggage value for head SDKs in dynamic sampling
  648. "sample_rate",
  649. "_measurements",
  650. "_contexts",
  651. "_profile",
  652. "_continuous_profile",
  653. "_baggage",
  654. "_sample_rand",
  655. )
  656. def __init__( # type: ignore[misc]
  657. self,
  658. name: str = "",
  659. parent_sampled: "Optional[bool]" = None,
  660. baggage: "Optional[Baggage]" = None,
  661. source: str = TransactionSource.CUSTOM,
  662. **kwargs: "Unpack[SpanKwargs]",
  663. ) -> None:
  664. super().__init__(**kwargs)
  665. self.name = name
  666. self.source = source
  667. self.sample_rate: "Optional[float]" = None
  668. self.parent_sampled = parent_sampled
  669. self._measurements: "Dict[str, MeasurementValue]" = {}
  670. self._contexts: "Dict[str, Any]" = {}
  671. self._profile: "Optional[Profile]" = None
  672. self._continuous_profile: "Optional[ContinuousProfile]" = None
  673. self._baggage = baggage
  674. baggage_sample_rand = (
  675. None if self._baggage is None else self._baggage._sample_rand()
  676. )
  677. if baggage_sample_rand is not None:
  678. self._sample_rand = baggage_sample_rand
  679. else:
  680. self._sample_rand = _generate_sample_rand(self.trace_id)
  681. def __repr__(self) -> str:
  682. return (
  683. "<%s(name=%r, op=%r, trace_id=%r, span_id=%r, parent_span_id=%r, sampled=%r, source=%r, origin=%r)>"
  684. % (
  685. self.__class__.__name__,
  686. self.name,
  687. self.op,
  688. self.trace_id,
  689. self.span_id,
  690. self.parent_span_id,
  691. self.sampled,
  692. self.source,
  693. self.origin,
  694. )
  695. )
  696. def _possibly_started(self) -> bool:
  697. """Returns whether the transaction might have been started.
  698. If this returns False, we know that the transaction was not started
  699. with sentry_sdk.start_transaction, and therefore the transaction will
  700. be discarded.
  701. """
  702. # We must explicitly check self.sampled is False since self.sampled can be None
  703. return self._span_recorder is not None or self.sampled is False
  704. def __enter__(self) -> "Transaction":
  705. if not self._possibly_started():
  706. logger.debug(
  707. "Transaction was entered without being started with sentry_sdk.start_transaction."
  708. "The transaction will not be sent to Sentry. To fix, start the transaction by"
  709. "passing it to sentry_sdk.start_transaction."
  710. )
  711. super().__enter__()
  712. if self._profile is not None:
  713. self._profile.__enter__()
  714. return self
  715. def __exit__(
  716. self, ty: "Optional[Any]", value: "Optional[Any]", tb: "Optional[Any]"
  717. ) -> None:
  718. if self._profile is not None:
  719. self._profile.__exit__(ty, value, tb)
  720. if self._continuous_profile is not None:
  721. self._continuous_profile.stop()
  722. super().__exit__(ty, value, tb)
  723. @property
  724. def containing_transaction(self) -> "Transaction":
  725. """The root element of the span tree.
  726. In the case of a transaction it is the transaction itself.
  727. """
  728. # Transactions (as spans) belong to themselves (as transactions). This
  729. # is a getter rather than a regular attribute to avoid having a circular
  730. # reference.
  731. return self
  732. def _get_scope_from_finish_args(
  733. self,
  734. scope_arg: "Optional[Union[sentry_sdk.Scope, sentry_sdk.Hub]]",
  735. hub_arg: "Optional[Union[sentry_sdk.Scope, sentry_sdk.Hub]]",
  736. ) -> "Optional[sentry_sdk.Scope]":
  737. """
  738. Logic to get the scope from the arguments passed to finish. This
  739. function exists for backwards compatibility with the old finish.
  740. TODO: Remove this function in the next major version.
  741. """
  742. scope_or_hub = scope_arg
  743. if hub_arg is not None:
  744. warnings.warn(
  745. "The `hub` parameter is deprecated. Please use the `scope` parameter, instead.",
  746. DeprecationWarning,
  747. stacklevel=3,
  748. )
  749. scope_or_hub = hub_arg
  750. if isinstance(scope_or_hub, sentry_sdk.Hub):
  751. warnings.warn(
  752. "Passing a Hub to finish is deprecated. Please pass a Scope, instead.",
  753. DeprecationWarning,
  754. stacklevel=3,
  755. )
  756. return scope_or_hub.scope
  757. return scope_or_hub
  758. def _get_log_representation(self) -> str:
  759. return "{op}transaction <{name}>".format(
  760. op=("<" + self.op + "> " if self.op else ""), name=self.name
  761. )
  762. def finish(
  763. self,
  764. scope: "Optional[sentry_sdk.Scope]" = None,
  765. end_timestamp: "Optional[Union[float, datetime]]" = None,
  766. *,
  767. hub: "Optional[sentry_sdk.Hub]" = None,
  768. ) -> "Optional[str]":
  769. """Finishes the transaction and sends it to Sentry.
  770. All finished spans in the transaction will also be sent to Sentry.
  771. :param scope: The Scope to use for this transaction.
  772. If not provided, the current Scope will be used.
  773. :param end_timestamp: Optional timestamp that should
  774. be used as timestamp instead of the current time.
  775. :param hub: The hub to use for this transaction.
  776. This argument is DEPRECATED. Please use the `scope`
  777. parameter, instead.
  778. :return: The event ID if the transaction was sent to Sentry,
  779. otherwise None.
  780. """
  781. if self.timestamp is not None:
  782. # This transaction is already finished, ignore.
  783. return None
  784. # For backwards compatibility, we must handle the case where `scope`
  785. # or `hub` could both either be a `Scope` or a `Hub`.
  786. scope: "Optional[sentry_sdk.Scope]" = self._get_scope_from_finish_args(
  787. scope, hub
  788. )
  789. scope = scope or self.scope or sentry_sdk.get_current_scope()
  790. client = sentry_sdk.get_client()
  791. if not client.is_active():
  792. # We have no active client and therefore nowhere to send this transaction.
  793. return None
  794. if self._span_recorder is None:
  795. # Explicit check against False needed because self.sampled might be None
  796. if self.sampled is False:
  797. logger.debug("Discarding transaction because sampled = False")
  798. else:
  799. logger.debug(
  800. "Discarding transaction because it was not started with sentry_sdk.start_transaction"
  801. )
  802. # This is not entirely accurate because discards here are not
  803. # exclusively based on sample rate but also traces sampler, but
  804. # we handle this the same here.
  805. if client.transport and has_tracing_enabled(client.options):
  806. if client.monitor and client.monitor.downsample_factor > 0:
  807. reason = "backpressure"
  808. else:
  809. reason = "sample_rate"
  810. client.transport.record_lost_event(reason, data_category="transaction")
  811. # Only one span (the transaction itself) is discarded, since we did not record any spans here.
  812. client.transport.record_lost_event(reason, data_category="span")
  813. return None
  814. if not self.name:
  815. logger.warning(
  816. "Transaction has no name, falling back to `<unlabeled transaction>`."
  817. )
  818. self.name = "<unlabeled transaction>"
  819. super().finish(scope, end_timestamp)
  820. status_code = self._data.get(SPANDATA.HTTP_STATUS_CODE)
  821. if (
  822. status_code is not None
  823. and status_code in client.options["trace_ignore_status_codes"]
  824. ):
  825. logger.debug(
  826. "[Tracing] Discarding {transaction_description} because the HTTP status code {status_code} is matched by trace_ignore_status_codes: {trace_ignore_status_codes}".format(
  827. transaction_description=self._get_log_representation(),
  828. status_code=self._data[SPANDATA.HTTP_STATUS_CODE],
  829. trace_ignore_status_codes=client.options[
  830. "trace_ignore_status_codes"
  831. ],
  832. )
  833. )
  834. if client.transport:
  835. client.transport.record_lost_event(
  836. "event_processor", data_category="transaction"
  837. )
  838. num_spans = len(self._span_recorder.spans) + 1
  839. client.transport.record_lost_event(
  840. "event_processor", data_category="span", quantity=num_spans
  841. )
  842. self.sampled = False
  843. if not self.sampled:
  844. # At this point a `sampled = None` should have already been resolved
  845. # to a concrete decision.
  846. if self.sampled is None:
  847. logger.warning("Discarding transaction without sampling decision.")
  848. return None
  849. finished_spans = [
  850. span.to_json()
  851. for span in self._span_recorder.spans
  852. if span.timestamp is not None
  853. ]
  854. len_diff = len(self._span_recorder.spans) - len(finished_spans)
  855. dropped_spans = len_diff + self._span_recorder.dropped_spans
  856. # we do this to break the circular reference of transaction -> span
  857. # recorder -> span -> containing transaction (which is where we started)
  858. # before either the spans or the transaction goes out of scope and has
  859. # to be garbage collected
  860. self._span_recorder = None
  861. contexts = {}
  862. contexts.update(self._contexts)
  863. contexts.update({"trace": self.get_trace_context()})
  864. profile_context = self.get_profile_context()
  865. if profile_context is not None:
  866. contexts.update({"profile": profile_context})
  867. event: "Event" = {
  868. "type": "transaction",
  869. "transaction": self.name,
  870. "transaction_info": {"source": self.source},
  871. "contexts": contexts,
  872. "tags": self._tags,
  873. "timestamp": self.timestamp,
  874. "start_timestamp": self.start_timestamp,
  875. "spans": finished_spans,
  876. }
  877. if dropped_spans > 0:
  878. event["_dropped_spans"] = dropped_spans
  879. if self._profile is not None and self._profile.valid():
  880. event["profile"] = self._profile
  881. self._profile = None
  882. event["measurements"] = self._measurements
  883. return scope.capture_event(event)
  884. def set_measurement(
  885. self, name: str, value: float, unit: "MeasurementUnit" = ""
  886. ) -> None:
  887. """
  888. .. deprecated:: 2.28.0
  889. This function is deprecated and will be removed in the next major release.
  890. """
  891. warnings.warn(
  892. "`set_measurement()` is deprecated and will be removed in the next major version. Please use `set_data()` instead.",
  893. DeprecationWarning,
  894. stacklevel=2,
  895. )
  896. self._measurements[name] = {"value": value, "unit": unit}
  897. def set_context(self, key: str, value: "dict[str, Any]") -> None:
  898. """Sets a context. Transactions can have multiple contexts
  899. and they should follow the format described in the "Contexts Interface"
  900. documentation.
  901. :param key: The name of the context.
  902. :param value: The information about the context.
  903. """
  904. self._contexts[key] = value
  905. def set_http_status(self, http_status: int) -> None:
  906. """Sets the status of the Transaction according to the given HTTP status.
  907. :param http_status: The HTTP status code."""
  908. super().set_http_status(http_status)
  909. self.set_context("response", {"status_code": http_status})
  910. def to_json(self) -> "Dict[str, Any]":
  911. """Returns a JSON-compatible representation of the transaction."""
  912. rv = super().to_json()
  913. rv["name"] = self.name
  914. rv["source"] = self.source
  915. rv["sampled"] = self.sampled
  916. return rv
  917. def get_trace_context(self) -> "Any":
  918. trace_context = super().get_trace_context()
  919. if self._data:
  920. trace_context["data"] = self._data
  921. return trace_context
  922. def get_baggage(self) -> "Baggage":
  923. """Returns the :py:class:`~sentry_sdk.tracing_utils.Baggage`
  924. associated with the Transaction.
  925. The first time a new baggage with Sentry items is made,
  926. it will be frozen."""
  927. if not self._baggage or self._baggage.mutable:
  928. self._baggage = Baggage.populate_from_transaction(self)
  929. return self._baggage
  930. def _set_initial_sampling_decision(
  931. self, sampling_context: "SamplingContext"
  932. ) -> None:
  933. """
  934. Sets the transaction's sampling decision, according to the following
  935. precedence rules:
  936. 1. If a sampling decision is passed to `start_transaction`
  937. (`start_transaction(name: "my transaction", sampled: True)`), that
  938. decision will be used, regardless of anything else
  939. 2. If `traces_sampler` is defined, its decision will be used. It can
  940. choose to keep or ignore any parent sampling decision, or use the
  941. sampling context data to make its own decision or to choose a sample
  942. rate for the transaction.
  943. 3. If `traces_sampler` is not defined, but there's a parent sampling
  944. decision, the parent sampling decision will be used.
  945. 4. If `traces_sampler` is not defined and there's no parent sampling
  946. decision, `traces_sample_rate` will be used.
  947. """
  948. client = sentry_sdk.get_client()
  949. transaction_description = self._get_log_representation()
  950. # nothing to do if tracing is disabled
  951. if not has_tracing_enabled(client.options):
  952. self.sampled = False
  953. return
  954. # if the user has forced a sampling decision by passing a `sampled`
  955. # value when starting the transaction, go with that
  956. if self.sampled is not None:
  957. self.sample_rate = float(self.sampled)
  958. return
  959. # we would have bailed already if neither `traces_sampler` nor
  960. # `traces_sample_rate` were defined, so one of these should work; prefer
  961. # the hook if so
  962. sample_rate = (
  963. client.options["traces_sampler"](sampling_context)
  964. if callable(client.options.get("traces_sampler"))
  965. # default inheritance behavior
  966. else (
  967. sampling_context["parent_sampled"]
  968. if sampling_context["parent_sampled"] is not None
  969. else client.options["traces_sample_rate"]
  970. )
  971. )
  972. # Since this is coming from the user (or from a function provided by the
  973. # user), who knows what we might get. (The only valid values are
  974. # booleans or numbers between 0 and 1.)
  975. if not is_valid_sample_rate(sample_rate, source="Tracing"):
  976. logger.warning(
  977. "[Tracing] Discarding {transaction_description} because of invalid sample rate.".format(
  978. transaction_description=transaction_description,
  979. )
  980. )
  981. self.sampled = False
  982. return
  983. self.sample_rate = float(sample_rate)
  984. if client.monitor:
  985. self.sample_rate /= 2**client.monitor.downsample_factor
  986. # if the function returned 0 (or false), or if `traces_sample_rate` is
  987. # 0, it's a sign the transaction should be dropped
  988. if not self.sample_rate:
  989. logger.debug(
  990. "[Tracing] Discarding {transaction_description} because {reason}".format(
  991. transaction_description=transaction_description,
  992. reason=(
  993. "traces_sampler returned 0 or False"
  994. if callable(client.options.get("traces_sampler"))
  995. else "traces_sample_rate is set to 0"
  996. ),
  997. )
  998. )
  999. self.sampled = False
  1000. return
  1001. # Now we roll the dice.
  1002. self.sampled = self._sample_rand < self.sample_rate
  1003. if self.sampled:
  1004. logger.debug(
  1005. "[Tracing] Starting {transaction_description}".format(
  1006. transaction_description=transaction_description,
  1007. )
  1008. )
  1009. else:
  1010. logger.debug(
  1011. "[Tracing] Discarding {transaction_description} because it's not included in the random sample (sampling rate = {sample_rate})".format(
  1012. transaction_description=transaction_description,
  1013. sample_rate=self.sample_rate,
  1014. )
  1015. )
  1016. class NoOpSpan(Span):
  1017. def __repr__(self) -> str:
  1018. return "<%s>" % self.__class__.__name__
  1019. @property
  1020. def containing_transaction(self) -> "Optional[Transaction]":
  1021. return None
  1022. def start_child(
  1023. self, instrumenter: str = INSTRUMENTER.SENTRY, **kwargs: "Any"
  1024. ) -> "NoOpSpan":
  1025. return NoOpSpan()
  1026. def to_traceparent(self) -> str:
  1027. return ""
  1028. def to_baggage(self) -> "Optional[Baggage]":
  1029. return None
  1030. def get_baggage(self) -> "Optional[Baggage]":
  1031. return None
  1032. def iter_headers(self) -> "Iterator[Tuple[str, str]]":
  1033. return iter(())
  1034. def set_tag(self, key: str, value: "Any") -> None:
  1035. pass
  1036. def set_data(self, key: str, value: "Any") -> None:
  1037. pass
  1038. def update_data(self, data: "Dict[str, Any]") -> None:
  1039. pass
  1040. def set_status(self, value: str) -> None:
  1041. pass
  1042. def set_http_status(self, http_status: int) -> None:
  1043. pass
  1044. def is_success(self) -> bool:
  1045. return True
  1046. def to_json(self) -> "Dict[str, Any]":
  1047. return {}
  1048. def get_trace_context(self) -> "Any":
  1049. return {}
  1050. def get_profile_context(self) -> "Any":
  1051. return {}
  1052. def finish(
  1053. self,
  1054. scope: "Optional[sentry_sdk.Scope]" = None,
  1055. end_timestamp: "Optional[Union[float, datetime]]" = None,
  1056. *,
  1057. hub: "Optional[sentry_sdk.Hub]" = None,
  1058. ) -> "Optional[str]":
  1059. """
  1060. The `hub` parameter is deprecated. Please use the `scope` parameter, instead.
  1061. """
  1062. pass
  1063. def set_measurement(
  1064. self, name: str, value: float, unit: "MeasurementUnit" = ""
  1065. ) -> None:
  1066. pass
  1067. def set_context(self, key: str, value: "dict[str, Any]") -> None:
  1068. pass
  1069. def init_span_recorder(self, maxlen: int) -> None:
  1070. pass
  1071. def _set_initial_sampling_decision(
  1072. self, sampling_context: "SamplingContext"
  1073. ) -> None:
  1074. pass
  1075. if TYPE_CHECKING:
  1076. @overload
  1077. def trace(
  1078. func: None = None,
  1079. *,
  1080. op: "Optional[str]" = None,
  1081. name: "Optional[str]" = None,
  1082. attributes: "Optional[dict[str, Any]]" = None,
  1083. template: "SPANTEMPLATE" = SPANTEMPLATE.DEFAULT,
  1084. ) -> "Callable[[Callable[P, R]], Callable[P, R]]":
  1085. # Handles: @trace() and @trace(op="custom")
  1086. pass
  1087. @overload
  1088. def trace(func: "Callable[P, R]") -> "Callable[P, R]":
  1089. # Handles: @trace
  1090. pass
  1091. def trace(
  1092. func: "Optional[Callable[P, R]]" = None,
  1093. *,
  1094. op: "Optional[str]" = None,
  1095. name: "Optional[str]" = None,
  1096. attributes: "Optional[dict[str, Any]]" = None,
  1097. template: "SPANTEMPLATE" = SPANTEMPLATE.DEFAULT,
  1098. ) -> "Union[Callable[P, R], Callable[[Callable[P, R]], Callable[P, R]]]":
  1099. """
  1100. Decorator to start a child span around a function call.
  1101. This decorator automatically creates a new span when the decorated function
  1102. is called, and finishes the span when the function returns or raises an exception.
  1103. :param func: The function to trace. When used as a decorator without parentheses,
  1104. this is the function being decorated. When used with parameters (e.g.,
  1105. ``@trace(op="custom")``, this should be None.
  1106. :type func: Callable or None
  1107. :param op: The operation name for the span. This is a high-level description
  1108. of what the span represents (e.g., "http.client", "db.query").
  1109. You can use predefined constants from :py:class:`sentry_sdk.consts.OP`
  1110. or provide your own string. If not provided, a default operation will
  1111. be assigned based on the template.
  1112. :type op: str or None
  1113. :param name: The human-readable name/description for the span. If not provided,
  1114. defaults to the function name. This provides more specific details about
  1115. what the span represents (e.g., "GET /api/users", "process_user_data").
  1116. :type name: str or None
  1117. :param attributes: A dictionary of key-value pairs to add as attributes to the span.
  1118. Attribute values must be strings, integers, floats, or booleans. These
  1119. attributes provide additional context about the span's execution.
  1120. :type attributes: dict[str, Any] or None
  1121. :param template: The type of span to create. This determines what kind of
  1122. span instrumentation and data collection will be applied. Use predefined
  1123. constants from :py:class:`sentry_sdk.consts.SPANTEMPLATE`.
  1124. The default is `SPANTEMPLATE.DEFAULT` which is the right choice for most
  1125. use cases.
  1126. :type template: :py:class:`sentry_sdk.consts.SPANTEMPLATE`
  1127. :returns: When used as ``@trace``, returns the decorated function. When used as
  1128. ``@trace(...)`` with parameters, returns a decorator function.
  1129. :rtype: Callable or decorator function
  1130. Example::
  1131. import sentry_sdk
  1132. from sentry_sdk.consts import OP, SPANTEMPLATE
  1133. # Simple usage with default values
  1134. @sentry_sdk.trace
  1135. def process_data():
  1136. # Function implementation
  1137. pass
  1138. # With custom parameters
  1139. @sentry_sdk.trace(
  1140. op=OP.DB_QUERY,
  1141. name="Get user data",
  1142. attributes={"postgres": True}
  1143. )
  1144. def make_db_query(sql):
  1145. # Function implementation
  1146. pass
  1147. # With a custom template
  1148. @sentry_sdk.trace(template=SPANTEMPLATE.AI_TOOL)
  1149. def calculate_interest_rate(amount, rate, years):
  1150. # Function implementation
  1151. pass
  1152. """
  1153. from sentry_sdk.tracing_utils import create_span_decorator
  1154. decorator = create_span_decorator(
  1155. op=op,
  1156. name=name,
  1157. attributes=attributes,
  1158. template=template,
  1159. )
  1160. if func:
  1161. return decorator(func)
  1162. else:
  1163. return decorator
  1164. # Circular imports
  1165. from sentry_sdk.tracing_utils import (
  1166. Baggage,
  1167. EnvironHeaders,
  1168. _generate_sample_rand,
  1169. extract_sentrytrace_data,
  1170. has_tracing_enabled,
  1171. maybe_create_breadcrumbs_from_span,
  1172. )