From b32324cb769f5fc7cf6ff20124164a8e9f9b4049 Mon Sep 17 00:00:00 2001 From: Chris Coutinho Date: Mon, 10 Nov 2025 07:24:27 +0100 Subject: [PATCH] feat: skip tracing for health and metrics endpoints MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../observability/middleware.py | 48 +++++++++++++------ 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/nextcloud_mcp_server/observability/middleware.py b/nextcloud_mcp_server/observability/middleware.py index 9921322..382a5ea 100644 --- a/nextcloud_mcp_server/observability/middleware.py +++ b/nextcloud_mcp_server/observability/middleware.py @@ -66,22 +66,40 @@ class ObservabilityMiddleware(BaseHTTPMiddleware): # Record start time start_time = time.time() - try: - # Create span for request (OpenTelemetry auto-instrumentation will create parent span) - with trace_operation( - f"HTTP {method} {endpoint}", - attributes={ - "http.method": method, - "http.path": path, - "http.scheme": request.url.scheme, - "http.host": request.url.hostname, - }, - ): - # Process request - response = await call_next(request) + # Skip tracing for health/metrics endpoints to reduce noise + should_trace = not (path.startswith("/health/") or path == "/metrics") - # Add response status to span - add_span_attribute("http.status_code", response.status_code) + try: + if should_trace: + # Create span for request (OpenTelemetry auto-instrumentation will create parent span) + with trace_operation( + f"HTTP {method} {endpoint}", + attributes={ + "http.method": method, + "http.path": path, + "http.scheme": request.url.scheme, + "http.host": request.url.hostname, + }, + ): + # Process request + response = await call_next(request) + + # Add response status to span + add_span_attribute("http.status_code", response.status_code) + + # Record metrics + duration = time.time() - start_time + self._record_request_metrics( + method=method, + endpoint=endpoint, + status_code=response.status_code, + 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