-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Logfire Only Logging FastAPI Requests, Not SQL Queries with SQLModel/SQLAlchemy #786
Comments
I see that some debug spans are hidden. If you show them: then you might see a With this code: from fastapi import FastAPI
from sqlmodel import Field, SQLModel, create_engine
import logfire
logfire.configure()
app = FastAPI()
logfire.instrument_fastapi(app)
@app.get('/heroes/{hero_id}')
def read_hero(hero_id: int):
engine = create_engine('sqlite:///:memory:')
logfire.instrument_sqlalchemy(engine=engine)
class Hero(SQLModel, table=True):
id: int = Field(primary_key=True)
SQLModel.metadata.create_all(engine)
if __name__ == '__main__':
import uvicorn
uvicorn.run(app) I see logs from both fastapi and sqlalchemy. Can you provide a complete reproduction? |
Thanks! I now see |
You should be seeing queries. The fact that you see |
I've provided complete code combining FastAPI and SQLModel that seems to work. Does it work for you? Can you provide similar code that doesn't work for you? |
Question
Hello Logfire Community,
I'm integrating Logfire with a FastAPI backend that uses SQLModel (built on SQLAlchemy) for database interactions. I aim to have Logfire display detailed logs for both incoming HTTP requests and the SQL queries executed by SQLModel. However, currently, only the FastAPI request data is being logged, and the SQL queries are not appearing in Logfire.
Here's an overview of my setup:
Main Application (main.py):
Configured Logfire with service name, environment, and token.
Instrumented Pydantic and FastAPI:
Included routers and set up the FastAPI app lifecycle.
Database Configuration (database.py):
Created the SQLAlchemy engine:
Instrumented SQLAlchemy with Logfire:
Provided a session dependency for FastAPI endpoints.
Endpoint Example:
Defined a FastAPI endpoint that uses a service and data access layer to execute database queries.
Issue: Despite the above setup, Logfire only captures and displays logs related to FastAPI requests. The SQL queries executed via SQLModel/SQLAlchemy are not being logged or visible in the Logfire dashboard.
What I've Tried:
Ensured that logfire.instrument_sqlalchemy(engine=engine) is called after creating the engine.
Verified that SQLAlchemy's echo parameter is set appropriately for logging.
Checked Logfire configurations and tokens for correctness.
Questions:
Am I missing any additional configuration steps to enable SQL query logging with SQLModel/SQLAlchemy in Logfire?
Are there any compatibility considerations between Logfire and SQLModel that I should be aware of?
Could there be any issues with the order of instrumentation calls that might prevent SQL logs from being captured?
Additional Information:
Logfire Version: logfire 3.0.0
FastAPI Version: 0.111.1
SQLModel Version: 0.0.19
SQLAlchemy Version: 2.0.36
Environment: Local
The text was updated successfully, but these errors were encountered: