Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 8 additions & 2 deletions google/cloud/spanner_v1/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"


Expand Down Expand Up @@ -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 <exceptions.ValueError>` if both ``read_only``
and ``admin`` are :data:`True`
"""
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Comment on lines +286 to +288

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The docstring seems to be a copy-paste from the test below. It should describe that this test validates disabling metrics via the environment variable, not the Spanner option.

Suggested change
"""
Test that Client constructor disable metrics using Spanner Option.
"""
"""
Test that Client constructor disables metrics using an environment variable.
"""

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

Expand Down