__init__.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. from abc import ABC, abstractmethod
  2. from threading import Lock
  3. from typing import TYPE_CHECKING
  4. from sentry_sdk.utils import logger
  5. if TYPE_CHECKING:
  6. from collections.abc import Sequence
  7. from typing import Any, Callable, Dict, Iterator, List, Optional, Set, Type, Union
  8. _DEFAULT_FAILED_REQUEST_STATUS_CODES = frozenset(range(500, 600))
  9. _installer_lock = Lock()
  10. # Set of all integration identifiers we have attempted to install
  11. _processed_integrations: "Set[str]" = set()
  12. # Set of all integration identifiers we have actually installed
  13. _installed_integrations: "Set[str]" = set()
  14. def _generate_default_integrations_iterator(
  15. integrations: "List[str]",
  16. auto_enabling_integrations: "List[str]",
  17. ) -> "Callable[[bool], Iterator[Type[Integration]]]":
  18. def iter_default_integrations(
  19. with_auto_enabling_integrations: bool,
  20. ) -> "Iterator[Type[Integration]]":
  21. """Returns an iterator of the default integration classes:"""
  22. from importlib import import_module
  23. if with_auto_enabling_integrations:
  24. all_import_strings = integrations + auto_enabling_integrations
  25. else:
  26. all_import_strings = integrations
  27. for import_string in all_import_strings:
  28. try:
  29. module, cls = import_string.rsplit(".", 1)
  30. yield getattr(import_module(module), cls)
  31. except (DidNotEnable, SyntaxError) as e:
  32. logger.debug(
  33. "Did not import default integration %s: %s", import_string, e
  34. )
  35. if isinstance(iter_default_integrations.__doc__, str):
  36. for import_string in integrations:
  37. iter_default_integrations.__doc__ += "\n- `{}`".format(import_string)
  38. return iter_default_integrations
  39. _DEFAULT_INTEGRATIONS = [
  40. # stdlib/base runtime integrations
  41. "sentry_sdk.integrations.argv.ArgvIntegration",
  42. "sentry_sdk.integrations.atexit.AtexitIntegration",
  43. "sentry_sdk.integrations.dedupe.DedupeIntegration",
  44. "sentry_sdk.integrations.excepthook.ExcepthookIntegration",
  45. "sentry_sdk.integrations.logging.LoggingIntegration",
  46. "sentry_sdk.integrations.modules.ModulesIntegration",
  47. "sentry_sdk.integrations.stdlib.StdlibIntegration",
  48. "sentry_sdk.integrations.threading.ThreadingIntegration",
  49. ]
  50. _AUTO_ENABLING_INTEGRATIONS = [
  51. "sentry_sdk.integrations.aiohttp.AioHttpIntegration",
  52. "sentry_sdk.integrations.anthropic.AnthropicIntegration",
  53. "sentry_sdk.integrations.ariadne.AriadneIntegration",
  54. "sentry_sdk.integrations.arq.ArqIntegration",
  55. "sentry_sdk.integrations.asyncpg.AsyncPGIntegration",
  56. "sentry_sdk.integrations.boto3.Boto3Integration",
  57. "sentry_sdk.integrations.bottle.BottleIntegration",
  58. "sentry_sdk.integrations.celery.CeleryIntegration",
  59. "sentry_sdk.integrations.chalice.ChaliceIntegration",
  60. "sentry_sdk.integrations.clickhouse_driver.ClickhouseDriverIntegration",
  61. "sentry_sdk.integrations.cohere.CohereIntegration",
  62. "sentry_sdk.integrations.django.DjangoIntegration",
  63. "sentry_sdk.integrations.falcon.FalconIntegration",
  64. "sentry_sdk.integrations.fastapi.FastApiIntegration",
  65. "sentry_sdk.integrations.flask.FlaskIntegration",
  66. "sentry_sdk.integrations.gql.GQLIntegration",
  67. "sentry_sdk.integrations.google_genai.GoogleGenAIIntegration",
  68. "sentry_sdk.integrations.graphene.GrapheneIntegration",
  69. "sentry_sdk.integrations.httpx.HttpxIntegration",
  70. "sentry_sdk.integrations.huey.HueyIntegration",
  71. "sentry_sdk.integrations.huggingface_hub.HuggingfaceHubIntegration",
  72. "sentry_sdk.integrations.langchain.LangchainIntegration",
  73. "sentry_sdk.integrations.langgraph.LanggraphIntegration",
  74. "sentry_sdk.integrations.litestar.LitestarIntegration",
  75. "sentry_sdk.integrations.loguru.LoguruIntegration",
  76. "sentry_sdk.integrations.mcp.MCPIntegration",
  77. "sentry_sdk.integrations.openai.OpenAIIntegration",
  78. "sentry_sdk.integrations.openai_agents.OpenAIAgentsIntegration",
  79. "sentry_sdk.integrations.pydantic_ai.PydanticAIIntegration",
  80. "sentry_sdk.integrations.pymongo.PyMongoIntegration",
  81. "sentry_sdk.integrations.pyramid.PyramidIntegration",
  82. "sentry_sdk.integrations.quart.QuartIntegration",
  83. "sentry_sdk.integrations.redis.RedisIntegration",
  84. "sentry_sdk.integrations.rq.RqIntegration",
  85. "sentry_sdk.integrations.sanic.SanicIntegration",
  86. "sentry_sdk.integrations.sqlalchemy.SqlalchemyIntegration",
  87. "sentry_sdk.integrations.starlette.StarletteIntegration",
  88. "sentry_sdk.integrations.starlite.StarliteIntegration",
  89. "sentry_sdk.integrations.strawberry.StrawberryIntegration",
  90. "sentry_sdk.integrations.tornado.TornadoIntegration",
  91. ]
  92. iter_default_integrations = _generate_default_integrations_iterator(
  93. integrations=_DEFAULT_INTEGRATIONS,
  94. auto_enabling_integrations=_AUTO_ENABLING_INTEGRATIONS,
  95. )
  96. del _generate_default_integrations_iterator
  97. _MIN_VERSIONS = {
  98. "aiohttp": (3, 4),
  99. "anthropic": (0, 16),
  100. "ariadne": (0, 20),
  101. "arq": (0, 23),
  102. "asyncpg": (0, 23),
  103. "beam": (2, 12),
  104. "boto3": (1, 12), # botocore
  105. "bottle": (0, 12),
  106. "celery": (4, 4, 7),
  107. "chalice": (1, 16, 0),
  108. "clickhouse_driver": (0, 2, 0),
  109. "cohere": (5, 4, 0),
  110. "django": (1, 8),
  111. "dramatiq": (1, 9),
  112. "falcon": (1, 4),
  113. "fastapi": (0, 79, 0),
  114. "flask": (1, 1, 4),
  115. "gql": (3, 4, 1),
  116. "graphene": (3, 3),
  117. "google_genai": (1, 29, 0), # google-genai
  118. "grpc": (1, 32, 0), # grpcio
  119. "httpx": (0, 16, 0),
  120. "huggingface_hub": (0, 24, 7),
  121. "langchain": (0, 1, 0),
  122. "langgraph": (0, 6, 6),
  123. "launchdarkly": (9, 8, 0),
  124. "litellm": (1, 77, 5),
  125. "loguru": (0, 7, 0),
  126. "mcp": (1, 15, 0),
  127. "openai": (1, 0, 0),
  128. "openai_agents": (0, 0, 19),
  129. "openfeature": (0, 7, 1),
  130. "pydantic_ai": (1, 0, 0),
  131. "pymongo": (3, 5, 0),
  132. "pyreqwest": (0, 11, 6),
  133. "quart": (0, 16, 0),
  134. "ray": (2, 7, 0),
  135. "requests": (2, 0, 0),
  136. "rq": (0, 6),
  137. "sanic": (0, 8),
  138. "sqlalchemy": (1, 2),
  139. "starlette": (0, 16),
  140. "starlite": (1, 48),
  141. "statsig": (0, 55, 3),
  142. "strawberry": (0, 209, 5),
  143. "tornado": (6, 0),
  144. "typer": (0, 15),
  145. "unleash": (6, 0, 1),
  146. }
  147. _INTEGRATION_DEACTIVATES = {
  148. "langchain": {"openai", "anthropic", "google_genai"},
  149. "openai_agents": {"openai"},
  150. "pydantic_ai": {"openai", "anthropic"},
  151. }
  152. def setup_integrations(
  153. integrations: "Sequence[Integration]",
  154. with_defaults: bool = True,
  155. with_auto_enabling_integrations: bool = False,
  156. disabled_integrations: "Optional[Sequence[Union[type[Integration], Integration]]]" = None,
  157. options: "Optional[Dict[str, Any]]" = None,
  158. ) -> "Dict[str, Integration]":
  159. """
  160. Given a list of integration instances, this installs them all.
  161. When `with_defaults` is set to `True` all default integrations are added
  162. unless they were already provided before.
  163. `disabled_integrations` takes precedence over `with_defaults` and
  164. `with_auto_enabling_integrations`.
  165. Some integrations are designed to automatically deactivate other integrations
  166. in order to avoid conflicts and prevent duplicate telemetry from being collected.
  167. For example, enabling the `langchain` integration will auto-deactivate both the
  168. `openai` and `anthropic` integrations.
  169. Users can override this behavior by:
  170. - Explicitly providing an integration in the `integrations=[]` list, or
  171. - Disabling the higher-level integration via the `disabled_integrations` option.
  172. """
  173. integrations = dict(
  174. (integration.identifier, integration) for integration in integrations or ()
  175. )
  176. logger.debug("Setting up integrations (with default = %s)", with_defaults)
  177. user_provided_integrations = set(integrations.keys())
  178. # Integrations that will not be enabled
  179. disabled_integrations = [
  180. integration if isinstance(integration, type) else type(integration)
  181. for integration in disabled_integrations or []
  182. ]
  183. # Integrations that are not explicitly set up by the user.
  184. used_as_default_integration = set()
  185. if with_defaults:
  186. for integration_cls in iter_default_integrations(
  187. with_auto_enabling_integrations
  188. ):
  189. if integration_cls.identifier not in integrations:
  190. instance = integration_cls()
  191. integrations[instance.identifier] = instance
  192. used_as_default_integration.add(instance.identifier)
  193. disabled_integration_identifiers = {
  194. integration.identifier for integration in disabled_integrations
  195. }
  196. for integration, targets_to_deactivate in _INTEGRATION_DEACTIVATES.items():
  197. if (
  198. integration in integrations
  199. and integration not in disabled_integration_identifiers
  200. ):
  201. for target in targets_to_deactivate:
  202. if target not in user_provided_integrations:
  203. for cls in iter_default_integrations(True):
  204. if cls.identifier == target:
  205. if cls not in disabled_integrations:
  206. disabled_integrations.append(cls)
  207. logger.debug(
  208. "Auto-deactivating %s integration because %s integration is active",
  209. target,
  210. integration,
  211. )
  212. for identifier, integration in integrations.items():
  213. with _installer_lock:
  214. if identifier not in _processed_integrations:
  215. if type(integration) in disabled_integrations:
  216. logger.debug("Ignoring integration %s", identifier)
  217. else:
  218. logger.debug(
  219. "Setting up previously not enabled integration %s", identifier
  220. )
  221. try:
  222. type(integration).setup_once()
  223. integration.setup_once_with_options(options)
  224. except DidNotEnable as e:
  225. if identifier not in used_as_default_integration:
  226. raise
  227. logger.debug(
  228. "Did not enable default integration %s: %s", identifier, e
  229. )
  230. else:
  231. _installed_integrations.add(identifier)
  232. _processed_integrations.add(identifier)
  233. integrations = {
  234. identifier: integration
  235. for identifier, integration in integrations.items()
  236. if identifier in _installed_integrations
  237. }
  238. for identifier in integrations:
  239. logger.debug("Enabling integration %s", identifier)
  240. return integrations
  241. def _check_minimum_version(
  242. integration: "type[Integration]",
  243. version: "Optional[tuple[int, ...]]",
  244. package: "Optional[str]" = None,
  245. ) -> None:
  246. package = package or integration.identifier
  247. if version is None:
  248. raise DidNotEnable(f"Unparsable {package} version.")
  249. min_version = _MIN_VERSIONS.get(integration.identifier)
  250. if min_version is None:
  251. return
  252. if version < min_version:
  253. raise DidNotEnable(
  254. f"Integration only supports {package} {'.'.join(map(str, min_version))} or newer."
  255. )
  256. class DidNotEnable(Exception): # noqa: N818
  257. """
  258. The integration could not be enabled due to a trivial user error like
  259. `flask` not being installed for the `FlaskIntegration`.
  260. This exception is silently swallowed for default integrations, but reraised
  261. for explicitly enabled integrations.
  262. """
  263. class Integration(ABC):
  264. """Baseclass for all integrations.
  265. To accept options for an integration, implement your own constructor that
  266. saves those options on `self`.
  267. """
  268. install = None
  269. """Legacy method, do not implement."""
  270. identifier: "str" = None # type: ignore[assignment]
  271. """String unique ID of integration type"""
  272. @staticmethod
  273. @abstractmethod
  274. def setup_once() -> None:
  275. """
  276. Initialize the integration.
  277. This function is only called once, ever. Configuration is not available
  278. at this point, so the only thing to do here is to hook into exception
  279. handlers, and perhaps do monkeypatches.
  280. Inside those hooks `Integration.current` can be used to access the
  281. instance again.
  282. """
  283. pass
  284. def setup_once_with_options(
  285. self, options: "Optional[Dict[str, Any]]" = None
  286. ) -> None:
  287. """
  288. Called after setup_once in rare cases on the instance and with options since we don't have those available above.
  289. """
  290. pass