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?
OpenTelemetry is a vendor-neutral standard on how to collect telemetry data for applications and their supporting infrastructures. OpenTelemetry was created via the merger of OpenCensus and OpenTracing projects.
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__) FlaskInstrumentor().instrument_app(app)
See example from details.
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) SQLAlchemyInstrumentor().instrument(engine=db.engine)
See Instrumenting SQLAlchemy with OpenTelemetry for details.
What is Uptrace?
Once your app is instrumented, you need a backend platform to store observability data collected by OpenTelemetry.
Uptrace is an open source APM 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 started with Uptrace by downloading a DEB/RPM package or a pre-compiled Go binary.