Skip to content

Commit f1ebc43

Browse files
authored
fix: Provide Spanner Option to disable metrics (#1460)
1 parent 64aebe7 commit f1ebc43

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

google/cloud/spanner_v1/client.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def _get_spanner_optimizer_statistics_package():
100100
log = logging.getLogger(__name__)
101101

102102

103-
def _get_spanner_enable_builtin_metrics():
103+
def _get_spanner_enable_builtin_metrics_env():
104104
return os.getenv(SPANNER_DISABLE_BUILTIN_METRICS_ENV_VAR) != "true"
105105

106106

@@ -180,6 +180,10 @@ class Client(ClientWithProject):
180180
This is intended only for experimental host spanner endpoints.
181181
If set, this will override the `api_endpoint` in `client_options`.
182182
183+
:type disable_builtin_metrics: bool
184+
:param disable_builtin_metrics: (Optional) Default False. Set to True to disable
185+
the Spanner built-in metrics collection and exporting.
186+
183187
:raises: :class:`ValueError <exceptions.ValueError>` if both ``read_only``
184188
and ``admin`` are :data:`True`
185189
"""
@@ -205,6 +209,7 @@ def __init__(
205209
observability_options=None,
206210
default_transaction_options: Optional[DefaultTransactionOptions] = None,
207211
experimental_host=None,
212+
disable_builtin_metrics=False,
208213
):
209214
self._emulator_host = _get_spanner_emulator_host()
210215
self._experimental_host = experimental_host
@@ -248,7 +253,8 @@ def __init__(
248253
warnings.warn(_EMULATOR_HOST_HTTP_SCHEME)
249254
# Check flag to enable Spanner builtin metrics
250255
if (
251-
_get_spanner_enable_builtin_metrics()
256+
_get_spanner_enable_builtin_metrics_env()
257+
and not disable_builtin_metrics
252258
and HAS_GOOGLE_CLOUD_MONITORING_INSTALLED
253259
):
254260
meter_provider = metrics.NoOpMeterProvider()

tests/unit/test_client.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,37 @@ def test_constructor_w_metrics_initialization_error(
278278
)
279279
mock_spanner_metrics_factory.assert_called_once()
280280

281+
@mock.patch("google.cloud.spanner_v1.client.SpannerMetricsTracerFactory")
282+
@mock.patch.dict(os.environ, {"SPANNER_DISABLE_BUILTIN_METRICS": "true"})
283+
def test_constructor_w_disable_builtin_metrics_using_env(
284+
self, mock_spanner_metrics_factory
285+
):
286+
"""
287+
Test that Client constructor disable metrics using Spanner Option.
288+
"""
289+
from google.cloud.spanner_v1.client import Client
290+
291+
creds = build_scoped_credentials()
292+
client = Client(project=self.PROJECT, credentials=creds)
293+
self.assertIsNotNone(client)
294+
mock_spanner_metrics_factory.assert_called_once_with(enabled=False)
295+
296+
@mock.patch("google.cloud.spanner_v1.client.SpannerMetricsTracerFactory")
297+
def test_constructor_w_disable_builtin_metrics_using_option(
298+
self, mock_spanner_metrics_factory
299+
):
300+
"""
301+
Test that Client constructor disable metrics using Spanner Option.
302+
"""
303+
from google.cloud.spanner_v1.client import Client
304+
305+
creds = build_scoped_credentials()
306+
client = Client(
307+
project=self.PROJECT, credentials=creds, disable_builtin_metrics=True
308+
)
309+
self.assertIsNotNone(client)
310+
mock_spanner_metrics_factory.assert_called_once_with(enabled=False)
311+
281312
def test_constructor_route_to_leader_disbled(self):
282313
from google.cloud.spanner_v1 import client as MUT
283314

0 commit comments

Comments
 (0)