diff --git a/.changelog/692.bugfix.md b/.changelog/692.bugfix.md new file mode 100644 index 000000000..7f5fd3be5 --- /dev/null +++ b/.changelog/692.bugfix.md @@ -0,0 +1 @@ +metrics: dont create new 'api_request_latencies_buckets' for 404s diff --git a/api/middleware.go b/api/middleware.go index 759dc2641..d63c66e03 100644 --- a/api/middleware.go +++ b/api/middleware.go @@ -65,7 +65,6 @@ func MetricsMiddleware(m metrics.RequestMetrics, logger log.Logger) func(next ht ) t := time.Now() metricName := normalizeEndpoint(r.URL.Path) - timer := m.RequestLatencies(metricName) // Serve the request. next.ServeHTTP(w, r.WithContext( @@ -74,14 +73,6 @@ func MetricsMiddleware(m metrics.RequestMetrics, logger log.Logger) func(next ht // Observe results and log/record them. httpStatus := reflect.ValueOf(w).Elem().FieldByName("status").Int() - if httpStatus < 400 || httpStatus >= 500 { - // Only observe the request timing if it's not going to be - // ignored (see below). - // Timers are reported to Prometheus only after they're - // observed, so it's OK to create it above and then ignore it - // like we're doing here if we don't want to use it. - timer.ObserveDuration() - } latency := time.Since(t) logger.Info("ending request", "query_path", r.URL.Path, @@ -103,6 +94,7 @@ func MetricsMiddleware(m metrics.RequestMetrics, logger log.Logger) func(next ht metricName = "ignored" } m.RequestCounts(metricName, statusTxt).Inc() + m.RequestLatencies(metricName).Observe(latency.Seconds()) }) } } diff --git a/metrics/requests.go b/metrics/requests.go index dbd404293..e79f3c480 100644 --- a/metrics/requests.go +++ b/metrics/requests.go @@ -48,7 +48,9 @@ func (m *RequestMetrics) RequestCounts(endpoint, status string) prometheus.Count return m.requestCounts.WithLabelValues(endpoint, status) } -// RequestLatencies creates a new latency timer for the provided request endpoint. -func (m *RequestMetrics) RequestLatencies(endpoint string) *prometheus.Timer { - return prometheus.NewTimer(m.requestLatencies.WithLabelValues(endpoint)) +// RequestLatencies fetches the Histogram Observer for the given endpoint +// If it doesn't exist, a new one is created. +// This creates 12 (with DefBuckets) empty tables in the prometheus database. +func (m *RequestMetrics) RequestLatencies(endpoint string) prometheus.Observer { + return m.requestLatencies.WithLabelValues(endpoint) }