Monitor SQLAlchemy performance with OpenTelemetry

SQLAlchemy is an Object Relational Mapper for Python that gives application developers the full power and flexibility of SQL.

In this article you will learn how to monitor and optimize SQLAlchemy performance using OpenTelemetry observability framework.

What is OpenTelemetry?

OpenTelemetryopen in new window is an open source and vendor-neutral API for OpenTelemetry tracingopen in new window (including logs and errors) and OpenTelemetry metricsopen in new window.

Otel specifies how to collect and export telemetry data in a vendor agnostic way. With OpenTelemetry, you can instrument your application once and then add or change vendors without changing the instrumentation, for example, many open source tracing toolsopen in new window already support OpenTelemetry.

OpenTelemetry is available for most programming languages and provides interoperability across different languages and environments.

SQLAlchemy instrumentation

To install OpenTelemetry instrumentation for SQLAlchemy:

pip install opentelemetry-instrumentation-sqlalchemy

Usage

To instrument sqlalchemy database client:

from sqlalchemy import create_engine
from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor

engine = create_engine("sqlite:///:memory:")
SQLAlchemyInstrumentor().instrument(engine=engine)

What is Uptrace?

Uptrace is an open source APMopen in new window for OpenTelemetry with an intuitive query builder, rich dashboards, automatic alerts, and integrations for most languages and frameworks.

You can get startedopen in new window with Uptrace by downloading a DEB/RPM package or a pre-compiled Go binary.

What's next

Next, instrumentopen in new window more operations, for example, database queries and network calls. You can also learn about OpenTelemetry Python Tracing APIopen in new window to create your own instrumentations.

Popular instrumentations:

See also:

Last Updated: