Skip to content

Commit 2f43a9b

Browse files
author
Ionut Zailic
committed
fix: make possible adding custom attributes to the shipped otelhttp client metrics - open-telemetry#5084
1 parent 305e533 commit 2f43a9b

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

instrumentation/net/http/otelhttp/handler.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,10 @@ func (h *middleware) serveHTTP(w http.ResponseWriter, r *http.Request, next http
219219
},
220220
})
221221

222-
labeler := &Labeler{}
223-
ctx = injectLabeler(ctx, labeler)
222+
labeler, found := LabelerFromContext(ctx)
223+
if !found {
224+
ctx = InjectLabeler(ctx, labeler)
225+
}
224226

225227
next.ServeHTTP(w, r.WithContext(ctx))
226228

instrumentation/net/http/otelhttp/labeler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type labelerContextKeyType int
4848

4949
const lablelerContextKey labelerContextKeyType = 0
5050

51-
func injectLabeler(ctx context.Context, l *Labeler) context.Context {
51+
func InjectLabeler(ctx context.Context, l *Labeler) context.Context {
5252
return context.WithValue(ctx, lablelerContextKey, l)
5353
}
5454

instrumentation/net/http/otelhttp/transport.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,10 @@ func (t *Transport) RoundTrip(r *http.Request) (*http.Response, error) {
148148
ctx = httptrace.WithClientTrace(ctx, t.clientTrace(ctx))
149149
}
150150

151-
labeler := &Labeler{}
152-
ctx = injectLabeler(ctx, labeler)
151+
labeler, found := LabelerFromContext(ctx)
152+
if !found {
153+
ctx = InjectLabeler(ctx, labeler)
154+
}
153155

154156
r = r.Clone(ctx) // According to RoundTripper spec, we shouldn't modify the origin request.
155157

0 commit comments

Comments
 (0)