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

FR: Easy way to add HTTP request/response logging #487

Closed
Capstan opened this issue Jan 19, 2017 · 10 comments
Closed

FR: Easy way to add HTTP request/response logging #487

Capstan opened this issue Jan 19, 2017 · 10 comments
Assignees
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@Capstan
Copy link

Capstan commented Jan 19, 2017

I looked at https://blog.golang.org/http-tracing, but it doesn't appear to be something I can shoehorn into the clients. Is there an easy recipe I'm missing?

@jba
Copy link
Contributor

jba commented Jan 19, 2017

@broady @rakyll

@broady
Copy link
Contributor

broady commented Jan 19, 2017

For the clients that use gRPC, you can try EnableTracing:
https://godoc.org/google.golang.org/grpc#EnableTracing

For the non-gRPC clients, like storage, you can use a similar method described in that blog post, though we don't allow you to pass a custom RoundTripper.

You'll want this option:
https://godoc.org/google.golang.org/api/option#WithHTTPClient

Something like this:

type wrapped struct {
    base http.RoundTripper
}

func (w wrapped) RoundTrip(r *http.Request) (*http.Response, error) {
    log.Print(r.URL)
    return w.base.RoundTrip(r)
}

hc := google.DefaultClient(ctx, ...)
hc.Transport = wrapped{hc.Transport}
storage.NewClient(ctx, option.WithHTTPClient(hc))

@Capstan
Copy link
Author

Capstan commented Jan 19, 2017

This is traditional HTTP. Your example worked, though I'm not exactly sure how to tee the Request.Body or Response.Body without interfering with the passthrough.

@broady
Copy link
Contributor

broady commented Jan 19, 2017

You can replace the resp.Body with a TeeReader:
https://golang.org/pkg/io/#TeeReader

func (w wrapped) RoundTrip(r *http.Request) (*http.Response, error) {
    log.Print(r.URL)
    resp, err := w.base.RoundTrip(r)
    if err != nil {
        return resp, err
    }
    resp.Body = io.TeeReader(resp.Body, os.Stderr)
    return resp, err
}

This will print the body to stderr as it's being read by the client.

You'll need to do a bit more work since resp.Body is a ReadCloser, but TeeReader is just a Reader.

@bradfitz
Copy link
Contributor

@Capstan, if you only care about seeing Google traffic, setting GODEBUG=http2debug=2 might work for you. It shows all http2 frames read & written.

@bradfitz
Copy link
Contributor

I filed golang/go#18733 for maybe adding a GODEBUG=http1debug.

@Sajmani
Copy link

Sajmani commented Feb 8, 2017

Pending CL for review: https://go-review.googlesource.com/#/c/36548/

@jba jba added the type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. label Mar 15, 2017
@jba jba added the priority: p2 Moderately-important priority. Fix may not be included in next release. label Aug 8, 2017
@jba
Copy link
Contributor

jba commented Dec 11, 2017

I think the code in this issue is sufficient. Closing.

@Slach
Copy link

Slach commented Oct 1, 2021

worked, code snippet with modern version
https://gist.github.com/Slach/57c5bb8331bcbc32e998166557220ad4

@yonderblue
Copy link

Is the above gist still the best way to use your own Transport/RoundTripper? (using googleHTTPTransport.NewTransport instead of googleHTTPTransport.NewClient if not wrapping a Transport but using an existing).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

No branches or pull requests

7 participants