|
| 1 | +--- |
| 2 | +title: OpenTelemetry Integration |
| 3 | +description: Emit traces and spans from Pharox via the OpenTelemetry SDK. |
| 4 | +--- |
| 5 | +# OpenTelemetry Integration |
| 6 | + |
| 7 | +Pharox ships with Prometheus metrics out of the box, but you can layer |
| 8 | +OpenTelemetry traces on top by hooking into the acquire/release callbacks. |
| 9 | + |
| 10 | +## Prerequisites |
| 11 | + |
| 12 | +```bash |
| 13 | +pip install opentelemetry-sdk opentelemetry-exporter-otlp-proto-grpc |
| 14 | +``` |
| 15 | + |
| 16 | +## Basic Setup |
| 17 | + |
| 18 | +```python |
| 19 | +from opentelemetry import trace |
| 20 | +from opentelemetry.sdk.trace import TracerProvider |
| 21 | +from opentelemetry.sdk.trace.export import BatchSpanProcessor |
| 22 | +from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter |
| 23 | + |
| 24 | +provider = TracerProvider() |
| 25 | +provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter())) |
| 26 | +trace.set_tracer_provider(provider) |
| 27 | + |
| 28 | +tracer = trace.get_tracer("pharox") |
| 29 | +``` |
| 30 | + |
| 31 | +## Acquire Callback |
| 32 | + |
| 33 | +Register a callback that opens a span for every proxy acquisition: |
| 34 | + |
| 35 | +```python |
| 36 | +from pharox import ProxyManager |
| 37 | +from pharox.models import AcquireEventPayload |
| 38 | + |
| 39 | +def otel_acquire_callback(payload: AcquireEventPayload) -> None: |
| 40 | + status = "hit" if payload.lease else "miss" |
| 41 | + with tracer.start_as_current_span("pharox.acquire") as span: |
| 42 | + span.set_attribute("pharox.pool", payload.pool_name) |
| 43 | + span.set_attribute("pharox.consumer", payload.consumer_name or "") |
| 44 | + span.set_attribute("pharox.status", status) |
| 45 | + span.set_attribute("pharox.duration_ms", payload.duration_ms) |
| 46 | + |
| 47 | +manager = ProxyManager(storage=...) |
| 48 | +manager.register_acquire_callback(otel_acquire_callback) |
| 49 | +``` |
| 50 | + |
| 51 | +## Release Callback |
| 52 | + |
| 53 | +```python |
| 54 | +from pharox.models import ReleaseEventPayload |
| 55 | + |
| 56 | +def otel_release_callback(payload: ReleaseEventPayload) -> None: |
| 57 | + with tracer.start_as_current_span("pharox.release") as span: |
| 58 | + span.set_attribute("pharox.pool", payload.pool_name) |
| 59 | + span.set_attribute("pharox.lease_duration_ms", payload.lease_duration_ms) |
| 60 | + |
| 61 | +manager.register_release_callback(otel_release_callback) |
| 62 | +``` |
| 63 | + |
| 64 | +## Propagating an Existing Span Context |
| 65 | + |
| 66 | +If the calling code already holds an active span, the callbacks run inside that |
| 67 | +context automatically — the `with tracer.start_as_current_span(...)` call will |
| 68 | +create a child span of whatever is current in the calling thread. |
| 69 | + |
| 70 | +For explicit propagation across thread boundaries (e.g. when using |
| 71 | +`acquire_proxy_with_retry_async` via `asyncio.to_thread`): |
| 72 | + |
| 73 | +```python |
| 74 | +from opentelemetry.context import attach, detach |
| 75 | +from opentelemetry.propagate import extract |
| 76 | + |
| 77 | +def otel_acquire_callback(payload: AcquireEventPayload) -> None: |
| 78 | + # carrier is set by your async caller before handing off to the thread |
| 79 | + token = attach(extract(carrier={})) |
| 80 | + try: |
| 81 | + with tracer.start_as_current_span("pharox.acquire"): |
| 82 | + ... |
| 83 | + finally: |
| 84 | + detach(token) |
| 85 | +``` |
| 86 | + |
| 87 | +## Notes |
| 88 | + |
| 89 | +- Pharox callbacks are **synchronous**. If your OTLP exporter is async-only, |
| 90 | + wrap the export in `asyncio.run_coroutine_threadsafe` or use a synchronous |
| 91 | + exporter. |
| 92 | +- For high-throughput workers, prefer the `BatchSpanProcessor` over |
| 93 | + `SimpleSpanProcessor` to avoid blocking the acquire path. |
| 94 | +- Combine with `PrometheusMetricsRecorder` — both can be registered as callbacks |
| 95 | + on the same manager instance. |
0 commit comments