APM
APM Instrumentation
Python
22 min
this guide walks you through setting up opentelemetry auto instrumentation for python applications and configuring kloudmate to collect telemetry data (traces, metrics, logs) without modifying your application code step 1 prerequisites python 3 6 or higher is installed on your machine kloudmate workspace api key (used to authorize telemetry ingestion) step 2 example application for demonstration, we'll use a simple flask and django application you can adapt the same instructions for any other supported framework 1\ create a project directory flask mkdir otel getting started cd otel getting started django mkdir hello django cd hello django 2\ create a virtual environment and activate it flask python3 m venv venv source /venv/bin/activate django python3 m venv venv source /venv/bin/activate 3\ install the framework flask pip install flask django pip install django 4\ create the application flask create a file named app py with the following code 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) django create a django project and app django admin startproject mysite python manage py startapp hello add the app to installed apps edit mysite/settings py and add 'hello' , to the installed apps list installed apps = \[ 'django contrib admin', 'django contrib auth', 'hello', ] create a simple view in hello/views py from django http import httpresponse def hello world(request) return httpresponse("hello, world!") create a url mapping in hello/urls py (new file) from django urls import path from views import hello world urlpatterns = \[ path('', hello world), ] and include it in mysite/urls py from django contrib import admin from django urls import path, include urlpatterns = \[ path('admin/', admin site urls), path('', include('hello urls')), ] 5\ run the application flask flask run p 8080 verify that the application is running by visiting http //localhost 8080/rolldice in your browser django run migrations and start server python manage py migrate python manage py runserver 127 0 0 1 8000 you can now access the app at http //localhost 8000/ http //localhost 8000/ step 3 installation & setup run the following commands in your project environment to install the essential opentelemetry packages and exporters pip install opentelemetry distro opentelemetry exporter otlp opentelemetry bootstrap a install opentelemetry distro includes opentelemetry api, sdk, and tools opentelemetry bootstrap a install auto installs instrumentation libraries for detected dependencies such as flask or django step 4 configure environment variable set the following environment variables to configure kloudmate otlp endpoint and service metadata export otel exporter otlp endpoint=https //otel kloudmate com 4318 export otel exporter otlp headers="authorization=\<private key>" export otel service name=\<service name> replace \<private key> with your kloudmate workspace api key and \<service name> with your application name step 5 run your application with auto instrumentation run your application using the opentelemetry cli wrapper to enable telemetry collection this enables automatic telemetry collection from supported libraries flask opentelemetry instrument traces exporter otlp metrics exporter otlp python app py django export django settings module=mysite settings opentelemetry instrument traces exporter otlp metrics exporter otlp exporter otlp protocol http/protobuf python manage py runserver noreload ensure you set django settings module appropriately for your django app in this example, the django project is named mysite settings the traces and metrics generated will be sent automatically to kloudmate step 6 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 endpoint 0 0 0 0 4317 exporters debug verbosity detailed 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 \[debug, otlphttp] processors \[batch] metrics receivers \[otlp] exporters \[debug, otlphttp] processors \[batch] logs receivers \[otlp] exporters \[debug, 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 python metrics metric name description cpython gc collections the number of times a generation was collected since interpreter start cpython gc generation value of the garbage collector collection generation cpython gc collected objects the total number of objects collected inside a generation since interpreter start cpython gc uncollectable objects the total number of objects which were found to be uncollectable inside a generation since interpreter start 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 for more details and advanced configurations, refer to the official opentelemetry documentation zero code instrumentation for python getting started with opentelemetry in python