getsentry / getsentry/sentry-python

SQL queries not traced when using FastAPI with encode/databases

Open
#2,599 7 comments 2 reactions 0 assignees View on GitHub
Feature Integration: SQLAlchemy New Integration Python
Dominant language
Python
Stars
2.2k
Forks
669
Avg merge
1d 1h
Merged PRs (30d)
213

Description

### How do you use Sentry?

Sentry Saas (sentry.io)

### Version

1.39.1

### Steps to Reproduce

When using the encode/databases package for async database access in FastAPI, queries are not traced.

Example code:
```python
from typing import List

import databases
from fastapi import FastAPI
from pydantic import BaseModel
import sentry_sdk
import sqlalchemy

DATABASE_URL = "sqlite:///./test.db"
ASYNC_DATABASE_URL = "sqlite+aiosqlite:///./test.db"

sentry_sdk.init(
dsn="https://....",
debug=True,
enable_tracing=True,
profiles_sample_rate=1.0,
traces_sample_rate=1.0
)

database = databases.Database(ASYNC_DATABASE_URL)
metadata = sqlalchemy.MetaData()
notes = sqlalchemy.Table(
"notes",
metadata,
sqlalchemy.Column("id", sqlalchemy.Integer, primary_key=True),
sqlalchemy.Column("text", sqlalchemy.String)
)
engine = sqlalchemy.create_engine(
DATABASE_URL, connect_args={"check_same_thread": False}
)
metadata.create_all(engine)

app = FastAPI(
title="SentryTest",
version="0.0.1"
)

class Note(BaseModel):
id: int
text: str

@app.get("/test")
async def test() -> List[Note]:
query = "SELECT * from notes;"
return await database.fetch_all(query=query)

```

Output of `pip freeze --all`:
```
aiosqlite==0.19.0
annotated-types==0.6.0
anyio==3.7.1
certifi==2023.11.17
click==8.1.7
databases==0.8.0
exceptiongroup==1.2.0
fastapi==0.105.0
greenlet==3.0.2
h11==0.14.0
httptools==0.6.1
idna==3.6
pip==23.3.1
pydantic==2.5.2
pydantic_core==2.14.5
python-dotenv==1.0.0
PyYAML==6.0.1
sentry-sdk==1.39.1
setuptools==68.2.2
sniffio==1.3.0
SQLAlchemy==1.4.50
starlette==0.27.0
typing_extensions==4.9.0
urllib3==2.1.0
uvicorn==0.24.0.post1
uvloop==0.19.0
watchfiles==0.21.0
websockets==12.0
wheel==0.41.3
```

### Expected Result

The queries should show up in the web interface

### Actual Result

The queries do not show in the web interface

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.