sentry-python icon indicating copy to clipboard operation
sentry-python copied to clipboard

Auto-enable Starlette and FastAPI

Open antonpirker opened this issue 2 years ago • 2 comments

The integrations for Starlette and FastAPI should be auto enabled. This is for easier installation of the Sentry SDK. The SDK will detect if Starlette and/or FastAPI is installed and then the integrations are enabled automatically.

Fixes #1531

antonpirker avatar Aug 02 '22 18:08 antonpirker

making this a draft since we should wait with shipping this

sl0thentr0py avatar Aug 05 '22 12:08 sl0thentr0py

In case that the ASGIMiddleware is used when also the integrations are auto enabled I now output an error message during startup: The Python SDK can now automatically support ASGI frameworks like Starlette and FastAPI. Please remove 'SentryAsgiMiddleware' from your project. See https://docs.sentry.io/platforms/python/guides/asgi/ for more information.

There is also a PR to improve the linked documentation: https://github.com/getsentry/sentry-docs/pull/5390

antonpirker avatar Aug 05 '22 13:08 antonpirker

This pull request has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog or Status: In Progress, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

github-actions[bot] avatar Aug 27 '22 00:08 github-actions[bot]

This integration is causing us issues and we'd like to turn off the starlette/fastapi sentry integration, is there a way to disable this auto-enable behaviour?

ps-george avatar Nov 01 '22 11:11 ps-george

Hey @ps-george !

First: What kind of issues are the auto-enabling starlette/fastapi integrations causing you, can you elaborate?

About your question: There is a option for sentry_sdk.init() that is called auto_enabling_integrations. If you set this to "False" the auto enabling integrations will NOT be loaded. See here in the code what auto enabling integrations exist: https://github.com/getsentry/sentry-python/blob/master/sentry_sdk/integrations/init.py#L54-L86

You can then add some of those auto enabling integrations with the integrations option: https://docs.sentry.io/platforms/python/configuration/options/#integrations (in case you just want to enable some of them but not all)

hth

antonpirker avatar Nov 07 '22 13:11 antonpirker

This integration is causing us issues and we'd like to turn off the starlette/fastapi sentry integration, is there a way to disable this auto-enable behaviour?

RE @ps-george , we are seeing something similar:

Traceback (most recent call last):
  File "/poetry/lib/python3.10/site-packages/google/cloud/pubsub_v1/subscriber/_protocol/streaming_pull_manager.py", line 126, in _wrap_callback_errors
    callback(message)
  File "/app/main.py", line 200, in callbackWithTracer
    return callback(message)
  File "/app/main.py", line 35, in callback
    sentry_sdk.init(config["SENTRY_DSN"])
  File "/poetry/lib/python3.10/site-packages/sentry_sdk/hub.py", line 119, in _init
    client = Client(*args, **kwargs)  # type: ignore
  File "/poetry/lib/python3.10/site-packages/sentry_sdk/client.py", line 102, in __init__
    self._init_impl()
  File "/poetry/lib/python3.10/site-packages/sentry_sdk/client.py", line 136, in _init_impl
    self.integrations = setup_integrations(
  File "/poetry/lib/python3.10/site-packages/sentry_sdk/integrations/__init__.py", line 109, in setup_integrations
    for integration_cls in iter_default_integrations(
  File "/poetry/lib/python3.10/site-packages/sentry_sdk/integrations/__init__.py", line 41, in iter_default_integrations
    yield getattr(import_module(module), cls)
  File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/poetry/lib/python3.10/site-packages/sentry_sdk/integrations/fastapi.py", line 4, in <module>
    from sentry_sdk.integrations.starlette import (
ImportError: cannot import name 'StarletteIntegration' from 'sentry_sdk.integrations.starlette' (/poetry/lib/python3.10/site-packages/sentry_sdk/integrations/starlette.py)

Seems related to #1603 and the order of imporing perhaps? Since the errir seems to come from the fastapi "detection", rather than starlette detection.

ckcr4lyf avatar Nov 30 '22 08:11 ckcr4lyf