Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reuse OpenTelemetry-wrapped http.Transport #802

Merged
merged 1 commit into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
}))
}
Loading