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 distributed tracingopen in new window (including logs and errors) and metricsopen in new window.

Otel specifies how to collect and export telemetry data in a vendor agnostic way. With OpenTelemetry, you can instrumentopen in new window 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.

Installation

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?

Need a backend to store telemetry data collected by OpenTelemetry instrumentations?

Uptrace is an open source APM 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, instrument more operations, for example, database queries and network calls. You can also learn about OpenTelemetry Python Tracing API to create your own instrumentations.

See also:

Last Updated: