Skip to content

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

@hairyhenderson

Description

@hairyhenderson

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions