-
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
Instrument RoundTripper via middleware #295
Conversation
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.
No real code review, I assume you discussed the implementation with @beorn7.
.travis.yml
Outdated
@@ -2,8 +2,8 @@ sudo: false | |||
language: go | |||
|
|||
go: | |||
- 1.6.3 |
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.
We just had a discussion about supported versions in #291 and the decision was to test against 1.6 but to just build tags for 1.7+ features.
prometheus/promhttp/client.go
Outdated
// may choose to use separately buckets Histograms, or implement custom | ||
// instance labels on a per function basis. | ||
type InstrumentTrace struct { | ||
GotConn, PutIdleConn, GotFirstResponseByte, Got100Continue, DNSStart, DNSDone, ConnectStart, ConnectDone, TLSHandshakeStart, TLSHandshakeDone, WroteHeaders, Wait100Continue, WroteRequest func(float64) |
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.
I'd prefer to list one struct member per line, even if that means repeating the value, than having such a long line.
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.
Totally cool with me
This reverts commit 2c1b043.
I guess the whole rountripper becomes a go1.7+ feature, e.g. adding the
build tag to both files you added here.
…On Wed, Apr 26, 2017, 15:39 stuart nelson ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In .travis.yml
<#295 (comment)>
:
> @@ -2,8 +2,8 @@ sudo: false
language: go
go:
- - 1.6.3
How do we want to handle testing features that are only available in 1.7,
like httptrace?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#295 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAANaO_066-XhJM7vKkWxWxbU1Fx9G1Tks5rz0kPgaJpZM4NIAka>
.
|
httptrace is only available in go-1.7+
httptrace hook functions I'm referencing were only added in 1.8 it seems.
On my review queue. Will get to it once the conference permits. |
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.
The filename client.go
is inconsistent with the existing instrument_server.go
. Please change either of it to create consistency (with the respective test files).
The package level doc comment needs to be updated as suggested earlier.
prometheus/promhttp/client.go
Outdated
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
dto "github.com/prometheus/client_model/go" |
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.
This line should be before the previous one and separated by a blank line (coming from a different repo).
prometheus/promhttp/client.go
Outdated
) | ||
|
||
// RoundTripperFunc is an adapter to allow wrapping an interface implementing | ||
// http.RoundTripper, allowing the user to construct layers of middleware. |
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.
The doc comment is the wrong way around. I'd just mirror the doc comment of the standard HandlerFunc, i.e. "The RoundTripperFunc type is an adapter to allow the use of ordinary functions as RoundTrippers. If f is a function with the appropriate signature, RountTriiperFunc(f) is a RoundTripper that calls f."
prometheus/promhttp/client.go
Outdated
if err != nil { | ||
return nil, err | ||
} | ||
gauge.Dec() |
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.
This needs to be defer
'd. See InstrucmentHandlerInFlight
for comparison.
prometheus/promhttp/client.go
Outdated
// in the CounterVec. For unpartitioned observations, use a CounterVec with | ||
// zero labels. | ||
// | ||
// If the wrapped RoundTripper panics, the Counter is not incremented. |
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.
...panics or returns a non-nil error, ...
prometheus/promhttp/client.go
Outdated
// names are "code" and "method". The function panics if any other instance | ||
// labels are provided. Partitioning of the CounterVec happens by HTTP status | ||
// code and/or HTTP method if the respective instance label names are present | ||
// in the CounterVec. For unpartitioned observations, use a CounterVec with |
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.
observations → counting
This actually applies to InstrumentHandlerCounter
, which I missed in the previous review.
prometheus/promhttp/client.go
Outdated
// ObserverVec with zero labels. Note that partitioning of Histograms is | ||
// expensive and should be used judiciously. | ||
// | ||
// If the wrapped RoundTripper panics, no values are reported. |
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.
... or returns a non-nil error...
prometheus/promhttp/client.go
Outdated
}) | ||
} | ||
|
||
func checkEventLabel(c prometheus.Collector) { |
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.
This isn't used anywhere.
prometheus/promhttp/client_1_8.go
Outdated
// See the example for ExampleInstrumentRoundTripperDuration for example usage. | ||
func InstrumentRoundTripperTrace(it *InstrumentTrace, next http.RoundTripper) RoundTripperFunc { | ||
return RoundTripperFunc(func(r *http.Request) (*http.Response, error) { | ||
var ( |
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.
I really wouldn't use a var block for a single initialization.
|
||
trace := &InstrumentTrace{ | ||
DNSStart: func(t float64) { | ||
dnsLatencyVec.WithLabelValues("DNSStart") |
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.
In general, we prefer snake_case for names in Prometheus.
Applicable to label names below, too.
prometheus/promhttp/client_1_8.go
Outdated
// httptrace.ClientTrace hook functions. Each function is passed a float64 | ||
// representing the time in seconds since the start of the http request. A user | ||
// may choose to use separately buckets Histograms, or implement custom | ||
// instance labels on a per function basis. |
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.
Perhaps document somehow that the various trace functions that get an error passed (PutIdleConn and others) don't trigger a call of the function given here if that error is non-nil.
Hello from the other side ... let me know how it looks :) |
I'm also on the other side. :-) Will have a look ASAP. |
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.
Just a minor concern about return values in case of errors.
return RoundTripperFunc(func(r *http.Request) (*http.Response, error) { | ||
gauge.Inc() | ||
defer gauge.Dec() | ||
resp, err := next.RoundTrip(r) |
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.
I think it's better to just directly return here, i.e. return next.RoundTrip(r)
. Should RountTrip return a non-nil error together with a non-nil Response, we probably want to keep that behavior.
return RoundTripperFunc(func(r *http.Request) (*http.Response, error) { | ||
resp, err := next.RoundTrip(r) | ||
if err != nil { | ||
return nil, err |
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.
Again, I think we want to return whatever RoundTrip returned in any case, i.e. return resp, err
.
Which means you can just increment the counter in a if err == nil
clause and have only one return line.
start := time.Now() | ||
resp, err := next.RoundTrip(r) | ||
if err != nil { | ||
return nil, err |
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.
Same concern as above.
@stuartnelson3 Have you seen my review? It's only minor changes that are still needed. Should be easy to update. (In different news, I'll have a look at improbable-eng/go-httpwares#12 and see if we can align with each other somehow. But that will be a separate thing from this PR.) |
Sorry for the delay, I missed your comments. Pushed! |
Thanks. I'll close the related issues, but will also check out improbable-eng/go-httpwares#12 to see where we can align. And of course, I have to think about #299 . |
Closing #288 as it was easier to open a new pull request than deal with the rebase/merge conflicts.