googlemaps / google-maps-services-go

Go client library for Google Maps API Web Services
https://pkg.go.dev/googlemaps.github.io/maps
Apache License 2.0
737 stars 214 forks source link

metrics.Request receives an already drained *http.Response #258

Closed lggomez closed 1 year ago

lggomez commented 2 years ago

Is your feature request related to a problem? Please describe. This is somewhat related to #245 in regards of the usefulness of the current metrics.Request interface

As of now, during the client APIcall execution the http response body is decoded before calling the EndRequest method, thus sending a drained response to it:

err = json.NewDecoder(httpResp.Body).Decode(resp) requestMetrics.EndRequest(ctx, err, httpResp, httpResp.Header.Get("x-goog-maps-metro-area"))

I get that this favors performance but also reduces the efficacy of these metrics interfaces in the first place since, for instance, any manual logging or introspection of the raw JSON payload is not possible

Describe the solution you'd like I'd like to be able to get the raw JSON response from the executed request

Describe alternatives you've considered I think a simple, non-obtrusive way would be to extend the Request interface with a hook method like the following, to be called before performing the JSON decoding:

 OnBeforeDecode(httpResp *http.Response)

I get that this is a breaking change on the interface level but an empty implementation should have a negligible performance impact and for debugging or logging scenarios the following pattern can be applied to intercept request payloads:

data, _ := ioutil.ReadAll(httpResp.Body)
log.Println(data) // do w/e is needed with the body data
httpResp.Body = ioutil.NopCloser(bytes.NewBuffer(data)) // restore the drained body
stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. Please comment here if it is still valid so that we can reprioritize. Thank you!

stale[bot] commented 1 year ago

Closing this. Please reopen if you believe it should be addressed. Thank you for your contribution.