From 8cc2b6c472a5d4cfc3d9fe37c5a23f7239faf707 Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Mon, 22 Aug 2022 01:28:46 -0400 Subject: [PATCH] Fix double-counting bug in promhttp.InstrumentRoundTripperCounter (#1118) Signed-off-by: Dave Henderson Signed-off-by: Dave Henderson --- prometheus/promhttp/instrument_client.go | 1 - prometheus/promhttp/instrument_client_test.go | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/prometheus/promhttp/instrument_client.go b/prometheus/promhttp/instrument_client.go index 097aff2df..57bb5f945 100644 --- a/prometheus/promhttp/instrument_client.go +++ b/prometheus/promhttp/instrument_client.go @@ -78,7 +78,6 @@ func InstrumentRoundTripperCounter(counter *prometheus.CounterVec, next http.Rou 1, rtOpts.getExemplarFn(r.Context()), ) - counter.With(labels(code, method, r.Method, resp.StatusCode, rtOpts.extraMethods...)).Inc() } return resp, err } diff --git a/prometheus/promhttp/instrument_client_test.go b/prometheus/promhttp/instrument_client_test.go index 98667e8f1..ce7c4da54 100644 --- a/prometheus/promhttp/instrument_client_test.go +++ b/prometheus/promhttp/instrument_client_test.go @@ -25,6 +25,7 @@ import ( "time" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/testutil" dto "github.com/prometheus/client_model/go" "google.golang.org/protobuf/proto" @@ -250,6 +251,19 @@ func TestClientMiddlewareAPI_WithRequestContext(t *testing.T) { t.Errorf("metric family %s must not be empty", mf.GetName()) } } + + // make sure counters aren't double-incremented (see #1117) + expected := ` + # HELP client_api_requests_total A counter for requests from the wrapped client. + # TYPE client_api_requests_total counter + client_api_requests_total{code="200",method="get"} 1 + ` + + if err := testutil.GatherAndCompare(reg, strings.NewReader(expected), + "client_api_requests_total", + ); err != nil { + t.Fatal(err) + } } func TestClientMiddlewareAPIWithRequestContextTimeout(t *testing.T) {