feat(middleware): record HTTP server request duration metrics#13
feat(middleware): record HTTP server request duration metrics#13gandalf-at-lerian wants to merge 2 commits into
Conversation
Implement the OpenTelemetry HTTP semantic-convention server metric http.server.request.duration as a Float64 seconds histogram on the Fiber WithTelemetry middleware. The instrument is created lazily at handler- construction time from the configured MeterProvider and is gated on the presence of a non-nil MetricsFactory so callers without a metrics pipeline are unaffected. Recording happens for every non-excluded request alongside the existing span behavior, attaching low-cardinality OTel attributes derived from the captured Fiber context: http.request.method (heap-copied before c.Next to survive fasthttp buffer recycling), http.route from c.Route().Path (route template, never the raw path), http.response.status_code, and error.type when the handler returns an error or the response is 5xx. Nil telemetry, nil MeterProvider, nil MetricsFactory, excluded routes, and instrument creation errors all silently skip the metric without affecting the request path. Existing span attributes and EndTracingSpans behavior are unchanged. Originally tracked as LerianStudio/lib-commons#462; the middleware now lives in lib-observability after the v5 split, so the change lands here. Tests cover success (200), client error (404), server status (503), handler-returned error, excluded route, nil telemetry, nil MeterProvider, and nil MetricsFactory paths using a real OTel SDK ManualReader. Requested-by: @gauchito91 X-Lerian-Ref: 0x1
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (2)
WalkthroughThe PR adds OpenTelemetry HTTP server request duration metrics to the telemetry middleware. A Float64 histogram named ChangesHTTP Request Duration Metrics
Sequence DiagramsequenceDiagram
participant Request
participant Middleware
participant Handler
participant OTelMeter
Request->>Middleware: HTTP request arrives
Middleware->>Middleware: requestStart ← current time
Middleware->>Handler: c.Next()
Handler-->>Middleware: status code + optional error
Middleware->>OTelMeter: recordHTTPServerDuration(duration, attributes)
OTelMeter->>OTelMeter: record histogram sample
OTelMeter->>OTelMeter: attach http.request.method
OTelMeter->>OTelMeter: attach http.route
OTelMeter->>OTelMeter: attach http.response.status_code
OTelMeter->>OTelMeter: conditionally attach error.type
OTelMeter-->>Request: response sent
Possibly related issues
Comment |
|
Pull requests to main can only come from:
Your source branch: Please change the base branch or create a PR from an allowed branch. |
🔍 PR Validation Summary✅ PR Mergeable — no blocking failures
|
🔒 Security Scan Results —
|
| Stage | Status | Blocking? |
|---|---|---|
| Filesystem Scan | ✅ Clean | — |
| Docker Image Scan | ➖ Skipped | — |
| Docker Hub Health Score | ➖ Skipped | — |
| Pre-release Version Check | ✅ Clean | — |
Trivy
Filesystem Scan
✅ No vulnerabilities or secrets found.
Pre-release Version Check
✅ No unstable version pins found.
📊 Unit Test Coverage Report:
|
| Metric | Value |
|---|---|
| Overall Coverage | 84.5% ✅ PASS |
| Threshold | 80% |
Coverage by Package
| Package | Coverage |
|---|---|
github.com/LerianStudio/lib-observability/assert |
97.9% |
github.com/LerianStudio/lib-observability/constants |
83.3% |
github.com/LerianStudio/lib-observability/log |
94.9% |
github.com/LerianStudio/lib-observability/metrics |
91.4% |
github.com/LerianStudio/lib-observability/middleware |
68.6% |
github.com/LerianStudio/lib-observability/redaction |
95.8% |
github.com/LerianStudio/lib-observability/runtime |
80.4% |
github.com/LerianStudio/lib-observability/tracing |
84.9% |
github.com/LerianStudio/lib-observability/zap |
96.0% |
github.com/LerianStudio/lib-observability |
91.1% |
Generated by Go PR Analysis workflow
|
Found 1 test failure on Blacksmith runners: Failure
|
Summary
http.server.request.durationmetrics to the FiberWithTelemetrymiddleware.http.request.method,http.route,http.response.status_code, anderror.typefor handler errors / 5xx responses.Context
Originally requested through
LerianStudio/lib-commons#462, but the middleware now lives inlib-observabilityafter the lib-commons v5 split, so the implementation lands here.Tests
go test -tags=unit ./middleware/ -count=1go test -tags=unit ./...golangci-lint run ./...Requested by: @gauchito91