refactor(api-core): simplify OTel interceptor helpers and extract tracer provider - #18263
Conversation
There was a problem hiding this comment.
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.
b9d2007 to
1a2f542
Compare
1a2f542 to
e6a6f08
Compare
e6a6f08 to
6979e50
Compare
| import opentelemetry.trace # noqa: F401 | ||
|
|
||
| from google.api_core.grpc_helpers import ClientInterceptor # noqa: F401 |
There was a problem hiding this comment.
Please can you clarify the reason that we need noqa: F401
| return None | ||
|
|
||
| return _get_otel_interceptor(client_options, is_async=True) | ||
| import opentelemetry.instrumentation.grpc as otel_grpc # type: ignore[import-not-found] |
There was a problem hiding this comment.
Please add a comment to clarify the reason for type: ignore[import-not-found]
Summary
Refactors
google.api_core._observabilityto simplify OpenTelemetry interceptor instantiation and use concrete return types:_get_tracer_provider(client_options)to centralize tracer provider lookup acrossClientOptionsobjects and dictionaries._get_otel_interceptor(is_async: bool)helper, allowingget_otel_interceptorandget_otel_async_interceptorto directly invoke synchronous and asynchronous OpenTelemetry APIs with concrete return types._get_tracer_providerand maintains symmetrical tests for sync and async interceptor getters.Addresses review feedback on #18236.