Skip to content

Commit

Permalink
fix: reuse OpenTelemetry-wrapped http.Transport (#802)
Browse files Browse the repository at this point in the history
  • Loading branch information
alnr authored Aug 6, 2024
1 parent 8049ccf commit 52f7d77
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
9 changes: 0 additions & 9 deletions httpx/resilient_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ import (
"io"
"log"
"net/http"
"net/http/httptrace"
"time"

"go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/trace"
"golang.org/x/oauth2"

Expand Down Expand Up @@ -130,12 +127,6 @@ func NewResilientClient(opts ...ResilientOptions) *retryablehttp.Client {
o.c.Transport = ifelse(o.ipV6, allowInternalAllowIPv6, allowInternalProhibitIPv6)
}

if o.tracer != nil {
o.c.Transport = otelhttp.NewTransport(o.c.Transport, otelhttp.WithClientTrace(func(ctx context.Context) *httptrace.ClientTrace {
return otelhttptrace.NewClientTrace(ctx, otelhttptrace.WithoutHeaders(), otelhttptrace.WithoutSubSpans())
}))
}

cl := retryablehttp.NewClient()
cl.HTTPClient = o.c
cl.Logger = o.l
Expand Down
17 changes: 13 additions & 4 deletions httpx/ssrf.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import (
"context"
"net"
"net/http"
"net/http/httptrace"
"net/netip"
"time"

"code.dny.dev/ssrf"
"github.com/gobwas/glob"
"go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)

var _ http.RoundTripper = (*noInternalIPRoundTripper)(nil)
Expand Down Expand Up @@ -64,7 +67,7 @@ func init() {
ssrf.WithAnyPort(),
ssrf.WithNetworks("tcp4", "tcp6"),
).Safe
prohibitInternalAllowIPv6 = t
prohibitInternalAllowIPv6 = otelTransport(t)
}

func init() {
Expand All @@ -76,7 +79,7 @@ func init() {
t.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
return d.DialContext(ctx, "tcp4", addr)
}
prohibitInternalProhibitIPv6 = t
prohibitInternalProhibitIPv6 = otelTransport(t)
}

func init() {
Expand All @@ -96,7 +99,7 @@ func init() {
netip.MustParsePrefix("fc00::/7"), // Unique Local (RFC 4193)
),
).Safe
allowInternalAllowIPv6 = t
allowInternalAllowIPv6 = otelTransport(t)
}

func init() {
Expand All @@ -119,7 +122,7 @@ func init() {
t.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
return d.DialContext(ctx, "tcp4", addr)
}
allowInternalProhibitIPv6 = t
allowInternalProhibitIPv6 = otelTransport(t)
}

func newDefaultTransport() (*http.Transport, *net.Dialer) {
Expand All @@ -137,3 +140,9 @@ func newDefaultTransport() (*http.Transport, *net.Dialer) {
ExpectContinueTimeout: 1 * time.Second,
}, &dialer
}

func otelTransport(t *http.Transport) http.RoundTripper {
return otelhttp.NewTransport(t, otelhttp.WithClientTrace(func(ctx context.Context) *httptrace.ClientTrace {
return otelhttptrace.NewClientTrace(ctx, otelhttptrace.WithoutHeaders(), otelhttptrace.WithoutSubSpans())
}))
}

0 comments on commit 52f7d77

Please sign in to comment.