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

added client finalizer #533

Merged
merged 2 commits into from
May 24, 2017
Merged

added client finalizer #533

merged 2 commits into from
May 24, 2017

Conversation

travissalascox
Copy link
Contributor

@travissalascox travissalascox commented May 18, 2017

based on the server finazlizer. @peterbourgon super open to suggestions to make this better, also excited as it will be my first real contribution to this project if it makes it in. should resolve #472 for the most part i believe.

based on the server finazlizer
Copy link
Member

@peterbourgon peterbourgon left a comment

Choose a reason for hiding this comment

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

Yeah, nice! I think there are a few differences between Client and Server that need addressing (see comments below) but this is pretty close!

// intended use is for request logging. In addition to the response code
// provided in the function signature, additional response parameters are
// provided in the context under keys with the ContextKeyResponse prefix.
type ClientFinalizerFunc func(ctx context.Context, code int, r *http.Request)
Copy link
Member

@peterbourgon peterbourgon May 21, 2017

Choose a reason for hiding this comment

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

Hmm. This is the same signature as in the ServerFinalizer, but I'm not sure it actually makes sense here. In the server we're guaranteed to have an http.Request, and we're guaranteed to have a code, so they make sense as parameters. But in the client, we may not make it all the way to generating an http.Request, and we may not get a status code back. And, we have an explicit error value that will always be returned via the endpoint (though it may be nil). So I think the better signature here is

type ClientFinalizerFunc func(ctx context.Context, err error) 

Of course, with a note in the comment about which fields in the context will be populated under which conditions. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

makes sense to me. the main reason i went with the signature of the ServerFinalizer was from a lack of how to tackle the problem. But it makes sense to me to have a signature like that. I'll update the PR later this evening and push up the requested changes. Thanks for the comments 😸 !

ctx = context.WithValue(ctx, ContextKeyResponseSize, resp.ContentLength)
c.finalizer(ctx, resp.StatusCode, req)
}()
}
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure it's correct to create zero-value http.Requests and Responses; my intuition is that they need to be created by package http in an e.g. NewReqeust constructor. But that's fine, I think we can get by without them. Going with the change in the ClientFinalizerFunc below, perhaps this can look like..

var (
    resp *http.Response
    err  error
)
if c.finalizer != nil {
    defer func() {
        if resp != nil {
            ctx = context.WithValue(ctx, ContextKeyResponseHeaders, resp.Header)
            ctx = context.WithValue(ctx, ContextKeyResponseSize, resp.ContentLength)
        }
        c.finalizer(ctx, err)
    }()
}

What do you think?

func TestClientFinalizer(t *testing.T) {
var (
headerKey = "X-Henlo-Lizer"
headerVal = "Helllo you stinky lizard"
Copy link
Member

Choose a reason for hiding this comment

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

It's ridiculous how my stream-of-consciousness garbage thoughts when I was writing this test get propagated into the infinite future 🤙

update based on comments to previous commit.
@travissalascox
Copy link
Contributor Author

@peterbourgon updated based your comments.

@peterbourgon
Copy link
Member

Sweet! Thanks a ton!

@peterbourgon peterbourgon merged commit 0262bac into go-kit:master May 24, 2017
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.

Logging request responses, including status codes and headers, with go-kit’s Client
2 participants