Skip to content

refactor(api-core): simplify OTel interceptor helpers and extract tracer provider - #18263

Merged
chalmerlowe merged 4 commits into
mainfrom
refactor/otel-tracing-concrete-interceptor-helpers
Sep 2, 2026
Merged

refactor(api-core): simplify OTel interceptor helpers and extract tracer provider#18263
chalmerlowe merged 4 commits into
mainfrom
refactor/otel-tracing-concrete-interceptor-helpers

Conversation

@chalmerlowe

@chalmerlowe chalmerlowe commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Summary

Refactors google.api_core._observability to simplify OpenTelemetry interceptor instantiation and use concrete return types:

  • Extracts _get_tracer_provider(client_options) to centralize tracer provider lookup across ClientOptions objects and dictionaries.
  • Removes the private _get_otel_interceptor(is_async: bool) helper, allowing get_otel_interceptor and get_otel_async_interceptor to directly invoke synchronous and asynchronous OpenTelemetry APIs with concrete return types.
  • Adds dedicated unit tests for _get_tracer_provider and maintains symmetrical tests for sync and async interceptor getters.

Addresses review feedback on #18236.

@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 refactors the internal observability helper _get_otel_interceptor into a simpler _get_tracer_provider function, which extracts the OpenTelemetry tracer provider from client options. The public functions get_otel_interceptor and get_otel_async_interceptor are updated to instantiate the interceptors directly. While the refactoring simplifies the code, the unit tests for these public APIs were removed during the process. It is recommended to restore and update these unit tests to ensure proper test coverage and verify integration with the OpenTelemetry gRPC instrumentation APIs.

Comment thread packages/google-api-core/tests/unit/test_observability.py
@chalmerlowe
chalmerlowe force-pushed the refactor/otel-tracing-concrete-interceptor-helpers branch from b9d2007 to 1a2f542 Compare September 2, 2026 12:48
@chalmerlowe chalmerlowe changed the title refactor(api-core): simplify OTel interceptor helpers and use concrete types refactor(api-core): simplify OTel interceptor helpers and extract tracer provider Sep 2, 2026
@chalmerlowe
chalmerlowe force-pushed the refactor/otel-tracing-concrete-interceptor-helpers branch from 1a2f542 to e6a6f08 Compare September 2, 2026 14:15
@chalmerlowe
chalmerlowe force-pushed the refactor/otel-tracing-concrete-interceptor-helpers branch from e6a6f08 to 6979e50 Compare September 2, 2026 14:22
@chalmerlowe
chalmerlowe marked this pull request as ready for review September 2, 2026 14:28
@chalmerlowe
chalmerlowe requested a review from a team as a code owner September 2, 2026 14:29
@chalmerlowe chalmerlowe self-assigned this Sep 2, 2026
Comment on lines +29 to +31
import opentelemetry.trace # noqa: F401

from google.api_core.grpc_helpers import ClientInterceptor # noqa: F401

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.

Please can you clarify the reason that we need noqa: F401

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Resolved.

return None

return _get_otel_interceptor(client_options, is_async=True)
import opentelemetry.instrumentation.grpc as otel_grpc # type: ignore[import-not-found]

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.

Please add a comment to clarify the reason for type: ignore[import-not-found]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Resolved.

Comment thread packages/google-api-core/google/api_core/_observability.py
Comment thread packages/google-api-core/google/api_core/_observability.py
Comment thread packages/google-api-core/google/api_core/_observability.py Outdated
@chalmerlowe
chalmerlowe merged commit 43731f3 into main Sep 2, 2026
109 checks passed
@chalmerlowe
chalmerlowe deleted the refactor/otel-tracing-concrete-interceptor-helpers branch September 2, 2026 16:36
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