Skip to content

Commit 89a63ba

Browse files
authored
Merge pull request #7 from furio/patch-1
Fixes #6 fix panic when request body length less than 0
2 parents a05de09 + d4d53ed commit 89a63ba

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

Diff for: ginmetrics/middleware.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,10 @@ func (m *Monitor) ginMetricHandle(ctx *gin.Context, start time.Time) {
114114
_ = m.GetMetric(metricURIRequestTotal).Inc([]string{ctx.FullPath(), r.Method, strconv.Itoa(w.Status())})
115115

116116
// set request body size
117-
_ = m.GetMetric(metricRequestBody).Add(nil, float64(r.ContentLength))
117+
// since r.ContentLength can be negative (in some occasions) guard the operation
118+
if r.ContentLength >= 0 {
119+
_ = m.GetMetric(metricRequestBody).Add(nil, float64(r.ContentLength))
120+
}
118121

119122
// set slow request
120123
latency := time.Since(start)

0 commit comments

Comments
 (0)