Skip to content

Commit

Permalink
Fix response ContentLength metrics tripperware (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
instabledesign authored Dec 7, 2020
1 parent d26a93f commit 23ef6d2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
8 changes: 7 additions & 1 deletion tripperware/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ func Metrics(recorder metrics.Recorder, options ... metrics.Option) httpware.Tri
contentLength := int64(0)
if resp != nil {
statusCode = resp.StatusCode
contentLength = resp.ContentLength
// ContentLength records the length of the associated content. The
// value -1 indicates that the length is unknown. Unless Request.Method
// is "HEAD", values >= 0 indicate that the given number of bytes may
// be read from Body.
if resp.ContentLength > 0 {
contentLength = resp.ContentLength
}
}
code := strconv.Itoa(statusCode)
if !config.SplitStatus {
Expand Down
36 changes: 36 additions & 0 deletions tripperware/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,42 @@ func TestMetrics(t *testing.T) {
_, _ = stack.DecorateRoundTripper(roundTripperMock).RoundTrip(req)
}

func TestMetricsContentLengthUnknown(t *testing.T) {
var recorderMock = &mocks.Recorder{}
var roundTripperMock = &mocks.RoundTripper{}
var req *http.Request
var requestTimeDuration = 10 * time.Millisecond
var resp = &http.Response{
Status: "OK",
StatusCode: http.StatusOK,
ContentLength: -1,
}
expectedContentLength := int64(0)
var baseTime = time.Unix(513216000, 0)

req = httptest.NewRequest(http.MethodGet, "http://fake-addr", nil)
// mock roundTripper calls
roundTripperMock.On("RoundTrip", req).Return(resp, nil)
// assert recorder calls
recorderMock.On("AddInflightRequests", req.Context(), req.URL.String(), 1).Once()
recorderMock.On("AddInflightRequests", req.Context(), req.URL.String(), -1).Once()
recorderMock.On("ObserveHTTPRequestDuration", req.Context(), req.URL.String(), requestTimeDuration, http.MethodGet, "2xx")
recorderMock.On("ObserveHTTPResponseSize", req.Context(), req.URL.String(), expectedContentLength, http.MethodGet, "2xx")
// mock time.Now method in order to return always the same time whenever the test is launched
monkey.Patch(time.Now, func() time.Time { return baseTime })
monkey.Patch(time.Since, func(since time.Time) time.Duration {
assert.Equal(t, baseTime, since)
return requestTimeDuration
})
defer monkey.UnpatchAll()

// create metrics httpClient middleware
stack := httpware.TripperwareStack(
tripperware.Metrics(recorderMock),
)
_, _ = stack.DecorateRoundTripper(roundTripperMock).RoundTrip(req)
}

// =====================================================================================================================
// ========================================= EXAMPLES ==================================================================
// =====================================================================================================================
Expand Down

0 comments on commit 23ef6d2

Please sign in to comment.