From f4be228ba01a70ffc5813e8fe46794ece39377b1 Mon Sep 17 00:00:00 2001 From: Bjoern Rabenstein Date: Wed, 8 Oct 2014 19:01:24 +0200 Subject: [PATCH] Fix a race condition in the http instrumentation. computeApproximateRequestSize is run in a goroutine, but the handlerFunc that runs in parallel may modify the URL, which is also needed by computeApproximateRequestSize. So get the URL length beforehand. Change-Id: Idb84735845afe7be4ef79b3d642d5764f6d26a7c --- prometheus/http.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/prometheus/http.go b/prometheus/http.go index 1b3b592..02726b6 100644 --- a/prometheus/http.go +++ b/prometheus/http.go @@ -140,7 +140,11 @@ func InstrumentHandlerFuncWithOpts(opts SummaryOpts, handlerFunc func(http.Respo delegate := &responseWriterDelegator{ResponseWriter: w} out := make(chan int) - go computeApproximateRequestSize(r, out) + urlLen := 0 + if r.URL != nil { + urlLen = len(r.URL.String()) + } + go computeApproximateRequestSize(r, out, urlLen) handlerFunc(delegate, r) elapsed := float64(time.Since(now)) / float64(time.Microsecond) @@ -154,11 +158,8 @@ func InstrumentHandlerFuncWithOpts(opts SummaryOpts, handlerFunc func(http.Respo }) } -func computeApproximateRequestSize(r *http.Request, out chan int) { - s := len(r.Method) - if r.URL != nil { - s += len(r.URL.String()) - } +func computeApproximateRequestSize(r *http.Request, out chan int, s int) { + s += len(r.Method) s += len(r.Proto) for name, values := range r.Header { s += len(name)