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

Add time to write header handler middleware #304

Merged
merged 4 commits into from
May 29, 2017

Conversation

stuartnelson3
Copy link
Contributor

@beorn7 first stab at writing the "time to writing header" middleware.

Things to note are:

  • I've forgone branching between "has the code label" and "doesn't have the code label". They paths are too similar to warrant it IMO.
  • As mentioned, this is just based off what I saw in Prometheus Monitoring Middleware/Tripperware improbable-eng/go-httpwares#12. No strong opinions one way or another, but this seemed like a simple way to implement it. I didn't think it necessary to generalize quite like that PR did with a slice of callback functions.

@stuartnelson3 stuartnelson3 requested a review from beorn7 May 11, 2017 20:31
@stuartnelson3 stuartnelson3 force-pushed the stn/time-to-write-header-middleware branch from ac5eb32 to 91ef55a Compare May 11, 2017 20:33
@beorn7
Copy link
Member

beorn7 commented May 16, 2017

On my review queue…

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.

Finally got to this.

// judiciously.
//
// If the wrapped Handler does not set a status code via WriteHeader, no value
// is reported.
Copy link
Member

Choose a reason for hiding this comment

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

See https://golang.org/pkg/net/http/#ResponseWriter . I understand this as WriteHeader being called eventually in any case, so we should be fine and don't need this note.


return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
now := time.Now()
d := &wroteHeaderDelegator{
Copy link
Member

Choose a reason for hiding this comment

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

This breaks interface upgrades (as the type of the embedded delegator is just the plain delegator interface, and the check for interface upgrades only type-switches on the outer type, not the type of the embedded struct element, see https://play.golang.org/p/uuqlJpUG7G as proof).

I suggest to chenge the general responseWriterDelegator implementation to include an observeWriteHeader function, which we check for non-nil in the WriteHeader method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

K, I updated it, not sure if this was what you imagined. Since newDelegator returns an interface type, I just went one more layer deep on what gets sent into that function so I could attach the observeWriteHeader method.

@@ -132,12 +129,12 @@ func InstrumentHandlerTimeToWriteHeader(obs prometheus.ObserverVec, next http.Ha

return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
now := time.Now()
d := &wroteHeaderDelegator{
delegator: newDelegator(w),
d := newDelegator(&responseWriterDelegator{
Copy link
Member

Choose a reason for hiding this comment

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

This will again kill the interface upgrade. I think the way to go is to change newDelegator to also accept the observeWriteHeader func (which can be nil) and set it in the created responseWriterDelegator. It doesn't look beautiful, but at least newDelegator is not exported.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Roger, pushed.

Constructor now accepts an additional parameter
that sets the function to call, if not nil, when
writing the header.
@beorn7
Copy link
Member

beorn7 commented May 29, 2017

This should work. Thanks.

@beorn7 beorn7 merged commit 2b3ab50 into master May 29, 2017
@beorn7 beorn7 deleted the stn/time-to-write-header-middleware branch May 29, 2017 09:42
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.

2 participants