-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
promhttp: count hard request failures in roundtripper metrics #943
Closed
EdSchouten
wants to merge
1
commit into
prometheus:main
from
EdSchouten:eschouten/20211130-client-errors
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,26 @@ func (rt RoundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) { | |
return rt(r) | ||
} | ||
|
||
// labelsForError obtains the values of the "code" and "method" labels | ||
// for the case where a HTTP request failed without getting a server | ||
// response. The "code" label is left empty to distinguish from cases | ||
// where a server response was obtained. | ||
func labelsForError(code, method bool, reqMethod string) prometheus.Labels { | ||
if !(code || method) { | ||
return emptyLabels | ||
} | ||
labels := prometheus.Labels{} | ||
|
||
if code { | ||
labels["code"] = "" | ||
} | ||
if method { | ||
labels["method"] = sanitizeMethod(reqMethod) | ||
} | ||
|
||
return labels | ||
} | ||
|
||
// InstrumentRoundTripperInFlight is a middleware that wraps the provided | ||
// http.RoundTripper. It sets the provided prometheus.Gauge to the number of | ||
// requests currently handled by the wrapped http.RoundTripper. | ||
|
@@ -53,8 +73,8 @@ func InstrumentRoundTripperInFlight(gauge prometheus.Gauge, next http.RoundTripp | |
// and/or HTTP method if the respective instance label names are present in the | ||
// CounterVec. For unpartitioned counting, use a CounterVec with zero labels. | ||
// | ||
// If the wrapped RoundTripper panics or returns a non-nil error, the Counter | ||
// is not incremented. | ||
// If the wrapped RoundTripper returns a non-nil error, the "code" label is | ||
// empty. If it panics, the Counter is not incremented. | ||
// | ||
// See the example for ExampleInstrumentRoundTripperDuration for example usage. | ||
func InstrumentRoundTripperCounter(counter *prometheus.CounterVec, next http.RoundTripper) RoundTripperFunc { | ||
|
@@ -64,6 +84,8 @@ func InstrumentRoundTripperCounter(counter *prometheus.CounterVec, next http.Rou | |
resp, err := next.RoundTrip(r) | ||
if err == nil { | ||
counter.With(labels(code, method, r.Method, resp.StatusCode)).Inc() | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could just |
||
counter.With(labelsForError(code, method, r.Method)).Inc() | ||
} | ||
return resp, err | ||
}) | ||
|
@@ -80,8 +102,8 @@ func InstrumentRoundTripperCounter(counter *prometheus.CounterVec, next http.Rou | |
// unpartitioned observations, use an ObserverVec with zero labels. Note that | ||
// partitioning of Histograms is expensive and should be used judiciously. | ||
// | ||
// If the wrapped RoundTripper panics or returns a non-nil error, no values are | ||
// reported. | ||
// If the wrapped RoundTripper returns a non-nil error, the "code" label is | ||
// empty. If it panics, no values are reported. | ||
// | ||
// Note that this method is only guaranteed to never observe negative durations | ||
// if used with Go1.9+. | ||
|
@@ -93,6 +115,8 @@ func InstrumentRoundTripperDuration(obs prometheus.ObserverVec, next http.RoundT | |
resp, err := next.RoundTrip(r) | ||
if err == nil { | ||
obs.With(labels(code, method, r.Method, resp.StatusCode)).Observe(time.Since(start).Seconds()) | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could just |
||
obs.With(labelsForError(code, method, r.Method)).Observe(time.Since(start).Seconds()) | ||
} | ||
return resp, err | ||
}) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not reusing
label
and injecting custom code (e.g -1) that would be a sentinel for us to put empty label ""? Less deduplicate functionality.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"code" suppose to be a valid HTTP code after the latest #962 change. I totally agree that we need to make it more sentinel.