Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: Metric is double-incremented in promhttp.InstrumentRoundTripperCounter #1117

Closed
hairyhenderson opened this issue Aug 18, 2022 · 1 comment · Fixed by #1118
Closed

bug: Metric is double-incremented in promhttp.InstrumentRoundTripperCounter #1117

hairyhenderson opened this issue Aug 18, 2022 · 1 comment · Fixed by #1118

Comments

@hairyhenderson
Copy link
Contributor

It looks like v0.13.0 introduced a bug in promhttp.InstrumentRoundTripperCounter (from #1055, it appears).

Instead of replacing the Inc() call with a wrapped call to exemplarAdd(...), the Inc() call was left in:

https://github.com/prometheus/client_golang/pull/1055/files#diff-7871a83741d29b5f29e3c47d1c284f948c258a0c6fc18895f0c2d4204cc46054R76-R81

The impact is that counter metrics are now double-counted when instrumented with promhttp.InstrumentRoundTripperCounter.

The bug can be observed with this test:

func TestBug(t *testing.T) {
	counter := prometheus.NewCounterVec(
		prometheus.CounterOpts{Name: "requests_total", Help: "A request counter"},
		[]string{"method", "code"},
	)

	reg := prometheus.NewRegistry()
	reg.MustRegister(counter)

	rt := promhttp.InstrumentRoundTripperCounter(counter, http.DefaultTransport)
	c := &http.Client{Transport: rt}

	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)
	}))

	req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, srv.URL, nil)
	resp, _ := c.Do(req)
	defer resp.Body.Close()

	expected := `
		# HELP requests_total A request counter
		# TYPE requests_total counter
		requests_total{code="200",method="get"} 1
	`

	if err := testutil.GatherAndCompare(reg, strings.NewReader(expected),
		"requests_total",
	); err != nil {
		t.Fatal(err)
	}
}

The output is:

[...]
        Diff:
        --- metric output does not match expectation; want
        +++ got:
        @@ -2,3 +2,3 @@
         # TYPE requests_total counter
        -requests_total{code="200",method="get"} 1
        +requests_total{code="200",method="get"} 2

I'll try to issue a fix for this today!

@hairyhenderson
Copy link
Contributor Author

#1118 will fix this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant