Skip to content

Unable to use Sentry Logs in FastAPI Background Task #4781

@leo-exee

Description

@leo-exee

How do you use Sentry?

Self-hosted/on-premise

Version

2.36.0

Steps to Reproduce

Hello everyone,

I’ve discovered Sentry logs feature, except that I have a small problem: when I try to use the functionality in a background task with FastAPI, they don’t seem to be sent.

My sentry configuration :

def before_log(log: Log, _hint: Hint):
    if log.get("severity_text") in ["info", "warn"]:
        return log
    return None

sentry_sdk.init(
    dsn="DSN_LINK", # <<< I voluntarily removed my DNS link for this example
    integrations=[
        FastApiIntegration(transaction_style="endpoint"),
        LoggingIntegration(
            level=logging.INFO,  # Capture error and above as breadcrumbs
            event_level=None,  # Don't send records as events if None
        ),
        AsyncioIntegration(),
    ],
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for performance monitoring.
    # We recommend adjusting this value in production,
    traces_sample_rate=1.0,
    environment=SENTRY_ENV,
    enable_logs=True,
    before_send_log=before_log,
)

After which, any log in a function called as a background task in FastAPI does not appear in Sentry Logs. All the others appear however well.

app = FastAPI()

def handle_background():
  logging.warning("Not log in sentry")

@app.get("/")
async def task(background_tasks: BackgroundTask):
  logging.info("Log in sentry")
  background_tasks.add_task(handle_background)

Result of pip freeze :

annotated-types==0.6.0
anyio==4.1.0
attrs==23.2.0
Automat==22.10.0
autopep8==2.0.2
Babel==2.8.0
bcrypt==4.1.1
black==23.7.0
blinker==1.4
boto3==1.28.28
botocore==1.31.85
bson==0.5.10
bump-pydantic==0.8.0
CacheControl==0.14.0
cachetools==5.3.2
certifi==2023.11.17
cfgv==3.4.0
chardet==4.0.0
charset-normalizer==2.0.12
click==8.1.7
cloud-init==25.1.4
colorama==0.4.4
command-not-found==0.3
configobj==5.0.6
constantly==23.10.4
coverage==7.4.1
cryptography==3.4.8
dbus-python==1.2.18
decorator==5.1.1
defusedxml==0.7.1
Deprecated==1.2.14
deprecation==2.1.0
distlib==0.3.7
distro==1.7.0
distro-info==1.1+ubuntu0.2
dnspython==2.4.2
email-validator==2.1.0.post1
et-xmlfile==1.1.0
exceptiongroup==1.2.0
Faker==20.1.0
fastapi==0.115.2
fastapi-cli==0.0.7
filelock==3.13.1
firebase-admin==6.1.0
flasgger==0.9.7.1
Flask==2.2.4
Flask-Cors==4.0.0
fonttools==4.47.0
fpdf2==2.5.7
geojson-pydantic==1.1.0
google-api-core==2.15.0
google-api-python-client==2.116.0
google-auth==2.26.1
google-auth-httplib2==0.2.0
google-cloud-bigquery==3.14.1
google-cloud-core==2.4.1
google-cloud-firestore==2.14.0
google-cloud-storage==2.14.0
google-crc32c==1.5.0
google-resumable-media==2.7.0
googleapis-common-protos==1.62.0
greenlet==3.0.3
grpcio==1.60.1
grpcio-status==1.60.1
gunicorn==21.2.0
h11==0.14.0
httpcore==0.17.3
httplib2==0.20.2
httptools==0.6.1
httpx==0.24.1
hyperlink==21.0.0
ibm-cos-sdk==2.10.0
ibm-cos-sdk-core==2.10.0
ibm-cos-sdk-s3transfer==2.10.0
identify==2.5.35
idna==2.10
importlib-metadata==4.6.4
incremental==22.10.0
iniconfig==2.0.0
itsdangerous==2.1.2
jeepney==0.7.1
Jinja2==3.1.2
jmespath==0.10.0
joblib==1.4.2
jsonpatch==1.32
jsonpointer==2.0
jsonschema==4.20.0
jsonschema-specifications==2023.12.1
keyring==23.5.0
launchpadlib==1.10.16
lazr.restfulclient==0.14.4
lazr.uri==1.0.6
libcst==1.2.0
lxml==4.9.3
markdown-it-py==3.0.0
MarkupSafe==2.1.3
mdurl==0.1.2
mindee==3.7.1
mistune==3.0.2
more-itertools==8.10.0
motor==3.3.2
motor-types==1.0.0b4
msgpack==1.0.7
mypy==1.4.1
mypy-extensions==1.0.0
nanoid==2.0.0
netifaces==0.11.0
nodeenv==1.8.0
numpy==2.1.3
oauthlib==3.2.0
opencv-python==4.10.0.84
openpyxl==3.1.2
outcome==1.3.0.post0
packaging==23.2
passlib==1.7.4
pathspec==0.11.2
phonenumbers==8.13.30
pikepdf==6.2.9
pillow==10.2.0
platformdirs==4.0.0
pluggy==1.3.0
polyfactory==2.12.0
pre-commit==3.6.0
proto-plus==1.23.0
protobuf==4.25.1
pyasn1==0.5.1
pyasn1-modules==0.3.0
pycodestyle==2.11.1
pycountry==23.12.11
pycryptodome==3.19.1
pycurl==7.44.1
pydantic==2.6.0
pydantic_core==2.16.1
Pygments==2.17.2
PyGObject==3.42.1
PyHamcrest==2.0.2
PyJWT==2.7.0
pymongo==4.6.1
pyOpenSSL==21.0.0
pyparsing==2.4.7
pyrsistent==0.18.1
pyserial==3.5
pytest==7.2.2
pytest-asyncio==0.21.0
pytest-cov==4.1.0
pytest-mock==3.10.0
pytest-tornasync==0.6.0.post2
pytest-trio==0.8.0
pytest-twisted==1.14.0
python-apt==2.4.0+ubuntu4
python-dateutil==2.8.2
python-dotenv==1.0.0
python-http-client==3.3.7
python-multipart==0.0.20
pytz==2023.3.post1
PyYAML==5.4.1
referencing==0.32.0
requests==2.32.3
rich==14.1.0
rich-toolkit==0.15.1
rpds-py==0.16.2
rsa==4.9
ruff==0.6.8
s3transfer==0.6.2
scikit-learn==1.5.2
scipy==1.14.1
SecretStorage==3.3.1
sendgrid==6.11.0
sentry-sdk==2.36.0
service-identity==18.1.0
shellingham==1.5.4
six==1.16.0
sniffio==1.3.0
sortedcontainers==2.4.0
ssh-import-id==5.11
starkbank-ecdsa==2.2.0
starlette==0.40.0
stripe==5.0.0
svg.path==6.3
systemd-python==234
threadpoolctl==3.5.0
tomli==2.0.1
tornado==6.4
trio==0.24.0
Twisted==23.10.0
typer==0.17.3
types-passlib==1.7.7.12
types-python-dateutil==2.8.19.14
types-requests==2.31.0.2
types-urllib3==1.26.25.14
typing==3.7.4.3
typing-inspect==0.9.0
typing_extensions==4.15.0
ubuntu-pro-client==8001
ufw==0.36.1
unattended-upgrades==0.1
Unidecode==1.3.7
uritemplate==4.1.1
urllib3==1.26.18
uvicorn==0.23.2
uvloop==0.19.0
virtualenv==20.24.7
vonage==3.9.1
vonage-jwt==1.1.0
wadllib==1.3.6
watchfiles==0.21.0
websockets==12.0
Werkzeug==3.0.1
wrapt==1.16.0
zipp==1.0.0
zope.interface==6.2

Expected Result

The logs in a FastAPI task background should be properly sent into sentry logs.

Actual Result

The logs in a FastAPI task background are not sent in Sentry logs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Status

    Waiting for: Product Owner

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions