Skip to content

Commit 1b042a2

Browse files
committed
refactor(secretmanager): align with new _observability helpers and sharpen type hints
1 parent 4db7ed4 commit 1b042a2

4 files changed

Lines changed: 67 additions & 44 deletions

File tree

packages/google-cloud-secret-manager/google/cloud/secretmanager_v1/services/secret_manager_service/client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
)
3636

3737
import google.protobuf
38-
from google.api_core import _feature_gating_helpers, _otel_helpers, gapic_v1
38+
from google.api_core import _feature_gating_helpers, _observability, gapic_v1
3939
from google.api_core import client_options as client_options_lib
4040
from google.api_core import exceptions as core_exceptions
4141
from google.api_core import retry as retries
@@ -764,9 +764,9 @@ def __init__(
764764
# pass the channel to transport_init. When the Transport finds an existing
765765
# channel it will use that, otherwise it will create one lazily.
766766
if transport_init is SecretManagerServiceGrpcTransport:
767-
if _otel_helpers.is_otel_capabilities_enabled(self._client_options):
767+
if _observability.is_otel_capabilities_enabled(self._client_options):
768768
# Eagerly create the channel using the Transport's classmethod
769-
raw_channel = transport_init.create_channel(
769+
raw_channel = SecretManagerServiceGrpcTransport.create_channel(
770770
self._api_endpoint,
771771
credentials=credentials,
772772
credentials_file=self._client_options.credentials_file,
@@ -775,7 +775,7 @@ def __init__(
775775
)
776776

777777
# Apply OTel capabilities to the channel
778-
wrapped_channel = _otel_helpers.apply_otel_capabilities_to_channel(
778+
wrapped_channel = _observability.apply_otel_capabilities_to_channel(
779779
raw_channel, self._client_options
780780
)
781781

packages/google-cloud-secret-manager/google/cloud/secretmanager_v1/services/secret_manager_service/transports/grpc.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@
4444

4545
_LOGGER = std_logging.getLogger(__name__)
4646

47+
ClientInterceptor = Union[
48+
grpc.UnaryUnaryClientInterceptor,
49+
grpc.UnaryStreamClientInterceptor,
50+
grpc.StreamUnaryClientInterceptor,
51+
grpc.StreamStreamClientInterceptor,
52+
]
53+
4754

4855
class _LoggingClientInterceptor(grpc.UnaryUnaryClientInterceptor): # pragma: NO COVER
4956
def intercept_unary_unary(self, continuation, client_call_details, request):
@@ -147,7 +154,7 @@ def __init__(
147154
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
148155
always_use_jwt_access: Optional[bool] = False,
149156
api_audience: Optional[str] = None,
150-
interceptors: Optional[Sequence[grpc.ClientInterceptor]] = None,
157+
interceptors: Optional[Sequence[ClientInterceptor]] = None,
151158
) -> None:
152159
"""Instantiate the transport.
153160
@@ -198,7 +205,7 @@ def __init__(
198205
to the service that will be set when using certain 3rd party
199206
authentication flows. Audience is typically a resource identifier.
200207
If not set, the host value will be used as a default.
201-
interceptors (Optional[Sequence[grpc.ClientInterceptor]]):
208+
interceptors (Optional[Sequence[ClientInterceptor]]):
202209
Additional interceptors to be injected into the gRPC channel pipeline.
203210
These are executed in order.
204211

packages/google-cloud-secret-manager/noxfile.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,14 @@ def system(session):
367367
)
368368

369369

370+
@nox.session(python=DEFAULT_PYTHON_VERSION)
371+
def integration(session):
372+
"""Run integration tests with OpenTelemetry."""
373+
session.install("-e", ".")
374+
session.install("pytest", "opentelemetry-sdk", "opentelemetry-instrumentation-grpc")
375+
session.run("py.test", "tests/integration", *session.posargs)
376+
377+
370378
@nox.session(python=DEFAULT_PYTHON_VERSION)
371379
def cover(session):
372380
"""Run the final coverage report.

packages/google-cloud-secret-manager/tests/unit/gapic/secretmanager_v1/test_secret_manager_service.py

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -774,56 +774,64 @@ def test_secret_manager_service_client_otel_eager_channel_injection():
774774
mock_raw_channel = mock.Mock()
775775
mock_wrapped_channel = mock.Mock()
776776

777-
with mock.patch(
778-
"google.cloud.secretmanager_v1.services.secret_manager_service.client._otel_helpers.is_otel_capabilities_enabled",
779-
return_value=True,
780-
) as mock_is_enabled:
781-
with mock.patch(
782-
"google.cloud.secretmanager_v1.services.secret_manager_service.client._otel_helpers.apply_otel_capabilities_to_channel",
777+
# We use parenthesized context managers (Python 3.10+) to flatten the mocks.
778+
# This prevents deep indentation while ensuring all mocks are active during
779+
# Client instantiation.
780+
with (
781+
mock.patch(
782+
"google.cloud.secretmanager_v1.services.secret_manager_service.client._observability.is_otel_capabilities_enabled",
783+
return_value=True,
784+
) as mock_is_enabled,
785+
mock.patch(
786+
"google.cloud.secretmanager_v1.services.secret_manager_service.client._observability.apply_otel_capabilities_to_channel",
783787
return_value=mock_wrapped_channel,
784-
) as mock_apply_otel:
785-
with mock.patch.object(
786-
transports.SecretManagerServiceGrpcTransport,
787-
"create_channel",
788-
return_value=mock_raw_channel,
789-
) as mock_create_channel:
790-
with mock.patch.object(
791-
transports.SecretManagerServiceGrpcTransport, "__init__"
792-
) as patched_transport_init:
793-
patched_transport_init.return_value = None
788+
) as mock_apply_otel,
789+
mock.patch.object(
790+
transports.SecretManagerServiceGrpcTransport,
791+
"create_channel",
792+
return_value=mock_raw_channel,
793+
) as mock_create_channel,
794+
mock.patch.object(
795+
transports.SecretManagerServiceGrpcTransport, "__init__", return_value=None
796+
) as patched_transport_init,
797+
):
798+
client = SecretManagerServiceClient(transport="grpc")
794799

795-
client = SecretManagerServiceClient(transport="grpc")
796800

797-
mock_is_enabled.assert_called_once()
798-
mock_create_channel.assert_called_once()
799-
mock_apply_otel.assert_called_once_with(mock_raw_channel, mock.ANY)
801+
mock_is_enabled.assert_called_once()
802+
mock_create_channel.assert_called_once()
803+
mock_apply_otel.assert_called_once_with(mock_raw_channel, mock.ANY)
800804

801-
called_kwargs = patched_transport_init.call_args.kwargs
802-
assert "channel" in called_kwargs
803-
assert called_kwargs["channel"] == mock_wrapped_channel
805+
called_kwargs = patched_transport_init.call_args.kwargs
806+
assert "channel" in called_kwargs
807+
assert called_kwargs["channel"] == mock_wrapped_channel
804808

805809

806810
def test_secret_manager_service_client_otel_eager_channel_injection_disabled():
807811
# Simulate OTel being disabled
808-
with mock.patch(
809-
"google.cloud.secretmanager_v1.services.secret_manager_service.client._otel_helpers.is_otel_capabilities_enabled",
810-
return_value=False,
811-
) as mock_is_enabled:
812-
with mock.patch.object(
812+
# We use parenthesized context managers (Python 3.10+) to flatten the mocks.
813+
# This prevents deep indentation while ensuring all mocks are active during
814+
# Client instantiation.
815+
with (
816+
mock.patch(
817+
"google.cloud.secretmanager_v1.services.secret_manager_service.client._observability.is_otel_capabilities_enabled",
818+
return_value=False,
819+
) as mock_is_enabled,
820+
mock.patch.object(
813821
transports.SecretManagerServiceGrpcTransport, "create_channel"
814-
) as mock_create_channel:
815-
with mock.patch.object(
816-
transports.SecretManagerServiceGrpcTransport, "__init__"
817-
) as patched_transport_init:
818-
patched_transport_init.return_value = None
822+
) as mock_create_channel,
823+
mock.patch.object(
824+
transports.SecretManagerServiceGrpcTransport, "__init__", return_value=None
825+
) as patched_transport_init,
826+
):
827+
client = SecretManagerServiceClient(transport="grpc")
819828

820-
client = SecretManagerServiceClient(transport="grpc")
829+
mock_is_enabled.assert_called_once()
830+
mock_create_channel.assert_not_called()
821831

822-
mock_is_enabled.assert_called_once()
823-
mock_create_channel.assert_not_called()
832+
called_kwargs = patched_transport_init.call_args.kwargs
833+
assert "channel" not in called_kwargs
824834

825-
called_kwargs = patched_transport_init.call_args.kwargs
826-
assert "channel" not in called_kwargs
827835

828836

829837
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)