Failed to Instrumenting Async and Sync engine
Description
Hello, I have in my FastAPI two sqlalchemy engines one async and one sync. And it fails to instrument the two at the same time. According to the documentation when letting empty, it should instrument the two. But when letting empty it's doesn't instrument nothing, no logs are in logfire interface.
logfire.configure(
token=Config.LOGIFIRE_TOKEN,
service_name=Config.SERVICE_NAME,
service_version=Config.API_VERSION,
environment=Config.APP_ENV,
distributed_tracing=True,
)
logfire.instrument_sqlalchemy()
Above, instrument nothing. When removing and setup the instrument method in contextmanager of FastAPI It instrument nothing too
@asynccontextmanager
async def lifespan(app: FastAPI):
async_engine = await get_async_engine()
engine = get_sync_engine()
logfire.instrument_sqlalchemy()
This only instrument async
@asynccontextmanager
async def lifespan(app: FastAPI):
async_engine = await get_async_engine()
engine = get_sync_engine()
logfire.instrument_sqlalchemy(engine=async_engine)
logfire.instrument_sqlalchemy(engine=engine)
This only instrument sync
@asynccontextmanager
async def lifespan(app: FastAPI):
async_engine = await get_async_engine()
engine = get_sync_engine()
logfire.instrument_sqlalchemy(engine=engine)
logfire.instrument_sqlalchemy(engine=async_engine)
Can you help me understand or if you have any idea
Python, Logfire & OS Versions, related packages (not required)
logfire="3.16.0"
platform="macOS-15.1.1-arm64-arm-64bit-Mach-O"
python="3.13.3 (main, Apr 8 2025, 13:54:08) [Clang 16.0.0 (clang-1600.0.26.6)]"
[related_packages]
requests="2.32.3"
pydantic="2.11.0"
fastapi="0.115.12"
openai="1.78.1"
protobuf="5.29.4"
rich="14.0.0"
executing="2.2.0"
opentelemetry-api="1.33.0"
opentelemetry-exporter-otlp-proto-common="1.33.0"
opentelemetry-exporter-otlp-proto-http="1.33.0"
opentelemetry-instrumentation="0.54b0"
opentelemetry-instrumentation-asgi="0.54b0"
opentelemetry-instrumentation-asyncpg="0.54b0"
opentelemetry-instrumentation-fastapi="0.54b0"
opentelemetry-instrumentation-sqlalchemy="0.54b0"
opentelemetry-proto="1.33.0"
opentelemetry-sdk="1.33.0"
opentelemetry-semantic-conventions="0.54b0"
opentelemetry-util-http="0.54b0"
Please share a complete reproducible script.
Hi, I'm experiencing a very similar issue and I've created a minimal reproducible script as @alexmojaki requested.
I believe this script minimally demonstrates the same underlying problem: logfire.instrument_sqlalchemy(engine=None) does not instrument all engine activity as the documentation suggests. It seems to only capture the initial connect spans.
import logfire
from sqlalchemy import create_engine, text
# 1. Configure Logfire to print spans to the console for easy debugging.
logfire.configure(send_to_logfire=False, console=True)
# 2. Instrument SQLAlchemy, intending to capture all engine activity.
print("--- Instrumenting SQLAlchemy with engine=None ---")
logfire.instrument_sqlalchemy(engine=None)
print("-" * 50)
# 3. Create two separate in-memory SQLite engines.
print("--- Creating two separate database engines ---")
engine1 = create_engine("sqlite:///:memory:")
engine2 = create_engine("sqlite:///:memory:")
print("Engines created.")
print("-" * 50)
# 4. Perform operations on the first engine.
print("--- Performing operations on engine1 ---")
with engine1.connect() as connection1:
print("Creating table 'users' on engine1...")
connection1.execute(text("CREATE TABLE users (id INT, name VARCHAR)"))
print("Inserting data into 'users' on engine1...")
connection1.execute(text("INSERT INTO users (id, name) VALUES (1, 'Alice')"))
connection1.commit()
print("Querying data from 'users' on engine1...")
result = connection1.execute(text("SELECT * FROM users")).first()
print(f"Query result from engine1: {result}")
print("-" * 50)
# 5. Perform an operation on the second engine.
print("--- Performing a simple query on engine2 ---")
with engine2.connect() as connection2:
print("Executing 'SELECT 1' on engine2...")
result = connection2.execute(text("SELECT 1")).scalar()
print(f"Query result from engine2: {result}")
print("-" * 50)
Output:
--- Instrumenting SQLAlchemy with engine=None ---
--------------------------------------------------
--- Creating two separate database engines ---
Engines created.
--------------------------------------------------
--- Performing operations on engine1 ---
13:18:42.602 connect
Creating table 'users' on engine1...
Inserting data into 'users' on engine1...
Querying data from 'users' on engine1...
Query result from engine1: (1, 'Alice')
--------------------------------------------------
--- Performing a simple query on engine2 ---
13:18:42.604 connect
Executing 'SELECT 1' on engine2...
Query result from engine2: 1
--------------------------------------------------
Notice that only the connect calls generate spans, while the CREATE, INSERT, and SELECT statements do not.
Looking at the library code, I suspect the issue might be that the underlying opentelemetry-instrumentation-sqlalchemy package doesn't support a None value to instrument all engines, even though the Logfire docstring suggests it does. Also, there is no option to pass a list of engines, which would be another way to instrument multiple engines at the same time (opentelemetry-instrumentation-sqlalchemy seems to receive an "engines" kwarg that the logfire integration does not).
It looks like opentelemetry-instrumentation-sqlalchemy patches sqlalchemy.create_engine, and moving from sqlalchemy import create_engine after logfire.instrument_sqlalchemy(engine=None) fixes things. This should be reported to https://github.com/open-telemetry/opentelemetry-python-contrib/
opentelemetry-instrumentation-sqlalchemy seems to receive an "engines" kwarg that the logfire integration does not
It accepts arbitrary kwargs and passes them on, so logfire.instrument_sqlalchemy(engines=[engine1, engine2]) also works.
@luizfneves404 thanks for opening https://github.com/open-telemetry/opentelemetry-python-contrib/issues/3696.
I think it'd be good for us to at least have docs about the pitfall of not providing an engine.
@alexmojaki I have created a PR for the above #1402
All done, thanks @dhruv-ahuja !