Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
gabor committed Aug 30, 2024
1 parent e1e82c0 commit 750bca5
Showing 1 changed file with 33 additions and 1 deletion.
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)
}

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

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

0 comments on commit 750bca5

Please sign in to comment.