|
1 | 1 | """ |
2 | 2 | Run all instrumented functions and display their trace outputs. |
3 | 3 |
|
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 |
6 | 19 | """ |
| 20 | +import os |
7 | 21 | import sys |
8 | 22 | from pathlib import Path |
9 | 23 |
|
|
21 | 35 | from src.data_processing.dataframe import dataframe_filter, groupby_mean, dataframe_merge |
22 | 36 | from src.statistics.descriptive import describe, correlation |
23 | 37 |
|
24 | | -# Initialize OpenTelemetry with console exporter |
| 38 | +# Initialize OpenTelemetry with auto-instrumentation |
| 39 | +# Uses environment variables if set, otherwise defaults to console exporter |
25 | 40 | print("=" * 80) |
26 | | -print("Initializing OpenTelemetry...") |
| 41 | +print("Initializing OpenTelemetry with auto-instrumentation...") |
27 | 42 | 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 | + |
28 | 54 | setup_telemetry( |
29 | 55 | service_name="optimize-me", |
30 | 56 | 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) |
32 | 60 | ) |
33 | 61 |
|
34 | 62 | print("\n" + "=" * 80) |
|
149 | 177 | print(f"Result: {corr_result}\n") |
150 | 178 |
|
151 | 179 | print("=" * 80) |
152 | | -print("ALL FUNCTIONS EXECUTED - Check the JSON trace spans above!") |
| 180 | +print("ALL FUNCTIONS EXECUTED") |
153 | 181 | 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") |
159 | 199 | 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() |
161 | 212 |
|
0 commit comments