Skip to main content

Flask

Send traces to Last9 from a Flask app using OpenTelemetry

Introduction

Flask is a lightweight WSGI web application framework. This comprehensive guide will help you instrument your Flask application with OpenTelemetry and smoothly send the traces to a Last9 cluster. You can also check out the example application on GitHub↗.

Pre-requisites

  1. You have a Flask application
  2. You have signed up for Last9, created a cluster, and obtained the following OTLP credentials from the Integrations page:
    • endpoint
    • auth_header

Install OpenTelemetry packages

To install the required packages, run the following command:

pip install python-dotenv opentelemetry-distro opentelemetry-exporter-otlp

After, installing the required packages, run the following command to install the required instrumentation packages:

opentelemetry-bootstrap -a install
tip

To know more about them, you can check: https://opentelemetry.io/docs/languages/python/libraries/

Set the environment variables

Now, that you've installed all the packages. Run the following command to initialize environment variables:

export OTEL_SERVICE_NAME=flask-app
export OTEL_EXPORTER_OTLP_ENDPOINT=<ENDPOINT>
export OTEL_EXPORTER_OTLP_HEADERS="Authorization=<BASIC_AUTH_HEADER>"
export OTEL_TRACES_EXPORTER=otlp
export OTEL_METRICS_EXPORTER=otlp

Note: Replace <BASIC_AUTH_HEADER> with the URL encoded value of the basic auth header.

Run the application

After setting the environment variables, run the Flask application:

opentelemetry-instrument flask run

Visualize data

After running the Flask app, you can visualize the traces in the Last9's APM dashboard.

image

Troubleshooting

Please get in touch with us on Discord or Email if you have any questions.