Skip to content

Commit a242170

Browse files
committed
feat(spanner): log client configuration at startup
1 parent 24394d6 commit a242170

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-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_env(),
323+
)
324+
295325
@property
296326
def _next_nth_request(self):
297327
return self._nth_request.increment()

tests/unit/test_client.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def _constructor_test_helper(
9898
query_options=query_options,
9999
directed_read_options=directed_read_options,
100100
default_transaction_options=default_transaction_options,
101-
**kwargs
101+
**kwargs,
102102
)
103103

104104
expected_creds = expected_creds or creds.with_scopes.return_value
@@ -329,6 +329,62 @@ def test_constructor_w_default_transaction_options(self):
329329
default_transaction_options=self.DEFAULT_TRANSACTION_OPTIONS,
330330
)
331331

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

0 commit comments

Comments
 (0)