# 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
