OTel For Python
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:
2. Next, install Flask using pip:
3. Create a file named app.py and add the following code to it:
4. Run the application using the following command:
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:
2. Next, run the opentelemetry-bootstrap command to install Flask instrumentation:
Step 5: Run the Instrumented App
Now, you can run your instrumented app with opentelemetry-instrument and have the data printed to the console:
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.pyto include code that initializes a tracer and creates a trace as a child of the automatically generated one:
2. Run the app again:
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.pyto include code that initializes a meter and uses it to create a counter instrument:
2. Run the app again:
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:
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:
3. Modify the command to export spans and metrics via OTLP by installing the OTLP exporter package:
4. Run the application again, but this time it will export telemetry data to the collector via OTLP:
By default, the application will export traces and metrics over OTLP/gRPC to the collector running on localhost:4317.
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/