Skip to content

Support passing otelhttp.Option and otelgrpc.Option for OpenTelemetry instrumentation of clients #3138

Open
@surik

Description

@surik

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: #3130.
It adds support for passing otelhttp.Option via a new WithOpenTelemetryOpts helper.

Example for HTTP:

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
}

Metadata

Metadata

Assignees

Labels

priority: p3Desirable enhancement or fix. May not be included in next release.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions