-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(api-core): Opentelemetry tracing support for gRPC transports in google-api-core #18069
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
Draft
chalmerlowe
wants to merge
9
commits into
main
Choose a base branch
from
feat/otel-tracing-core-infra
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f2ecbc0
feat(api_core): Add tracer_provider to ClientOptions
chalmerlowe f8e5f4c
feat(api_core): Plumb tracer_provider in grpc_helpers.create_channel
chalmerlowe 49a88ff
fix(api_core): Prevent TypeError in grpc_helpers_async.create_channel
chalmerlowe 73f91fe
test(api_core): Add OTel gRPC helpers tests
chalmerlowe ca1559b
chore(api_core): Clean up imports in tests
chalmerlowe 6c3cca8
refactor(api_core): Parametrize OTel installation and enablement tests
chalmerlowe 8297044
Update packages/google-api-core/google/api_core/grpc_helpers_async.py
chalmerlowe d019dd3
Update packages/google-api-core/google/api_core/grpc_helpers_async.py
chalmerlowe 182da98
fix(api_core): Use grpc.intercept_channel instead of invalid otel_grp…
chalmerlowe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,3 +12,4 @@ requests==2.33.0 | |
| grpcio==1.59.0 | ||
| grpcio-status==1.59.0 | ||
| proto-plus==1.26.1 | ||
| opentelemetry-api==1.27.0 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,3 +13,4 @@ grpcio==1.59.0 | |
| grpcio-status==1.59.0 | ||
| proto-plus==1.26.1 | ||
| aiohttp==3.13.4 | ||
| opentelemetry-api==1.27.0 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,7 +33,6 @@ | |
|
|
||
|
|
||
| import google.auth.credentials | ||
|
|
||
| from google.api_core import exceptions, grpc_helpers_async | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
packages/google-api-core/tests/unit/test_grpc_helpers_otel.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| # 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. | ||
|
|
||
| """Tests for OpenTelemetry gRPC interceptor integration in google-api-core.""" | ||
|
|
||
| import sys | ||
| import types | ||
| from unittest import mock | ||
|
|
||
| import pytest | ||
|
|
||
| try: | ||
| from google.api_core import grpc_helpers | ||
|
|
||
| HAS_GRPC_HELPERS = True | ||
| except ImportError: | ||
| HAS_GRPC_HELPERS = False | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_otel_grpc(monkeypatch): | ||
| """Fixture to mock OpenTelemetry gRPC hierarchy.""" | ||
| mock_otel = mock.Mock() | ||
| mock_otel_grpc = mock_otel.instrumentation.grpc | ||
| mock_interceptor = mock.Mock() | ||
| mock_otel_grpc.client_interceptor.return_value = mock_interceptor | ||
|
|
||
| modules = { | ||
| "opentelemetry": mock_otel, | ||
| "opentelemetry.instrumentation": mock_otel.instrumentation, | ||
| "opentelemetry.instrumentation.grpc": mock_otel_grpc, | ||
| } | ||
|
|
||
| for name, mod in modules.items(): | ||
| monkeypatch.setitem(sys.modules, name, mod) | ||
|
|
||
| return mock_otel_grpc | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "is_otel_installed, tracing_env_var_value, expect_otel_interceptor", | ||
| [ | ||
| pytest.param(True, "true", True, id="installed_and_enabled"), | ||
| pytest.param(True, "false", False, id="installed_but_disabled"), | ||
| pytest.param(False, "true", False, id="not_installed_fails_open"), | ||
| ], | ||
| ) | ||
| @pytest.mark.skipif(not HAS_GRPC_HELPERS, reason="Requires google-api-core[grpc]") | ||
| def test_create_channel_otel_combos( | ||
| monkeypatch, | ||
| mock_otel_grpc, | ||
| is_otel_installed, | ||
| tracing_env_var_value, | ||
| expect_otel_interceptor, | ||
| ): | ||
| """Verify create_channel behavior with various OTel installation and enablement states.""" | ||
|
|
||
| monkeypatch.setenv("GOOGLE_CLOUD_PYTHON_TRACING_ENABLED", tracing_env_var_value) | ||
|
|
||
| if not is_otel_installed: | ||
| monkeypatch.setitem(sys.modules, "opentelemetry.instrumentation.grpc", None) | ||
|
|
||
| mock_channel = "raw_channel" | ||
| with mock.patch( | ||
| "grpc.secure_channel", return_value=mock_channel | ||
| ) as mock_secure_channel, mock.patch( | ||
| "grpc.intercept_channel", side_effect=lambda ch, inc: f"wrapped_{ch}" | ||
| ) as mock_intercept_channel: | ||
| with mock.patch( | ||
| "google.api_core.grpc_helpers._create_composite_credentials", | ||
| return_value=mock.Mock(), | ||
| ): | ||
| channel = grpc_helpers.create_channel("localhost:1234") | ||
|
|
||
| # Always expect raw channel creation | ||
| mock_secure_channel.assert_called_once() | ||
|
|
||
| if expect_otel_interceptor: | ||
| mock_otel_grpc.client_interceptor.assert_called_once() | ||
| mock_intercept_channel.assert_called_once_with( | ||
| mock_channel, mock_otel_grpc.client_interceptor.return_value | ||
| ) | ||
| assert channel == f"wrapped_{mock_channel}" | ||
| else: | ||
| # OTel should NOT have been called | ||
| mock_intercept_channel.assert_not_called() | ||
| assert channel == mock_channel | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "config_factory", | ||
| [ | ||
| lambda tp: {"tracer_provider": tp}, | ||
| lambda tp: types.SimpleNamespace(tracer_provider=tp), | ||
| ], | ||
| ids=["dict", "object"], | ||
| ) | ||
| @pytest.mark.skipif(not HAS_GRPC_HELPERS, reason="Requires google-api-core[grpc]") | ||
| def test_create_channel_with_custom_tracer_provider(monkeypatch, mock_otel_grpc, config_factory): | ||
| """Verify that create_channel passes custom tracer_provider to OTel interceptor.""" | ||
|
|
||
| mock_tracer_provider = mock.Mock() | ||
| config = config_factory(mock_tracer_provider) | ||
|
|
||
| mock_channel = "raw_channel" | ||
| with mock.patch("grpc.secure_channel", return_value=mock_channel), mock.patch( | ||
| "grpc.intercept_channel", side_effect=lambda ch, inc: f"wrapped_{ch}" | ||
| ): | ||
| with mock.patch("google.api_core.grpc_helpers._create_composite_credentials", return_value=mock.Mock()): | ||
| grpc_helpers.create_channel("localhost:1234", configuration=config) | ||
|
|
||
| mock_otel_grpc.client_interceptor.assert_called_once_with(tracer_provider=mock_tracer_provider) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.