Skip to content

Commit 0844061

Browse files
author
Zeidan, Omar
committed
Add quantiles to request metrics, correct metric postfix
...the new summary collector will expose the metrics with three names: http_server_requests_seconds(_sum|_count)?, making the former counter metric redundant and making the metric names more consistent with conventions. Leaving in a "legacy" metric collector to avoid breaking existing monitoring and alerting setups that depend on the metric with postfix "_sum_sum".
1 parent 87d35ed commit 0844061

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

web/middleware/requestmetrics/requestmetrics.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,49 @@ import (
1111
)
1212

1313
var (
14-
RequestCounterName = "http_server_requests_seconds_count"
15-
RequestDurationName = "http_server_requests_seconds_sum"
14+
requestSummaryName = "http_server_requests_seconds"
15+
requestSummaryLegacyName = "http_server_requests_seconds_sum"
1616

17-
reqs *prometheus.CounterVec
18-
latency *prometheus.SummaryVec
17+
requestSummary *prometheus.SummaryVec
18+
requestSummaryLegacy *prometheus.SummaryVec
1919
)
2020

2121
func Setup() {
22-
reqs = prometheus.NewCounterVec(
23-
prometheus.CounterOpts{
24-
Name: RequestCounterName,
25-
Help: "Number of incoming HTTP requests processed, partitioned by status code, method and HTTP path (grouped by patterns).",
22+
requestSummary = prometheus.NewSummaryVec(
23+
prometheus.SummaryOpts{
24+
Name: requestSummaryName,
25+
Help: "Request counts, durations, accumulated and in quantiles, partitioned by method, \"outcome\", status code, and HTTP path (grouped by patterns).",
26+
Objectives: map[float64]float64{0.5: 0.05, 0.95: 0.01, 0.99: 0.001},
2627
},
2728
[]string{"method", "outcome", "status", "uri"},
2829
)
29-
prometheus.MustRegister(reqs)
3030

31-
latency = prometheus.NewSummaryVec(
31+
prometheus.MustRegister(requestSummaryLegacy)
32+
33+
requestSummaryLegacy = prometheus.NewSummaryVec(
3234
prometheus.SummaryOpts{
33-
Name: RequestDurationName,
34-
Help: "How long it took to process requests, partitioned by status code, method and HTTP path (grouped by patterns).",
35+
Name: requestSummaryLegacyName,
36+
Help: "(Legacy, replaced by \"http_server_requests_seconds(_sum|_count)?\") Accumulated request durations and counts partitioned by method, \"outcome\", status code, and HTTP path (grouped by patterns).",
3537
},
3638
[]string{"method", "outcome", "status", "uri"},
3739
)
38-
prometheus.MustRegister(latency)
40+
41+
prometheus.MustRegister(requestSummary)
3942
}
4043

4144
func RecordRequestMetrics(next http.Handler) http.Handler {
4245
fn := func(w http.ResponseWriter, r *http.Request) {
4346
start := time.Now()
4447
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
4548
next.ServeHTTP(ww, r)
49+
duration := time.Since(start)
4650

4751
rctx := chi.RouteContext(r.Context())
4852
routePattern := strings.Join(rctx.RoutePatterns, "")
4953
routePattern = strings.Replace(routePattern, "/*/", "/", -1)
5054

51-
reqs.WithLabelValues(r.Method, outcome(ww.Status()), fmt.Sprintf("%d", ww.Status()), routePattern).Inc()
52-
latency.WithLabelValues(r.Method, outcome(ww.Status()), fmt.Sprintf("%d", ww.Status()), routePattern).Observe(float64(time.Since(start).Microseconds()) / 1000000)
55+
requestSummary.WithLabelValues(r.Method, outcome(ww.Status()), fmt.Sprintf("%d", ww.Status()), routePattern).Observe(duration.Seconds())
56+
requestSummaryLegacy.WithLabelValues(r.Method, outcome(ww.Status()), fmt.Sprintf("%d", ww.Status()), routePattern).Observe(duration.Seconds())
5357
}
5458
return http.HandlerFunc(fn)
5559
}

0 commit comments

Comments
 (0)