googleapis / googleapis/google-api-go-client
Support passing otelhttp.Option and otelgrpc.Option for OpenTelemetry instrumentation of clients
- Dominant language
- Go
- Stars
- 4.5k
- Forks
- 1.5k
- Avg merge
- 1d 15h
- Merged PRs (30d)
- 32
Description
**Is your feature request related to a problem? Please describe.**
Currently, OpenTelemetry is enabled by default, but there is no way to pass a custom slice of `otelhttp.Option` or `otelgrpc.Option`, such as a custom tracer provider. This limits flexibility for applications that must integrate with specific OpenTelemetry configurations (e.g., having multiple providers, custom propagators, or tailored span settings for gRPC or HTTP separately).
**Describe the solution you’d like**
Allow users to pass slices of both `otelhttp.Option` and `otelgrpc.Option` when creating a Google API client, enabling customization of the OpenTelemetry instrumentation across both HTTP and gRPC transports. This would allow configuration of tracer providers, propagators, span name formatters, and more, per transport layer.
**Describe alternatives you’ve considered**
- Using a global tracer provider. This doesn’t allow for per-client or per-subsystem customization.
- Wrapping the HTTP transport manually using `option.WithHTTPClient` is technically possible. Doing so requires re-implementing other pieces like authentication, retry logic, and context propagation, which introduces boilerplate and increases the chance of errors.
- For gRPC, manually configuring interceptors is possible using `option.WithGRPCDialOption`, but requires users to understand and reconstruct internal dialing logic, making it error-prone.
**Additional context**
I’ve submitted a pull request that adds this functionality for HTTP: https://github.com/googleapis/google-api-go-client/pull/3130.
It adds support for passing `otelhttp.Option` via a new `WithOpenTelemetryOpts` helper.
Example for HTTP:
```go
import (
"context"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"cloud.google.com/go/bigquery"
"google.golang.org/api/option"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)
func newBigQueryClient(ctx context.Context) (*bigquery.Client, error) {
tp := sdktrace.NewTracerProvider()
client, err := bigquery.NewClient(ctx, "my-project-id",
option.WithOpenTelemetryOpts(
otelhttp.WithTracerProvider(tp),
),
)
if err != nil {
return nil, err
}
return client, nil
}
```
Contributor guide
Assessment
This issue has not been assessed yet.