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

"ImportError: cannot import name 'StarletteIntegration' from 'sentry_sdk.integrations.starlette'" after sentry version update

Open pravindahal opened this issue 2 years ago • 1 comments

How do you use Sentry?

Sentry Saas (sentry.io)

Version

1.9.7

Steps to Reproduce

It only happens every once in a while so it is not easy to reproduce.

Here is my sentry.py (see exception trace):

import functools
import inspect
import os
from typing import Callable

import sentry_sdk

from archimedes_data_fetcher.config import config


def initialize():
    """Initializes sentry"""
    sentry_sdk.init(
        config.get("SENTRY_DSN"),
        environment=config.get("ENVIRONMENT"),
    )
...
...
...

Here is the output of pip list on the failing container:

Package                 Version
----------------------- ---------
affine                  2.3.1
archimedes-data-fetcher 0.5.19
archimedes-flow-utils   1.0.2
attrs                   22.1.0
azure-core              1.25.1
azure-storage-blob      12.13.1
cachetools              5.2.0
certifi                 2022.6.15
cffi                    1.15.1
cftime                  1.6.1
charset-normalizer      2.1.1
click                   8.1.3
click-plugins           1.1.1
cligj                   0.7.2
cloudpickle             2.1.0
croniter                1.3.5
cryptography            37.0.4
dask                    2022.8.1
datadog                 0.44.0
datadog-api-client      2.3.0
distributed             2022.8.1
docker                  6.0.0
fsspec                  2022.8.2
gitdb                   4.0.9
GitPython               3.1.27
HeapDict                1.0.1
idna                    3.3
importlib-resources     5.9.0
investpy                1.0.8
isodate                 0.6.1
Jinja2                  3.1.2
locket                  1.0.0
lxml                    4.9.1
MarkupSafe              2.1.1
marshmallow             3.17.1
marshmallow-oneofschema 3.0.1
minio                   7.1.11
msgpack                 1.0.4
msrest                  0.7.1
mypy-extensions         0.4.3
netCDF4                 1.6.0
numpy                   1.23.2
oauthlib                3.2.0
packaging               21.3
pandas                  1.4.4
partd                   1.3.0
pendulum                2.1.2
pip                     22.2.2
prefect                 1.3.0
psutil                  5.9.1
psycopg                 3.1
pycparser               2.21
pyparsing               3.0.9
pyproj                  3.3.1
python-box              6.0.2
python-dateutil         2.8.2
python-dotenv           0.20.0
python-slugify          6.1.2
pytz                    2022.2.1
pytzdata                2020.1
PyYAML                  6.0
rasterio                1.3.2
requests                2.28.1
requests-oauthlib       1.3.1
rioxarray               0.10.3
sentry-sdk              1.9.7
setuptools              65.3.0
six                     1.16.0
smmap                   5.0.0
snuggs                  1.4.7
sortedcontainers        2.4.0
tabulate                0.8.10
tblib                   1.7.0
text-unidecode          1.3
toml                    0.10.2
toolz                   0.12.0
tornado                 6.1
typing_extensions       4.3.0
Unidecode               1.3.4
urllib3                 1.26.12
websocket-client        1.4.0
wheel                   0.37.1
xarray                  2022.6.0
zict                    2.2.0
zipp                    3.8.1

This has not happened when I pin sentry-sdk version to 1.9.5, so I think it must have been introduced in either 1.9.6 or 1.9.7.

Expected Result

No crash on sentry initialization.

Actual Result

Task 'store[1]': Exception encountered during task execution!
Traceback (most recent call last):
  File "/app/.venv/lib/python3.9/site-packages/prefect/engine/task_runner.py", line 880, in get_task_run_state
    value = prefect.utilities.executors.run_task_with_timeout(
  File "/app/.venv/lib/python3.9/site-packages/prefect/utilities/executors.py", line 468, in run_task_with_timeout
    return task.run(*args, **kwargs)  # type: ignore
  File "/home/runner/work/archimedes-data-fetcher/archimedes-data-fetcher/archimedes_data_fetcher/sentry.py", line 36, in wrapper
  File "/app/archimedes_data_fetcher/sentry.py", line 13, in initialize
    sentry_sdk.init(
  File "/app/.venv/lib/python3.9/site-packages/sentry_sdk/hub.py", line 119, in _init
    client = Client(*args, **kwargs)  # type: ignore
  File "/app/.venv/lib/python3.9/site-packages/sentry_sdk/client.py", line 89, in __init__
    self._init_impl()
  File "/app/.venv/lib/python3.9/site-packages/sentry_sdk/client.py", line 123, in _init_impl
    self.integrations = setup_integrations(
  File "/app/.venv/lib/python3.9/site-packages/sentry_sdk/integrations/__init__.py", line 109, in setup_integrations
    for integration_cls in iter_default_integrations(
  File "/app/.venv/lib/python3.9/site-packages/sentry_sdk/integrations/__init__.py", line 41, in iter_default_integrations
    yield getattr(import_module(module), cls)
  File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/app/.venv/lib/python3.9/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' (/app/.venv/lib/python3.9/site-packages/sentry_sdk/integrations/starlette.py)

pravindahal avatar Sep 06 '22 14:09 pravindahal

From what I see everything looks good and it should work. The file that StarletteIntegration is try to import from is there and StarletteIntegration is also in there.

From the /app path in your stack traces, I guess you are in a Docker container. Is the Python code outside the Docker container, maybe some syncing between the docker host and the docker container is not working? (This happened to me once and had something to do with the IDE was watching the python files for changes and also Docker is watching the synced directory with the same files for changes and something then broke)

Hope this helps solving your problem!

antonpirker avatar Sep 13 '22 07:09 antonpirker

We're seeing this issue too, in a project that uses neither fastapi or starlette.

My theory is it's due to the order the auto enabling integrations runs: "sentry_sdk.integrations.starlette.StarletteIntegration", "sentry_sdk.integrations.fastapi.FastApiIntegration",

It tries StarletteIntegration, which raises DidNotEnable, but as a side-effect that module is defined, but empty. Then it tries FastApiIntegration, which tries to import from the empty sentry_sdk.integrations.starlette and gets the error above.

For now, we've switched off the auto enabling integrations to workaround.

barnybug avatar Oct 19 '22 09:10 barnybug

This issue still exists as of version 1.10.1.

pravindahal avatar Nov 09 '22 14:11 pravindahal

This issue 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 Dec 02 '22 00:12 github-actions[bot]

Hi there

We specifically set our integrations to be:

integrations=[
    sentry_sdk.integrations.celery.CeleryIntegration(),
    sentry_sdk.integrations.flask.FlaskIntegration(),
    sentry_sdk.integrations.sqlalchemy.SqlalchemyIntegration(),
]

But we keep getting the error:

cannot import name 'StarletteIntegration' from 'sentry_sdk.integrations.starlette' (/usr/local/lib/python3.7/site-packages/sentry_sdk/integrations/starlette.py)

This started after we bumped from Sentry SDK 0.20.3 to 1.11.1

caiolopes avatar Dec 16 '22 14:12 caiolopes

@antonpirker No indication that this has been resolved. I will double-check and post an update... could you please re-open for now?

pravindahal avatar Dec 19 '22 10:12 pravindahal

Confirming that this issue still exists with version 1.12.1.

pravindahal avatar Dec 19 '22 10:12 pravindahal

If this doesn't get reopened and if someone else finds this, I did the same as one of the users has mentioned above which is to disable auto-enabling integrations as a workaround:

    sentry_sdk.init(
        ...
        ...
        auto_enabling_integrations=False,
        ...
        ...
    )

pravindahal avatar Dec 29 '22 14:12 pravindahal

@antonpirker this is not resolved so re-opening, please take a look once you're back

sl0thentr0py avatar Jan 03 '23 10:01 sl0thentr0py