Monitor Flask performance with OpenTelemetry

Flask is a small and lightweight Python web framework that provides useful tools and features that make creating web applications in Python easier

In this article you will learn how to monitor and optimize Flask 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.

Flask instrumentation

To instrument a Flask app, you need to install a correspoding Flask OpenTelemetry instrumentation:

pip install opentelemetry-instrumentation-flask


Then you can instrument the Flask app:

from opentelemetry.instrumentation.flask import FlaskInstrumentor

app = Flask(__name__)

See exampleopen in new window from details.

Instrumenting SQLAlchemy

To instrument SQLAlchemy database client, you need to install a corresponding SQLAlchemy instrumentation:

pip install opentelemetry-instrumentation-sqlalchemy

Then instrument the db engine:

from flask_sqlalchemy import SQLAlchemy
from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor

app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///:memory:"
db = SQLAlchemy(app)

See Instrumenting SQLAlchemy with OpenTelemetry for details.

What is Uptrace?

Uptrace is an open source APMopen in new window for OpenTelemetry that helps developers pinpoint failures and find performance bottlenecks. Uptrace can process billions of spans on a single server and allows to monitor your software at 10x lower cost.

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:

More examples:

See also:

Last Updated: