utils.py 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031
  1. import base64
  2. import json
  3. import linecache
  4. import logging
  5. import math
  6. import os
  7. import random
  8. import re
  9. import subprocess
  10. import sys
  11. import threading
  12. import time
  13. from collections import namedtuple
  14. from datetime import datetime, timezone
  15. from decimal import Decimal
  16. from functools import partial, partialmethod, wraps
  17. from numbers import Real
  18. from urllib.parse import parse_qs, unquote, urlencode, urlsplit, urlunsplit
  19. try:
  20. # Python 3.11
  21. from builtins import BaseExceptionGroup
  22. except ImportError:
  23. # Python 3.10 and below
  24. BaseExceptionGroup = None # type: ignore
  25. import sentry_sdk
  26. from sentry_sdk._compat import PY37
  27. from sentry_sdk.consts import (
  28. DEFAULT_ADD_FULL_STACK,
  29. DEFAULT_MAX_STACK_FRAMES,
  30. DEFAULT_MAX_VALUE_LENGTH,
  31. EndpointType,
  32. )
  33. from sentry_sdk._types import Annotated, AnnotatedValue, SENSITIVE_DATA_SUBSTITUTE
  34. from typing import TYPE_CHECKING
  35. if TYPE_CHECKING:
  36. from types import FrameType, TracebackType
  37. from typing import (
  38. Any,
  39. Callable,
  40. cast,
  41. ContextManager,
  42. Dict,
  43. Iterator,
  44. List,
  45. NoReturn,
  46. Optional,
  47. overload,
  48. ParamSpec,
  49. Set,
  50. Tuple,
  51. Type,
  52. TypeVar,
  53. Union,
  54. )
  55. from gevent.hub import Hub
  56. from sentry_sdk._types import Event, ExcInfo, Log, Hint, Metric
  57. P = ParamSpec("P")
  58. R = TypeVar("R")
  59. epoch = datetime(1970, 1, 1)
  60. # The logger is created here but initialized in the debug support module
  61. logger = logging.getLogger("sentry_sdk.errors")
  62. _installed_modules = None
  63. BASE64_ALPHABET = re.compile(r"^[a-zA-Z0-9/+=]*$")
  64. FALSY_ENV_VALUES = frozenset(("false", "f", "n", "no", "off", "0"))
  65. TRUTHY_ENV_VALUES = frozenset(("true", "t", "y", "yes", "on", "1"))
  66. MAX_STACK_FRAMES = 2000
  67. """Maximum number of stack frames to send to Sentry.
  68. If we have more than this number of stack frames, we will stop processing
  69. the stacktrace to avoid getting stuck in a long-lasting loop. This value
  70. exceeds the default sys.getrecursionlimit() of 1000, so users will only
  71. be affected by this limit if they have a custom recursion limit.
  72. """
  73. def env_to_bool(value, *, strict=False):
  74. # type: (Any, Optional[bool]) -> bool | None
  75. """Casts an ENV variable value to boolean using the constants defined above.
  76. In strict mode, it may return None if the value doesn't match any of the predefined values.
  77. """
  78. normalized = str(value).lower() if value is not None else None
  79. if normalized in FALSY_ENV_VALUES:
  80. return False
  81. if normalized in TRUTHY_ENV_VALUES:
  82. return True
  83. return None if strict else bool(value)
  84. def json_dumps(data):
  85. # type: (Any) -> bytes
  86. """Serialize data into a compact JSON representation encoded as UTF-8."""
  87. return json.dumps(data, allow_nan=False, separators=(",", ":")).encode("utf-8")
  88. def get_git_revision():
  89. # type: () -> Optional[str]
  90. try:
  91. with open(os.path.devnull, "w+") as null:
  92. # prevent command prompt windows from popping up on windows
  93. startupinfo = None
  94. if sys.platform == "win32" or sys.platform == "cygwin":
  95. startupinfo = subprocess.STARTUPINFO()
  96. startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
  97. revision = (
  98. subprocess.Popen(
  99. ["git", "rev-parse", "HEAD"],
  100. startupinfo=startupinfo,
  101. stdout=subprocess.PIPE,
  102. stderr=null,
  103. stdin=null,
  104. )
  105. .communicate()[0]
  106. .strip()
  107. .decode("utf-8")
  108. )
  109. except (OSError, IOError, FileNotFoundError):
  110. return None
  111. return revision
  112. def get_default_release():
  113. # type: () -> Optional[str]
  114. """Try to guess a default release."""
  115. release = os.environ.get("SENTRY_RELEASE")
  116. if release:
  117. return release
  118. release = get_git_revision()
  119. if release:
  120. return release
  121. for var in (
  122. "HEROKU_SLUG_COMMIT",
  123. "SOURCE_VERSION",
  124. "CODEBUILD_RESOLVED_SOURCE_VERSION",
  125. "CIRCLE_SHA1",
  126. "GAE_DEPLOYMENT_ID",
  127. ):
  128. release = os.environ.get(var)
  129. if release:
  130. return release
  131. return None
  132. def get_sdk_name(installed_integrations):
  133. # type: (List[str]) -> str
  134. """Return the SDK name including the name of the used web framework."""
  135. # Note: I can not use for example sentry_sdk.integrations.django.DjangoIntegration.identifier
  136. # here because if django is not installed the integration is not accessible.
  137. framework_integrations = [
  138. "django",
  139. "flask",
  140. "fastapi",
  141. "bottle",
  142. "falcon",
  143. "quart",
  144. "sanic",
  145. "starlette",
  146. "litestar",
  147. "starlite",
  148. "chalice",
  149. "serverless",
  150. "pyramid",
  151. "tornado",
  152. "aiohttp",
  153. "aws_lambda",
  154. "gcp",
  155. "beam",
  156. "asgi",
  157. "wsgi",
  158. ]
  159. for integration in framework_integrations:
  160. if integration in installed_integrations:
  161. return "sentry.python.{}".format(integration)
  162. return "sentry.python"
  163. class CaptureInternalException:
  164. __slots__ = ()
  165. def __enter__(self):
  166. # type: () -> ContextManager[Any]
  167. return self
  168. def __exit__(self, ty, value, tb):
  169. # type: (Optional[Type[BaseException]], Optional[BaseException], Optional[TracebackType]) -> bool
  170. if ty is not None and value is not None:
  171. capture_internal_exception((ty, value, tb))
  172. return True
  173. _CAPTURE_INTERNAL_EXCEPTION = CaptureInternalException()
  174. def capture_internal_exceptions():
  175. # type: () -> ContextManager[Any]
  176. return _CAPTURE_INTERNAL_EXCEPTION
  177. def capture_internal_exception(exc_info):
  178. # type: (ExcInfo) -> None
  179. """
  180. Capture an exception that is likely caused by a bug in the SDK
  181. itself.
  182. These exceptions do not end up in Sentry and are just logged instead.
  183. """
  184. if sentry_sdk.get_client().is_active():
  185. logger.error("Internal error in sentry_sdk", exc_info=exc_info)
  186. def to_timestamp(value):
  187. # type: (datetime) -> float
  188. return (value - epoch).total_seconds()
  189. def format_timestamp(value):
  190. # type: (datetime) -> str
  191. """Formats a timestamp in RFC 3339 format.
  192. Any datetime objects with a non-UTC timezone are converted to UTC, so that all timestamps are formatted in UTC.
  193. """
  194. utctime = value.astimezone(timezone.utc)
  195. # We use this custom formatting rather than isoformat for backwards compatibility (we have used this format for
  196. # several years now), and isoformat is slightly different.
  197. return utctime.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
  198. ISO_TZ_SEPARATORS = frozenset(("+", "-"))
  199. def datetime_from_isoformat(value):
  200. # type: (str) -> datetime
  201. try:
  202. result = datetime.fromisoformat(value)
  203. except (AttributeError, ValueError):
  204. # py 3.6
  205. timestamp_format = (
  206. "%Y-%m-%dT%H:%M:%S.%f" if "." in value else "%Y-%m-%dT%H:%M:%S"
  207. )
  208. if value.endswith("Z"):
  209. value = value[:-1] + "+0000"
  210. if value[-6] in ISO_TZ_SEPARATORS:
  211. timestamp_format += "%z"
  212. value = value[:-3] + value[-2:]
  213. elif value[-5] in ISO_TZ_SEPARATORS:
  214. timestamp_format += "%z"
  215. result = datetime.strptime(value, timestamp_format)
  216. return result.astimezone(timezone.utc)
  217. def event_hint_with_exc_info(exc_info=None):
  218. # type: (Optional[ExcInfo]) -> Dict[str, Optional[ExcInfo]]
  219. """Creates a hint with the exc info filled in."""
  220. if exc_info is None:
  221. exc_info = sys.exc_info()
  222. else:
  223. exc_info = exc_info_from_error(exc_info)
  224. if exc_info[0] is None:
  225. exc_info = None
  226. return {"exc_info": exc_info}
  227. class BadDsn(ValueError):
  228. """Raised on invalid DSNs."""
  229. class Dsn:
  230. """Represents a DSN."""
  231. def __init__(self, value):
  232. # type: (Union[Dsn, str]) -> None
  233. if isinstance(value, Dsn):
  234. self.__dict__ = dict(value.__dict__)
  235. return
  236. parts = urlsplit(str(value))
  237. if parts.scheme not in ("http", "https"):
  238. raise BadDsn("Unsupported scheme %r" % parts.scheme)
  239. self.scheme = parts.scheme
  240. if parts.hostname is None:
  241. raise BadDsn("Missing hostname")
  242. self.host = parts.hostname
  243. if parts.port is None:
  244. self.port = self.scheme == "https" and 443 or 80 # type: int
  245. else:
  246. self.port = parts.port
  247. if not parts.username:
  248. raise BadDsn("Missing public key")
  249. self.public_key = parts.username
  250. self.secret_key = parts.password
  251. path = parts.path.rsplit("/", 1)
  252. try:
  253. self.project_id = str(int(path.pop()))
  254. except (ValueError, TypeError):
  255. raise BadDsn("Invalid project in DSN (%r)" % (parts.path or "")[1:])
  256. self.path = "/".join(path) + "/"
  257. @property
  258. def netloc(self):
  259. # type: () -> str
  260. """The netloc part of a DSN."""
  261. rv = self.host
  262. if (self.scheme, self.port) not in (("http", 80), ("https", 443)):
  263. rv = "%s:%s" % (rv, self.port)
  264. return rv
  265. def to_auth(self, client=None):
  266. # type: (Optional[Any]) -> Auth
  267. """Returns the auth info object for this dsn."""
  268. return Auth(
  269. scheme=self.scheme,
  270. host=self.netloc,
  271. path=self.path,
  272. project_id=self.project_id,
  273. public_key=self.public_key,
  274. secret_key=self.secret_key,
  275. client=client,
  276. )
  277. def __str__(self):
  278. # type: () -> str
  279. return "%s://%s%s@%s%s%s" % (
  280. self.scheme,
  281. self.public_key,
  282. self.secret_key and "@" + self.secret_key or "",
  283. self.netloc,
  284. self.path,
  285. self.project_id,
  286. )
  287. class Auth:
  288. """Helper object that represents the auth info."""
  289. def __init__(
  290. self,
  291. scheme,
  292. host,
  293. project_id,
  294. public_key,
  295. secret_key=None,
  296. version=7,
  297. client=None,
  298. path="/",
  299. ):
  300. # type: (str, str, str, str, Optional[str], int, Optional[Any], str) -> None
  301. self.scheme = scheme
  302. self.host = host
  303. self.path = path
  304. self.project_id = project_id
  305. self.public_key = public_key
  306. self.secret_key = secret_key
  307. self.version = version
  308. self.client = client
  309. def get_api_url(
  310. self,
  311. type=EndpointType.ENVELOPE, # type: EndpointType
  312. ):
  313. # type: (...) -> str
  314. """Returns the API url for storing events."""
  315. return "%s://%s%sapi/%s/%s/" % (
  316. self.scheme,
  317. self.host,
  318. self.path,
  319. self.project_id,
  320. type.value,
  321. )
  322. def to_header(self):
  323. # type: () -> str
  324. """Returns the auth header a string."""
  325. rv = [("sentry_key", self.public_key), ("sentry_version", self.version)]
  326. if self.client is not None:
  327. rv.append(("sentry_client", self.client))
  328. if self.secret_key is not None:
  329. rv.append(("sentry_secret", self.secret_key))
  330. return "Sentry " + ", ".join("%s=%s" % (key, value) for key, value in rv)
  331. def get_type_name(cls):
  332. # type: (Optional[type]) -> Optional[str]
  333. return getattr(cls, "__qualname__", None) or getattr(cls, "__name__", None)
  334. def get_type_module(cls):
  335. # type: (Optional[type]) -> Optional[str]
  336. mod = getattr(cls, "__module__", None)
  337. if mod not in (None, "builtins", "__builtins__"):
  338. return mod
  339. return None
  340. def should_hide_frame(frame):
  341. # type: (FrameType) -> bool
  342. try:
  343. mod = frame.f_globals["__name__"]
  344. if mod.startswith("sentry_sdk."):
  345. return True
  346. except (AttributeError, KeyError):
  347. pass
  348. for flag_name in "__traceback_hide__", "__tracebackhide__":
  349. try:
  350. if frame.f_locals[flag_name]:
  351. return True
  352. except Exception:
  353. pass
  354. return False
  355. def iter_stacks(tb):
  356. # type: (Optional[TracebackType]) -> Iterator[TracebackType]
  357. tb_ = tb # type: Optional[TracebackType]
  358. while tb_ is not None:
  359. if not should_hide_frame(tb_.tb_frame):
  360. yield tb_
  361. tb_ = tb_.tb_next
  362. def get_lines_from_file(
  363. filename, # type: str
  364. lineno, # type: int
  365. max_length=None, # type: Optional[int]
  366. loader=None, # type: Optional[Any]
  367. module=None, # type: Optional[str]
  368. ):
  369. # type: (...) -> Tuple[List[Annotated[str]], Optional[Annotated[str]], List[Annotated[str]]]
  370. context_lines = 5
  371. source = None
  372. if loader is not None and hasattr(loader, "get_source"):
  373. try:
  374. source_str = loader.get_source(module) # type: Optional[str]
  375. except (ImportError, IOError):
  376. source_str = None
  377. if source_str is not None:
  378. source = source_str.splitlines()
  379. if source is None:
  380. try:
  381. source = linecache.getlines(filename)
  382. except (OSError, IOError):
  383. return [], None, []
  384. if not source:
  385. return [], None, []
  386. lower_bound = max(0, lineno - context_lines)
  387. upper_bound = min(lineno + 1 + context_lines, len(source))
  388. try:
  389. pre_context = [
  390. strip_string(line.strip("\r\n"), max_length=max_length)
  391. for line in source[lower_bound:lineno]
  392. ]
  393. context_line = strip_string(source[lineno].strip("\r\n"), max_length=max_length)
  394. post_context = [
  395. strip_string(line.strip("\r\n"), max_length=max_length)
  396. for line in source[(lineno + 1) : upper_bound]
  397. ]
  398. return pre_context, context_line, post_context
  399. except IndexError:
  400. # the file may have changed since it was loaded into memory
  401. return [], None, []
  402. def get_source_context(
  403. frame, # type: FrameType
  404. tb_lineno, # type: Optional[int]
  405. max_value_length=None, # type: Optional[int]
  406. ):
  407. # type: (...) -> Tuple[List[Annotated[str]], Optional[Annotated[str]], List[Annotated[str]]]
  408. try:
  409. abs_path = frame.f_code.co_filename # type: Optional[str]
  410. except Exception:
  411. abs_path = None
  412. try:
  413. module = frame.f_globals["__name__"]
  414. except Exception:
  415. return [], None, []
  416. try:
  417. loader = frame.f_globals["__loader__"]
  418. except Exception:
  419. loader = None
  420. if tb_lineno is not None and abs_path:
  421. lineno = tb_lineno - 1
  422. return get_lines_from_file(
  423. abs_path, lineno, max_value_length, loader=loader, module=module
  424. )
  425. return [], None, []
  426. def safe_str(value):
  427. # type: (Any) -> str
  428. try:
  429. return str(value)
  430. except Exception:
  431. return safe_repr(value)
  432. def safe_repr(value):
  433. # type: (Any) -> str
  434. try:
  435. return repr(value)
  436. except Exception:
  437. return "<broken repr>"
  438. def filename_for_module(module, abs_path):
  439. # type: (Optional[str], Optional[str]) -> Optional[str]
  440. if not abs_path or not module:
  441. return abs_path
  442. try:
  443. if abs_path.endswith(".pyc"):
  444. abs_path = abs_path[:-1]
  445. base_module = module.split(".", 1)[0]
  446. if base_module == module:
  447. return os.path.basename(abs_path)
  448. base_module_path = sys.modules[base_module].__file__
  449. if not base_module_path:
  450. return abs_path
  451. return abs_path.split(base_module_path.rsplit(os.sep, 2)[0], 1)[-1].lstrip(
  452. os.sep
  453. )
  454. except Exception:
  455. return abs_path
  456. def serialize_frame(
  457. frame,
  458. tb_lineno=None,
  459. include_local_variables=True,
  460. include_source_context=True,
  461. max_value_length=None,
  462. custom_repr=None,
  463. ):
  464. # type: (FrameType, Optional[int], bool, bool, Optional[int], Optional[Callable[..., Optional[str]]]) -> Dict[str, Any]
  465. f_code = getattr(frame, "f_code", None)
  466. if not f_code:
  467. abs_path = None
  468. function = None
  469. else:
  470. abs_path = frame.f_code.co_filename
  471. function = frame.f_code.co_name
  472. try:
  473. module = frame.f_globals["__name__"]
  474. except Exception:
  475. module = None
  476. if tb_lineno is None:
  477. tb_lineno = frame.f_lineno
  478. try:
  479. os_abs_path = os.path.abspath(abs_path) if abs_path else None
  480. except Exception:
  481. os_abs_path = None
  482. rv = {
  483. "filename": filename_for_module(module, abs_path) or None,
  484. "abs_path": os_abs_path,
  485. "function": function or "<unknown>",
  486. "module": module,
  487. "lineno": tb_lineno,
  488. } # type: Dict[str, Any]
  489. if include_source_context:
  490. rv["pre_context"], rv["context_line"], rv["post_context"] = get_source_context(
  491. frame, tb_lineno, max_value_length
  492. )
  493. if include_local_variables:
  494. from sentry_sdk.serializer import serialize
  495. rv["vars"] = serialize(
  496. dict(frame.f_locals), is_vars=True, custom_repr=custom_repr
  497. )
  498. return rv
  499. def current_stacktrace(
  500. include_local_variables=True, # type: bool
  501. include_source_context=True, # type: bool
  502. max_value_length=None, # type: Optional[int]
  503. ):
  504. # type: (...) -> Dict[str, Any]
  505. __tracebackhide__ = True
  506. frames = []
  507. f = sys._getframe() # type: Optional[FrameType]
  508. while f is not None:
  509. if not should_hide_frame(f):
  510. frames.append(
  511. serialize_frame(
  512. f,
  513. include_local_variables=include_local_variables,
  514. include_source_context=include_source_context,
  515. max_value_length=max_value_length,
  516. )
  517. )
  518. f = f.f_back
  519. frames.reverse()
  520. return {"frames": frames}
  521. def get_errno(exc_value):
  522. # type: (BaseException) -> Optional[Any]
  523. return getattr(exc_value, "errno", None)
  524. def get_error_message(exc_value):
  525. # type: (Optional[BaseException]) -> str
  526. message = (
  527. getattr(exc_value, "message", "")
  528. or getattr(exc_value, "detail", "")
  529. or safe_str(exc_value)
  530. ) # type: str
  531. # __notes__ should be a list of strings when notes are added
  532. # via add_note, but can be anything else if __notes__ is set
  533. # directly. We only support strings in __notes__, since that
  534. # is the correct use.
  535. notes = getattr(exc_value, "__notes__", None) # type: object
  536. if isinstance(notes, list) and len(notes) > 0:
  537. message += "\n" + "\n".join(note for note in notes if isinstance(note, str))
  538. return message
  539. def single_exception_from_error_tuple(
  540. exc_type, # type: Optional[type]
  541. exc_value, # type: Optional[BaseException]
  542. tb, # type: Optional[TracebackType]
  543. client_options=None, # type: Optional[Dict[str, Any]]
  544. mechanism=None, # type: Optional[Dict[str, Any]]
  545. exception_id=None, # type: Optional[int]
  546. parent_id=None, # type: Optional[int]
  547. source=None, # type: Optional[str]
  548. full_stack=None, # type: Optional[list[dict[str, Any]]]
  549. ):
  550. # type: (...) -> Dict[str, Any]
  551. """
  552. Creates a dict that goes into the events `exception.values` list and is ingestible by Sentry.
  553. See the Exception Interface documentation for more details:
  554. https://develop.sentry.dev/sdk/event-payloads/exception/
  555. """
  556. exception_value = {} # type: Dict[str, Any]
  557. exception_value["mechanism"] = (
  558. mechanism.copy() if mechanism else {"type": "generic", "handled": True}
  559. )
  560. if exception_id is not None:
  561. exception_value["mechanism"]["exception_id"] = exception_id
  562. if exc_value is not None:
  563. errno = get_errno(exc_value)
  564. else:
  565. errno = None
  566. if errno is not None:
  567. exception_value["mechanism"].setdefault("meta", {}).setdefault(
  568. "errno", {}
  569. ).setdefault("number", errno)
  570. if source is not None:
  571. exception_value["mechanism"]["source"] = source
  572. is_root_exception = exception_id == 0
  573. if not is_root_exception and parent_id is not None:
  574. exception_value["mechanism"]["parent_id"] = parent_id
  575. exception_value["mechanism"]["type"] = "chained"
  576. if is_root_exception and "type" not in exception_value["mechanism"]:
  577. exception_value["mechanism"]["type"] = "generic"
  578. is_exception_group = BaseExceptionGroup is not None and isinstance(
  579. exc_value, BaseExceptionGroup
  580. )
  581. if is_exception_group:
  582. exception_value["mechanism"]["is_exception_group"] = True
  583. exception_value["module"] = get_type_module(exc_type)
  584. exception_value["type"] = get_type_name(exc_type)
  585. exception_value["value"] = get_error_message(exc_value)
  586. if client_options is None:
  587. include_local_variables = True
  588. include_source_context = True
  589. max_value_length = DEFAULT_MAX_VALUE_LENGTH # fallback
  590. custom_repr = None
  591. else:
  592. include_local_variables = client_options["include_local_variables"]
  593. include_source_context = client_options["include_source_context"]
  594. max_value_length = client_options["max_value_length"]
  595. custom_repr = client_options.get("custom_repr")
  596. frames = [
  597. serialize_frame(
  598. tb.tb_frame,
  599. tb_lineno=tb.tb_lineno,
  600. include_local_variables=include_local_variables,
  601. include_source_context=include_source_context,
  602. max_value_length=max_value_length,
  603. custom_repr=custom_repr,
  604. )
  605. # Process at most MAX_STACK_FRAMES + 1 frames, to avoid hanging on
  606. # processing a super-long stacktrace.
  607. for tb, _ in zip(iter_stacks(tb), range(MAX_STACK_FRAMES + 1))
  608. ] # type: List[Dict[str, Any]]
  609. if len(frames) > MAX_STACK_FRAMES:
  610. # If we have more frames than the limit, we remove the stacktrace completely.
  611. # We don't trim the stacktrace here because we have not processed the whole
  612. # thing (see above, we stop at MAX_STACK_FRAMES + 1). Normally, Relay would
  613. # intelligently trim by removing frames in the middle of the stacktrace, but
  614. # since we don't have the whole stacktrace, we can't do that. Instead, we
  615. # drop the entire stacktrace.
  616. exception_value["stacktrace"] = AnnotatedValue.removed_because_over_size_limit(
  617. value=None
  618. )
  619. elif frames:
  620. if not full_stack:
  621. new_frames = frames
  622. else:
  623. new_frames = merge_stack_frames(frames, full_stack, client_options)
  624. exception_value["stacktrace"] = {"frames": new_frames}
  625. return exception_value
  626. HAS_CHAINED_EXCEPTIONS = hasattr(Exception, "__suppress_context__")
  627. if HAS_CHAINED_EXCEPTIONS:
  628. def walk_exception_chain(exc_info):
  629. # type: (ExcInfo) -> Iterator[ExcInfo]
  630. exc_type, exc_value, tb = exc_info
  631. seen_exceptions = []
  632. seen_exception_ids = set() # type: Set[int]
  633. while (
  634. exc_type is not None
  635. and exc_value is not None
  636. and id(exc_value) not in seen_exception_ids
  637. ):
  638. yield exc_type, exc_value, tb
  639. # Avoid hashing random types we don't know anything
  640. # about. Use the list to keep a ref so that the `id` is
  641. # not used for another object.
  642. seen_exceptions.append(exc_value)
  643. seen_exception_ids.add(id(exc_value))
  644. if exc_value.__suppress_context__:
  645. cause = exc_value.__cause__
  646. else:
  647. cause = exc_value.__context__
  648. if cause is None:
  649. break
  650. exc_type = type(cause)
  651. exc_value = cause
  652. tb = getattr(cause, "__traceback__", None)
  653. else:
  654. def walk_exception_chain(exc_info):
  655. # type: (ExcInfo) -> Iterator[ExcInfo]
  656. yield exc_info
  657. def exceptions_from_error(
  658. exc_type, # type: Optional[type]
  659. exc_value, # type: Optional[BaseException]
  660. tb, # type: Optional[TracebackType]
  661. client_options=None, # type: Optional[Dict[str, Any]]
  662. mechanism=None, # type: Optional[Dict[str, Any]]
  663. exception_id=0, # type: int
  664. parent_id=0, # type: int
  665. source=None, # type: Optional[str]
  666. full_stack=None, # type: Optional[list[dict[str, Any]]]
  667. ):
  668. # type: (...) -> Tuple[int, List[Dict[str, Any]]]
  669. """
  670. Creates the list of exceptions.
  671. This can include chained exceptions and exceptions from an ExceptionGroup.
  672. See the Exception Interface documentation for more details:
  673. https://develop.sentry.dev/sdk/event-payloads/exception/
  674. """
  675. parent = single_exception_from_error_tuple(
  676. exc_type=exc_type,
  677. exc_value=exc_value,
  678. tb=tb,
  679. client_options=client_options,
  680. mechanism=mechanism,
  681. exception_id=exception_id,
  682. parent_id=parent_id,
  683. source=source,
  684. full_stack=full_stack,
  685. )
  686. exceptions = [parent]
  687. parent_id = exception_id
  688. exception_id += 1
  689. should_supress_context = (
  690. hasattr(exc_value, "__suppress_context__") and exc_value.__suppress_context__ # type: ignore
  691. )
  692. if should_supress_context:
  693. # Add direct cause.
  694. # The field `__cause__` is set when raised with the exception (using the `from` keyword).
  695. exception_has_cause = (
  696. exc_value
  697. and hasattr(exc_value, "__cause__")
  698. and exc_value.__cause__ is not None
  699. )
  700. if exception_has_cause:
  701. cause = exc_value.__cause__ # type: ignore
  702. (exception_id, child_exceptions) = exceptions_from_error(
  703. exc_type=type(cause),
  704. exc_value=cause,
  705. tb=getattr(cause, "__traceback__", None),
  706. client_options=client_options,
  707. mechanism=mechanism,
  708. exception_id=exception_id,
  709. source="__cause__",
  710. full_stack=full_stack,
  711. )
  712. exceptions.extend(child_exceptions)
  713. else:
  714. # Add indirect cause.
  715. # The field `__context__` is assigned if another exception occurs while handling the exception.
  716. exception_has_content = (
  717. exc_value
  718. and hasattr(exc_value, "__context__")
  719. and exc_value.__context__ is not None
  720. )
  721. if exception_has_content:
  722. context = exc_value.__context__ # type: ignore
  723. (exception_id, child_exceptions) = exceptions_from_error(
  724. exc_type=type(context),
  725. exc_value=context,
  726. tb=getattr(context, "__traceback__", None),
  727. client_options=client_options,
  728. mechanism=mechanism,
  729. exception_id=exception_id,
  730. source="__context__",
  731. full_stack=full_stack,
  732. )
  733. exceptions.extend(child_exceptions)
  734. # Add exceptions from an ExceptionGroup.
  735. is_exception_group = exc_value and hasattr(exc_value, "exceptions")
  736. if is_exception_group:
  737. for idx, e in enumerate(exc_value.exceptions): # type: ignore
  738. (exception_id, child_exceptions) = exceptions_from_error(
  739. exc_type=type(e),
  740. exc_value=e,
  741. tb=getattr(e, "__traceback__", None),
  742. client_options=client_options,
  743. mechanism=mechanism,
  744. exception_id=exception_id,
  745. parent_id=parent_id,
  746. source="exceptions[%s]" % idx,
  747. full_stack=full_stack,
  748. )
  749. exceptions.extend(child_exceptions)
  750. return (exception_id, exceptions)
  751. def exceptions_from_error_tuple(
  752. exc_info, # type: ExcInfo
  753. client_options=None, # type: Optional[Dict[str, Any]]
  754. mechanism=None, # type: Optional[Dict[str, Any]]
  755. full_stack=None, # type: Optional[list[dict[str, Any]]]
  756. ):
  757. # type: (...) -> List[Dict[str, Any]]
  758. exc_type, exc_value, tb = exc_info
  759. is_exception_group = BaseExceptionGroup is not None and isinstance(
  760. exc_value, BaseExceptionGroup
  761. )
  762. if is_exception_group:
  763. (_, exceptions) = exceptions_from_error(
  764. exc_type=exc_type,
  765. exc_value=exc_value,
  766. tb=tb,
  767. client_options=client_options,
  768. mechanism=mechanism,
  769. exception_id=0,
  770. parent_id=0,
  771. full_stack=full_stack,
  772. )
  773. else:
  774. exceptions = []
  775. for exc_type, exc_value, tb in walk_exception_chain(exc_info):
  776. exceptions.append(
  777. single_exception_from_error_tuple(
  778. exc_type=exc_type,
  779. exc_value=exc_value,
  780. tb=tb,
  781. client_options=client_options,
  782. mechanism=mechanism,
  783. full_stack=full_stack,
  784. )
  785. )
  786. exceptions.reverse()
  787. return exceptions
  788. def to_string(value):
  789. # type: (str) -> str
  790. try:
  791. return str(value)
  792. except UnicodeDecodeError:
  793. return repr(value)[1:-1]
  794. def iter_event_stacktraces(event):
  795. # type: (Event) -> Iterator[Annotated[Dict[str, Any]]]
  796. if "stacktrace" in event:
  797. yield event["stacktrace"]
  798. if "threads" in event:
  799. for thread in event["threads"].get("values") or ():
  800. if "stacktrace" in thread:
  801. yield thread["stacktrace"]
  802. if "exception" in event:
  803. for exception in event["exception"].get("values") or ():
  804. if isinstance(exception, dict) and "stacktrace" in exception:
  805. yield exception["stacktrace"]
  806. def iter_event_frames(event):
  807. # type: (Event) -> Iterator[Dict[str, Any]]
  808. for stacktrace in iter_event_stacktraces(event):
  809. if isinstance(stacktrace, AnnotatedValue):
  810. stacktrace = stacktrace.value or {}
  811. for frame in stacktrace.get("frames") or ():
  812. yield frame
  813. def handle_in_app(event, in_app_exclude=None, in_app_include=None, project_root=None):
  814. # type: (Event, Optional[List[str]], Optional[List[str]], Optional[str]) -> Event
  815. for stacktrace in iter_event_stacktraces(event):
  816. if isinstance(stacktrace, AnnotatedValue):
  817. stacktrace = stacktrace.value or {}
  818. set_in_app_in_frames(
  819. stacktrace.get("frames"),
  820. in_app_exclude=in_app_exclude,
  821. in_app_include=in_app_include,
  822. project_root=project_root,
  823. )
  824. return event
  825. def set_in_app_in_frames(frames, in_app_exclude, in_app_include, project_root=None):
  826. # type: (Any, Optional[List[str]], Optional[List[str]], Optional[str]) -> Optional[Any]
  827. if not frames:
  828. return None
  829. for frame in frames:
  830. # if frame has already been marked as in_app, skip it
  831. current_in_app = frame.get("in_app")
  832. if current_in_app is not None:
  833. continue
  834. module = frame.get("module")
  835. # check if module in frame is in the list of modules to include
  836. if _module_in_list(module, in_app_include):
  837. frame["in_app"] = True
  838. continue
  839. # check if module in frame is in the list of modules to exclude
  840. if _module_in_list(module, in_app_exclude):
  841. frame["in_app"] = False
  842. continue
  843. # if frame has no abs_path, skip further checks
  844. abs_path = frame.get("abs_path")
  845. if abs_path is None:
  846. continue
  847. if _is_external_source(abs_path):
  848. frame["in_app"] = False
  849. continue
  850. if _is_in_project_root(abs_path, project_root):
  851. frame["in_app"] = True
  852. continue
  853. return frames
  854. def exc_info_from_error(error):
  855. # type: (Union[BaseException, ExcInfo]) -> ExcInfo
  856. if isinstance(error, tuple) and len(error) == 3:
  857. exc_type, exc_value, tb = error
  858. elif isinstance(error, BaseException):
  859. tb = getattr(error, "__traceback__", None)
  860. if tb is not None:
  861. exc_type = type(error)
  862. exc_value = error
  863. else:
  864. exc_type, exc_value, tb = sys.exc_info()
  865. if exc_value is not error:
  866. tb = None
  867. exc_value = error
  868. exc_type = type(error)
  869. else:
  870. raise ValueError("Expected Exception object to report, got %s!" % type(error))
  871. exc_info = (exc_type, exc_value, tb)
  872. if TYPE_CHECKING:
  873. # This cast is safe because exc_type and exc_value are either both
  874. # None or both not None.
  875. exc_info = cast(ExcInfo, exc_info)
  876. return exc_info
  877. def merge_stack_frames(frames, full_stack, client_options):
  878. # type: (List[Dict[str, Any]], List[Dict[str, Any]], Optional[Dict[str, Any]]) -> List[Dict[str, Any]]
  879. """
  880. Add the missing frames from full_stack to frames and return the merged list.
  881. """
  882. frame_ids = {
  883. (
  884. frame["abs_path"],
  885. frame["context_line"],
  886. frame["lineno"],
  887. frame["function"],
  888. )
  889. for frame in frames
  890. }
  891. new_frames = [
  892. stackframe
  893. for stackframe in full_stack
  894. if (
  895. stackframe["abs_path"],
  896. stackframe["context_line"],
  897. stackframe["lineno"],
  898. stackframe["function"],
  899. )
  900. not in frame_ids
  901. ]
  902. new_frames.extend(frames)
  903. # Limit the number of frames
  904. max_stack_frames = (
  905. client_options.get("max_stack_frames", DEFAULT_MAX_STACK_FRAMES)
  906. if client_options
  907. else None
  908. )
  909. if max_stack_frames is not None:
  910. new_frames = new_frames[len(new_frames) - max_stack_frames :]
  911. return new_frames
  912. def event_from_exception(
  913. exc_info, # type: Union[BaseException, ExcInfo]
  914. client_options=None, # type: Optional[Dict[str, Any]]
  915. mechanism=None, # type: Optional[Dict[str, Any]]
  916. ):
  917. # type: (...) -> Tuple[Event, Dict[str, Any]]
  918. exc_info = exc_info_from_error(exc_info)
  919. hint = event_hint_with_exc_info(exc_info)
  920. if client_options and client_options.get("add_full_stack", DEFAULT_ADD_FULL_STACK):
  921. full_stack = current_stacktrace(
  922. include_local_variables=client_options["include_local_variables"],
  923. max_value_length=client_options["max_value_length"],
  924. )["frames"]
  925. else:
  926. full_stack = None
  927. return (
  928. {
  929. "level": "error",
  930. "exception": {
  931. "values": exceptions_from_error_tuple(
  932. exc_info, client_options, mechanism, full_stack
  933. )
  934. },
  935. },
  936. hint,
  937. )
  938. def _module_in_list(name, items):
  939. # type: (Optional[str], Optional[List[str]]) -> bool
  940. if name is None:
  941. return False
  942. if not items:
  943. return False
  944. for item in items:
  945. if item == name or name.startswith(item + "."):
  946. return True
  947. return False
  948. def _is_external_source(abs_path):
  949. # type: (Optional[str]) -> bool
  950. # check if frame is in 'site-packages' or 'dist-packages'
  951. if abs_path is None:
  952. return False
  953. external_source = (
  954. re.search(r"[\\/](?:dist|site)-packages[\\/]", abs_path) is not None
  955. )
  956. return external_source
  957. def _is_in_project_root(abs_path, project_root):
  958. # type: (Optional[str], Optional[str]) -> bool
  959. if abs_path is None or project_root is None:
  960. return False
  961. # check if path is in the project root
  962. if abs_path.startswith(project_root):
  963. return True
  964. return False
  965. def _truncate_by_bytes(string, max_bytes):
  966. # type: (str, int) -> str
  967. """
  968. Truncate a UTF-8-encodable string to the last full codepoint so that it fits in max_bytes.
  969. """
  970. truncated = string.encode("utf-8")[: max_bytes - 3].decode("utf-8", errors="ignore")
  971. return truncated + "..."
  972. def _get_size_in_bytes(value):
  973. # type: (str) -> Optional[int]
  974. try:
  975. return len(value.encode("utf-8"))
  976. except (UnicodeEncodeError, UnicodeDecodeError):
  977. return None
  978. def strip_string(value, max_length=None):
  979. # type: (str, Optional[int]) -> Union[AnnotatedValue, str]
  980. if not value:
  981. return value
  982. if max_length is None:
  983. max_length = DEFAULT_MAX_VALUE_LENGTH
  984. byte_size = _get_size_in_bytes(value)
  985. text_size = len(value)
  986. if byte_size is not None and byte_size > max_length:
  987. # truncate to max_length bytes, preserving code points
  988. truncated_value = _truncate_by_bytes(value, max_length)
  989. elif text_size is not None and text_size > max_length:
  990. # fallback to truncating by string length
  991. truncated_value = value[: max_length - 3] + "..."
  992. else:
  993. return value
  994. return AnnotatedValue(
  995. value=truncated_value,
  996. metadata={
  997. "len": byte_size or text_size,
  998. "rem": [["!limit", "x", max_length - 3, max_length]],
  999. },
  1000. )
  1001. def parse_version(version):
  1002. # type: (str) -> Optional[Tuple[int, ...]]
  1003. """
  1004. Parses a version string into a tuple of integers.
  1005. This uses the parsing loging from PEP 440:
  1006. https://peps.python.org/pep-0440/#appendix-b-parsing-version-strings-with-regular-expressions
  1007. """
  1008. VERSION_PATTERN = r""" # noqa: N806
  1009. v?
  1010. (?:
  1011. (?:(?P<epoch>[0-9]+)!)? # epoch
  1012. (?P<release>[0-9]+(?:\.[0-9]+)*) # release segment
  1013. (?P<pre> # pre-release
  1014. [-_\.]?
  1015. (?P<pre_l>(a|b|c|rc|alpha|beta|pre|preview))
  1016. [-_\.]?
  1017. (?P<pre_n>[0-9]+)?
  1018. )?
  1019. (?P<post> # post release
  1020. (?:-(?P<post_n1>[0-9]+))
  1021. |
  1022. (?:
  1023. [-_\.]?
  1024. (?P<post_l>post|rev|r)
  1025. [-_\.]?
  1026. (?P<post_n2>[0-9]+)?
  1027. )
  1028. )?
  1029. (?P<dev> # dev release
  1030. [-_\.]?
  1031. (?P<dev_l>dev)
  1032. [-_\.]?
  1033. (?P<dev_n>[0-9]+)?
  1034. )?
  1035. )
  1036. (?:\+(?P<local>[a-z0-9]+(?:[-_\.][a-z0-9]+)*))? # local version
  1037. """
  1038. pattern = re.compile(
  1039. r"^\s*" + VERSION_PATTERN + r"\s*$",
  1040. re.VERBOSE | re.IGNORECASE,
  1041. )
  1042. try:
  1043. release = pattern.match(version).groupdict()["release"] # type: ignore
  1044. release_tuple = tuple(map(int, release.split(".")[:3])) # type: Tuple[int, ...]
  1045. except (TypeError, ValueError, AttributeError):
  1046. return None
  1047. return release_tuple
  1048. def _is_contextvars_broken():
  1049. # type: () -> bool
  1050. """
  1051. Returns whether gevent/eventlet have patched the stdlib in a way where thread locals are now more "correct" than contextvars.
  1052. """
  1053. try:
  1054. import gevent
  1055. from gevent.monkey import is_object_patched
  1056. # Get the MAJOR and MINOR version numbers of Gevent
  1057. version_tuple = tuple(
  1058. [int(part) for part in re.split(r"a|b|rc|\.", gevent.__version__)[:2]]
  1059. )
  1060. if is_object_patched("threading", "local"):
  1061. # Gevent 20.9.0 depends on Greenlet 0.4.17 which natively handles switching
  1062. # context vars when greenlets are switched, so, Gevent 20.9.0+ is all fine.
  1063. # Ref: https://github.com/gevent/gevent/blob/83c9e2ae5b0834b8f84233760aabe82c3ba065b4/src/gevent/monkey.py#L604-L609
  1064. # Gevent 20.5, that doesn't depend on Greenlet 0.4.17 with native support
  1065. # for contextvars, is able to patch both thread locals and contextvars, in
  1066. # that case, check if contextvars are effectively patched.
  1067. if (
  1068. # Gevent 20.9.0+
  1069. (sys.version_info >= (3, 7) and version_tuple >= (20, 9))
  1070. # Gevent 20.5.0+ or Python < 3.7
  1071. or (is_object_patched("contextvars", "ContextVar"))
  1072. ):
  1073. return False
  1074. return True
  1075. except ImportError:
  1076. pass
  1077. try:
  1078. import greenlet
  1079. from eventlet.patcher import is_monkey_patched # type: ignore
  1080. greenlet_version = parse_version(greenlet.__version__)
  1081. if greenlet_version is None:
  1082. logger.error(
  1083. "Internal error in Sentry SDK: Could not parse Greenlet version from greenlet.__version__."
  1084. )
  1085. return False
  1086. if is_monkey_patched("thread") and greenlet_version < (0, 5):
  1087. return True
  1088. except ImportError:
  1089. pass
  1090. return False
  1091. def _make_threadlocal_contextvars(local):
  1092. # type: (type) -> type
  1093. class ContextVar:
  1094. # Super-limited impl of ContextVar
  1095. def __init__(self, name, default=None):
  1096. # type: (str, Any) -> None
  1097. self._name = name
  1098. self._default = default
  1099. self._local = local()
  1100. self._original_local = local()
  1101. def get(self, default=None):
  1102. # type: (Any) -> Any
  1103. return getattr(self._local, "value", default or self._default)
  1104. def set(self, value):
  1105. # type: (Any) -> Any
  1106. token = str(random.getrandbits(64))
  1107. original_value = self.get()
  1108. setattr(self._original_local, token, original_value)
  1109. self._local.value = value
  1110. return token
  1111. def reset(self, token):
  1112. # type: (Any) -> None
  1113. self._local.value = getattr(self._original_local, token)
  1114. # delete the original value (this way it works in Python 3.6+)
  1115. del self._original_local.__dict__[token]
  1116. return ContextVar
  1117. def _get_contextvars():
  1118. # type: () -> Tuple[bool, type]
  1119. """
  1120. Figure out the "right" contextvars installation to use. Returns a
  1121. `contextvars.ContextVar`-like class with a limited API.
  1122. See https://docs.sentry.io/platforms/python/contextvars/ for more information.
  1123. """
  1124. if not _is_contextvars_broken():
  1125. # aiocontextvars is a PyPI package that ensures that the contextvars
  1126. # backport (also a PyPI package) works with asyncio under Python 3.6
  1127. #
  1128. # Import it if available.
  1129. if sys.version_info < (3, 7):
  1130. # `aiocontextvars` is absolutely required for functional
  1131. # contextvars on Python 3.6.
  1132. try:
  1133. from aiocontextvars import ContextVar
  1134. return True, ContextVar
  1135. except ImportError:
  1136. pass
  1137. else:
  1138. # On Python 3.7 contextvars are functional.
  1139. try:
  1140. from contextvars import ContextVar
  1141. return True, ContextVar
  1142. except ImportError:
  1143. pass
  1144. # Fall back to basic thread-local usage.
  1145. from threading import local
  1146. return False, _make_threadlocal_contextvars(local)
  1147. HAS_REAL_CONTEXTVARS, ContextVar = _get_contextvars()
  1148. CONTEXTVARS_ERROR_MESSAGE = """
  1149. With asyncio/ASGI applications, the Sentry SDK requires a functional
  1150. installation of `contextvars` to avoid leaking scope/context data across
  1151. requests.
  1152. Please refer to https://docs.sentry.io/platforms/python/contextvars/ for more information.
  1153. """
  1154. def qualname_from_function(func):
  1155. # type: (Callable[..., Any]) -> Optional[str]
  1156. """Return the qualified name of func. Works with regular function, lambda, partial and partialmethod."""
  1157. func_qualname = None # type: Optional[str]
  1158. # Python 2
  1159. try:
  1160. return "%s.%s.%s" % (
  1161. func.im_class.__module__, # type: ignore
  1162. func.im_class.__name__, # type: ignore
  1163. func.__name__,
  1164. )
  1165. except Exception:
  1166. pass
  1167. prefix, suffix = "", ""
  1168. if isinstance(func, partial) and hasattr(func.func, "__name__"):
  1169. prefix, suffix = "partial(<function ", ">)"
  1170. func = func.func
  1171. else:
  1172. # The _partialmethod attribute of methods wrapped with partialmethod() was renamed to __partialmethod__ in CPython 3.13:
  1173. # https://github.com/python/cpython/pull/16600
  1174. partial_method = getattr(func, "_partialmethod", None) or getattr(
  1175. func, "__partialmethod__", None
  1176. )
  1177. if isinstance(partial_method, partialmethod):
  1178. prefix, suffix = "partialmethod(<function ", ">)"
  1179. func = partial_method.func
  1180. if hasattr(func, "__qualname__"):
  1181. func_qualname = func.__qualname__
  1182. elif hasattr(func, "__name__"): # Python 2.7 has no __qualname__
  1183. func_qualname = func.__name__
  1184. # Python 3: methods, functions, classes
  1185. if func_qualname is not None:
  1186. if hasattr(func, "__module__") and isinstance(func.__module__, str):
  1187. func_qualname = func.__module__ + "." + func_qualname
  1188. func_qualname = prefix + func_qualname + suffix
  1189. return func_qualname
  1190. def transaction_from_function(func):
  1191. # type: (Callable[..., Any]) -> Optional[str]
  1192. return qualname_from_function(func)
  1193. disable_capture_event = ContextVar("disable_capture_event")
  1194. class ServerlessTimeoutWarning(Exception): # noqa: N818
  1195. """Raised when a serverless method is about to reach its timeout."""
  1196. pass
  1197. class TimeoutThread(threading.Thread):
  1198. """Creates a Thread which runs (sleeps) for a time duration equal to
  1199. waiting_time and raises a custom ServerlessTimeout exception.
  1200. """
  1201. def __init__(self, waiting_time, configured_timeout):
  1202. # type: (float, int) -> None
  1203. threading.Thread.__init__(self)
  1204. self.waiting_time = waiting_time
  1205. self.configured_timeout = configured_timeout
  1206. self._stop_event = threading.Event()
  1207. def stop(self):
  1208. # type: () -> None
  1209. self._stop_event.set()
  1210. def run(self):
  1211. # type: () -> None
  1212. self._stop_event.wait(self.waiting_time)
  1213. if self._stop_event.is_set():
  1214. return
  1215. integer_configured_timeout = int(self.configured_timeout)
  1216. # Setting up the exact integer value of configured time(in seconds)
  1217. if integer_configured_timeout < self.configured_timeout:
  1218. integer_configured_timeout = integer_configured_timeout + 1
  1219. # Raising Exception after timeout duration is reached
  1220. raise ServerlessTimeoutWarning(
  1221. "WARNING : Function is expected to get timed out. Configured timeout duration = {} seconds.".format(
  1222. integer_configured_timeout
  1223. )
  1224. )
  1225. def to_base64(original):
  1226. # type: (str) -> Optional[str]
  1227. """
  1228. Convert a string to base64, via UTF-8. Returns None on invalid input.
  1229. """
  1230. base64_string = None
  1231. try:
  1232. utf8_bytes = original.encode("UTF-8")
  1233. base64_bytes = base64.b64encode(utf8_bytes)
  1234. base64_string = base64_bytes.decode("UTF-8")
  1235. except Exception as err:
  1236. logger.warning("Unable to encode {orig} to base64:".format(orig=original), err)
  1237. return base64_string
  1238. def from_base64(base64_string):
  1239. # type: (str) -> Optional[str]
  1240. """
  1241. Convert a string from base64, via UTF-8. Returns None on invalid input.
  1242. """
  1243. utf8_string = None
  1244. try:
  1245. only_valid_chars = BASE64_ALPHABET.match(base64_string)
  1246. assert only_valid_chars
  1247. base64_bytes = base64_string.encode("UTF-8")
  1248. utf8_bytes = base64.b64decode(base64_bytes)
  1249. utf8_string = utf8_bytes.decode("UTF-8")
  1250. except Exception as err:
  1251. logger.warning(
  1252. "Unable to decode {b64} from base64:".format(b64=base64_string), err
  1253. )
  1254. return utf8_string
  1255. Components = namedtuple("Components", ["scheme", "netloc", "path", "query", "fragment"])
  1256. def sanitize_url(url, remove_authority=True, remove_query_values=True, split=False):
  1257. # type: (str, bool, bool, bool) -> Union[str, Components]
  1258. """
  1259. Removes the authority and query parameter values from a given URL.
  1260. """
  1261. parsed_url = urlsplit(url)
  1262. query_params = parse_qs(parsed_url.query, keep_blank_values=True)
  1263. # strip username:password (netloc can be usr:pwd@example.com)
  1264. if remove_authority:
  1265. netloc_parts = parsed_url.netloc.split("@")
  1266. if len(netloc_parts) > 1:
  1267. netloc = "%s:%s@%s" % (
  1268. SENSITIVE_DATA_SUBSTITUTE,
  1269. SENSITIVE_DATA_SUBSTITUTE,
  1270. netloc_parts[-1],
  1271. )
  1272. else:
  1273. netloc = parsed_url.netloc
  1274. else:
  1275. netloc = parsed_url.netloc
  1276. # strip values from query string
  1277. if remove_query_values:
  1278. query_string = unquote(
  1279. urlencode({key: SENSITIVE_DATA_SUBSTITUTE for key in query_params})
  1280. )
  1281. else:
  1282. query_string = parsed_url.query
  1283. components = Components(
  1284. scheme=parsed_url.scheme,
  1285. netloc=netloc,
  1286. query=query_string,
  1287. path=parsed_url.path,
  1288. fragment=parsed_url.fragment,
  1289. )
  1290. if split:
  1291. return components
  1292. else:
  1293. return urlunsplit(components)
  1294. ParsedUrl = namedtuple("ParsedUrl", ["url", "query", "fragment"])
  1295. def parse_url(url, sanitize=True):
  1296. # type: (str, bool) -> ParsedUrl
  1297. """
  1298. Splits a URL into a url (including path), query and fragment. If sanitize is True, the query
  1299. parameters will be sanitized to remove sensitive data. The autority (username and password)
  1300. in the URL will always be removed.
  1301. """
  1302. parsed_url = sanitize_url(
  1303. url, remove_authority=True, remove_query_values=sanitize, split=True
  1304. )
  1305. base_url = urlunsplit(
  1306. Components(
  1307. scheme=parsed_url.scheme, # type: ignore
  1308. netloc=parsed_url.netloc, # type: ignore
  1309. query="",
  1310. path=parsed_url.path, # type: ignore
  1311. fragment="",
  1312. )
  1313. )
  1314. return ParsedUrl(
  1315. url=base_url,
  1316. query=parsed_url.query, # type: ignore
  1317. fragment=parsed_url.fragment, # type: ignore
  1318. )
  1319. def is_valid_sample_rate(rate, source):
  1320. # type: (Any, str) -> bool
  1321. """
  1322. Checks the given sample rate to make sure it is valid type and value (a
  1323. boolean or a number between 0 and 1, inclusive).
  1324. """
  1325. # both booleans and NaN are instances of Real, so a) checking for Real
  1326. # checks for the possibility of a boolean also, and b) we have to check
  1327. # separately for NaN and Decimal does not derive from Real so need to check that too
  1328. if not isinstance(rate, (Real, Decimal)) or math.isnan(rate):
  1329. logger.warning(
  1330. "{source} Given sample rate is invalid. Sample rate must be a boolean or a number between 0 and 1. Got {rate} of type {type}.".format(
  1331. source=source, rate=rate, type=type(rate)
  1332. )
  1333. )
  1334. return False
  1335. # in case rate is a boolean, it will get cast to 1 if it's True and 0 if it's False
  1336. rate = float(rate)
  1337. if rate < 0 or rate > 1:
  1338. logger.warning(
  1339. "{source} Given sample rate is invalid. Sample rate must be between 0 and 1. Got {rate}.".format(
  1340. source=source, rate=rate
  1341. )
  1342. )
  1343. return False
  1344. return True
  1345. def match_regex_list(item, regex_list=None, substring_matching=False):
  1346. # type: (str, Optional[List[str]], bool) -> bool
  1347. if regex_list is None:
  1348. return False
  1349. for item_matcher in regex_list:
  1350. if not substring_matching and item_matcher[-1] != "$":
  1351. item_matcher += "$"
  1352. matched = re.search(item_matcher, item)
  1353. if matched:
  1354. return True
  1355. return False
  1356. def is_sentry_url(client, url):
  1357. # type: (sentry_sdk.client.BaseClient, str) -> bool
  1358. """
  1359. Determines whether the given URL matches the Sentry DSN.
  1360. """
  1361. return (
  1362. client is not None
  1363. and client.transport is not None
  1364. and client.transport.parsed_dsn is not None
  1365. and client.transport.parsed_dsn.netloc in url
  1366. )
  1367. def _generate_installed_modules():
  1368. # type: () -> Iterator[Tuple[str, str]]
  1369. try:
  1370. from importlib import metadata
  1371. yielded = set()
  1372. for dist in metadata.distributions():
  1373. name = dist.metadata.get("Name", None) # type: ignore[attr-defined]
  1374. # `metadata` values may be `None`, see:
  1375. # https://github.com/python/cpython/issues/91216
  1376. # and
  1377. # https://github.com/python/importlib_metadata/issues/371
  1378. if name is not None:
  1379. normalized_name = _normalize_module_name(name)
  1380. if dist.version is not None and normalized_name not in yielded:
  1381. yield normalized_name, dist.version
  1382. yielded.add(normalized_name)
  1383. except ImportError:
  1384. # < py3.8
  1385. try:
  1386. import pkg_resources
  1387. except ImportError:
  1388. return
  1389. for info in pkg_resources.working_set:
  1390. yield _normalize_module_name(info.key), info.version
  1391. def _normalize_module_name(name):
  1392. # type: (str) -> str
  1393. return name.lower()
  1394. def _get_installed_modules():
  1395. # type: () -> Dict[str, str]
  1396. global _installed_modules
  1397. if _installed_modules is None:
  1398. _installed_modules = dict(_generate_installed_modules())
  1399. return _installed_modules
  1400. def package_version(package):
  1401. # type: (str) -> Optional[Tuple[int, ...]]
  1402. installed_packages = _get_installed_modules()
  1403. version = installed_packages.get(package)
  1404. if version is None:
  1405. return None
  1406. return parse_version(version)
  1407. def reraise(tp, value, tb=None):
  1408. # type: (Optional[Type[BaseException]], Optional[BaseException], Optional[Any]) -> NoReturn
  1409. assert value is not None
  1410. if value.__traceback__ is not tb:
  1411. raise value.with_traceback(tb)
  1412. raise value
  1413. def _no_op(*_a, **_k):
  1414. # type: (*Any, **Any) -> None
  1415. """No-op function for ensure_integration_enabled."""
  1416. pass
  1417. if TYPE_CHECKING:
  1418. @overload
  1419. def ensure_integration_enabled(
  1420. integration, # type: type[sentry_sdk.integrations.Integration]
  1421. original_function, # type: Callable[P, R]
  1422. ):
  1423. # type: (...) -> Callable[[Callable[P, R]], Callable[P, R]]
  1424. ...
  1425. @overload
  1426. def ensure_integration_enabled(
  1427. integration, # type: type[sentry_sdk.integrations.Integration]
  1428. ):
  1429. # type: (...) -> Callable[[Callable[P, None]], Callable[P, None]]
  1430. ...
  1431. def ensure_integration_enabled(
  1432. integration, # type: type[sentry_sdk.integrations.Integration]
  1433. original_function=_no_op, # type: Union[Callable[P, R], Callable[P, None]]
  1434. ):
  1435. # type: (...) -> Callable[[Callable[P, R]], Callable[P, R]]
  1436. """
  1437. Ensures a given integration is enabled prior to calling a Sentry-patched function.
  1438. The function takes as its parameters the integration that must be enabled and the original
  1439. function that the SDK is patching. The function returns a function that takes the
  1440. decorated (Sentry-patched) function as its parameter, and returns a function that, when
  1441. called, checks whether the given integration is enabled. If the integration is enabled, the
  1442. function calls the decorated, Sentry-patched function. If the integration is not enabled,
  1443. the original function is called.
  1444. The function also takes care of preserving the original function's signature and docstring.
  1445. Example usage:
  1446. ```python
  1447. @ensure_integration_enabled(MyIntegration, my_function)
  1448. def patch_my_function():
  1449. with sentry_sdk.start_transaction(...):
  1450. return my_function()
  1451. ```
  1452. """
  1453. if TYPE_CHECKING:
  1454. # Type hint to ensure the default function has the right typing. The overloads
  1455. # ensure the default _no_op function is only used when R is None.
  1456. original_function = cast(Callable[P, R], original_function)
  1457. def patcher(sentry_patched_function):
  1458. # type: (Callable[P, R]) -> Callable[P, R]
  1459. def runner(*args: "P.args", **kwargs: "P.kwargs"):
  1460. # type: (...) -> R
  1461. if sentry_sdk.get_client().get_integration(integration) is None:
  1462. return original_function(*args, **kwargs)
  1463. return sentry_patched_function(*args, **kwargs)
  1464. if original_function is _no_op:
  1465. return wraps(sentry_patched_function)(runner)
  1466. return wraps(original_function)(runner)
  1467. return patcher
  1468. if PY37:
  1469. def nanosecond_time():
  1470. # type: () -> int
  1471. return time.perf_counter_ns()
  1472. else:
  1473. def nanosecond_time():
  1474. # type: () -> int
  1475. return int(time.perf_counter() * 1e9)
  1476. def now():
  1477. # type: () -> float
  1478. return time.perf_counter()
  1479. try:
  1480. from gevent import get_hub as get_gevent_hub
  1481. from gevent.monkey import is_module_patched
  1482. except ImportError:
  1483. # it's not great that the signatures are different, get_hub can't return None
  1484. # consider adding an if TYPE_CHECKING to change the signature to Optional[Hub]
  1485. def get_gevent_hub(): # type: ignore[misc]
  1486. # type: () -> Optional[Hub]
  1487. return None
  1488. def is_module_patched(mod_name):
  1489. # type: (str) -> bool
  1490. # unable to import from gevent means no modules have been patched
  1491. return False
  1492. def is_gevent():
  1493. # type: () -> bool
  1494. return is_module_patched("threading") or is_module_patched("_thread")
  1495. def get_current_thread_meta(thread=None):
  1496. # type: (Optional[threading.Thread]) -> Tuple[Optional[int], Optional[str]]
  1497. """
  1498. Try to get the id of the current thread, with various fall backs.
  1499. """
  1500. # if a thread is specified, that takes priority
  1501. if thread is not None:
  1502. try:
  1503. thread_id = thread.ident
  1504. thread_name = thread.name
  1505. if thread_id is not None:
  1506. return thread_id, thread_name
  1507. except AttributeError:
  1508. pass
  1509. # if the app is using gevent, we should look at the gevent hub first
  1510. # as the id there differs from what the threading module reports
  1511. if is_gevent():
  1512. gevent_hub = get_gevent_hub()
  1513. if gevent_hub is not None:
  1514. try:
  1515. # this is undocumented, so wrap it in try except to be safe
  1516. return gevent_hub.thread_ident, None
  1517. except AttributeError:
  1518. pass
  1519. # use the current thread's id if possible
  1520. try:
  1521. thread = threading.current_thread()
  1522. thread_id = thread.ident
  1523. thread_name = thread.name
  1524. if thread_id is not None:
  1525. return thread_id, thread_name
  1526. except AttributeError:
  1527. pass
  1528. # if we can't get the current thread id, fall back to the main thread id
  1529. try:
  1530. thread = threading.main_thread()
  1531. thread_id = thread.ident
  1532. thread_name = thread.name
  1533. if thread_id is not None:
  1534. return thread_id, thread_name
  1535. except AttributeError:
  1536. pass
  1537. # we've tried everything, time to give up
  1538. return None, None
  1539. def should_be_treated_as_error(ty, value):
  1540. # type: (Any, Any) -> bool
  1541. if ty == SystemExit and hasattr(value, "code") and value.code in (0, None):
  1542. # https://docs.python.org/3/library/exceptions.html#SystemExit
  1543. return False
  1544. return True
  1545. if TYPE_CHECKING:
  1546. T = TypeVar("T")
  1547. def try_convert(convert_func, value):
  1548. # type: (Callable[[Any], T], Any) -> Optional[T]
  1549. """
  1550. Attempt to convert from an unknown type to a specific type, using the
  1551. given function. Return None if the conversion fails, i.e. if the function
  1552. raises an exception.
  1553. """
  1554. try:
  1555. if isinstance(value, convert_func): # type: ignore
  1556. return value
  1557. except TypeError:
  1558. pass
  1559. try:
  1560. return convert_func(value)
  1561. except Exception:
  1562. return None
  1563. def safe_serialize(data):
  1564. # type: (Any) -> str
  1565. """Safely serialize to a readable string."""
  1566. def serialize_item(item):
  1567. # type: (Any) -> Union[str, dict[Any, Any], list[Any], tuple[Any, ...]]
  1568. if callable(item):
  1569. try:
  1570. module = getattr(item, "__module__", None)
  1571. qualname = getattr(item, "__qualname__", None)
  1572. name = getattr(item, "__name__", "anonymous")
  1573. if module and qualname:
  1574. full_path = f"{module}.{qualname}"
  1575. elif module and name:
  1576. full_path = f"{module}.{name}"
  1577. else:
  1578. full_path = name
  1579. return f"<function {full_path}>"
  1580. except Exception:
  1581. return f"<callable {type(item).__name__}>"
  1582. elif isinstance(item, dict):
  1583. return {k: serialize_item(v) for k, v in item.items()}
  1584. elif isinstance(item, (list, tuple)):
  1585. return [serialize_item(x) for x in item]
  1586. elif hasattr(item, "__dict__"):
  1587. try:
  1588. attrs = {
  1589. k: serialize_item(v)
  1590. for k, v in vars(item).items()
  1591. if not k.startswith("_")
  1592. }
  1593. return f"<{type(item).__name__} {attrs}>"
  1594. except Exception:
  1595. return repr(item)
  1596. else:
  1597. return item
  1598. try:
  1599. serialized = serialize_item(data)
  1600. return json.dumps(serialized, default=str)
  1601. except Exception:
  1602. return str(data)
  1603. def has_logs_enabled(options):
  1604. # type: (Optional[dict[str, Any]]) -> bool
  1605. if options is None:
  1606. return False
  1607. return bool(
  1608. options.get("enable_logs", False)
  1609. or options["_experiments"].get("enable_logs", False)
  1610. )
  1611. def get_before_send_log(options):
  1612. # type: (Optional[dict[str, Any]]) -> Optional[Callable[[Log, Hint], Optional[Log]]]
  1613. if options is None:
  1614. return None
  1615. return options.get("before_send_log") or options["_experiments"].get(
  1616. "before_send_log"
  1617. )
  1618. def has_metrics_enabled(options):
  1619. # type: (Optional[dict[str, Any]]) -> bool
  1620. if options is None:
  1621. return False
  1622. return bool(options["_experiments"].get("enable_metrics", False))
  1623. def get_before_send_metric(options):
  1624. # type: (Optional[dict[str, Any]]) -> Optional[Callable[[Metric, Hint], Optional[Metric]]]
  1625. if options is None:
  1626. return None
  1627. return options["_experiments"].get("before_send_metric")