Skip to content

Commit 0ba3fdd

Browse files
committed
feat(transport/http): Add ability to specify []otelhttp.Opts for transport when create new HTTP client
1 parent 70d3b4f commit 0ba3fdd

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

internal/settings.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"time"
1616

1717
"cloud.google.com/go/auth"
18+
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
1819
"golang.org/x/oauth2"
1920
"golang.org/x/oauth2/google"
2021
"google.golang.org/api/internal/impersonate"
@@ -76,6 +77,9 @@ type DialSettings struct {
7677

7778
// TODO(b/372244283): Remove after b/358175516 has been fixed
7879
EnableAsyncRefreshDryRun func()
80+
81+
// otelhttp options
82+
OtelHTTPOpts []otelhttp.Option
7983
}
8084

8185
// GetScopes returns the user-provided scopes, if set, or else falls back to the

option/option.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"net/http"
1212

1313
"cloud.google.com/go/auth"
14+
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
1415
"golang.org/x/oauth2"
1516
"golang.org/x/oauth2/google"
1617
"google.golang.org/api/internal"
@@ -414,3 +415,17 @@ type withLogger struct{ l *slog.Logger }
414415
func (w withLogger) Apply(o *internal.DialSettings) {
415416
o.Logger = w.l
416417
}
418+
419+
// WithOtelHTTPOpts returns a ClientOption that sets the options for
420+
// the OpenTelemetry HTTP client. This option is used to configure
421+
// the OpenTelemetry HTTP client instrumentation.
422+
func WithOtelHTTPOpts(opts ...otelhttp.Option) ClientOption {
423+
return withOtelHTTPOpts{opts}
424+
}
425+
426+
type withOtelHTTPOpts struct{ opts []otelhttp.Option }
427+
428+
func (w withOtelHTTPOpts) Apply(o *internal.DialSettings) {
429+
o.OtelHTTPOpts = make([]otelhttp.Option, len(w.opts))
430+
copy(o.OtelHTTPOpts, w.opts)
431+
}

option/option_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/google/go-cmp/cmp"
1414
"github.com/google/go-cmp/cmp/cmpopts"
15+
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
1516
"golang.org/x/oauth2/google"
1617
"google.golang.org/api/internal"
1718
"google.golang.org/grpc"
@@ -133,3 +134,29 @@ func TestApplyClientCertSource(t *testing.T) {
133134
t.Error(cmp.Diff(certGot, certWant, cmpopts.IgnoreUnexported(big.Int{}), cmpopts.IgnoreFields(tls.Certificate{}, "Leaf")))
134135
}
135136
}
137+
138+
func TestOtelHTTPOpts(t *testing.T) {
139+
otelhttpopts := []otelhttp.Option{
140+
otelhttp.WithServerName("test"),
141+
}
142+
opts := []ClientOption{
143+
WithOtelHTTPOpts(otelhttpopts...),
144+
}
145+
var got internal.DialSettings
146+
for _, opt := range opts {
147+
opt.Apply(&got)
148+
}
149+
want := internal.DialSettings{
150+
OtelHTTPOpts: []otelhttp.Option{
151+
otelhttp.WithServerName("test"),
152+
},
153+
}
154+
155+
comparer := cmp.Comparer(func(x, y internal.DialSettings) bool {
156+
return len(x.OtelHTTPOpts) == len(y.OtelHTTPOpts)
157+
})
158+
159+
if !cmp.Equal(got, want, comparer) {
160+
t.Error(cmp.Diff(got, want, comparer))
161+
}
162+
}

transport/http/dial.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ func addOpenTelemetryTransport(trans http.RoundTripper, settings *internal.DialS
306306
if settings.TelemetryDisabled {
307307
return trans
308308
}
309-
return otelhttp.NewTransport(trans)
309+
return otelhttp.NewTransport(trans, settings.OtelHTTPOpts...)
310310
}
311311

312312
// clonedTransport returns the given RoundTripper as a cloned *http.Transport.

0 commit comments

Comments
 (0)