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
This commit is contained in:
Bjoern Rabenstein 2014-10-08 19:01:24 +02:00
parent 6fbc8ef5c2
commit f4be228ba0
1 changed files with 7 additions and 6 deletions

View File

@ -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)