Skip to content

Commit

Permalink
fix: make possible adding custom attributes to the shipped otelhttp c…
Browse files Browse the repository at this point in the history
…lient metrics - open-telemetry#5084
  • Loading branch information
zailic authored and Ionut Zailic committed Feb 13, 2024
1 parent 305e533 commit 070f9d7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
6 changes: 4 additions & 2 deletions instrumentation/net/http/otelhttp/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,10 @@ func (h *middleware) serveHTTP(w http.ResponseWriter, r *http.Request, next http
},
})

labeler := &Labeler{}
ctx = injectLabeler(ctx, labeler)
labeler, found := LabelerFromContext(ctx)
if !found {
ctx = InjectLabeler(ctx, labeler)
}

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

Expand Down
2 changes: 1 addition & 1 deletion instrumentation/net/http/otelhttp/labeler.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type labelerContextKeyType int

const lablelerContextKey labelerContextKeyType = 0

func injectLabeler(ctx context.Context, l *Labeler) context.Context {
func InjectLabeler(ctx context.Context, l *Labeler) context.Context {
return context.WithValue(ctx, lablelerContextKey, l)
}

Expand Down
6 changes: 4 additions & 2 deletions instrumentation/net/http/otelhttp/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,10 @@ func (t *Transport) RoundTrip(r *http.Request) (*http.Response, error) {
ctx = httptrace.WithClientTrace(ctx, t.clientTrace(ctx))
}

labeler := &Labeler{}
ctx = injectLabeler(ctx, labeler)
labeler, found := LabelerFromContext(ctx)
if !found {
ctx = InjectLabeler(ctx, labeler)
}

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

Expand Down

0 comments on commit 070f9d7

Please sign in to comment.