tracing_utils.py 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621
  1. import contextlib
  2. import functools
  3. import inspect
  4. import os
  5. import re
  6. import sys
  7. import warnings
  8. from collections.abc import Mapping, MutableMapping
  9. from datetime import timedelta
  10. from random import Random
  11. from urllib.parse import quote, unquote
  12. import uuid
  13. try:
  14. from re import Pattern
  15. except ImportError:
  16. # 3.6
  17. from typing import Pattern
  18. import sentry_sdk
  19. from sentry_sdk.consts import OP, SPANDATA, SPANSTATUS, SPANTEMPLATE
  20. from sentry_sdk.utils import (
  21. capture_internal_exceptions,
  22. filename_for_module,
  23. logger,
  24. match_regex_list,
  25. qualname_from_function,
  26. safe_repr,
  27. to_string,
  28. try_convert,
  29. is_sentry_url,
  30. is_valid_sample_rate,
  31. _is_external_source,
  32. _is_in_project_root,
  33. _module_in_list,
  34. )
  35. from typing import TYPE_CHECKING
  36. if TYPE_CHECKING:
  37. from typing import Any
  38. from typing import Dict
  39. from typing import Generator
  40. from typing import Optional
  41. from typing import Union
  42. from typing import Iterator
  43. from typing import Tuple
  44. from types import FrameType
  45. from sentry_sdk._types import Attributes
  46. SENTRY_TRACE_REGEX = re.compile(
  47. "^[ \t]*" # whitespace
  48. "([0-9a-f]{32})?" # trace_id
  49. "-?([0-9a-f]{16})?" # span_id
  50. "-?([01])?" # sampled
  51. "[ \t]*$" # whitespace
  52. )
  53. # This is a normal base64 regex, modified to reflect that fact that we strip the
  54. # trailing = or == off
  55. base64_stripped = (
  56. # any of the characters in the base64 "alphabet", in multiples of 4
  57. "([a-zA-Z0-9+/]{4})*"
  58. # either nothing or 2 or 3 base64-alphabet characters (see
  59. # https://en.wikipedia.org/wiki/Base64#Decoding_Base64_without_padding for
  60. # why there's never only 1 extra character)
  61. "([a-zA-Z0-9+/]{2,3})?"
  62. )
  63. class EnvironHeaders(Mapping): # type: ignore
  64. def __init__(
  65. self,
  66. environ: "Mapping[str, str]",
  67. prefix: str = "HTTP_",
  68. ) -> None:
  69. self.environ = environ
  70. self.prefix = prefix
  71. def __getitem__(self, key: str) -> "Optional[Any]":
  72. return self.environ[self.prefix + key.replace("-", "_").upper()]
  73. def __len__(self) -> int:
  74. return sum(1 for _ in iter(self))
  75. def __iter__(self) -> "Generator[str, None, None]":
  76. for k in self.environ:
  77. if not isinstance(k, str):
  78. continue
  79. k = k.replace("-", "_").upper()
  80. if not k.startswith(self.prefix):
  81. continue
  82. yield k[len(self.prefix) :]
  83. def has_tracing_enabled(options: "Optional[Dict[str, Any]]") -> bool:
  84. """
  85. Returns True if either traces_sample_rate or traces_sampler is
  86. defined and enable_tracing is set and not false.
  87. """
  88. if options is None:
  89. return False
  90. return bool(
  91. options.get("enable_tracing") is not False
  92. and (
  93. options.get("traces_sample_rate") is not None
  94. or options.get("traces_sampler") is not None
  95. )
  96. )
  97. def has_span_streaming_enabled(options: "Optional[dict[str, Any]]") -> bool:
  98. if options is None:
  99. return False
  100. return (options.get("_experiments") or {}).get("trace_lifecycle") == "stream"
  101. @contextlib.contextmanager
  102. def record_sql_queries(
  103. cursor: "Any",
  104. query: "Any",
  105. params_list: "Any",
  106. paramstyle: "Optional[str]",
  107. executemany: bool,
  108. record_cursor_repr: bool = False,
  109. span_origin: str = "manual",
  110. ) -> "Generator[sentry_sdk.tracing.Span, None, None]":
  111. # TODO: Bring back capturing of params by default
  112. if sentry_sdk.get_client().options["_experiments"].get("record_sql_params", False):
  113. if not params_list or params_list == [None]:
  114. params_list = None
  115. if paramstyle == "pyformat":
  116. paramstyle = "format"
  117. else:
  118. params_list = None
  119. paramstyle = None
  120. query = _format_sql(cursor, query)
  121. data = {}
  122. if params_list is not None:
  123. data["db.params"] = params_list
  124. if paramstyle is not None:
  125. data["db.paramstyle"] = paramstyle
  126. if executemany:
  127. data["db.executemany"] = True
  128. if record_cursor_repr and cursor is not None:
  129. data["db.cursor"] = cursor
  130. with capture_internal_exceptions():
  131. sentry_sdk.add_breadcrumb(message=query, category="query", data=data)
  132. with sentry_sdk.start_span(
  133. op=OP.DB,
  134. name=query,
  135. origin=span_origin,
  136. ) as span:
  137. for k, v in data.items():
  138. span.set_data(k, v)
  139. yield span
  140. def maybe_create_breadcrumbs_from_span(
  141. scope: "sentry_sdk.Scope", span: "sentry_sdk.tracing.Span"
  142. ) -> None:
  143. if span.op == OP.DB_REDIS:
  144. scope.add_breadcrumb(
  145. message=span.description, type="redis", category="redis", data=span._tags
  146. )
  147. elif span.op == OP.HTTP_CLIENT:
  148. level = None
  149. status_code = span._data.get(SPANDATA.HTTP_STATUS_CODE)
  150. if status_code:
  151. if 500 <= status_code <= 599:
  152. level = "error"
  153. elif 400 <= status_code <= 499:
  154. level = "warning"
  155. if level:
  156. scope.add_breadcrumb(
  157. type="http", category="httplib", data=span._data, level=level
  158. )
  159. else:
  160. scope.add_breadcrumb(type="http", category="httplib", data=span._data)
  161. elif span.op == "subprocess":
  162. scope.add_breadcrumb(
  163. type="subprocess",
  164. category="subprocess",
  165. message=span.description,
  166. data=span._data,
  167. )
  168. def _get_frame_module_abs_path(frame: "FrameType") -> "Optional[str]":
  169. try:
  170. return frame.f_code.co_filename
  171. except Exception:
  172. return None
  173. def _should_be_included(
  174. is_sentry_sdk_frame: bool,
  175. namespace: "Optional[str]",
  176. in_app_include: "Optional[list[str]]",
  177. in_app_exclude: "Optional[list[str]]",
  178. abs_path: "Optional[str]",
  179. project_root: "Optional[str]",
  180. ) -> bool:
  181. # in_app_include takes precedence over in_app_exclude
  182. should_be_included = _module_in_list(namespace, in_app_include)
  183. should_be_excluded = _is_external_source(abs_path) or _module_in_list(
  184. namespace, in_app_exclude
  185. )
  186. return not is_sentry_sdk_frame and (
  187. should_be_included
  188. or (_is_in_project_root(abs_path, project_root) and not should_be_excluded)
  189. )
  190. def add_source(
  191. span: "sentry_sdk.tracing.Span",
  192. project_root: "Optional[str]",
  193. in_app_include: "Optional[list[str]]",
  194. in_app_exclude: "Optional[list[str]]",
  195. ) -> None:
  196. """
  197. Adds OTel compatible source code information to the span
  198. """
  199. # Find the correct frame
  200. frame: "Union[FrameType, None]" = sys._getframe()
  201. while frame is not None:
  202. abs_path = _get_frame_module_abs_path(frame)
  203. try:
  204. namespace: "Optional[str]" = frame.f_globals.get("__name__")
  205. except Exception:
  206. namespace = None
  207. is_sentry_sdk_frame = namespace is not None and namespace.startswith(
  208. "sentry_sdk."
  209. )
  210. should_be_included = _should_be_included(
  211. is_sentry_sdk_frame=is_sentry_sdk_frame,
  212. namespace=namespace,
  213. in_app_include=in_app_include,
  214. in_app_exclude=in_app_exclude,
  215. abs_path=abs_path,
  216. project_root=project_root,
  217. )
  218. if should_be_included:
  219. break
  220. frame = frame.f_back
  221. else:
  222. frame = None
  223. # Set the data
  224. if frame is not None:
  225. try:
  226. lineno = frame.f_lineno
  227. except Exception:
  228. lineno = None
  229. if lineno is not None:
  230. span.set_data(SPANDATA.CODE_LINENO, frame.f_lineno)
  231. try:
  232. namespace = frame.f_globals.get("__name__")
  233. except Exception:
  234. namespace = None
  235. if namespace is not None:
  236. span.set_data(SPANDATA.CODE_NAMESPACE, namespace)
  237. filepath = _get_frame_module_abs_path(frame)
  238. if filepath is not None:
  239. if namespace is not None:
  240. in_app_path = filename_for_module(namespace, filepath)
  241. elif project_root is not None and filepath.startswith(project_root):
  242. in_app_path = filepath.replace(project_root, "").lstrip(os.sep)
  243. else:
  244. in_app_path = filepath
  245. span.set_data(SPANDATA.CODE_FILEPATH, in_app_path)
  246. try:
  247. code_function = frame.f_code.co_name
  248. except Exception:
  249. code_function = None
  250. if code_function is not None:
  251. span.set_data(SPANDATA.CODE_FUNCTION, frame.f_code.co_name)
  252. def add_query_source(span: "sentry_sdk.tracing.Span") -> None:
  253. """
  254. Adds OTel compatible source code information to a database query span
  255. """
  256. client = sentry_sdk.get_client()
  257. if not client.is_active():
  258. return
  259. if span.timestamp is None or span.start_timestamp is None:
  260. return
  261. should_add_query_source = client.options.get("enable_db_query_source", True)
  262. if not should_add_query_source:
  263. return
  264. duration = span.timestamp - span.start_timestamp
  265. threshold = client.options.get("db_query_source_threshold_ms", 0)
  266. slow_query = duration / timedelta(milliseconds=1) > threshold
  267. if not slow_query:
  268. return
  269. add_source(
  270. span=span,
  271. project_root=client.options["project_root"],
  272. in_app_include=client.options.get("in_app_include"),
  273. in_app_exclude=client.options.get("in_app_exclude"),
  274. )
  275. def add_http_request_source(span: "sentry_sdk.tracing.Span") -> None:
  276. """
  277. Adds OTel compatible source code information to a span for an outgoing HTTP request
  278. """
  279. client = sentry_sdk.get_client()
  280. if not client.is_active():
  281. return
  282. if span.timestamp is None or span.start_timestamp is None:
  283. return
  284. should_add_request_source = client.options.get("enable_http_request_source", True)
  285. if not should_add_request_source:
  286. return
  287. duration = span.timestamp - span.start_timestamp
  288. threshold = client.options.get("http_request_source_threshold_ms", 0)
  289. slow_query = duration / timedelta(milliseconds=1) > threshold
  290. if not slow_query:
  291. return
  292. add_source(
  293. span=span,
  294. project_root=client.options["project_root"],
  295. in_app_include=client.options.get("in_app_include"),
  296. in_app_exclude=client.options.get("in_app_exclude"),
  297. )
  298. def extract_sentrytrace_data(
  299. header: "Optional[str]",
  300. ) -> "Optional[Dict[str, Union[str, bool, None]]]":
  301. """
  302. Given a `sentry-trace` header string, return a dictionary of data.
  303. """
  304. if not header:
  305. return None
  306. if header.startswith("00-") and header.endswith("-00"):
  307. header = header[3:-3]
  308. match = SENTRY_TRACE_REGEX.match(header)
  309. if not match:
  310. return None
  311. trace_id, parent_span_id, sampled_str = match.groups()
  312. parent_sampled = None
  313. if trace_id:
  314. trace_id = "{:032x}".format(int(trace_id, 16))
  315. if parent_span_id:
  316. parent_span_id = "{:016x}".format(int(parent_span_id, 16))
  317. if sampled_str:
  318. parent_sampled = sampled_str != "0"
  319. return {
  320. "trace_id": trace_id,
  321. "parent_span_id": parent_span_id,
  322. "parent_sampled": parent_sampled,
  323. }
  324. def _format_sql(cursor: "Any", sql: str) -> "Optional[str]":
  325. real_sql = None
  326. # If we're using psycopg2, it could be that we're
  327. # looking at a query that uses Composed objects. Use psycopg2's mogrify
  328. # function to format the query. We lose per-parameter trimming but gain
  329. # accuracy in formatting.
  330. try:
  331. if hasattr(cursor, "mogrify"):
  332. real_sql = cursor.mogrify(sql)
  333. if isinstance(real_sql, bytes):
  334. real_sql = real_sql.decode(cursor.connection.encoding)
  335. except Exception:
  336. real_sql = None
  337. return real_sql or to_string(sql)
  338. class PropagationContext:
  339. """
  340. The PropagationContext represents the data of a trace in Sentry.
  341. """
  342. __slots__ = (
  343. "_trace_id",
  344. "_span_id",
  345. "parent_span_id",
  346. "parent_sampled",
  347. "baggage",
  348. "custom_sampling_context",
  349. )
  350. def __init__(
  351. self,
  352. trace_id: "Optional[str]" = None,
  353. span_id: "Optional[str]" = None,
  354. parent_span_id: "Optional[str]" = None,
  355. parent_sampled: "Optional[bool]" = None,
  356. dynamic_sampling_context: "Optional[Dict[str, str]]" = None,
  357. baggage: "Optional[Baggage]" = None,
  358. ) -> None:
  359. self._trace_id = trace_id
  360. """The trace id of the Sentry trace."""
  361. self._span_id = span_id
  362. """The span id of the currently executing span."""
  363. self.parent_span_id = parent_span_id
  364. """The id of the parent span that started this span.
  365. The parent span could also be a span in an upstream service."""
  366. self.parent_sampled = parent_sampled
  367. """Boolean indicator if the parent span was sampled.
  368. Important when the parent span originated in an upstream service,
  369. because we want to sample the whole trace, or nothing from the trace."""
  370. self.baggage = baggage
  371. """Parsed baggage header that is used for dynamic sampling decisions."""
  372. """DEPRECATED this only exists for backwards compat of constructor."""
  373. if baggage is None and dynamic_sampling_context is not None:
  374. self.baggage = Baggage(dynamic_sampling_context)
  375. self.custom_sampling_context: "Optional[dict[str, Any]]" = None
  376. @classmethod
  377. def from_incoming_data(
  378. cls, incoming_data: "Dict[str, Any]"
  379. ) -> "PropagationContext":
  380. propagation_context = PropagationContext()
  381. normalized_data = normalize_incoming_data(incoming_data)
  382. sentry_trace_header = normalized_data.get(SENTRY_TRACE_HEADER_NAME)
  383. sentrytrace_data = extract_sentrytrace_data(sentry_trace_header)
  384. # nothing to propagate if no sentry-trace
  385. if sentrytrace_data is None:
  386. return propagation_context
  387. baggage_header = normalized_data.get(BAGGAGE_HEADER_NAME)
  388. baggage = (
  389. Baggage.from_incoming_header(baggage_header) if baggage_header else None
  390. )
  391. if not _should_continue_trace(baggage):
  392. return propagation_context
  393. propagation_context.update(sentrytrace_data)
  394. if baggage:
  395. propagation_context.baggage = baggage
  396. propagation_context._fill_sample_rand()
  397. return propagation_context
  398. @property
  399. def trace_id(self) -> str:
  400. """The trace id of the Sentry trace."""
  401. if not self._trace_id:
  402. # New trace, don't fill in sample_rand
  403. self._trace_id = uuid.uuid4().hex
  404. return self._trace_id
  405. @trace_id.setter
  406. def trace_id(self, value: str) -> None:
  407. self._trace_id = value
  408. @property
  409. def span_id(self) -> str:
  410. """The span id of the currently executed span."""
  411. if not self._span_id:
  412. self._span_id = uuid.uuid4().hex[16:]
  413. return self._span_id
  414. @span_id.setter
  415. def span_id(self, value: str) -> None:
  416. self._span_id = value
  417. @property
  418. def dynamic_sampling_context(self) -> "Optional[Dict[str, Any]]":
  419. return self.get_baggage().dynamic_sampling_context()
  420. def to_traceparent(self) -> str:
  421. return f"{self.trace_id}-{self.span_id}"
  422. def get_baggage(self) -> "Baggage":
  423. if self.baggage is None:
  424. self.baggage = Baggage.populate_from_propagation_context(self)
  425. return self.baggage
  426. def iter_headers(self) -> "Iterator[Tuple[str, str]]":
  427. """
  428. Creates a generator which returns the propagation_context's ``sentry-trace`` and ``baggage`` headers.
  429. """
  430. yield SENTRY_TRACE_HEADER_NAME, self.to_traceparent()
  431. baggage = self.get_baggage().serialize()
  432. if baggage:
  433. yield BAGGAGE_HEADER_NAME, baggage
  434. def update(self, other_dict: "Dict[str, Any]") -> None:
  435. """
  436. Updates the PropagationContext with data from the given dictionary.
  437. """
  438. for key, value in other_dict.items():
  439. try:
  440. setattr(self, key, value)
  441. except AttributeError:
  442. pass
  443. def _set_custom_sampling_context(
  444. self, custom_sampling_context: "dict[str, Any]"
  445. ) -> None:
  446. self.custom_sampling_context = custom_sampling_context
  447. def __repr__(self) -> str:
  448. return "<PropagationContext _trace_id={} _span_id={} parent_span_id={} parent_sampled={} baggage={}>".format(
  449. self._trace_id,
  450. self._span_id,
  451. self.parent_span_id,
  452. self.parent_sampled,
  453. self.baggage,
  454. )
  455. def _fill_sample_rand(self) -> None:
  456. """
  457. Ensure that there is a valid sample_rand value in the baggage.
  458. If there is a valid sample_rand value in the baggage, we keep it.
  459. Otherwise, we generate a sample_rand value according to the following:
  460. - If we have a parent_sampled value and a sample_rate in the DSC, we compute
  461. a sample_rand value randomly in the range:
  462. - [0, sample_rate) if parent_sampled is True,
  463. - or, in the range [sample_rate, 1) if parent_sampled is False.
  464. - If either parent_sampled or sample_rate is missing, we generate a random
  465. value in the range [0, 1).
  466. The sample_rand is deterministically generated from the trace_id, if present.
  467. This function does nothing if there is no baggage.
  468. """
  469. if self.baggage is None:
  470. return
  471. sample_rand = try_convert(float, self.baggage.sentry_items.get("sample_rand"))
  472. if sample_rand is not None and 0 <= sample_rand < 1:
  473. # sample_rand is present and valid, so don't overwrite it
  474. return
  475. # Get the sample rate and compute the transformation that will map the random value
  476. # to the desired range: [0, 1), [0, sample_rate), or [sample_rate, 1).
  477. sample_rate = try_convert(float, self.baggage.sentry_items.get("sample_rate"))
  478. lower, upper = _sample_rand_range(self.parent_sampled, sample_rate)
  479. try:
  480. sample_rand = _generate_sample_rand(self.trace_id, interval=(lower, upper))
  481. except ValueError:
  482. # ValueError is raised if the interval is invalid, i.e. lower >= upper.
  483. # lower >= upper might happen if the incoming trace's sampled flag
  484. # and sample_rate are inconsistent, e.g. sample_rate=0.0 but sampled=True.
  485. # We cannot generate a sensible sample_rand value in this case.
  486. logger.debug(
  487. f"Could not backfill sample_rand, since parent_sampled={self.parent_sampled} "
  488. f"and sample_rate={sample_rate}."
  489. )
  490. return
  491. self.baggage.sentry_items["sample_rand"] = f"{sample_rand:.6f}" # noqa: E231
  492. def _sample_rand(self) -> "Optional[str]":
  493. """Convenience method to get the sample_rand value from the baggage."""
  494. if self.baggage is None:
  495. return None
  496. return self.baggage.sentry_items.get("sample_rand")
  497. class Baggage:
  498. """
  499. The W3C Baggage header information (see https://www.w3.org/TR/baggage/).
  500. Before mutating a `Baggage` object, calling code must check that `mutable` is `True`.
  501. Mutating a `Baggage` object that has `mutable` set to `False` is not allowed, but
  502. it is the caller's responsibility to enforce this restriction.
  503. """
  504. __slots__ = ("sentry_items", "third_party_items", "mutable")
  505. SENTRY_PREFIX = "sentry-"
  506. SENTRY_PREFIX_REGEX = re.compile("^sentry-")
  507. def __init__(
  508. self,
  509. sentry_items: "Dict[str, str]",
  510. third_party_items: str = "",
  511. mutable: bool = True,
  512. ):
  513. self.sentry_items = sentry_items
  514. self.third_party_items = third_party_items
  515. self.mutable = mutable
  516. @classmethod
  517. def from_incoming_header(
  518. cls,
  519. header: "Optional[str]",
  520. *,
  521. _sample_rand: "Optional[str]" = None,
  522. ) -> "Baggage":
  523. """
  524. freeze if incoming header already has sentry baggage
  525. """
  526. sentry_items = {}
  527. third_party_items = ""
  528. mutable = True
  529. if header:
  530. for item in header.split(","):
  531. if "=" not in item:
  532. continue
  533. with capture_internal_exceptions():
  534. item = item.strip()
  535. key, val = item.split("=")
  536. if Baggage.SENTRY_PREFIX_REGEX.match(key):
  537. baggage_key = unquote(key.split("-")[1])
  538. sentry_items[baggage_key] = unquote(val)
  539. mutable = False
  540. else:
  541. third_party_items += ("," if third_party_items else "") + item
  542. if _sample_rand is not None:
  543. sentry_items["sample_rand"] = str(_sample_rand)
  544. mutable = False
  545. return Baggage(sentry_items, third_party_items, mutable)
  546. @classmethod
  547. def from_options(cls, scope: "sentry_sdk.scope.Scope") -> "Optional[Baggage]":
  548. """
  549. Deprecated: use populate_from_propagation_context
  550. """
  551. if scope._propagation_context is None:
  552. return Baggage({})
  553. return Baggage.populate_from_propagation_context(scope._propagation_context)
  554. @classmethod
  555. def populate_from_propagation_context(
  556. cls, propagation_context: "PropagationContext"
  557. ) -> "Baggage":
  558. sentry_items: "Dict[str, str]" = {}
  559. third_party_items = ""
  560. mutable = False
  561. client = sentry_sdk.get_client()
  562. if not client.is_active():
  563. return Baggage(sentry_items)
  564. options = client.options
  565. sentry_items["trace_id"] = propagation_context.trace_id
  566. if options.get("environment"):
  567. sentry_items["environment"] = options["environment"]
  568. if options.get("release"):
  569. sentry_items["release"] = options["release"]
  570. if client.parsed_dsn:
  571. sentry_items["public_key"] = client.parsed_dsn.public_key
  572. if client.parsed_dsn.org_id:
  573. sentry_items["org_id"] = client.parsed_dsn.org_id
  574. if options.get("traces_sample_rate"):
  575. sentry_items["sample_rate"] = str(options["traces_sample_rate"])
  576. return Baggage(sentry_items, third_party_items, mutable)
  577. @classmethod
  578. def populate_from_transaction(
  579. cls, transaction: "sentry_sdk.tracing.Transaction"
  580. ) -> "Baggage":
  581. """
  582. Populate fresh baggage entry with sentry_items and make it immutable
  583. if this is the head SDK which originates traces.
  584. """
  585. client = sentry_sdk.get_client()
  586. sentry_items: "Dict[str, str]" = {}
  587. if not client.is_active():
  588. return Baggage(sentry_items)
  589. options = client.options or {}
  590. sentry_items["trace_id"] = transaction.trace_id
  591. sentry_items["sample_rand"] = f"{transaction._sample_rand:.6f}" # noqa: E231
  592. if options.get("environment"):
  593. sentry_items["environment"] = options["environment"]
  594. if options.get("release"):
  595. sentry_items["release"] = options["release"]
  596. if client.parsed_dsn:
  597. sentry_items["public_key"] = client.parsed_dsn.public_key
  598. if client.parsed_dsn.org_id:
  599. sentry_items["org_id"] = client.parsed_dsn.org_id
  600. if (
  601. transaction.name
  602. and transaction.source not in LOW_QUALITY_TRANSACTION_SOURCES
  603. ):
  604. sentry_items["transaction"] = transaction.name
  605. if transaction.sample_rate is not None:
  606. sentry_items["sample_rate"] = str(transaction.sample_rate)
  607. if transaction.sampled is not None:
  608. sentry_items["sampled"] = "true" if transaction.sampled else "false"
  609. # there's an existing baggage but it was mutable,
  610. # which is why we are creating this new baggage.
  611. # However, if by chance the user put some sentry items in there, give them precedence.
  612. if transaction._baggage and transaction._baggage.sentry_items:
  613. sentry_items.update(transaction._baggage.sentry_items)
  614. return Baggage(sentry_items, mutable=False)
  615. @classmethod
  616. def populate_from_segment(cls, segment: "StreamedSpan") -> "Baggage":
  617. """
  618. Populate fresh baggage entry with sentry_items and make it immutable
  619. if this is the head SDK which originates traces.
  620. """
  621. client = sentry_sdk.get_client()
  622. sentry_items: "Dict[str, str]" = {}
  623. if not client.is_active():
  624. return Baggage(sentry_items)
  625. options = client.options or {}
  626. sentry_items["trace_id"] = segment.trace_id
  627. sentry_items["sample_rand"] = f"{segment._sample_rand:.6f}" # noqa: E231
  628. if options.get("environment"):
  629. sentry_items["environment"] = options["environment"]
  630. if options.get("release"):
  631. sentry_items["release"] = options["release"]
  632. if client.parsed_dsn:
  633. sentry_items["public_key"] = client.parsed_dsn.public_key
  634. if client.parsed_dsn.org_id:
  635. sentry_items["org_id"] = client.parsed_dsn.org_id
  636. if (
  637. segment.get_attributes().get("sentry.span.source")
  638. not in LOW_QUALITY_SEGMENT_SOURCES
  639. ) and segment._name:
  640. sentry_items["transaction"] = segment._name
  641. if segment._sample_rate is not None:
  642. sentry_items["sample_rate"] = str(segment._sample_rate)
  643. if segment.sampled is not None:
  644. sentry_items["sampled"] = "true" if segment.sampled else "false"
  645. # There's an existing baggage but it was mutable, which is why we are
  646. # creating this new baggage.
  647. # However, if by chance the user put some sentry items in there, give
  648. # them precedence.
  649. if segment._baggage and segment._baggage.sentry_items:
  650. sentry_items.update(segment._baggage.sentry_items)
  651. return Baggage(sentry_items, mutable=False)
  652. def freeze(self) -> None:
  653. self.mutable = False
  654. def dynamic_sampling_context(self) -> "Dict[str, str]":
  655. header = {}
  656. for key, item in self.sentry_items.items():
  657. header[key] = item
  658. return header
  659. def serialize(self, include_third_party: bool = False) -> str:
  660. items = []
  661. for key, val in self.sentry_items.items():
  662. with capture_internal_exceptions():
  663. item = Baggage.SENTRY_PREFIX + quote(key) + "=" + quote(str(val))
  664. items.append(item)
  665. if include_third_party:
  666. items.append(self.third_party_items)
  667. return ",".join(items)
  668. @staticmethod
  669. def strip_sentry_baggage(header: str) -> str:
  670. """Remove Sentry baggage from the given header.
  671. Given a Baggage header, return a new Baggage header with all Sentry baggage items removed.
  672. """
  673. return ",".join(
  674. (
  675. item
  676. for item in header.split(",")
  677. if not Baggage.SENTRY_PREFIX_REGEX.match(item.strip())
  678. )
  679. )
  680. def _sample_rand(self) -> "Optional[float]":
  681. """Convenience method to get the sample_rand value from the sentry_items.
  682. We validate the value and parse it as a float before returning it. The value is considered
  683. valid if it is a float in the range [0, 1).
  684. """
  685. sample_rand = try_convert(float, self.sentry_items.get("sample_rand"))
  686. if sample_rand is not None and 0.0 <= sample_rand < 1.0:
  687. return sample_rand
  688. return None
  689. def __repr__(self) -> str:
  690. return f'<Baggage "{self.serialize(include_third_party=True)}", mutable={self.mutable}>'
  691. def should_propagate_trace(client: "sentry_sdk.client.BaseClient", url: str) -> bool:
  692. """
  693. Returns True if url matches trace_propagation_targets configured in the given client. Otherwise, returns False.
  694. """
  695. trace_propagation_targets = client.options["trace_propagation_targets"]
  696. if is_sentry_url(client, url):
  697. return False
  698. return match_regex_list(url, trace_propagation_targets, substring_matching=True)
  699. def normalize_incoming_data(incoming_data: "Dict[str, Any]") -> "Dict[str, Any]":
  700. """
  701. Normalizes incoming data so the keys are all lowercase with dashes instead of underscores and stripped from known prefixes.
  702. """
  703. data = {}
  704. for key, value in incoming_data.items():
  705. if key.startswith("HTTP_"):
  706. key = key[5:]
  707. key = key.replace("_", "-").lower()
  708. data[key] = value
  709. return data
  710. def create_span_decorator(
  711. op: "Optional[Union[str, OP]]" = None,
  712. name: "Optional[str]" = None,
  713. attributes: "Optional[dict[str, Any]]" = None,
  714. template: "SPANTEMPLATE" = SPANTEMPLATE.DEFAULT,
  715. ) -> "Any":
  716. """
  717. Create a span decorator that can wrap both sync and async functions.
  718. :param op: The operation type for the span.
  719. :type op: str or :py:class:`sentry_sdk.consts.OP` or None
  720. :param name: The name of the span.
  721. :type name: str or None
  722. :param attributes: Additional attributes to set on the span.
  723. :type attributes: dict or None
  724. :param template: The type of span to create. This determines what kind of
  725. span instrumentation and data collection will be applied. Use predefined
  726. constants from :py:class:`sentry_sdk.consts.SPANTEMPLATE`.
  727. The default is `SPANTEMPLATE.DEFAULT` which is the right choice for most
  728. use cases.
  729. :type template: :py:class:`sentry_sdk.consts.SPANTEMPLATE`
  730. """
  731. from sentry_sdk.scope import should_send_default_pii
  732. def span_decorator(f: "Any") -> "Any":
  733. """
  734. Decorator to create a span for the given function.
  735. """
  736. @functools.wraps(f)
  737. async def async_wrapper(*args: "Any", **kwargs: "Any") -> "Any":
  738. current_span = get_current_span()
  739. if current_span is None:
  740. logger.debug(
  741. "Cannot create a child span for %s. "
  742. "Please start a Sentry transaction before calling this function.",
  743. qualname_from_function(f),
  744. )
  745. return await f(*args, **kwargs)
  746. if isinstance(current_span, StreamedSpan):
  747. warnings.warn(
  748. "Use the @sentry_sdk.traces.trace decorator in span streaming mode.",
  749. DeprecationWarning,
  750. stacklevel=2,
  751. )
  752. return await f(*args, **kwargs)
  753. span_op = op or _get_span_op(template)
  754. function_name = name or qualname_from_function(f) or ""
  755. span_name = _get_span_name(template, function_name, kwargs)
  756. send_pii = should_send_default_pii()
  757. with current_span.start_child(
  758. op=span_op,
  759. name=span_name,
  760. ) as span:
  761. span.update_data(attributes or {})
  762. _set_input_attributes(
  763. span, template, send_pii, function_name, f, args, kwargs
  764. )
  765. result = await f(*args, **kwargs)
  766. _set_output_attributes(span, template, send_pii, result)
  767. return result
  768. try:
  769. async_wrapper.__signature__ = inspect.signature(f) # type: ignore[attr-defined]
  770. except Exception:
  771. pass
  772. @functools.wraps(f)
  773. def sync_wrapper(*args: "Any", **kwargs: "Any") -> "Any":
  774. current_span = get_current_span()
  775. if current_span is None:
  776. logger.debug(
  777. "Cannot create a child span for %s. "
  778. "Please start a Sentry transaction before calling this function.",
  779. qualname_from_function(f),
  780. )
  781. return f(*args, **kwargs)
  782. if isinstance(current_span, StreamedSpan):
  783. warnings.warn(
  784. "Use the @sentry_sdk.traces.trace decorator in span streaming mode.",
  785. DeprecationWarning,
  786. stacklevel=2,
  787. )
  788. return f(*args, **kwargs)
  789. span_op = op or _get_span_op(template)
  790. function_name = name or qualname_from_function(f) or ""
  791. span_name = _get_span_name(template, function_name, kwargs)
  792. send_pii = should_send_default_pii()
  793. with current_span.start_child(
  794. op=span_op,
  795. name=span_name,
  796. ) as span:
  797. span.update_data(attributes or {})
  798. _set_input_attributes(
  799. span, template, send_pii, function_name, f, args, kwargs
  800. )
  801. result = f(*args, **kwargs)
  802. _set_output_attributes(span, template, send_pii, result)
  803. return result
  804. try:
  805. sync_wrapper.__signature__ = inspect.signature(f) # type: ignore[attr-defined]
  806. except Exception:
  807. pass
  808. if inspect.iscoroutinefunction(f):
  809. return async_wrapper
  810. else:
  811. return sync_wrapper
  812. return span_decorator
  813. def create_streaming_span_decorator(
  814. name: "Optional[str]" = None,
  815. attributes: "Optional[dict[str, Any]]" = None,
  816. active: bool = True,
  817. ) -> "Any":
  818. """
  819. Create a span creating decorator that can wrap both sync and async functions.
  820. """
  821. def span_decorator(f: "Any") -> "Any":
  822. """
  823. Decorator to create a span for the given function.
  824. """
  825. @functools.wraps(f)
  826. async def async_wrapper(*args: "Any", **kwargs: "Any") -> "Any":
  827. client = sentry_sdk.get_client()
  828. if not has_span_streaming_enabled(client.options):
  829. warnings.warn(
  830. "Using span streaming API in non-span-streaming mode. Use "
  831. "@sentry_sdk.trace instead.",
  832. stacklevel=2,
  833. )
  834. span_name = name or qualname_from_function(f) or ""
  835. with start_streaming_span(
  836. name=span_name, attributes=attributes, active=active
  837. ):
  838. result = await f(*args, **kwargs)
  839. return result
  840. try:
  841. async_wrapper.__signature__ = inspect.signature(f) # type: ignore[attr-defined]
  842. except Exception:
  843. pass
  844. @functools.wraps(f)
  845. def sync_wrapper(*args: "Any", **kwargs: "Any") -> "Any":
  846. client = sentry_sdk.get_client()
  847. if not has_span_streaming_enabled(client.options):
  848. warnings.warn(
  849. "Using span streaming API in non-span-streaming mode. Use "
  850. "@sentry_sdk.trace instead.",
  851. stacklevel=2,
  852. )
  853. span_name = name or qualname_from_function(f) or ""
  854. with start_streaming_span(
  855. name=span_name, attributes=attributes, active=active
  856. ):
  857. return f(*args, **kwargs)
  858. try:
  859. sync_wrapper.__signature__ = inspect.signature(f) # type: ignore[attr-defined]
  860. except Exception:
  861. pass
  862. if inspect.iscoroutinefunction(f):
  863. return async_wrapper
  864. else:
  865. return sync_wrapper
  866. return span_decorator
  867. def get_current_span(
  868. scope: "Optional[sentry_sdk.Scope]" = None,
  869. ) -> "Optional[Union[Span, StreamedSpan]]":
  870. """
  871. Returns the currently active span if there is one running, otherwise `None`
  872. """
  873. scope = scope or sentry_sdk.get_current_scope()
  874. current_span = scope.span
  875. return current_span
  876. def set_span_errored(span: "Optional[Union[Span, StreamedSpan]]" = None) -> None:
  877. """
  878. Set the status of the current or given span to INTERNAL_ERROR.
  879. Also sets the status of the transaction (root span) to INTERNAL_ERROR.
  880. """
  881. from sentry_sdk.traces import StreamedSpan, SpanStatus
  882. span = span or get_current_span()
  883. if span is not None:
  884. if isinstance(span, Span):
  885. span.set_status(SPANSTATUS.INTERNAL_ERROR)
  886. if span.containing_transaction is not None:
  887. span.containing_transaction.set_status(SPANSTATUS.INTERNAL_ERROR)
  888. elif isinstance(span, StreamedSpan):
  889. span.status = SpanStatus.ERROR
  890. if span._segment is not None:
  891. span._segment.status = SpanStatus.ERROR
  892. def _generate_sample_rand(
  893. trace_id: "Optional[str]",
  894. *,
  895. interval: "tuple[float, float]" = (0.0, 1.0),
  896. ) -> float:
  897. """Generate a sample_rand value from a trace ID.
  898. The generated value will be pseudorandomly chosen from the provided
  899. interval. Specifically, given (lower, upper) = interval, the generated
  900. value will be in the range [lower, upper). The value has 6-digit precision,
  901. so when printing with .6f, the value will never be rounded up.
  902. The pseudorandom number generator is seeded with the trace ID.
  903. """
  904. lower, upper = interval
  905. if not lower < upper: # using `if lower >= upper` would handle NaNs incorrectly
  906. raise ValueError("Invalid interval: lower must be less than upper")
  907. rng = Random(trace_id)
  908. lower_scaled = int(lower * 1_000_000)
  909. upper_scaled = int(upper * 1_000_000)
  910. try:
  911. sample_rand_scaled = rng.randrange(lower_scaled, upper_scaled)
  912. except ValueError:
  913. # In some corner cases it might happen that the range is too small
  914. # In that case, just take the lower bound
  915. sample_rand_scaled = lower_scaled
  916. return sample_rand_scaled / 1_000_000
  917. def _sample_rand_range(
  918. parent_sampled: "Optional[bool]", sample_rate: "Optional[float]"
  919. ) -> "tuple[float, float]":
  920. """
  921. Compute the lower (inclusive) and upper (exclusive) bounds of the range of values
  922. that a generated sample_rand value must fall into, given the parent_sampled and
  923. sample_rate values.
  924. """
  925. if parent_sampled is None or sample_rate is None:
  926. return 0.0, 1.0
  927. elif parent_sampled is True:
  928. return 0.0, sample_rate
  929. else: # parent_sampled is False
  930. return sample_rate, 1.0
  931. def _get_value(source: "Any", key: str) -> "Optional[Any]":
  932. """
  933. Gets a value from a source object. The source can be a dict or an object.
  934. It is checked for dictionary keys and object attributes.
  935. """
  936. value = None
  937. if isinstance(source, dict):
  938. value = source.get(key)
  939. else:
  940. if hasattr(source, key):
  941. try:
  942. value = getattr(source, key)
  943. except Exception:
  944. value = None
  945. return value
  946. def _get_span_name(
  947. template: "Union[str, SPANTEMPLATE]",
  948. name: str,
  949. kwargs: "Optional[dict[str, Any]]" = None,
  950. ) -> str:
  951. """
  952. Get the name of the span based on the template and the name.
  953. """
  954. span_name = name
  955. if template == SPANTEMPLATE.AI_CHAT:
  956. model = None
  957. if kwargs:
  958. for key in ("model", "model_name"):
  959. if kwargs.get(key) and isinstance(kwargs[key], str):
  960. model = kwargs[key]
  961. break
  962. span_name = f"chat {model}" if model else "chat"
  963. elif template == SPANTEMPLATE.AI_AGENT:
  964. span_name = f"invoke_agent {name}"
  965. elif template == SPANTEMPLATE.AI_TOOL:
  966. span_name = f"execute_tool {name}"
  967. return span_name
  968. def _get_span_op(template: "Union[str, SPANTEMPLATE]") -> str:
  969. """
  970. Get the operation of the span based on the template.
  971. """
  972. mapping: "dict[Union[str, SPANTEMPLATE], Union[str, OP]]" = {
  973. SPANTEMPLATE.AI_CHAT: OP.GEN_AI_CHAT,
  974. SPANTEMPLATE.AI_AGENT: OP.GEN_AI_INVOKE_AGENT,
  975. SPANTEMPLATE.AI_TOOL: OP.GEN_AI_EXECUTE_TOOL,
  976. }
  977. op = mapping.get(template, OP.FUNCTION)
  978. return str(op)
  979. def _get_input_attributes(
  980. template: "Union[str, SPANTEMPLATE]",
  981. send_pii: bool,
  982. args: "tuple[Any, ...]",
  983. kwargs: "dict[str, Any]",
  984. ) -> "dict[str, Any]":
  985. """
  986. Get input attributes for the given span template.
  987. """
  988. attributes: "dict[str, Any]" = {}
  989. if template in [SPANTEMPLATE.AI_AGENT, SPANTEMPLATE.AI_TOOL, SPANTEMPLATE.AI_CHAT]:
  990. mapping = {
  991. "model": (SPANDATA.GEN_AI_REQUEST_MODEL, str),
  992. "model_name": (SPANDATA.GEN_AI_REQUEST_MODEL, str),
  993. "agent": (SPANDATA.GEN_AI_AGENT_NAME, str),
  994. "agent_name": (SPANDATA.GEN_AI_AGENT_NAME, str),
  995. "max_tokens": (SPANDATA.GEN_AI_REQUEST_MAX_TOKENS, int),
  996. "frequency_penalty": (SPANDATA.GEN_AI_REQUEST_FREQUENCY_PENALTY, float),
  997. "presence_penalty": (SPANDATA.GEN_AI_REQUEST_PRESENCE_PENALTY, float),
  998. "temperature": (SPANDATA.GEN_AI_REQUEST_TEMPERATURE, float),
  999. "top_p": (SPANDATA.GEN_AI_REQUEST_TOP_P, float),
  1000. "top_k": (SPANDATA.GEN_AI_REQUEST_TOP_K, int),
  1001. }
  1002. def _set_from_key(key: str, value: "Any") -> None:
  1003. if key in mapping:
  1004. (attribute, data_type) = mapping[key]
  1005. if value is not None and isinstance(value, data_type):
  1006. attributes[attribute] = value
  1007. for key, value in list(kwargs.items()):
  1008. if key == "prompt" and isinstance(value, str):
  1009. attributes.setdefault(SPANDATA.GEN_AI_REQUEST_MESSAGES, []).append(
  1010. {"role": "user", "content": value}
  1011. )
  1012. continue
  1013. if key == "system_prompt" and isinstance(value, str):
  1014. attributes.setdefault(SPANDATA.GEN_AI_REQUEST_MESSAGES, []).append(
  1015. {"role": "system", "content": value}
  1016. )
  1017. continue
  1018. _set_from_key(key, value)
  1019. if template == SPANTEMPLATE.AI_TOOL and send_pii:
  1020. attributes[SPANDATA.GEN_AI_TOOL_INPUT] = safe_repr(
  1021. {"args": args, "kwargs": kwargs}
  1022. )
  1023. # Coerce to string
  1024. if SPANDATA.GEN_AI_REQUEST_MESSAGES in attributes:
  1025. attributes[SPANDATA.GEN_AI_REQUEST_MESSAGES] = safe_repr(
  1026. attributes[SPANDATA.GEN_AI_REQUEST_MESSAGES]
  1027. )
  1028. return attributes
  1029. def _get_usage_attributes(usage: "Any") -> "dict[str, Any]":
  1030. """
  1031. Get usage attributes.
  1032. """
  1033. attributes = {}
  1034. def _set_from_keys(attribute: str, keys: "tuple[str, ...]") -> None:
  1035. for key in keys:
  1036. value = _get_value(usage, key)
  1037. if value is not None and isinstance(value, int):
  1038. attributes[attribute] = value
  1039. _set_from_keys(
  1040. SPANDATA.GEN_AI_USAGE_INPUT_TOKENS,
  1041. ("prompt_tokens", "input_tokens"),
  1042. )
  1043. _set_from_keys(
  1044. SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS,
  1045. ("completion_tokens", "output_tokens"),
  1046. )
  1047. _set_from_keys(
  1048. SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS,
  1049. ("total_tokens",),
  1050. )
  1051. return attributes
  1052. def _get_output_attributes(
  1053. template: "Union[str, SPANTEMPLATE]", send_pii: bool, result: "Any"
  1054. ) -> "dict[str, Any]":
  1055. """
  1056. Get output attributes for the given span template.
  1057. """
  1058. attributes: "dict[str, Any]" = {}
  1059. if template in [SPANTEMPLATE.AI_AGENT, SPANTEMPLATE.AI_TOOL, SPANTEMPLATE.AI_CHAT]:
  1060. with capture_internal_exceptions():
  1061. # Usage from result, result.usage, and result.metadata.usage
  1062. usage_candidates = [result]
  1063. usage = _get_value(result, "usage")
  1064. usage_candidates.append(usage)
  1065. meta = _get_value(result, "metadata")
  1066. usage = _get_value(meta, "usage")
  1067. usage_candidates.append(usage)
  1068. for usage_candidate in usage_candidates:
  1069. if usage_candidate is not None:
  1070. attributes.update(_get_usage_attributes(usage_candidate))
  1071. # Response model
  1072. model_name = _get_value(result, "model")
  1073. if model_name is not None and isinstance(model_name, str):
  1074. attributes[SPANDATA.GEN_AI_RESPONSE_MODEL] = model_name
  1075. model_name = _get_value(result, "model_name")
  1076. if model_name is not None and isinstance(model_name, str):
  1077. attributes[SPANDATA.GEN_AI_RESPONSE_MODEL] = model_name
  1078. # Tool output
  1079. if template == SPANTEMPLATE.AI_TOOL and send_pii:
  1080. attributes[SPANDATA.GEN_AI_TOOL_OUTPUT] = safe_repr(result)
  1081. return attributes
  1082. def _set_input_attributes(
  1083. span: "Span",
  1084. template: "Union[str, SPANTEMPLATE]",
  1085. send_pii: bool,
  1086. name: str,
  1087. f: "Any",
  1088. args: "tuple[Any, ...]",
  1089. kwargs: "dict[str, Any]",
  1090. ) -> None:
  1091. """
  1092. Set span input attributes based on the given span template.
  1093. :param span: The span to set attributes on.
  1094. :param template: The template to use to set attributes on the span.
  1095. :param send_pii: Whether to send PII data.
  1096. :param f: The wrapped function.
  1097. :param args: The arguments to the wrapped function.
  1098. :param kwargs: The keyword arguments to the wrapped function.
  1099. """
  1100. attributes: "dict[str, Any]" = {}
  1101. if template == SPANTEMPLATE.AI_AGENT:
  1102. attributes = {
  1103. SPANDATA.GEN_AI_OPERATION_NAME: "invoke_agent",
  1104. SPANDATA.GEN_AI_AGENT_NAME: name,
  1105. }
  1106. elif template == SPANTEMPLATE.AI_CHAT:
  1107. attributes = {
  1108. SPANDATA.GEN_AI_OPERATION_NAME: "chat",
  1109. }
  1110. elif template == SPANTEMPLATE.AI_TOOL:
  1111. attributes = {
  1112. SPANDATA.GEN_AI_OPERATION_NAME: "execute_tool",
  1113. SPANDATA.GEN_AI_TOOL_NAME: name,
  1114. }
  1115. docstring = f.__doc__
  1116. if docstring is not None:
  1117. attributes[SPANDATA.GEN_AI_TOOL_DESCRIPTION] = docstring
  1118. attributes.update(_get_input_attributes(template, send_pii, args, kwargs))
  1119. span.update_data(attributes or {})
  1120. def _set_output_attributes(
  1121. span: "Span", template: "Union[str, SPANTEMPLATE]", send_pii: bool, result: "Any"
  1122. ) -> None:
  1123. """
  1124. Set span output attributes based on the given span template.
  1125. :param span: The span to set attributes on.
  1126. :param template: The template to use to set attributes on the span.
  1127. :param send_pii: Whether to send PII data.
  1128. :param result: The result of the wrapped function.
  1129. """
  1130. span.update_data(_get_output_attributes(template, send_pii, result) or {})
  1131. def _should_continue_trace(baggage: "Optional[Baggage]") -> bool:
  1132. """
  1133. Check if we should continue the incoming trace according to the strict_trace_continuation spec.
  1134. https://develop.sentry.dev/sdk/telemetry/traces/#stricttracecontinuation
  1135. """
  1136. client = sentry_sdk.get_client()
  1137. parsed_dsn = client.parsed_dsn
  1138. client_org_id = parsed_dsn.org_id if parsed_dsn else None
  1139. baggage_org_id = baggage.sentry_items.get("org_id") if baggage else None
  1140. if (
  1141. client_org_id is not None
  1142. and baggage_org_id is not None
  1143. and client_org_id != baggage_org_id
  1144. ):
  1145. logger.debug(
  1146. f"Starting a new trace because org IDs don't match (incoming baggage org_id: {baggage_org_id}, SDK org_id: {client_org_id})"
  1147. )
  1148. return False
  1149. strict_trace_continuation: bool = client.options.get(
  1150. "strict_trace_continuation", False
  1151. )
  1152. if strict_trace_continuation:
  1153. if (baggage_org_id is not None and client_org_id is None) or (
  1154. baggage_org_id is None and client_org_id is not None
  1155. ):
  1156. logger.debug(
  1157. f"Starting a new trace because strict trace continuation is enabled and one org ID is missing (incoming baggage org_id: {baggage_org_id}, SDK org_id: {client_org_id})"
  1158. )
  1159. return False
  1160. return True
  1161. def add_sentry_baggage_to_headers(
  1162. headers: "MutableMapping[str, str]", sentry_baggage: str
  1163. ) -> None:
  1164. """Add the Sentry baggage to the headers.
  1165. This function directly mutates the provided headers. The provided sentry_baggage
  1166. is appended to the existing baggage. If the baggage already contains Sentry items,
  1167. they are stripped out first.
  1168. """
  1169. existing_baggage = headers.get(BAGGAGE_HEADER_NAME, "")
  1170. stripped_existing_baggage = Baggage.strip_sentry_baggage(existing_baggage)
  1171. separator = "," if len(stripped_existing_baggage) > 0 else ""
  1172. headers[BAGGAGE_HEADER_NAME] = (
  1173. stripped_existing_baggage + separator + sentry_baggage
  1174. )
  1175. def _make_sampling_decision(
  1176. name: str,
  1177. attributes: "Optional[Attributes]",
  1178. scope: "sentry_sdk.Scope",
  1179. ) -> "tuple[bool, Optional[float], Optional[float], Optional[str]]":
  1180. """
  1181. Decide whether a span should be sampled.
  1182. Returns a tuple with:
  1183. 1. the sampling decision
  1184. 2. the effective sample rate
  1185. 3. the sample rand
  1186. 4. the reason for not sampling the span, if unsampled
  1187. """
  1188. client = sentry_sdk.get_client()
  1189. if not has_tracing_enabled(client.options):
  1190. return False, None, None, None
  1191. propagation_context = scope.get_active_propagation_context()
  1192. sample_rand = None
  1193. if propagation_context.baggage is not None:
  1194. sample_rand = propagation_context.baggage._sample_rand()
  1195. if sample_rand is None:
  1196. sample_rand = _generate_sample_rand(propagation_context.trace_id)
  1197. # If there's a traces_sampler, use that; otherwise use traces_sample_rate
  1198. traces_sampler_defined = callable(client.options.get("traces_sampler"))
  1199. if traces_sampler_defined:
  1200. sampling_context = {
  1201. "span_context": {
  1202. "name": name,
  1203. "trace_id": propagation_context.trace_id,
  1204. "parent_span_id": propagation_context.parent_span_id,
  1205. "parent_sampled": propagation_context.parent_sampled,
  1206. "attributes": dict(attributes) if attributes else {},
  1207. },
  1208. }
  1209. if propagation_context.custom_sampling_context:
  1210. sampling_context.update(propagation_context.custom_sampling_context)
  1211. sample_rate = client.options["traces_sampler"](sampling_context)
  1212. else:
  1213. if propagation_context.parent_sampled is not None:
  1214. sample_rate = propagation_context.parent_sampled
  1215. else:
  1216. sample_rate = client.options["traces_sample_rate"]
  1217. # Validate whether the sample_rate we got is actually valid. Since
  1218. # traces_sampler is user-provided, it could return anything.
  1219. if not is_valid_sample_rate(sample_rate, source="Tracing"):
  1220. logger.warning(f"[Tracing] Discarding {name} because of invalid sample rate.")
  1221. return False, None, None, "sample_rate"
  1222. sample_rate = float(sample_rate)
  1223. if not sample_rate:
  1224. if traces_sampler_defined:
  1225. reason = "traces_sampler returned 0 or False"
  1226. else:
  1227. reason = "traces_sample_rate is set to 0"
  1228. logger.debug(f"[Tracing] Discarding {name} because {reason}")
  1229. return False, 0.0, None, "sample_rate"
  1230. # Adjust sample rate if we're under backpressure
  1231. if client.monitor:
  1232. sample_rate /= 2**client.monitor.downsample_factor
  1233. if not sample_rate:
  1234. logger.debug(f"[Tracing] Discarding {name} because backpressure")
  1235. return False, 0.0, None, "backpressure"
  1236. sampled = sample_rand < sample_rate
  1237. if sampled:
  1238. logger.debug(f"[Tracing] Starting {name}")
  1239. outcome = None
  1240. else:
  1241. logger.debug(
  1242. f"[Tracing] Discarding {name} because it's not included in the random sample (sampling rate = {sample_rate})"
  1243. )
  1244. outcome = "sample_rate"
  1245. return sampled, sample_rate, sample_rand, outcome
  1246. def is_ignored_span(name: str, attributes: "Optional[Attributes]") -> bool:
  1247. """Determine if a span fits one of the rules in ignore_spans."""
  1248. client = sentry_sdk.get_client()
  1249. ignore_spans = (client.options.get("_experiments") or {}).get("ignore_spans")
  1250. if not ignore_spans:
  1251. return False
  1252. def _matches(rule: "Any", value: "Any") -> bool:
  1253. if isinstance(rule, Pattern):
  1254. if isinstance(value, str):
  1255. return bool(rule.fullmatch(value))
  1256. else:
  1257. return False
  1258. return rule == value
  1259. for rule in ignore_spans:
  1260. if isinstance(rule, (str, Pattern)):
  1261. if _matches(rule, name):
  1262. return True
  1263. elif isinstance(rule, dict) and ("name" in rule or "attributes" in rule):
  1264. name_matches = True
  1265. attributes_match = True
  1266. attributes = attributes or {}
  1267. if "name" in rule:
  1268. name_matches = _matches(rule["name"], name)
  1269. if "attributes" in rule:
  1270. for attribute, value in rule["attributes"].items():
  1271. if attribute not in attributes or not _matches(
  1272. value, attributes[attribute]
  1273. ):
  1274. attributes_match = False
  1275. break
  1276. if name_matches and attributes_match:
  1277. return True
  1278. return False
  1279. # Circular imports
  1280. from sentry_sdk.tracing import (
  1281. BAGGAGE_HEADER_NAME,
  1282. LOW_QUALITY_TRANSACTION_SOURCES,
  1283. SENTRY_TRACE_HEADER_NAME,
  1284. Span,
  1285. )
  1286. from sentry_sdk.traces import (
  1287. LOW_QUALITY_SEGMENT_SOURCES,
  1288. StreamedSpan,
  1289. start_span as start_streaming_span,
  1290. )
  1291. if TYPE_CHECKING:
  1292. from sentry_sdk.tracing import Span