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

automatically debug-log upstream http-response-sizes #1054

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion backend/httpclient/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net"
"net/http"

"github.com/grafana/grafana-plugin-sdk-go/backend/log"
"github.com/grafana/grafana-plugin-sdk-go/backend/proxy"
)

Expand Down Expand Up @@ -41,6 +42,30 @@ func New(opts ...Options) (*http.Client, error) {
return c, nil
}

type reportSizeRoundtripper struct {
next http.RoundTripper
}

func newReportSizeRoundtripper(next http.RoundTripper) http.RoundTripper {
return &reportSizeRoundtripper{next: next}
}

func (rt *reportSizeRoundtripper) RoundTrip(req *http.Request) (*http.Response, error) {
res, err := rt.next.RoundTrip(req)

if err != nil {
return res, err
}

res.Body = CountBytesReader(res.Body, func(size int64) {
log.DefaultLogger.Debug("downstream response info", "bytes", size, "url", req.URL.String())
})

return res, err
}

var _ http.RoundTripper = &reportSizeRoundtripper{}

// GetTransport creates a new http.RoundTripper given provided options.
// If opts is nil the http.DefaultTransport will be returned.
// If no middlewares are provided the DefaultMiddlewares() will be used. If you
Expand Down Expand Up @@ -93,7 +118,14 @@ func GetTransport(opts ...Options) (http.RoundTripper, error) {
return nil, err
}

return roundTripperFromMiddlewares(clientOpts, clientOpts.Middlewares, transport)
_, hasDatasourceTypeLabel := clientOpts.Labels["datasource_type"]

var rt http.RoundTripper = transport
if hasDatasourceTypeLabel {
rt = newReportSizeRoundtripper(rt)
}
Comment on lines +124 to +126
Copy link
Member

Choose a reason for hiding this comment

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

You could put this logic in the middleware as well, see this example to get a hold of the options

return NamedMiddlewareFunc(BasicAuthenticationMiddlewareName, func(opts Options, next http.RoundTripper) http.RoundTripper {
. And I think then adding the middleware via DefaultMiddlewares() would allow both
providerOpts.Middlewares = DefaultMiddlewares()
and http_client.go to support this.

One important thing is that this middleware should probably/maybe execute after the https://github.com/grafana/grafana-plugin-sdk-go/blob/main/backend/httpclient/max_bytes_reader.go so if a limit is in place your middleware isn't continuing to read the response to the end 🤷

One thing to consider might be to somehow share the response size reader between limit and reporting middlewares, but maybe something for the future.


return roundTripperFromMiddlewares(clientOpts, clientOpts.Middlewares, rt)
}

// GetTLSConfig creates a new tls.Config given provided options.
Expand Down