From 8762e5160dc6ad92fcd10fd25f5db8e232999be8 Mon Sep 17 00:00:00 2001 From: Sylvain Rabot Date: Tue, 11 Feb 2020 16:40:05 +0100 Subject: [PATCH] Let metrics libs handle the atomicity --- pkg/middlewares/metrics/metrics.go | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/pkg/middlewares/metrics/metrics.go b/pkg/middlewares/metrics/metrics.go index 34d9dccc8..6006119c8 100644 --- a/pkg/middlewares/metrics/metrics.go +++ b/pkg/middlewares/metrics/metrics.go @@ -5,7 +5,6 @@ import ( "net/http" "strconv" "strings" - "sync/atomic" "time" "unicode/utf8" @@ -27,9 +26,6 @@ const ( ) type metricsMiddleware struct { - // Important: Since this int64 field is using sync/atomic, it has to be at the top of the struct due to a bug on 32-bit platform - // See: https://golang.org/pkg/sync/atomic/ for more information - openConns int64 next http.Handler reqsCounter gokitmetrics.Counter reqDurationHistogram gokitmetrics.Histogram @@ -78,23 +74,22 @@ func WrapServiceHandler(ctx context.Context, registry metrics.Registry, serviceN } func (m *metricsMiddleware) ServeHTTP(rw http.ResponseWriter, req *http.Request) { - labels := []string{"method", getMethod(req), "protocol", getRequestProtocol(req)} + var labels []string labels = append(labels, m.baseLabels...) + labels = append(labels, "method", getMethod(req), "protocol", getRequestProtocol(req)) - openConns := atomic.AddInt64(&m.openConns, 1) - m.openConnsGauge.With(labels...).Set(float64(openConns)) - defer func(labelValues []string) { - openConns := atomic.AddInt64(&m.openConns, -1) - m.openConnsGauge.With(labelValues...).Set(float64(openConns)) - }(labels) + m.openConnsGauge.With(labels...).Add(1) + defer m.openConnsGauge.With(labels...).Add(-1) - start := time.Now() recorder := newResponseRecorder(rw) + start := time.Now() m.next.ServeHTTP(recorder, req) + duration := time.Since(start).Seconds() labels = append(labels, "code", strconv.Itoa(recorder.getCode())) + m.reqsCounter.With(labels...).Add(1) - m.reqDurationHistogram.With(labels...).Observe(time.Since(start).Seconds()) + m.reqDurationHistogram.With(labels...).Observe(duration) } func getRequestProtocol(req *http.Request) string {