Skip to content

Commit dcc206f

Browse files
committed
Enhance the OTEL setup, adding docs, adding docker compose for Jaeger and OpenTelemetry Collector
1 parent 55c39d1 commit dcc206f

File tree

10 files changed

+1103
-35
lines changed

10 files changed

+1103
-35
lines changed

.dockerignore

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
__pycache__
2+
*.pyc
3+
*.pyo
4+
*.pyd
5+
.Python
6+
.venv
7+
venv/
8+
ENV/
9+
env/
10+
*.egg-info/
11+
dist/
12+
build/
13+
.pytest_cache/
14+
.coverage
15+
htmlcov/
16+
.DS_Store
17+
*.log
18+

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,23 @@ Watch as Codeflash:
9090
7. Shows impressive speedups (up to 90x in some cases!)
9191

9292

93+
## 📊 OpenTelemetry Integration
94+
95+
This project uses OpenTelemetry auto-instrumentation (the standard pattern used by large open-source projects) for observability.
96+
97+
**Quick Start:**
98+
```bash
99+
# Run with auto-instrumentation (recommended)
100+
opentelemetry-instrument python examples/run_all_traces.py
101+
102+
# Or use Docker for visualization
103+
cd src/telemetry && docker-compose up -d # Start Jaeger
104+
OTEL_EXPORTER_TYPE=otlp python examples/run_all_traces.py
105+
# View traces at: http://localhost:16686
106+
```
107+
108+
For detailed setup instructions, see [src/telemetry/README.md](src/telemetry/README.md).
109+
93110
## 🤝 Need Help?
94111

95112
Join our [Discord community](https://www.codeflash.ai/discord) for support and to connect with other developers who love fast code.

examples/run_all_traces.py

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
"""
22
Run all instrumented functions and display their trace outputs.
33
4-
This script demonstrates all functions decorated with @trace_function
5-
and shows their OpenTelemetry trace spans in the console.
4+
This script demonstrates OpenTelemetry auto-instrumentation.
5+
Functions are automatically instrumented via OpenTelemetry auto-instrumentation
6+
(no decorators needed for libraries like NumPy and Pandas).
7+
8+
Usage:
9+
# With console exporter (default)
10+
python examples/run_all_traces.py
11+
12+
# With OTLP exporter (requires docker-compose up)
13+
# First: cd src/telemetry && docker-compose up -d
14+
OTEL_TRACES_EXPORTER=otlp OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317 python examples/run_all_traces.py
15+
16+
# Or use opentelemetry-instrument command (recommended)
17+
# Note: Set OTEL_TRACES_EXPORTER=console to see traces in console
18+
OTEL_TRACES_EXPORTER=console opentelemetry-instrument python examples/run_all_traces.py
619
"""
20+
import os
721
import sys
822
from pathlib import Path
923

@@ -21,14 +35,28 @@
2135
from src.data_processing.dataframe import dataframe_filter, groupby_mean, dataframe_merge
2236
from src.statistics.descriptive import describe, correlation
2337

24-
# Initialize OpenTelemetry with console exporter
38+
# Initialize OpenTelemetry with auto-instrumentation
39+
# Uses environment variables if set, otherwise defaults to console exporter
2540
print("=" * 80)
26-
print("Initializing OpenTelemetry...")
41+
print("Initializing OpenTelemetry with auto-instrumentation...")
2742
print("=" * 80)
43+
44+
# Enable debug logging if needed
45+
import logging
46+
if os.getenv("OTEL_LOG_LEVEL", "").upper() == "DEBUG":
47+
logging.basicConfig(level=logging.DEBUG)
48+
49+
# Check if running via opentelemetry-instrument (which sets up OTel automatically)
50+
# opentelemetry-instrument uses OTEL_TRACES_EXPORTER, but we also support OTEL_EXPORTER_TYPE for compatibility
51+
exporter_type = os.getenv("OTEL_TRACES_EXPORTER") or os.getenv("OTEL_EXPORTER_TYPE", "console")
52+
exporter_endpoint = os.getenv("OTEL_EXPORTER_OTLP_ENDPOINT", "http://localhost:4317")
53+
2854
setup_telemetry(
2955
service_name="optimize-me",
3056
service_version="0.1.0",
31-
exporter_type="console",
57+
exporter_type=exporter_type,
58+
exporter_endpoint=exporter_endpoint,
59+
use_auto_instrumentation=True, # Use auto-instrumentation (standard pattern)
3260
)
3361

3462
print("\n" + "=" * 80)
@@ -149,13 +177,36 @@
149177
print(f"Result: {corr_result}\n")
150178

151179
print("=" * 80)
152-
print("ALL FUNCTIONS EXECUTED - Check the JSON trace spans above!")
180+
print("ALL FUNCTIONS EXECUTED")
153181
print("=" * 80)
154-
print("\nEach function call above generated a trace span with:")
155-
print(" - Function name")
156-
print(" - Execution time (start_time, end_time)")
157-
print(" - Captured arguments (for gradient_descent: iterations, learning_rate)")
158-
print(" - Status (OK or ERROR)")
182+
183+
if exporter_type == "console":
184+
print("\n✅ Traces printed to console above (JSON format)")
185+
print("\nTo view traces in Jaeger UI:")
186+
print(" 1. Start services: cd src/telemetry && docker-compose up -d")
187+
print(" 2. Set environment: export OTEL_TRACES_EXPORTER=otlp")
188+
print(" 3. Run: python examples/run_all_traces.py")
189+
print(" 4. Open: http://localhost:16686")
190+
elif exporter_type == "otlp":
191+
print(f"\n✅ Traces sent to OTLP endpoint: {exporter_endpoint}")
192+
print("\nView traces in Jaeger UI: http://localhost:16686")
193+
print("(Make sure docker-compose is running: cd src/telemetry && docker-compose up -d)")
194+
195+
print("\nOpenTelemetry auto-instrumentation automatically captured:")
196+
print(" - NumPy operations (array operations)")
197+
print(" - Pandas operations (DataFrame operations)")
198+
print(" - Function execution times")
159199
print(" - Service information (service.name, service.version)")
160-
print("\n")
200+
print("\nFor more details, see: src/telemetry/README.md")
201+
202+
# Force flush spans to ensure they're exported (especially for console exporter)
203+
try:
204+
from opentelemetry import trace
205+
provider = trace.get_tracer_provider()
206+
if hasattr(provider, "force_flush"):
207+
provider.force_flush()
208+
except Exception:
209+
pass
210+
211+
print()
161212

0 commit comments

Comments
 (0)