OpenTelemetry is an observability framework designed to aid in the generation and collection of application telemetry data such as metrics, logs, and traces.
Automatic instrumentation uses a Python agent that can be attached to any Python application. It dynamically injects bytecode to capture telemetry from many popular libraries and frameworks. This reduces the amount of work required to integrate OpenTelemetry into the application code.
The appropriate Python packages for the automatic instrumentation have already been included in the pyproject.toml.
The agent is highly configurable (see doc). Here’s an example of agent configuration via environment variables:
OTEL_SERVICE_NAME=your-service-name \
OTEL_TRACES_EXPORTER=console,otlp \
OTEL_METRICS_EXPORTER=console \
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=0.0.0.0:4317 \
opentelemetry-instrument \
uvicorn src.main:app --host localhost --port 5002
Here’s an explanation of what each environment variables does:
OTEL_SERVICE_NAMEsets the name of the service associated with your telemetry, and is sent to your Observability backend.OTEL_TRACES_EXPORTERspecifies which traces exporter to use. In this case, traces are being exported toconsole(stdout) and withotlp.OTEL_METRICS_EXPORTERspecifies which metrics exporter to use. In this case, metrics are being exported toconsole(stdout).OTEL_EXPORTER_OTLP_TRACES_ENDPOINTsets the endpoint where telemetry is exported to. If omitted, the defaultCollectorendpoint will be used, which is0.0.0.0:4317for gRPC and0.0.0.0:4318for HTTP.
One of the biggest advantages of using OpenTelemetry is that it is vendor-agnostic. It can export data in multiple formats which you can send to a backend of your choice.
As observability backend we will refer to SigNoz. SigNoz is an open-source APM tool that can be used for both metrics and distributed tracing.
Note Before you install SigNoz, ensure that Docker Compose is installed on your machine.
- In a directory of your choosing, clone the SigNoz repository and
cdinto thesignoz/deploydirectory by entering the following commands:
git clone -b main https://github.com/SigNoz/signoz.git && cd signoz/deploy/
- To install SigNoz, enter the
docker-compose upcommand, specifying the following:
-fand the path to your configuration file-dto run containers in the background.
docker-compose -f docker/clickhouse-setup/docker-compose.yaml up -d
For detailed instructions, you can visit the official documentation here.
When you are done installing SigNoz, you can access the UI at http://<IP-ADDRESS>:3301/, replacing with the <IP address> of the machine where you installed SigNoz.
If you are running SigNoz on your local machine, you should point your browser to http://localhost:3301/.
The application list shown in the dashboard is from a sample app called HOT R.O.D that comes bundled with the SigNoz installation package.
You just need to configure a few environment variables for your OTLP exporters. Environment variables that need to be configured:
service.name- application service name (you can name it as you like).OTEL_EXPORTER_OTLP_ENDPOINT- in this case, IP of the machine where SigNoz is installed.
You need to put these environment variables in the below command:
OTEL_RESOURCE_ATTRIBUTES=service.name=<service_name> OTEL_EXPORTER_OTLP_ENDPOINT="http://<IP of SigNoz>:4317" opentelemetry-instrument uvicorn src.main:app --host localhost --port 5002
If you are running SigNoz on local host, IP of SigNoz can be replaced with localhost in this case. And, for service_name let's use PythonSpecificProvisioner. Hence, the final command becomes:
OTEL_RESOURCE_ATTRIBUTES=service.name=PythonSpecificProvisioner OTEL_EXPORTER_OTLP_ENDPOINT="http://localhost:4317" opentelemetry-instrument uvicorn src.main:app --host localhost --port 5002
You can check if your app is running or not by hitting the endpoint at http://localhost:5002.
If you have installed SigNoz on your local host, then you can access the SigNoz dashboard at http://localhost:3301 to monitor your app for performance metrics.