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

Instrument RoundTripper via middleware #295

Merged
merged 12 commits into from
May 9, 2017
Merged

Instrument RoundTripper via middleware #295

merged 12 commits into from
May 9, 2017

Conversation

stuartnelson3
Copy link
Contributor

Closing #288 as it was easier to open a new pull request than deal with the rebase/merge conflicts.

@stuartnelson3 stuartnelson3 requested a review from beorn7 April 25, 2017 20:24
@stuartnelson3 stuartnelson3 mentioned this pull request Apr 25, 2017
Copy link
Member

@grobie grobie left a 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
Copy link
Member

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.

// 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)
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Totally cool with me

@grobie
Copy link
Member

grobie commented Apr 26, 2017 via email

httptrace is only available in go-1.7+
httptrace hook functions I'm referencing were only
added in 1.8 it seems.
@beorn7
Copy link
Member

beorn7 commented Apr 27, 2017

On my review queue. Will get to it once the conference permits.

Copy link
Member

@beorn7 beorn7 left a 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.

"time"

"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
Copy link
Member

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).

)

// RoundTripperFunc is an adapter to allow wrapping an interface implementing
// http.RoundTripper, allowing the user to construct layers of middleware.
Copy link
Member

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."

if err != nil {
return nil, err
}
gauge.Dec()
Copy link
Member

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.

// in the CounterVec. For unpartitioned observations, use a CounterVec with
// zero labels.
//
// If the wrapped RoundTripper panics, the Counter is not incremented.
Copy link
Member

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, ...

// 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
Copy link
Member

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.

// 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.
Copy link
Member

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...

})
}

func checkEventLabel(c prometheus.Collector) {
Copy link
Member

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.

// 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 (
Copy link
Member

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")
Copy link
Member

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.

// 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.
Copy link
Member

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.

@stuartnelson3
Copy link
Contributor Author

Hello from the other side ... let me know how it looks :)

@beorn7
Copy link
Member

beorn7 commented May 2, 2017

I'm also on the other side. :-)

Will have a look ASAP.

Copy link
Member

@beorn7 beorn7 left a 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)
Copy link
Member

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
Copy link
Member

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
Copy link
Member

Choose a reason for hiding this comment

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

Same concern as above.

@beorn7
Copy link
Member

beorn7 commented May 8, 2017

@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.)

@stuartnelson3
Copy link
Contributor Author

Sorry for the delay, I missed your comments. Pushed!

@beorn7 beorn7 merged commit d300d5c into master May 9, 2017
@beorn7 beorn7 deleted the stn/http-client branch May 9, 2017 16:46
@beorn7
Copy link
Member

beorn7 commented May 9, 2017

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 .

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 this pull request may close these issues.

3 participants