From 6351d96691df56792157315629ddb47badaf01ba Mon Sep 17 00:00:00 2001 From: Denis Peshkov Date: Sun, 5 Oct 2025 15:02:33 +0400 Subject: [PATCH] fix: avoid modifying the original http.Request in RoundTripper According to the RoundTripper documentation, RoundTrip should not modify the request except for consuming and closing the Request's Body --- correlation/outbound_http.go | 3 ++- tracing/outbound_http.go | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/correlation/outbound_http.go b/correlation/outbound_http.go index a1369eb..65fb863 100644 --- a/correlation/outbound_http.go +++ b/correlation/outbound_http.go @@ -14,7 +14,7 @@ type instrumentedRoundTripper struct { config instrumentedRoundTripperConfig } -// injectRequest will pass the CorrelationId through to a downstream http request +// injectRequest will pass the CorrelationID through to a downstream http request // for propagation. func (c instrumentedRoundTripper) injectRequest(req *http.Request) { correlationID := ExtractFromContext(req.Context()) @@ -28,6 +28,7 @@ func (c instrumentedRoundTripper) injectRequest(req *http.Request) { } func (c instrumentedRoundTripper) RoundTrip(req *http.Request) (res *http.Response, e error) { + req = req.Clone(req.Context()) c.injectRequest(req) return c.delegate.RoundTrip(req) } diff --git a/tracing/outbound_http.go b/tracing/outbound_http.go index 6915dd6..ae9ac98 100644 --- a/tracing/outbound_http.go +++ b/tracing/outbound_http.go @@ -43,7 +43,7 @@ func (c tracingRoundTripper) RoundTrip(req *http.Request) (res *http.Response, e // attach ClientTrace to the Context, and Context to request trace := newClientTrace(span) ctx = httptrace.WithClientTrace(ctx, trace) - req = req.WithContext(ctx) + req = req.Clone(ctx) ext.SpanKindRPCClient.Set(span) ext.HTTPUrl.Set(span, req.URL.String()) -- GitLab