| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -100,7 +100,7 @@ def _get_spanner_optimizer_statistics_package(): | |||
| 100 | 100 | log = logging.getLogger(__name__) | |
| 101 | 101 | ||
| 102 | 102 | ||
| 103 | - def _get_spanner_enable_builtin_metrics(): | ||
| 103 | + def _get_spanner_enable_builtin_metrics_env(): | ||
| 104 | 104 | return os.getenv(SPANNER_DISABLE_BUILTIN_METRICS_ENV_VAR) != "true" | |
| 105 | 105 | ||
| 106 | 106 | ||
@@ -180,6 +180,10 @@ class Client(ClientWithProject): | |||
| 180 | 180 | This is intended only for experimental host spanner endpoints. | |
| 181 | 181 | If set, this will override the `api_endpoint` in `client_options`. | |
| 182 | 182 | ||
| 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 | + | ||
| 183 | 187 | :raises: :class:`ValueError <exceptions.ValueError>` if both ``read_only`` | |
| 184 | 188 | and ``admin`` are :data:`True` | |
| 185 | 189 | """ | |
@@ -205,6 +209,7 @@ def __init__( | |||
| 205 | 209 | observability_options=None, | |
| 206 | 210 | default_transaction_options: Optional[DefaultTransactionOptions] = None, | |
| 207 | 211 | experimental_host=None, | |
| 212 | + disable_builtin_metrics=False, | ||
| 208 | 213 | ): | |
| 209 | 214 | self._emulator_host = _get_spanner_emulator_host() | |
| 210 | 215 | self._experimental_host = experimental_host | |
@@ -248,7 +253,8 @@ def __init__( | |||
| 248 | 253 | warnings.warn(_EMULATOR_HOST_HTTP_SCHEME) | |
| 249 | 254 | # Check flag to enable Spanner builtin metrics | |
| 250 | 255 | if ( | |
| 251 | - _get_spanner_enable_builtin_metrics() | ||
| 256 | + _get_spanner_enable_builtin_metrics_env() | ||
| 257 | + and not disable_builtin_metrics | ||
| 252 | 258 | and HAS_GOOGLE_CLOUD_MONITORING_INSTALLED | |
| 253 | 259 | ): | |
| 254 | 260 | meter_provider = metrics.NoOpMeterProvider() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -278,6 +278,37 @@ def test_constructor_w_metrics_initialization_error( | |||
| 278 | 278 | ) | |
| 279 | 279 | mock_spanner_metrics_factory.assert_called_once() | |
| 280 | 280 | ||
| 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 | + | ||
| 281 | 312 | def test_constructor_route_to_leader_disbled(self): | |
| 282 | 313 | from google.cloud.spanner_v1 import client as MUT | |
| 283 | 314 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments