This repository provides a clear, step-by-step demonstration of how to integrate your Python (Flask) applications with the KloudMate observability platform.
We demonstrate two primary approaches:
- Zero-Code Auto-Instrumentation: Instantly gain visibility into your HTTP routes, database calls, and external requests without writing a single line of OpenTelemetry code.
- Manual Instrumentation: Enrich your auto-generated traces with deep, business-specific custom spans, attributes, and span events (demonstrated via an E-Commerce checkout scenario called
K-Commerce).
- Python: 3.8 or higher.
- KloudMate Account: You will need your Workspace API Key to send telemetry data to your dashboard. (You can find this on your KloudMate dashboard under Settings -> Workspaces -> Click on API Keys on your desired Workspace)
-
Install dependencies:
pip install -r requirements.txt
-
Configure KloudMate Telemetry Exporter: Copy the example environment file and add your KloudMate API Key.
cp .env.example .env
Open
.envand configure your API key and service name you want:# .env file OTEL_EXPORTER_OTLP_ENDPOINT=https://otel.kloudmate.com:4318 OTEL_EXPORTER_OTLP_HEADERS=Authorization=YOUR_KLOUDMATE_API_KEY OTEL_SERVICE_NAME=otel-python-example
The fastest way to get started with KloudMate. By running your existing application through the opentelemetry-instrument wrapper, OpenTelemetry automatically injects itself into standard Python libraries (like Flask, Requests, SQLAlchemy, etc.) and generates traces.
Target File: app_auto.py (Notice this file imports no OpenTelemetry libraries).
Start the auto-instrumented application:
./run_auto.shcurl http://localhost:5000/api/users: Simulates a standard database delay. Auto-instrumentation will capture the exact duration of the request automatically.curl http://localhost:5000/api/computation: Simulates heavy CPU load.
Outcome: Check your KloudMate dashboard to see beautiful traces tracking the entry and exit of your HTTP requests instantly.
While auto-instrumentation is powerful, distributed architectures often require deeper insights into custom business logic. app_manual.py demonstrates how to layer manual tracing over auto-instrumentation.
Target File: app_manual.py
The /checkout endpoint in this application simulates a complex K-Commerce checkout pipeline:
inventory.check: A custom span tracking inventory, which may throw a simulated "Out of Stock" exception.payment.process: Adds rich custom attributes (payment.amount,payment.gateway) and milestones via Span Events (payment.gateway.initiated).logistics.schedule: Generates a mock tracking number appended to the trace.
By leveraging span.set_attribute(), span.add_event(), and span.record_exception(), you gain immense filtering textuality inside the KloudMate dashboard.
Start the manually instrumented application:
./run_manual.shTrigger the stochastic K-Commerce scenario (which has built-in simulated failure rates to demonstrate OpenTelemetry error reporting):
curl http://localhost:5000/checkoutOutcome: In KloudMate, you will see a detailed, nested trace starting from the HTTP POST route, delving down into specific operations like inventory.check and payment.process, complete with their associated metadata and internal event timelines.
- Log in to your KloudMate Dashboard.
- Navigate to the APM / Services Module section.
- Select the service name
otel-python-example(or whatever you setOTEL_SERVICE_NAMEto in your.envfile). - Alternatively you can view individual traces emitted by your application, by clicking on Traces Module.
- Click into individual traces to view request latency, custom attributes (e.g.,
user.id,payment.gateway), and application errors directly mapped to the source code exceptions.
Made with 🧡 by the KloudMate Team