Google-cloud-go: FR: Easy way to add HTTP request/response logging

Created on 19 Jan 2017  路  8Comments  路  Source: googleapis/google-cloud-go

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?

p2 feature request

Most helpful comment

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

All 8 comments

@broady @rakyll

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

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.

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.

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

I filed https://github.com/golang/go/issues/18733 for maybe adding a GODEBUG=http1debug.

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

Was this page helpful?
0 / 5 - 0 ratings