Skip to content

Commit 048ca08

Browse files
committed
Implement OpenTelemetryOpts as variadic to support otelhttp and otelgrpc
1 parent 0ba3fdd commit 048ca08

File tree

5 files changed

+37
-31
lines changed

5 files changed

+37
-31
lines changed

internal/settings.go

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

1717
"cloud.google.com/go/auth"
18-
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
1918
"golang.org/x/oauth2"
2019
"golang.org/x/oauth2/google"
2120
"google.golang.org/api/internal/impersonate"
@@ -78,8 +77,8 @@ type DialSettings struct {
7877
// TODO(b/372244283): Remove after b/358175516 has been fixed
7978
EnableAsyncRefreshDryRun func()
8079

81-
// otelhttp options
82-
OtelHTTPOpts []otelhttp.Option
80+
// otelhttp/otelgrpc options
81+
OpenTelemetryOpts []any
8382
}
8483

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

option/option.go

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

1313
"cloud.google.com/go/auth"
14-
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
1514
"golang.org/x/oauth2"
1615
"golang.org/x/oauth2/google"
1716
"google.golang.org/api/internal"
@@ -416,16 +415,17 @@ func (w withLogger) Apply(o *internal.DialSettings) {
416415
o.Logger = w.l
417416
}
418417

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}
418+
// WithOpenTelemetryOpts returns a ClientOption that sets the options for
419+
// the OpenTelemetry HTTP/gRPC client. This option is used to configure
420+
// the OpenTelemetry HTTP/gRPC client instrumentation.
421+
// It can accept any number of options, which can be otelhttp.Option or otelgrpc.Option.
422+
func WithOpenTelemetryOpts(opts ...any) ClientOption {
423+
return withOpenTelemetryOpts{opts}
424424
}
425425

426-
type withOtelHTTPOpts struct{ opts []otelhttp.Option }
426+
type withOpenTelemetryOpts struct{ opts []any }
427427

428-
func (w withOtelHTTPOpts) Apply(o *internal.DialSettings) {
429-
o.OtelHTTPOpts = make([]otelhttp.Option, len(w.opts))
430-
copy(o.OtelHTTPOpts, w.opts)
428+
func (w withOpenTelemetryOpts) Apply(o *internal.DialSettings) {
429+
o.OpenTelemetryOpts = make([]any, len(w.opts))
430+
copy(o.OpenTelemetryOpts, w.opts)
431431
}

option/option_test.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ 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"
1615
"golang.org/x/oauth2/google"
1716
"google.golang.org/api/internal"
1817
"google.golang.org/grpc"
@@ -135,28 +134,22 @@ func TestApplyClientCertSource(t *testing.T) {
135134
}
136135
}
137136

138-
func TestOtelHTTPOpts(t *testing.T) {
139-
otelhttpopts := []otelhttp.Option{
140-
otelhttp.WithServerName("test"),
137+
func TestOpenTelemetryOpts(t *testing.T) {
138+
otelOpts := []any{
139+
"non-otelhttp-otelgrpc-option",
141140
}
142141
opts := []ClientOption{
143-
WithOtelHTTPOpts(otelhttpopts...),
142+
WithOpenTelemetryOpts(otelOpts...),
144143
}
145144
var got internal.DialSettings
146145
for _, opt := range opts {
147146
opt.Apply(&got)
148147
}
149148
want := internal.DialSettings{
150-
OtelHTTPOpts: []otelhttp.Option{
151-
otelhttp.WithServerName("test"),
152-
},
149+
OpenTelemetryOpts: []any{},
153150
}
154151

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))
152+
if cmp.Equal(got, want) {
153+
t.Error(cmp.Diff(got, want))
161154
}
162155
}

transport/grpc/dial.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ var (
6868

6969
// otelGRPCStatsHandler returns singleton otelStatsHandler for reuse across all
7070
// dial connections.
71-
func otelGRPCStatsHandler() stats.Handler {
71+
func otelGRPCStatsHandler(opts []otelgrpc.Option) stats.Handler {
7272
initOtelStatsHandlerOnce.Do(func() {
73-
otelStatsHandler = otelgrpc.NewClientHandler()
73+
otelStatsHandler = otelgrpc.NewClientHandler(opts...)
7474
})
7575
return otelStatsHandler
7676
}
@@ -400,7 +400,13 @@ func addOpenTelemetryStatsHandler(opts []grpc.DialOption, settings *internal.Dia
400400
if settings.TelemetryDisabled {
401401
return opts
402402
}
403-
return append(opts, grpc.WithStatsHandler(otelGRPCStatsHandler()))
403+
otelOpts := []otelgrpc.Option{}
404+
for _, opt := range settings.OpenTelemetryOpts {
405+
if opt, ok := opt.(otelgrpc.Option); ok {
406+
otelOpts = append(otelOpts, opt)
407+
}
408+
}
409+
return append(opts, grpc.WithStatsHandler(otelGRPCStatsHandler(otelOpts)))
404410
}
405411

406412
// grpcTokenSource supplies PerRPCCredentials from an oauth.TokenSource.

transport/http/dial.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,15 @@ func addOpenTelemetryTransport(trans http.RoundTripper, settings *internal.DialS
306306
if settings.TelemetryDisabled {
307307
return trans
308308
}
309-
return otelhttp.NewTransport(trans, settings.OtelHTTPOpts...)
309+
310+
opts := []otelhttp.Option{}
311+
for _, opt := range settings.OpenTelemetryOpts {
312+
if opt, ok := opt.(otelhttp.Option); ok {
313+
opts = append(opts, opt)
314+
}
315+
}
316+
317+
return otelhttp.NewTransport(trans, opts...)
310318
}
311319

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

0 commit comments

Comments
 (0)