Skip to main content

Python with OpenTelemetry manual instrumention

Tracetest is a testing tool based on OpenTelemetry that allows you to test your distributed application. It allows you to use data from distributed traces generated by OpenTelemetry to validate and assert if your application has the desired behavior defined by your test definitions.

Python app with OpenTelemetry Collector and Tracetest

This is a simple quick start on how to configure a Python app to use OpenTelemetry instrumentation with traces, and Tracetest for enhancing your e2e and integration tests with trace-based testing.


Tracetest Account:

Docker: Have Docker and Docker Compose installed on your machine.

Run This Quckstart Example

The example below is provided as part of the Tracetest project. You can download and run the example by following these steps:

Clone the Tracetest project and go to the Node.js Quickstart with Manual Instrumentation:

git clone
cd tracetest/examples/quick-start-python

Follow these instructions to run the quick start:

  1. Copy the .env.template file to .env.
  2. Log into the Tracetest app.
  3. This example is configured to use the OpenTelemetry Tracing Backend. Ensure the environment you will be utilizing to run this example is also configured to use the OpenTelemetry Tracing Backend by clicking on Settings, Tracing Backend, OpenTelemetry, Save.
  4. Fill out the token and API key details by editing your .env file. You can find these values in the Settings area for your environment.
  5. Run docker compose up -d.
  6. Run tests from the Tracetest Web UI by accessing the app with the URL http://app:8080/.

Follow along with the sections below for a detailed breakdown of what the example you just ran did and how it works.

Project Structure

The quick start Python project is built with Docker Compose and contains the Tracetest Agent and a Python Flask app.

The docker-compose.yaml file in the root directory of the quick start runs the Python app and the Tracetest Agent setup.

Configuring the Python App

The Python app is a simple Flask app, contained in the file.

Configure the .env like shown below.


The OpenTelemetry tracing is contained both as automatic and manual instrumentation. Traces will be sent to the Tracetest Agent. Here's the requirements.txt with OpenTelemetry libraries.


Automatic instrumentation is configured in the Dockerfile and the docker-compose.yaml. The command field will include opentelemetry-instrument.

FROM python:3.10.1-slim
WORKDIR /opt/app
COPY . .
RUN pip install --no-cache-dir -r requirements.txt
RUN opentelemetry-bootstrap -a install

The docker-compose.yaml contains just one service for the Python app. The service is started with the command parameter.

command: opentelemetry-instrument --traces_exporter otlp --service_name app --exporter_otlp_endpoint ${OTEL_EXPORTER_OTLP_TRACES_ENDPOINT} --exporter_otlp_insecure true flask run --host= --port=8080

Manual instrumentation is configured in the

from flask import Flask, request
import json

from opentelemetry import trace
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.sdk.trace.export import ConsoleSpanExporter

provider = TracerProvider()
processor = BatchSpanProcessor(ConsoleSpanExporter())
tracer = trace.get_tracer(__name__)

Running the Python App

To start the full setup, run the following command:

docker compose up -d

There are 3 endpoints in the Flask app. To see manual instrumentation, trigger the "/manual" endpoint. To see the automatic instrumentation, trigger the "/automatic" endpoint respectively.

app = Flask(__name__)

def manual():
with tracer.start_as_current_span(
attributes={ "endpoint": "/manual", "foo": "bar" }
return "App works with a manual instrumentation."

def automatic():
return "App works with automatic instrumentation."

def home():
return "App works."

Run Tracetest Tests

  1. Open Tracetest
  2. Start creating tests! Make sure to use the http://app:8080/automatic or http://app:8080/manual URLs in your test creation.
  3. To trigger tests in the CLI, first install the CLI, configure it, and run a test. From the root of the quick start directory, run:
tracetest configure -t <YOUR_API_TOKEN>
tracetest run test -f ./test-api.yaml

Here's a sample of a failed test run, which happens if you use this selector and assertion pair.


span[tracetest.span.type="http" name="/automatic""/automatic" http.method="GET"]


attr:http.status_code = 200


Learn more

Feel free to check out our examples in GitHub, and join our Slack Community for more info!