WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 1935692

Browse files
authored
Add Metrics to RetryableHTTPClient (#4545)
* add response body size metric to InstrumentedTransport, add InstrumentedTransport to RetryableHTTPClient * add doc to responseSizeCounterReadCloser, remove unused field from test * record response using ContentLength
1 parent 2b35d68 commit 1935692

File tree

3 files changed

+90
-7
lines changed

3 files changed

+90
-7
lines changed

pkg/common/http.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ func (t *InstrumentedTransport) RoundTrip(req *http.Request) (*http.Response, er
133133
}
134134

135135
if resp != nil {
136-
// record latency and increment counter for non-200 status code
137-
recordHTTPResponse(sanitizedURL, resp.StatusCode, duration.Seconds())
136+
// record latency, response size and increment counter for non-200 status code
137+
recordHTTPResponse(sanitizedURL, resp.StatusCode, duration.Seconds(), resp.ContentLength)
138138
}
139139

140140
return resp, err
@@ -198,7 +198,7 @@ func WithRetryWaitMax(wait time.Duration) ClientOption {
198198
func PinnedRetryableHttpClient() *http.Client {
199199
httpClient := retryablehttp.NewClient()
200200
httpClient.Logger = nil
201-
httpClient.HTTPClient.Transport = NewCustomTransport(&http.Transport{
201+
httpClient.HTTPClient.Transport = NewInstrumentedTransport(NewCustomTransport(&http.Transport{
202202
TLSClientConfig: &tls.Config{
203203
RootCAs: PinnedCertPool(),
204204
},
@@ -212,15 +212,15 @@ func PinnedRetryableHttpClient() *http.Client {
212212
IdleConnTimeout: 90 * time.Second,
213213
TLSHandshakeTimeout: 10 * time.Second,
214214
ExpectContinueTimeout: 1 * time.Second,
215-
})
215+
}))
216216
return httpClient.StandardClient()
217217
}
218218

219219
func RetryableHTTPClient(opts ...ClientOption) *http.Client {
220220
httpClient := retryablehttp.NewClient()
221221
httpClient.RetryMax = 3
222222
httpClient.Logger = nil
223-
httpClient.HTTPClient.Transport = NewCustomTransport(nil)
223+
httpClient.HTTPClient.Transport = NewInstrumentedTransport(NewCustomTransport(nil))
224224

225225
for _, opt := range opts {
226226
opt(httpClient)
@@ -234,7 +234,7 @@ func RetryableHTTPClientTimeout(timeOutSeconds int64, opts ...ClientOption) *htt
234234
httpClient.RetryMax = 3
235235
httpClient.Logger = nil
236236
httpClient.HTTPClient.Timeout = time.Duration(timeOutSeconds) * time.Second
237-
httpClient.HTTPClient.Transport = NewCustomTransport(nil)
237+
httpClient.HTTPClient.Transport = NewInstrumentedTransport(NewCustomTransport(nil))
238238

239239
for _, opt := range opts {
240240
opt(httpClient)

pkg/common/http_metrics.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ var (
3939
},
4040
[]string{"url", "status_code"},
4141
)
42+
43+
httpResponseBodySizeBytes = promauto.NewHistogramVec(
44+
prometheus.HistogramOpts{
45+
Namespace: MetricsNamespace,
46+
Subsystem: "http_client",
47+
Name: "response_body_size_bytes",
48+
Help: "Size of HTTP response bodies in bytes, labeled by URL.",
49+
Buckets: prometheus.ExponentialBuckets(100, 10, 5), // [100B, 1KB, 10KB, 100KB, 1MB]
50+
},
51+
[]string{"url"},
52+
)
4253
)
4354

4455
// sanitizeURL sanitizes a URL to avoid high cardinality metrics.
@@ -92,14 +103,19 @@ func recordHTTPRequest(sanitizedURL string) {
92103
}
93104

94105
// recordHTTPResponse records metrics for an HTTP response.
95-
func recordHTTPResponse(sanitizedURL string, statusCode int, durationSeconds float64) {
106+
func recordHTTPResponse(sanitizedURL string, statusCode int, durationSeconds float64, contentLength int64) {
96107
// Record latency
97108
httpRequestDuration.WithLabelValues(sanitizedURL).Observe(durationSeconds)
98109

99110
// Record non-200 responses
100111
if statusCode != 200 {
101112
httpNon200ResponsesTotal.WithLabelValues(sanitizedURL, strconv.Itoa(statusCode)).Inc()
102113
}
114+
115+
// Record response body size if known
116+
if contentLength >= 0 {
117+
httpResponseBodySizeBytes.WithLabelValues(sanitizedURL).Observe(float64(contentLength))
118+
}
103119
}
104120

105121
// recordNetworkError records metrics for failed HTTP response

pkg/common/http_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,73 @@ func TestSaneHttpClientMetrics(t *testing.T) {
405405
}
406406
}
407407

408+
func TestRetryableHttpClientMetrics(t *testing.T) {
409+
// Create a test server that returns different status codes
410+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
411+
switch r.URL.Path {
412+
case "/success":
413+
w.WriteHeader(http.StatusOK)
414+
_, _ = w.Write([]byte("success"))
415+
case "/error":
416+
w.WriteHeader(http.StatusInternalServerError)
417+
_, _ = w.Write([]byte("error"))
418+
case "/notfound":
419+
w.WriteHeader(http.StatusNotFound)
420+
_, _ = w.Write([]byte("not found"))
421+
default:
422+
w.WriteHeader(http.StatusOK)
423+
_, _ = w.Write([]byte("default"))
424+
}
425+
}))
426+
defer server.Close()
427+
428+
// Create a RetryableHttpClient
429+
client := RetryableHTTPClient()
430+
431+
testCases := []struct {
432+
name string
433+
path string
434+
expectedStatusCode int
435+
}{
436+
{
437+
name: "successful request",
438+
path: "/success",
439+
expectedStatusCode: 200,
440+
},
441+
{
442+
name: "not found request",
443+
path: "/notfound",
444+
expectedStatusCode: 404,
445+
},
446+
}
447+
448+
for _, tc := range testCases {
449+
t.Run(tc.name, func(t *testing.T) {
450+
var requestURL string
451+
if strings.HasPrefix(tc.path, "http") {
452+
requestURL = tc.path
453+
} else {
454+
requestURL = server.URL + tc.path
455+
}
456+
457+
// Get initial metric values
458+
sanitizedURL := sanitizeURL(requestURL)
459+
initialRequestsTotal := testutil.ToFloat64(httpRequestsTotal.WithLabelValues(sanitizedURL))
460+
461+
// Make the request
462+
resp, err := client.Get(requestURL)
463+
464+
require.NoError(t, err)
465+
defer resp.Body.Close()
466+
assert.Equal(t, tc.expectedStatusCode, resp.StatusCode)
467+
468+
// Check that request counter was incremented
469+
requestsTotal := testutil.ToFloat64(httpRequestsTotal.WithLabelValues(sanitizedURL))
470+
assert.Equal(t, initialRequestsTotal+1, requestsTotal)
471+
})
472+
}
473+
}
474+
408475
func TestInstrumentedTransport(t *testing.T) {
409476
// Create a mock transport that we can control
410477
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)