From 6c21707ae82774e950456ceef28b854091344782 Mon Sep 17 00:00:00 2001 From: Surbhi Garg Date: Tue, 9 Dec 2025 15:47:10 +0530 Subject: [PATCH] fix: Provide Spanner Option to disable metrics --- google/cloud/spanner_v1/client.py | 10 ++++++++-- tests/unit/test_client.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/google/cloud/spanner_v1/client.py b/google/cloud/spanner_v1/client.py index 4d562d354b..5f72905616 100644 --- a/google/cloud/spanner_v1/client.py +++ b/google/cloud/spanner_v1/client.py @@ -100,7 +100,7 @@ def _get_spanner_optimizer_statistics_package(): log = logging.getLogger(__name__) -def _get_spanner_enable_builtin_metrics(): +def _get_spanner_enable_builtin_metrics_env(): return os.getenv(SPANNER_DISABLE_BUILTIN_METRICS_ENV_VAR) != "true" @@ -180,6 +180,10 @@ class Client(ClientWithProject): This is intended only for experimental host spanner endpoints. If set, this will override the `api_endpoint` in `client_options`. + :type disable_builtin_metrics: bool + :param disable_builtin_metrics: (Optional) Default False. Set to True to disable + the Spanner built-in metrics collection and exporting. + :raises: :class:`ValueError ` if both ``read_only`` and ``admin`` are :data:`True` """ @@ -205,6 +209,7 @@ def __init__( observability_options=None, default_transaction_options: Optional[DefaultTransactionOptions] = None, experimental_host=None, + disable_builtin_metrics=False, ): self._emulator_host = _get_spanner_emulator_host() self._experimental_host = experimental_host @@ -248,7 +253,8 @@ def __init__( warnings.warn(_EMULATOR_HOST_HTTP_SCHEME) # Check flag to enable Spanner builtin metrics if ( - _get_spanner_enable_builtin_metrics() + _get_spanner_enable_builtin_metrics_env() + and not disable_builtin_metrics and HAS_GOOGLE_CLOUD_MONITORING_INSTALLED ): meter_provider = metrics.NoOpMeterProvider() diff --git a/tests/unit/test_client.py b/tests/unit/test_client.py index 94481836ce..ab00d45268 100644 --- a/tests/unit/test_client.py +++ b/tests/unit/test_client.py @@ -278,6 +278,37 @@ def test_constructor_w_metrics_initialization_error( ) mock_spanner_metrics_factory.assert_called_once() + @mock.patch("google.cloud.spanner_v1.client.SpannerMetricsTracerFactory") + @mock.patch.dict(os.environ, {"SPANNER_DISABLE_BUILTIN_METRICS": "true"}) + def test_constructor_w_disable_builtin_metrics_using_env( + self, mock_spanner_metrics_factory + ): + """ + Test that Client constructor disable metrics using Spanner Option. + """ + from google.cloud.spanner_v1.client import Client + + creds = build_scoped_credentials() + client = Client(project=self.PROJECT, credentials=creds) + self.assertIsNotNone(client) + mock_spanner_metrics_factory.assert_called_once_with(enabled=False) + + @mock.patch("google.cloud.spanner_v1.client.SpannerMetricsTracerFactory") + def test_constructor_w_disable_builtin_metrics_using_option( + self, mock_spanner_metrics_factory + ): + """ + Test that Client constructor disable metrics using Spanner Option. + """ + from google.cloud.spanner_v1.client import Client + + creds = build_scoped_credentials() + client = Client( + project=self.PROJECT, credentials=creds, disable_builtin_metrics=True + ) + self.assertIsNotNone(client) + mock_spanner_metrics_factory.assert_called_once_with(enabled=False) + def test_constructor_route_to_leader_disbled(self): from google.cloud.spanner_v1 import client as MUT