Files
ophion/examples/otel-python/README.md
bigtux 771cf6cf50 feat: Add OpenTelemetry OTLP HTTP receiver
- Add POST /v1/traces endpoint for OTLP JSON trace ingestion
- Convert OTLP spans to internal format and save to PostgreSQL
- Manual JSON parsing (no Go 1.24 dependencies)
- Add Node.js instrumentation example with Express
- Add Python instrumentation example with Flask
- Auto-instrumentation support for both languages
2026-02-06 14:59:29 -03:00

86 lines
2.1 KiB
Markdown

# Python OpenTelemetry Example for Ophion
This example demonstrates how to instrument a Python Flask application with OpenTelemetry and send traces to Ophion.
## Setup
```bash
# Create virtual environment (recommended)
python -m venv venv
source venv/bin/activate # Linux/Mac
# or: venv\Scripts\activate # Windows
# Install dependencies
pip install -r requirements.txt
# Start Ophion server (in another terminal)
# cd ~/projetos_jarvis/ophion && go run cmd/server/main.go
# Run the app
python app.py
```
## Environment Variables
| Variable | Default | Description |
|----------|---------|-------------|
| `OTEL_EXPORTER_OTLP_ENDPOINT` | `http://localhost:8080` | Ophion base URL |
| `OTEL_SERVICE_NAME` | `python-example` | Service name in traces |
| `PORT` | `5000` | App HTTP port |
## Test Endpoints
```bash
# Health check
curl http://localhost:5000/health
# Get all users (generates trace)
curl http://localhost:5000/users
# Get single user
curl http://localhost:5000/users/1
# Create order (complex trace with nested spans)
curl -X POST http://localhost:5000/orders \
-H "Content-Type: application/json" \
-d '{"items": [{"id": 1, "qty": 2}]}'
# Trigger error (error trace)
curl http://localhost:5000/error
# External HTTP call (distributed tracing)
curl http://localhost:5000/external-call
```
## View Traces in Ophion
```bash
# List recent traces
curl http://localhost:8080/api/v1/traces
# Get specific trace
curl http://localhost:8080/api/v1/traces/<trace_id>
```
## Auto-Instrumentation Alternative
You can also use OpenTelemetry's auto-instrumentation:
```bash
# Install auto-instrumentation
pip install opentelemetry-distro
opentelemetry-bootstrap -a install
# Run with auto-instrumentation
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:8080 \
OTEL_SERVICE_NAME=python-example \
opentelemetry-instrument python app.py
```
## How It Works
1. `tracing.py` - Initializes OpenTelemetry SDK with OTLP HTTP exporter
2. `FlaskInstrumentor` auto-captures HTTP requests
3. Manual spans in `app.py` add custom business logic traces
4. All spans are sent to Ophion's `/v1/traces` endpoint in OTLP proto/JSON format