OpenTelemetry guide for Flask and SQLAlchemy

In this article, you will learn how to use OpenTelemetry with Uptrace to monitor Flask and SQLAlchemy performance.

Uptrace

What is tracing?

Distributed tracingFlask and SQLAlchemy - 图2open in new window allows to observe requests as they propagate through distributed systems, especially those built using a microservices architecture.

Tracing provides insights into your app performance along with any errors and logs. You immediately see what conditions cause errors and how particular error affects app performance.

Flask and SQLAlchemy - 图3

Using tracing, you can break down requests into spansFlask and SQLAlchemy - 图4open in new window. Span is an operation (unit of work) your app performs handling a request, for example, a database query or a network call.

Trace is a tree of spans that shows the path that a request makes through an app. Root span is the first span in a trace.

Flask and SQLAlchemy - 图5

To learn more about tracing, see Distributed tracing using OpenTelemetryFlask and SQLAlchemy - 图6open in new window.

What is OpenTelemetry?

OpenTelemetryFlask and SQLAlchemy - 图7open in new window is an open-source observability framework for distributed tracingFlask and SQLAlchemy - 图8open in new window (including logs and errors) and metricsFlask and SQLAlchemy - 图9open in new window.

Otel allows developers to collect and export telemetry data in a vendor agnostic way. With OpenTelemetry, you can instrumentFlask and SQLAlchemy - 图10open in new window your application once and then add or change vendors without changing the instrumentation, for example, here is a list popular DataDog alternativesFlask and SQLAlchemy - 图11open in new window that support OpenTelemetry.

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

Creating spans

You can create a span using OpenTelemetry Python APIFlask and SQLAlchemy - 图12open in new window like this:

  1. from opentelemetry import trace
  2. tracer = trace.get_tracer("app_or_package_name", "1.0.0")
  3. def some_func(**kwargs):
  4. with tracer.start_as_current_span("some-func") as span:
  5. // the code you are measuring

What is Uptrace?

UptraceFlask and SQLAlchemy - 图13open in new window is an open source DataDog alternativeFlask and SQLAlchemy - 图14open in new window 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 install UptraceFlask and SQLAlchemy - 图15open in new window by downloading a DEB/RPM package or a pre-compiled binary.

Example application

In this tutorial, you will be instrumenting a toy appFlask and SQLAlchemy - 图16open in new window that uses Flask and SQLAlchemy database client. You can retrieve the source code with the following command:

  1. git clone git@github.com:uptrace/uptrace.git
  2. cd example/flask

The app comes with some dependencies that you can install with:

  1. pip install -r requirements.txt

Configuring OpenTelemetry

Uptrace provides OpenTelemetry PythonFlask and SQLAlchemy - 图17open in new window distro that configures OpenTelemetry SDK for you. To install the distro:

  1. pip install uptrace

Then you need to initialize the distro whenever you app is started, for example, in manage.py:

  1. # manage.py
  2. import uptrace
  3. def main():
  4. uptrace.configure_opentelemetry(
  5. # Copy DSN here or use UPTRACE_DSN env var.
  6. # dsn="",
  7. service_name="myservice",
  8. service_version="v1.0.0",
  9. )
  10. # other code

See documentationFlask and SQLAlchemy - 图18open in new window for details.

Instrumenting Flask app

To instrument Flask app, you need to install a correspoding OpenTelemetry Flask instrumentationFlask and SQLAlchemy - 图19open in new window:

  1. pip install opentelemetry-instrumentation-flask

Then you can instrument the Flask app:

  1. from opentelemetry.instrumentation.flask import FlaskInstrumentor
  2. app = Flask(__name__)
  3. FlaskInstrumentor().instrument_app(app)

Instrumenting SQLAlchemy

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

  1. pip install opentelemetry-instrumentation-sqlalchemy

Then instrument the db engine:

  1. from flask_sqlalchemy import SQLAlchemy
  2. from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor
  3. app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///:memory:"
  4. db = SQLAlchemy(app)
  5. SQLAlchemyInstrumentor().instrument(engine=db.engine)

Running the example

You can start Uptrace backend with a single command using Docker exampleFlask and SQLAlchemy - 图20open in new window:

  1. docker-compose up -d

And then start the appFlask and SQLAlchemy - 图21open in new window passing Uptrace DSN as an env variable:

  1. export UPTRACE_DSN=http://project2_secret_token@localhost:14317/2
  2. python3 main.py

The app should be serving requests on http://localhost:8000 and should render a link to Uptrace UI. After opening the link, you should see this:

Flask OpenTelemetry

What’s next?

Next, you can learn about OpenTelemetry Python APIFlask and SQLAlchemy - 图23open in new window to create your own instrumentations or browse existing instrumentationsFlask and SQLAlchemy - 图24open in new window provided by the community.