google-api-go-client icon indicating copy to clipboard operation
google-api-go-client copied to clipboard

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

Open surik opened this issue 7 months ago • 5 comments

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:

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
}

surik avatar May 05 '25 11:05 surik

@surik Thank you for opening this feature request. Can you expand it to also somehow include go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc.Option?

quartzmo avatar May 05 '25 19:05 quartzmo

@quartzmo expanded. I haven't looked at how to implement for otelgrpc, but I believe this will be very similar to otelhttp

surik avatar May 06 '25 08:05 surik

@surik Thank you! I will try to move this forward.

quartzmo avatar May 06 '25 14:05 quartzmo

@surik We can move forward with trying to find a solution for this. An important constraint will be to avoid exporting the dependency on any go.opentelemetry.io packages from the google.golang.org/api/option package. Do you think WithOpenTelemetryOpts could accept a variadic of empty interface type, the contents of which would be later typecast to either otelhttp or otelgrpc options?

quartzmo avatar May 08 '25 23:05 quartzmo

@quartzmo, please take a look at this commit https://github.com/googleapis/google-api-go-client/pull/3130/commits/334fb105d3ddd7a73775cdc87df8932c67ee485a

surik avatar May 12 '25 15:05 surik