Skip to content

feat(api-core): add ClientInterceptor and apply_interceptors helper - #18236

Open
chalmerlowe wants to merge 5 commits into
mainfrom
feat/otel-tracing-centralized-interceptor
Open

feat(api-core): add ClientInterceptor and apply_interceptors helper#18236
chalmerlowe wants to merge 5 commits into
mainfrom
feat/otel-tracing-centralized-interceptor

Conversation

@chalmerlowe

@chalmerlowe chalmerlowe commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Problem

Generated client libraries currently lack a standardized, centralized helper in google-api-core to apply client interceptors to a gRPC channel. Without a shared helper, each client library would need duplicate channel interceptor wrapping logic.

Solution

This change introduces interceptor utilities in google.api_core.grpc_helpers:

  • ClientInterceptor: A generic type alias representing client-side gRPC interceptors across unary and streaming modes.
  • apply_interceptors: A utility function that applies an optional list of interceptors to a gRPC channel in a single call via grpc.intercept_channel(channel, *interceptors). If no interceptors are provided, it returns the original channel unmodified.
  • We include comprehensive unit tests covering passthrough behavior and single/multiple interceptor application.

Notes for Reviewers

  • Passing all interceptors unpacked into grpc.intercept_channel(channel, *interceptors) delegates directly to gRPC's native variadic API and ensures standard execution order where the first interceptor in the sequence executes first on outbound requests.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces the apply_interceptors helper function to sequentially apply a list of client interceptors to a gRPC channel, along with comprehensive unit tests verifying its behavior. The reviewer feedback correctly points out that applying interceptors sequentially in a loop introduces unnecessary nesting overhead and reverses the standard gRPC execution order. To resolve this, the reviewer suggests unpacking the interceptors directly into a single grpc.intercept_channel call and updating the corresponding execution order test assertion.

Comment thread packages/google-api-core/google/api_core/grpc_helpers.py
Comment thread packages/google-api-core/tests/unit/test_grpc_helpers.py Outdated
@chalmerlowe
chalmerlowe marked this pull request as ready for review August 27, 2026 18:00
@chalmerlowe
chalmerlowe requested a review from a team as a code owner August 27, 2026 18:00

def apply_interceptors(
channel: grpc.Channel,
interceptors: Optional[Sequence[ClientInterceptor]] = None,

@daniel-sanche daniel-sanche Aug 27, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In your other PR, I suggested accepting Callable wrappers here as an alternate interceptor type, to support the otel interceptor.

That would make this into something like:

modified_channel = channel
for interceptor in interceptors or []:
    if isinstance(interceptor, ClientInterceptor):
        modified_channel = grpc.intercept_channel(channel, interceptor)
    else:
        modified_channel = interceptor(modified_channel)
return modified channel

Let me know if you think that could work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants