feat: skip tracing for health and metrics endpoints
Health check and metrics endpoints are frequently polled and don't provide meaningful trace data. This change skips OpenTelemetry span creation for: - /health/* (liveness, readiness checks) - /metrics (Prometheus metrics) These endpoints still record Prometheus metrics (request count, latency, in-flight requests) but no longer create trace spans, reducing tracing noise and storage costs. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -66,7 +66,11 @@ class ObservabilityMiddleware(BaseHTTPMiddleware):
|
|||||||
# Record start time
|
# Record start time
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
|
|
||||||
|
# Skip tracing for health/metrics endpoints to reduce noise
|
||||||
|
should_trace = not (path.startswith("/health/") or path == "/metrics")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
if should_trace:
|
||||||
# Create span for request (OpenTelemetry auto-instrumentation will create parent span)
|
# Create span for request (OpenTelemetry auto-instrumentation will create parent span)
|
||||||
with trace_operation(
|
with trace_operation(
|
||||||
f"HTTP {method} {endpoint}",
|
f"HTTP {method} {endpoint}",
|
||||||
@@ -92,6 +96,20 @@ class ObservabilityMiddleware(BaseHTTPMiddleware):
|
|||||||
duration=duration,
|
duration=duration,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
return response
|
||||||
|
else:
|
||||||
|
# No tracing for health/metrics endpoints, but still record metrics
|
||||||
|
response = await call_next(request)
|
||||||
|
|
||||||
|
# Record metrics
|
||||||
|
duration = time.time() - start_time
|
||||||
|
self._record_request_metrics(
|
||||||
|
method=method,
|
||||||
|
endpoint=endpoint,
|
||||||
|
status_code=response.status_code,
|
||||||
|
duration=duration,
|
||||||
|
)
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
except Exception:
|
except Exception:
|
||||||
|
|||||||
Reference in New Issue
Block a user