Skip to content

Conversation

@mustansir14
Copy link
Contributor

Description:

This PR adds a new HTTP response body size metric to InstrumentedTransport, which is the current Transport for SaneHTTPClient. InstrumentedTransport is also set to be used as the Transport for RetryableHTTPClient.

Challenge with Response Body Size Calculation:

  • The Response object has a ContentLength field that may have exactly what we're looking for, but it is not guaranteed to be provided by the server. Also it is never available for a chunked response.
  • A simple approach would be to read the full body in memory and get it's size, but this is inefficient as responses can be large. Also it may add a delay to the RoundTrip() call, and would require restoring the body so that downstream code can read it again.

Solution:

To address this, a responseSizeCounterReadCloser is introduced to measure the size of the response body WHILE the downstream code reads it. This ensures no overhead is added in the RoundTrip() call. This however will not work if the downstream code never reads the body (e.g. detectors where only the StatusCode is used to verify), so to handle this, we add a check in the Close() method of the ReadCloser to drain the body if it's not consumed.

Checklist:

  • Tests passing (make test-community)?
  • Lint passing (make lint this requires golangci-lint)?

@mustansir14 mustansir14 requested a review from a team November 7, 2025 14:46
@mustansir14 mustansir14 requested a review from a team as a code owner November 7, 2025 14:46
Comment on lines +138 to +146

// wrap the response body to count bytes read
resp.Body = &responseSizeCounterReadCloser{
ReadCloser: resp.Body,
recordSizeFunc: func(size int) {
// Record response body size
recordResponseBodySize(sanitizedURL, size)
},
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This would also start generating metrics for SaneHTTPClient. Just wanna make sure if this is intentional.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants