Skip to content

Commit 2dde79b

Browse files
dmathieuMrAlias
authored and
Ionut Zailic
committed
Use an atomic.Int64 as bodyWrapper read bytes counter (open-telemetry#5080)
* use an atomic.Int64 as bodyWrapper read bytes counter * add changelog entry --------- Co-authored-by: Tyler Yahn <[email protected]>
1 parent 1a0432a commit 2dde79b

File tree

4 files changed

+10
-5
lines changed

4 files changed

+10
-5
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1919
- The deprecated `ResponseContentLength` constant in `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp` is removed. (#4894)
2020
- The deprecated `ServerLatency` constant in `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp` is removed. (#4894)
2121

22+
### Fixed
23+
24+
- Retrieving the body bytes count in `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp` does not cause a data race anymore. (#5080)
25+
2226
## [1.23.0/0.48.0/0.17.0/0.3.0] - 2024-02-06
2327

2428
### Added

instrumentation/net/http/otelhttp/handler.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -226,15 +226,15 @@ func (h *middleware) serveHTTP(w http.ResponseWriter, r *http.Request, next http
226226

227227
next.ServeHTTP(w, r.WithContext(ctx))
228228

229-
setAfterServeAttributes(span, bw.read, rww.written, rww.statusCode, bw.err, rww.err)
229+
setAfterServeAttributes(span, bw.read.Load(), rww.written, rww.statusCode, bw.err, rww.err)
230230

231231
// Add metrics
232232
attributes := append(labeler.Get(), semconvutil.HTTPServerRequestMetrics(h.server, r)...)
233233
if rww.statusCode > 0 {
234234
attributes = append(attributes, semconv.HTTPStatusCode(rww.statusCode))
235235
}
236236
o := metric.WithAttributes(attributes...)
237-
h.requestBytesCounter.Add(ctx, bw.read, o)
237+
h.requestBytesCounter.Add(ctx, bw.read.Load(), o)
238238
h.responseBytesCounter.Add(ctx, rww.written, o)
239239

240240
// Use floating point division here for higher precision (instead of Millisecond method).

instrumentation/net/http/otelhttp/transport.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func (t *Transport) RoundTrip(r *http.Request) (*http.Response, error) {
184184
metricAttrs = append(metricAttrs, semconv.HTTPStatusCode(res.StatusCode))
185185
}
186186
o := metric.WithAttributes(metricAttrs...)
187-
t.requestBytesCounter.Add(ctx, bw.read, o)
187+
t.requestBytesCounter.Add(ctx, bw.read.Load(), o)
188188
// For handling response bytes we leverage a callback when the client reads the http response
189189
readRecordFunc := func(n int64) {
190190
t.responseBytesCounter.Add(ctx, n, o)

instrumentation/net/http/otelhttp/wrap.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"context"
1919
"io"
2020
"net/http"
21+
"sync/atomic"
2122

2223
"go.opentelemetry.io/otel/propagation"
2324
)
@@ -30,14 +31,14 @@ type bodyWrapper struct {
3031
io.ReadCloser
3132
record func(n int64) // must not be nil
3233

33-
read int64
34+
read atomic.Int64
3435
err error
3536
}
3637

3738
func (w *bodyWrapper) Read(b []byte) (int, error) {
3839
n, err := w.ReadCloser.Read(b)
3940
n1 := int64(n)
40-
w.read += n1
41+
w.read.Add(n1)
4142
w.err = err
4243
w.record(n1)
4344
return n, err

0 commit comments

Comments
 (0)