Instrument a Python App
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
Section titled “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
Section titled “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
2. Create a Virtual Environment and Activate It
3. Install the Framework
4. Create the Application
- Create a file named
app.pywith the following code:
- Create a Django project and app:
- Add the App to INSTALLED_APPS
Edit
mysite/settings.pyand add'hello', to theINSTALLED_APPSlist.
- Create a simple view in
hello/views.py:
- Create a URL mapping in
hello/urls.py(new file):
- And include it in
mysite/urls.py:
5. Run the Application
Verify that the application is running by visiting localhost:8080/rolldice in your browser.
Run Migrations and Start Server
You can now access the app at localhost:8000.
Step 3: Installation & Setup
Section titled “Step 3: Installation & Setup”Run the following commands in your project environment to install the essential OpenTelemetry packages and exporters:
opentelemetry-distroincludes OpenTelemetry API, SDK, and tools.opentelemetry-bootstrap -a installauto-installs instrumentation libraries for detected dependencies such as Flask or Django.
Step 4: Configure Environment Variable
Section titled “Step 4: Configure Environment Variable”Set the following environment variables to configure KloudMate OTLP endpoint and service metadata:
Step 5: Run Your Application with Auto-Instrumentation
Section titled “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.
The traces and metrics generated will be sent automatically to KloudMate.
Step 6: Sending Telemetry to an OpenTelemetry Collector
Section titled “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:
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.
Python Metrics
Section titled “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
Section titled “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: