You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(api-core): add channel orchestration for OpenTelemetry (B) (#18237)
This pull request introduces OpenTelemetry helper functions in
`google.api_core._observability` to produce channel wrappers for
synchronous gRPC channels and interceptors for asynchronous gRPC
channels.
### Problem
Generated client libraries need a consistent and maintainable way to
instrument gRPC channels with OpenTelemetry tracing when enabled via
environment variables or client options.
Because synchronous gRPC channels can be wrapped post-creation while
asynchronous gRPC channels require interceptors at channel creation
time, client transports need helpers that return the appropriate channel
wrapper or async interceptors without duplicating OpenTelemetry
resolution logic across client libraries.
### Solution
This pull request introduces the following helper functions in
`google.api_core._observability`:
1. `get_otel_channel_wrapper(client_options)`:
* Returns a channel-wrapping function (`Callable[[Channel], Channel]`)
for synchronous gRPC channels when OpenTelemetry tracing is enabled and
installed.
* Integrates with `grpc_helpers.apply_channel_wrappers` to wrap raw
channels using OpenTelemetry's `intercept_channel`.
2. `get_otel_async_interceptor(client_options)`:
* Returns a list of OpenTelemetry asynchronous client interceptors
(`aio_client_interceptors`) for use when constructing `grpc.aio`
channels.
3. `_get_otel_interceptor(client_options, is_async)`:
* Internal helper that extracts `tracer_provider` from `ClientOptions`
and creates the appropriate OpenTelemetry sync or async interceptors.
### Testing
* Added unit tests in `tests/unit/test_observability.py` covering:
* Sync and async interceptor extraction and `tracer_provider`
configuration.
* `get_otel_channel_wrapper` behavior when tracing is disabled, when
OpenTelemetry is not installed, and when tracing is enabled.
* Integration between `get_otel_channel_wrapper` and
`grpc_helpers.apply_channel_wrappers`.
* `get_otel_async_interceptor` behavior across disabled, missing, and
enabled states.
### Notes for Reviewers
* This PR builds upon PR #18236 (`ChannelWrapper` and
`apply_channel_wrappers`).
* `get_otel_channel_wrapper` returns a callable rather than modifying
the channel immediately, allowing transport layers to combine
OpenTelemetry wrapping with user-supplied custom channel wrappers.
0 commit comments