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

Fix double-counting bug in promhttp.InstrumentRoundTripperCounter #1118

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion prometheus/promhttp/instrument_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking loudly: I think the function name exemplarAdd() doesn't make it obvious the metric is incremented. Maybe renaming it to addWithExemplar() may clarify it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed! My goal here was to fix the bug with as few changes as possible so I didn't consider changing the function name... IMO that'd best be deferred to a separate PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏽 Thanks for noticing this

}
return resp, err
}
Expand Down
14 changes: 14 additions & 0 deletions prometheus/promhttp/instrument_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down