-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(api-core): add tracer_provider to ClientOptions for OTel support #18139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
45ef7ac
5a84a8f
343e4ca
9fa7af9
5f8f97d
ef05d0e
85d4784
7a894d8
ed18bac
0f0e6e7
bac7f7d
f9caa92
8caf428
7c8e58a
053fde9
f96333a
5fdd203
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
|
|
||
| """OpenTelemetry helpers for resolving and instantiating interceptors.""" | ||
|
|
||
| from typing import Any, Optional | ||
|
|
||
| from google.api_core import _feature_gating_helpers | ||
| from google.api_core.client_options import ClientOptions | ||
|
|
||
| _TRACER_PROVIDER = "tracer_provider" | ||
|
|
||
|
|
||
| def is_otel_capabilities_enabled( | ||
| client_options: Optional[ClientOptions | dict[str, Any]] = None, | ||
| env_var: str = "GOOGLE_CLOUD_PYTHON_TRACING_ENABLED", | ||
| ) -> bool: | ||
| """Checks if OTel capabilities are enabled and installed. | ||
|
|
||
| Args: | ||
| client_options: The client options object or dictionary. | ||
| env_var: The environment variable to check for enablement. | ||
|
|
||
| Returns: | ||
| bool: True if enabled and installed, False otherwise. | ||
| """ | ||
| is_tracing_enabled = _feature_gating_helpers.resolve_feature_flags( | ||
| env_var=env_var, | ||
| feature_key=_TRACER_PROVIDER, | ||
| configuration=client_options, | ||
| ) | ||
|
|
||
| if is_tracing_enabled: | ||
| try: | ||
| import opentelemetry.instrumentation.grpc as otel_grpc # type: ignore[import-not-found] # noqa: F401 | ||
|
|
||
| return True | ||
| except ImportError: | ||
| pass | ||
|
|
||
| return False | ||
|
|
||
|
|
||
| def apply_otel_capabilities_to_channel( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we have an async version of this?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @daniel-sanche
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need to include async code now, but I do think we need to at least consider it in our designs, so it doesn't diverge too much. IIRC, async channels can't be mutated, so we probably shouldn't build a system around this API. Maybe we should just build and return the interceptor to the client, and let it decide how to add it to the channel?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It may be worth clarifying a couple of specific constraints we bumped into regarding how OpenTelemetry handles gRPC in Python. No Mutation in Place: the The TypeError Trap: The main reason we can't just build and return the interceptor to be processed by standard transport pipelines is a specific quirk of the OTel Python SDK. OTel's gRPC interceptors use custom protocols ( Why Eager Wrapping: We are forced to use OTel's specialized Regarding Async Forward-Compatibility: When we tackle async in the next phase, we may have more flexibility. OTel's async interceptors (
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting, I didn't catch that quirk about otel_grpc wrapping. I still have concerns about what this will look like for async, but yeah, we can deal with that when we get there |
||
| channel: Any, | ||
| client_options: Optional[ClientOptions | dict[str, Any]] = None, | ||
| ) -> Any: | ||
| """Applies OTel capabilities (like tracing) to the channel. | ||
|
|
||
| Precondition: This function assumes `is_otel_capabilities_enabled` has already | ||
| been called and returned `True`, i.e. in the Client. At this time | ||
| this function is not intended to be standalone. | ||
|
|
||
| Args: | ||
| channel: The raw gRPC channel to wrap. | ||
| client_options: The client options object or dictionary. | ||
|
|
||
| Returns: | ||
| Any: The intercepted channel. | ||
|
|
||
| Raises: | ||
| ImportError: If OpenTelemetry packages are not installed and this function | ||
| is called directly (bypassing the precondition). | ||
| """ | ||
|
daniel-sanche marked this conversation as resolved.
|
||
| import opentelemetry.instrumentation.grpc as otel_grpc # type: ignore[import-not-found] | ||
|
|
||
| tracer_provider = None | ||
| if isinstance(client_options, dict): | ||
| tracer_provider = client_options.get(_TRACER_PROVIDER) | ||
| elif client_options is not None: | ||
| tracer_provider = getattr(client_options, _TRACER_PROVIDER, None) | ||
|
|
||
| interceptor = otel_grpc.client_interceptor(tracer_provider=tracer_provider) | ||
|
|
||
| # We use OTel's own compatible applier to avoid standard gRPC TypeError. | ||
| return otel_grpc.intercept_channel(channel, interceptor) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import sys | ||
| from unittest import mock | ||
|
|
||
| from google.api_core import _observability | ||
| from google.api_core.client_options import ClientOptions | ||
|
|
||
|
|
||
| def test_is_otel_capabilities_enabled_disabled(monkeypatch): | ||
| monkeypatch.setenv("GOOGLE_CLOUD_PYTHON_TRACING_ENABLED", "false") | ||
| assert not _observability.is_otel_capabilities_enabled() | ||
|
|
||
|
|
||
| def test_is_otel_capabilities_enabled_otel_missing(monkeypatch): | ||
| monkeypatch.setenv("GOOGLE_CLOUD_PYTHON_TRACING_ENABLED", "true") | ||
| # Simulate OTel not being installed by blocking imports | ||
| monkeypatch.setitem(sys.modules, "opentelemetry.instrumentation.grpc", None) | ||
|
|
||
| assert not _observability.is_otel_capabilities_enabled() | ||
|
|
||
|
|
||
| def test_is_otel_capabilities_enabled_otel_installed(monkeypatch): | ||
| monkeypatch.setenv("GOOGLE_CLOUD_PYTHON_TRACING_ENABLED", "true") | ||
|
|
||
| mock_otel = mock.Mock() | ||
| mock_otel_grpc = mock_otel.instrumentation.grpc | ||
|
|
||
| monkeypatch.setitem(sys.modules, "opentelemetry", mock_otel) | ||
| monkeypatch.setitem( | ||
| sys.modules, "opentelemetry.instrumentation", mock_otel.instrumentation | ||
| ) | ||
| monkeypatch.setitem( | ||
| sys.modules, "opentelemetry.instrumentation.grpc", mock_otel_grpc | ||
| ) | ||
|
|
||
| assert _observability.is_otel_capabilities_enabled() | ||
|
|
||
|
|
||
| def test_apply_otel_capabilities_to_channel_enabled_otel_installed(monkeypatch): | ||
| mock_channel = mock.Mock() | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NOTE TO REVIEWERS: We are aware that there is significant duplication in these three tests. With confirmation in hand we are happy to update these tests to simplify them, create mock fixtures, parametrize them where possible, but do not want to invest the time until we are confident that the approach will be approved. Happy to consider updating the tests here OR in a fast follow PR. |
||
| mock_intercepted_channel = mock.Mock() | ||
|
|
||
| mock_otel = mock.Mock() | ||
| mock_otel_grpc = mock_otel.instrumentation.grpc | ||
| mock_interceptor = mock.Mock() | ||
|
|
||
| mock_otel_grpc.client_interceptor.return_value = mock_interceptor | ||
| mock_otel_grpc.intercept_channel.return_value = mock_intercepted_channel | ||
|
|
||
| monkeypatch.setitem(sys.modules, "opentelemetry", mock_otel) | ||
| monkeypatch.setitem( | ||
| sys.modules, "opentelemetry.instrumentation", mock_otel.instrumentation | ||
| ) | ||
| monkeypatch.setitem( | ||
| sys.modules, "opentelemetry.instrumentation.grpc", mock_otel_grpc | ||
| ) | ||
|
|
||
| result = _observability.apply_otel_capabilities_to_channel(mock_channel) | ||
|
|
||
| assert result is mock_intercepted_channel | ||
| mock_otel_grpc.client_interceptor.assert_called_once_with(tracer_provider=None) | ||
| mock_otel_grpc.intercept_channel.assert_called_once_with( | ||
| mock_channel, mock_interceptor | ||
| ) | ||
|
|
||
|
|
||
| def test_apply_otel_capabilities_to_channel_enabled_via_config(monkeypatch): | ||
| # Tracing enabled via config (tracer_provider is set) | ||
| mock_tracer_provider = object() | ||
| options = ClientOptions(tracer_provider=mock_tracer_provider) | ||
|
|
||
| mock_channel = mock.Mock() | ||
| mock_intercepted_channel = mock.Mock() | ||
|
|
||
| mock_otel = mock.Mock() | ||
| mock_otel_grpc = mock_otel.instrumentation.grpc | ||
| mock_interceptor = mock.Mock() | ||
|
|
||
| mock_otel_grpc.client_interceptor.return_value = mock_interceptor | ||
| mock_otel_grpc.intercept_channel.return_value = mock_intercepted_channel | ||
|
|
||
| monkeypatch.setitem(sys.modules, "opentelemetry", mock_otel) | ||
| monkeypatch.setitem( | ||
| sys.modules, "opentelemetry.instrumentation", mock_otel.instrumentation | ||
| ) | ||
| monkeypatch.setitem( | ||
| sys.modules, "opentelemetry.instrumentation.grpc", mock_otel_grpc | ||
| ) | ||
|
|
||
| result = _observability.apply_otel_capabilities_to_channel( | ||
| mock_channel, client_options=options | ||
| ) | ||
|
|
||
| assert result is mock_intercepted_channel | ||
| mock_otel_grpc.client_interceptor.assert_called_once_with( | ||
| tracer_provider=mock_tracer_provider | ||
| ) | ||
| mock_otel_grpc.intercept_channel.assert_called_once_with( | ||
| mock_channel, mock_interceptor | ||
| ) | ||
|
|
||
|
|
||
| def test_apply_otel_capabilities_to_channel_enabled_via_dict_config(monkeypatch): | ||
| # Tracing enabled via dict config | ||
| mock_tracer_provider = object() | ||
| options = {"tracer_provider": mock_tracer_provider} | ||
|
|
||
| mock_channel = mock.Mock() | ||
| mock_intercepted_channel = mock.Mock() | ||
|
|
||
| mock_otel = mock.Mock() | ||
| mock_otel_grpc = mock_otel.instrumentation.grpc | ||
| mock_interceptor = mock.Mock() | ||
|
|
||
| mock_otel_grpc.client_interceptor.return_value = mock_interceptor | ||
| mock_otel_grpc.intercept_channel.return_value = mock_intercepted_channel | ||
|
|
||
| monkeypatch.setitem(sys.modules, "opentelemetry", mock_otel) | ||
| monkeypatch.setitem( | ||
| sys.modules, "opentelemetry.instrumentation", mock_otel.instrumentation | ||
| ) | ||
| monkeypatch.setitem( | ||
| sys.modules, "opentelemetry.instrumentation.grpc", mock_otel_grpc | ||
| ) | ||
|
|
||
| result = _observability.apply_otel_capabilities_to_channel( | ||
| mock_channel, client_options=options | ||
| ) | ||
|
|
||
| assert result is mock_intercepted_channel | ||
| mock_otel_grpc.client_interceptor.assert_called_once_with( | ||
| tracer_provider=mock_tracer_provider | ||
| ) | ||
| mock_otel_grpc.intercept_channel.assert_called_once_with( | ||
| mock_channel, mock_interceptor | ||
| ) | ||
Uh oh!
There was an error while loading. Please reload this page.