Skip to content

Commit 60e0da3

Browse files
committed
feat(spanner): log client configuration at startup
1 parent f1ebc43 commit 60e0da3

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

google/cloud/spanner_v1/client.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,36 @@ def __init__(
292292
self._nth_client_id = Client.NTH_CLIENT.increment()
293293
self._nth_request = AtomicCounter(0)
294294

295+
self._log_spanner_options()
296+
297+
def _log_spanner_options(self):
298+
"""Logs Spanner client options."""
299+
host = "spanner.googleapis.com"
300+
if self._client_options and self._client_options.api_endpoint:
301+
host = self._client_options.api_endpoint
302+
if self._emulator_host:
303+
host = self._emulator_host
304+
if self._experimental_host:
305+
host = self._experimental_host
306+
307+
log.info(
308+
"Spanner options: \n"
309+
" Project ID: %s\n"
310+
" Host: %s\n"
311+
" Route to leader enabled: %s\n"
312+
" Directed read options: %s\n"
313+
" Default transaction options: %s\n"
314+
" Observability options: %s\n"
315+
" Built-in metrics enabled: %s",
316+
self.project,
317+
host,
318+
self.route_to_leader_enabled,
319+
self._directed_read_options,
320+
self._default_transaction_options,
321+
self._observability_options,
322+
_get_spanner_enable_builtin_metrics(),
323+
)
324+
295325
@property
296326
def _next_nth_request(self):
297327
return self._nth_request.increment()

tests/unit/test_client.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import unittest
1616

17+
import logging
1718
import os
1819
import mock
1920
from google.auth.credentials import AnonymousCredentials
@@ -98,7 +99,7 @@ def _constructor_test_helper(
9899
query_options=query_options,
99100
directed_read_options=directed_read_options,
100101
default_transaction_options=default_transaction_options,
101-
**kwargs
102+
**kwargs,
102103
)
103104

104105
expected_creds = expected_creds or creds.with_scopes.return_value
@@ -329,6 +330,62 @@ def test_constructor_w_default_transaction_options(self):
329330
default_transaction_options=self.DEFAULT_TRANSACTION_OPTIONS,
330331
)
331332

333+
def test_constructor_logs_options(self):
334+
from google.cloud.spanner_v1 import client as MUT
335+
336+
creds = build_scoped_credentials()
337+
observability_options = {"enable_extended_tracing": True}
338+
with self.assertLogs(MUT.__name__, level="INFO") as cm:
339+
client = self._make_one(
340+
project=self.PROJECT,
341+
credentials=creds,
342+
route_to_leader_enabled=False,
343+
directed_read_options=self.DIRECTED_READ_OPTIONS,
344+
default_transaction_options=self.DEFAULT_TRANSACTION_OPTIONS,
345+
observability_options=observability_options,
346+
)
347+
self.assertIsNotNone(client)
348+
349+
self.assertEqual(len(cm.output), 1)
350+
log_output = cm.output[0]
351+
self.assertIn("Spanner options:", log_output)
352+
self.assertIn(f"\n Project ID: {self.PROJECT}", log_output)
353+
self.assertIn("\n Host: spanner.googleapis.com", log_output)
354+
self.assertIn("\n Route to leader enabled: False", log_output)
355+
self.assertIn(
356+
f"\n Directed read options: {self.DIRECTED_READ_OPTIONS}", log_output
357+
)
358+
self.assertIn(
359+
f"\n Default transaction options: {self.DEFAULT_TRANSACTION_OPTIONS}",
360+
log_output,
361+
)
362+
self.assertIn(f"\n Observability options: {observability_options}", log_output)
363+
# SPANNER_DISABLE_BUILTIN_METRICS is "true" from class-level patch
364+
self.assertIn("\n Built-in metrics enabled: False", log_output)
365+
366+
# Test with custom host
367+
endpoint = "test.googleapis.com"
368+
with self.assertLogs(MUT.__name__, level="INFO") as cm:
369+
self._make_one(
370+
project=self.PROJECT,
371+
credentials=creds,
372+
client_options={"api_endpoint": endpoint},
373+
)
374+
375+
self.assertEqual(len(cm.output), 1)
376+
log_output = cm.output[0]
377+
self.assertIn(f"\n Host: {endpoint}", log_output)
378+
379+
# Test with emulator host
380+
emulator_host = "localhost:9010"
381+
with mock.patch.dict(os.environ, {MUT.EMULATOR_ENV_VAR: emulator_host}):
382+
with self.assertLogs(MUT.__name__, level="INFO") as cm:
383+
self._make_one(project=self.PROJECT)
384+
385+
self.assertEqual(len(cm.output), 1)
386+
log_output = cm.output[0]
387+
self.assertIn(f"\n Host: {emulator_host}", log_output)
388+
332389
@mock.patch("google.cloud.spanner_v1.client._get_spanner_emulator_host")
333390
def test_instance_admin_api(self, mock_em):
334391
from google.cloud.spanner_v1.client import SPANNER_ADMIN_SCOPE

0 commit comments

Comments
 (0)