OpenTelemetry Support
Using OpenTelemetry SDKs

OTel For Python

13min
in this guide, we will walk you through the process of setting up and using opentelemetry in python you will learn how to instrument a simple application to emit traces, metrics, and logs step 1 prerequisites before diving into opentelemetry, make sure you have installed python 3 on your local machine step 2 example application for this tutorial, we will be using a basic flask application however, opentelemetry python is compatible with other web frameworks like django and fastapi as well feel free to adapt the instructions to your preferred framework step 3 installation let’s set up the environment by creating a new directory and activating a virtual environment mkdir otel getting started cd otel getting started python3 m venv venv source /venv/bin/activate 2\ next, install flask using pip pip install flask 3\ create a file named app py and add the following code to it from random import randint from flask import flask app = flask( name ) @app route("/rolldice") def roll dice() return str(do roll()) def do roll() return randint(1, 6) if name == " main " app run(debug=true) 4\ run the application using the following command flask run p 8080 verify that the application is running by opening http //localhost 8080/rolldice in your web browser step 4 instrumentation to automatically generate telemetry data, we will use the opentelemetry instrument agent start by installing the opentelemetry distro package, which includes the opentelemetry api, sdk, and necessary tools pip install opentelemetry distro 2\ next, run the opentelemetry bootstrap command to install flask instrumentation opentelemetry bootstrap a install step 5 run the instrumented app now, you can run your instrumented app with opentelemetry instrument and have the data printed to the console export otel python logging auto instrumentation enabled=true opentelemetry instrument \ \ traces exporter console \ \ metrics exporter console \ \ logs exporter console \ \ service name dice server \ flask run p 8080 open http //localhost 8080/rolldice in your web browser and reload the page a few times you should see the generated spans printed in the console, indicating the lifetime of a request to the /rolldice route step 6 add manual instrumentation to automatic instrumentation while automatic instrumentation captures telemetry at the system edges, it doesn’t capture what’s happening within your application to achieve that, you need to add manual instrumentation let’s link it with the automatic instrumentation we set up earlier traces modify app py to include code that initializes a tracer and creates a trace as a child of the automatically generated one from opentelemetry import trace from random import randint from flask import flask tracer = trace get tracer(“diceroller tracer”) app = flask( name ) @app route(“/rolldice”) def roll dice() return str(do roll()) def do roll() with tracer start as current span(“do roll”) as rollspan res = randint(1, 6) rollspan set attribute(“roll value”, res) return res 2\ run the app again opentelemetry instrument \\ \ traces exporter console \\ \ metrics exporter console \\ flask run p 8080 when you send a request to the server, you’ll see two spans in the trace emitted to the console, with the parent span being the automatically created one metrics modify app py to include code that initializes a meter and uses it to create a counter instrument from opentelemetry import trace from opentelemetry import metrics from random import randint from flask import flask tracer = trace get tracer(“diceroller tracer”) meter = metrics get meter(“diceroller meter”) roll counter = meter create counter( “roll counter”, description=”the number of rolls by roll value”, ) app = flask( name ) @app route(“/rolldice”) def roll dice() return str(do roll()) def do roll() with tracer start as current span(“do roll”) as rollspan res = randint(1, 6) rollspan set attribute(“roll value”, res) roll counter add(1, {“roll value” res}) return res 2\ run the app again opentelemetry instrument \\ \ traces exporter console \\ \ metrics exporter console \\ flask run p 8080 when you send a request to the server, you’ll see the roll counter metric emitted to the console, counting the number of rolls for each roll value step 7 sending telemetry to an opentelemetry collector in most production deployments, it’s beneficial to use an opentelemetry collector follow these steps to configure and run a local collector create a file named otel collector config yaml in the /tmp/ directory and save the following configuration code to it receivers otlp protocols grpc exporters logging loglevel debug otlphttp endpoint https //otel kloudmate com 4318 headers authorization \<your km secret key> processors batch send batch size 5000 timeout 10s service pipelines traces receivers \[otlp] exporters \[logging, otlphttp] processors \[batch] metrics receivers \[otlp] exporters \[logging, otlphttp] processors \[batch] the otlphttp exporter in config yml , along with the kloudmate api key as the authorization header will send the data from the collector to kloudmate 2\ run the following docker command to start the collector using the configuration file docker run p 4317 4317 \\ v /tmp/otel collector config yaml /etc/otel collector config yaml \ otel/opentelemetry collector\ latest \\ \ config=/etc/otel collector config yaml 3\ modify the command to export spans and metrics via otlp by installing the otlp exporter package pip install opentelemetry exporter otlp 4\ run the application again, but this time it will export telemetry data to the collector via otlp opentelemetry instrument flask run p 8080 by default, the application will export traces and metrics over otlp/grpc to the collector running on localhost 4317 conclusion remember, opentelemetry offers various options for automatic instrumentation, manual instrumentation, and exporting telemetry data explore the official documentation to delve deeper into these topics and further enhance your observability capabilities source url for the example application https //opentelemetry io/docs/instrumentation/python/getting started/