Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
f2ecbc0
feat(api_core): Add tracer_provider to ClientOptions
chalmerlowe Aug 11, 2026
f8e5f4c
feat(api_core): Plumb tracer_provider in grpc_helpers.create_channel
chalmerlowe Aug 11, 2026
49a88ff
fix(api_core): Prevent TypeError in grpc_helpers_async.create_channel
chalmerlowe Aug 11, 2026
73f91fe
test(api_core): Add OTel gRPC helpers tests
chalmerlowe Aug 11, 2026
ca1559b
chore(api_core): Clean up imports in tests
chalmerlowe Aug 11, 2026
6c3cca8
refactor(api_core): Parametrize OTel installation and enablement tests
chalmerlowe Aug 11, 2026
8297044
Update packages/google-api-core/google/api_core/grpc_helpers_async.py
chalmerlowe Aug 11, 2026
d019dd3
Update packages/google-api-core/google/api_core/grpc_helpers_async.py
chalmerlowe Aug 11, 2026
182da98
fix(api_core): Use grpc.intercept_channel instead of invalid otel_grp…
chalmerlowe Aug 11, 2026
061fdaa
updates comment
chalmerlowe Aug 13, 2026
9aa3a00
feat(api_core): add tracing extra and improve client options tests
chalmerlowe Aug 13, 2026
0309a90
chore(api_core): fix formatting/linter issues
chalmerlowe Aug 13, 2026
d2fac5d
feat(api_core): Add tracer_provider to ClientOptions
chalmerlowe Aug 11, 2026
f3d25e1
feat(api_core): Plumb tracer_provider in grpc_helpers.create_channel
chalmerlowe Aug 11, 2026
79ae051
fix(api_core): Prevent TypeError in grpc_helpers_async.create_channel
chalmerlowe Aug 11, 2026
e2adb4a
test(api_core): Add OTel gRPC helpers tests
chalmerlowe Aug 11, 2026
31c47df
chore(api_core): Clean up imports in tests
chalmerlowe Aug 11, 2026
22bb100
refactor(api_core): Parametrize OTel installation and enablement tests
chalmerlowe Aug 11, 2026
1894d3a
Update packages/google-api-core/google/api_core/grpc_helpers_async.py
chalmerlowe Aug 11, 2026
7e2cf55
Update packages/google-api-core/google/api_core/grpc_helpers_async.py
chalmerlowe Aug 11, 2026
aa14f31
fix(api_core): Use grpc.intercept_channel instead of invalid otel_grp…
chalmerlowe Aug 11, 2026
8db779b
updates comment
chalmerlowe Aug 13, 2026
9bc0115
feat(api_core): add tracing extra and improve client options tests
chalmerlowe Aug 13, 2026
080516e
chore(api_core): fix formatting/linter issues
chalmerlowe Aug 13, 2026
8819c13
test(api-core): add robust grpc server test for otel integration
chalmerlowe Aug 14, 2026
f247010
fix(api-core): use otel_grpc.intercept_channel, raise bounds, and ref…
chalmerlowe Aug 14, 2026
2f37cf1
Merge branch 'feat/otel-tracing-core-infra' into feat/otel-tracing-ro…
chalmerlowe Aug 14, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/google-api-core/google/api_core/client_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ class ClientOptions(object):
`googleapis.com`. If both `api_endpoint` and `universe_domain` are set,
then `api_endpoint` is used as the service endpoint. If `api_endpoint` is
not specified, the format will be `{service}.{universe_domain}`.
tracer_provider (Optional[object]): The OpenTelemetry TracerProvider to use
for tracing. If not set, the global tracer provider is used, if
available.

Raises:
ValueError: If both ``client_cert_source`` and ``client_encrypted_cert_source``
Expand All @@ -117,6 +120,7 @@ def __init__(
api_key: Optional[str] = None,
api_audience: Optional[str] = None,
universe_domain: Optional[str] = None,
tracer_provider: Optional[object] = None,
):
if credentials_file is not None:
warnings.warn(general_helpers._CREDENTIALS_FILE_WARNING, DeprecationWarning)
Expand All @@ -136,6 +140,7 @@ def __init__(
self.api_key = api_key
self.api_audience = api_audience
self.universe_domain = universe_domain
self.tracer_provider = tracer_provider

def __repr__(self) -> str:
return "ClientOptions: " + repr(self.__dict__)
Expand Down
32 changes: 29 additions & 3 deletions packages/google-api-core/google/api_core/grpc_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
import google.auth.transport.requests
import google.protobuf
import grpc

from google.api_core import exceptions, general_helpers
from google.api_core import _feature_gating_helpers, exceptions, general_helpers

# The list of gRPC Callable interfaces that return iterators.
_STREAM_WRAP_CLASSES = (grpc.UnaryStreamMultiCallable, grpc.StreamStreamMultiCallable)
Expand Down Expand Up @@ -384,10 +383,37 @@ def create_channel(
if attempt_direct_path:
target = _modify_target_for_direct_path(target)

return grpc.secure_channel(
configuration = kwargs.pop("configuration", None)

channel = grpc.secure_channel(
target, composite_credentials, compression=compression, **kwargs
)

is_tracing_enabled = _feature_gating_helpers.resolve_feature_flags(
env_var="GOOGLE_CLOUD_PYTHON_TRACING_ENABLED",
feature_key="tracer_provider",
configuration=configuration,
)

if is_tracing_enabled:
try:
import opentelemetry.instrumentation.grpc as otel_grpc # type: ignore[import-not-found]

tracer_provider = None
if configuration is not None:
if isinstance(configuration, dict):
tracer_provider = configuration.get("tracer_provider")
else:
tracer_provider = getattr(configuration, "tracer_provider", None)

interceptor = otel_grpc.client_interceptor(tracer_provider=tracer_provider)
channel = otel_grpc.intercept_channel(channel, interceptor)
except ImportError:
# If OpenTelemetry gRPC instrumentation is missing, this should simply NOOP and fail open rather than failing import.
pass

return channel


def _modify_target_for_direct_path(target: str) -> str:
"""
Expand Down
12 changes: 10 additions & 2 deletions packages/google-api-core/google/api_core/grpc_helpers_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@
from typing import AsyncGenerator, Generic, Iterator, Optional, TypeVar

import grpc
from grpc import aio

from google.api_core import exceptions, general_helpers, grpc_helpers
from grpc import aio

# denotes the proto response type for grpc calls
P = TypeVar("P")
Expand Down Expand Up @@ -303,6 +302,15 @@ def create_channel(
if attempt_direct_path:
target = grpc_helpers._modify_target_for_direct_path(target)

# NOTE: 'configuration' is popped to prevent a TypeError.
# Generated async transports (like those in google-cloud-* libs) pass 'configuration'
# down to this helper via **kwargs to support tracing in sync transports.
# However, 'aio.secure_channel' does not recognize this parameter yet and will
# crash if it is passed through.
# Async gRPC tracing is deferred to a future phase/PR, so we simply discard
# this parameter for now to ensure generated async code doesn't fail at runtime.
kwargs.pop("configuration", None)

return aio.secure_channel(
target, composite_credentials, compression=compression, **kwargs
)
Expand Down
2 changes: 1 addition & 1 deletion packages/google-api-core/noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def default(
install_extras = []
if install_grpc:
# Note: The extra is called `grpc` and not `grpcio`.
install_extras.append("grpc")
install_extras.extend(["grpc", "tracing", "testing"])

constraints_dir = str(CURRENT_DIRECTORY / "testing")
if install_async_rest:
Expand Down
10 changes: 10 additions & 0 deletions packages/google-api-core/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ dependencies = [
"proto-plus >= 1.26.1, < 2.0.0",
"google-auth >= 2.14.1, < 3.0.0",
"requests >= 2.33.0, < 3.0.0",
"opentelemetry-api >= 1.44.0, < 2.0.0",
]
dynamic = ["version"]

Expand All @@ -64,6 +65,13 @@ grpc = [
"grpcio-status >= 1.59.0, < 2.0.0",
"grpcio-status >= 1.75.1, < 2.0.0; python_version >= '3.14'",
]
tracing = [
"opentelemetry-instrumentation-grpc >= 0.65b0, < 1.0.0",
]
testing = [
"opentelemetry-sdk >= 1.44.0, < 2.0.0",
]



[tool.setuptools.dynamic]
Expand Down Expand Up @@ -93,4 +101,6 @@ filterwarnings = [
"ignore:.*custom tp_new.*in Python 3.14:DeprecationWarning",
# Remove once https://github.com/grpc/grpc/issues/35086 is fixed (and version newer than 1.60.0 is published)
"ignore:There is no current event loop:DeprecationWarning",
# Ignore external OpenTelemetry/importlib.metadata SelectableGroups warning
"ignore:.*SelectableGroups dict interface is deprecated:DeprecationWarning",
]
3 changes: 3 additions & 0 deletions packages/google-api-core/testing/constraints-3.10.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ requests==2.33.0
grpcio==1.59.0
grpcio-status==1.59.0
proto-plus==1.26.1
opentelemetry-api==1.44.0
opentelemetry-sdk==1.44.0
opentelemetry-instrumentation-grpc==0.65b0
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ grpcio==1.59.0
grpcio-status==1.59.0
proto-plus==1.26.1
aiohttp==3.13.4
opentelemetry-api==1.44.0
opentelemetry-sdk==1.44.0
opentelemetry-instrumentation-grpc==0.65b0
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@


import google.auth.credentials

from google.api_core import exceptions, grpc_helpers_async


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
# limitations under the License.

from re import match
from unittest import mock

import pytest

from google.api_core import client_options

from ..helpers import warn_deprecated_credentials_file
Expand All @@ -42,6 +42,7 @@ def test_constructor():
],
api_audience="foo2.googleapis.com",
universe_domain="googleapis.com",
tracer_provider=mock.Mock(),
)

assert options.api_endpoint == "foo.googleapis.com"
Expand All @@ -54,6 +55,7 @@ def test_constructor():
]
assert options.api_audience == "foo2.googleapis.com"
assert options.universe_domain == "googleapis.com"
assert options.tracer_provider is not None


def test_constructor_with_encrypted_cert_source():
Expand Down Expand Up @@ -123,6 +125,7 @@ def test_from_dict():
"https://www.googleapis.com/auth/cloud-platform.read-only",
],
"api_audience": "foo2.googleapis.com",
"tracer_provider": mock.Mock(),
}
)

Expand All @@ -136,6 +139,7 @@ def test_from_dict():
"https://www.googleapis.com/auth/cloud-platform.read-only",
]
assert options.api_key is None
assert options.tracer_provider is not None
assert options.api_audience == "foo2.googleapis.com"


Expand All @@ -162,6 +166,7 @@ def test_repr():
"scopes",
"api_key",
"api_audience",
"tracer_provider",
]
)
options = client_options.ClientOptions(api_endpoint="foo.googleapis.com")
Expand Down
3 changes: 1 addition & 2 deletions packages/google-api-core/tests/unit/test_grpc_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@
pytest.skip("No GRPC", allow_module_level=True)

import google.auth.credentials
from google.longrunning import operations_pb2

from google.api_core import exceptions, grpc_helpers
from google.longrunning import operations_pb2


def test__patch_callable_name():
Expand Down
Loading
Loading