Skip to content

Commit

Permalink
refactor: pass CommitStats via extra kwarg
Browse files Browse the repository at this point in the history
  • Loading branch information
larkee committed Feb 19, 2021
1 parent 507a4ec commit 11f62bc
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
5 changes: 4 additions & 1 deletion google/cloud/spanner_v1/database.py
Expand Up @@ -657,7 +657,10 @@ def __exit__(self, exc_type, exc_val, exc_tb):
self._batch.commit(return_commit_stats=self._database.log_commit_stats)
finally:
if self._database.log_commit_stats:
self._database.logger.info(self._batch.commit_stats)
self._database.logger.info(
"CommitStats: {}".format(self._batch.commit_stats),
extra={"commit_stats": self._batch.commit_stats},
)
self._database._pool.put(self._session)


Expand Down
5 changes: 4 additions & 1 deletion google/cloud/spanner_v1/session.py
Expand Up @@ -358,7 +358,10 @@ def run_in_transaction(self, func, *args, **kw):
raise
else:
if self._database.log_commit_stats:
self._database.logger.info(txn.commit_stats)
self._database.logger.info(
"CommitStats: {}".format(txn.commit_stats),
extra={"commit_stats": txn.commit_stats},
)
return return_value


Expand Down
4 changes: 3 additions & 1 deletion tests/unit/test_database.py
Expand Up @@ -1374,7 +1374,9 @@ def test_context_mgr_w_commit_stats(self):
request=request, metadata=[("google-cloud-resource-prefix", database.name)],
)

database.logger.info.assert_called_once_with(commit_stats)
database.logger.info.assert_called_once_with(
"CommitStats: mutation_count: 4\n", extra={"commit_stats": commit_stats}
)

def test_context_mgr_failure(self):
from google.cloud.spanner_v1.batch import Batch
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/test_session.py
Expand Up @@ -1378,7 +1378,9 @@ def unit_of_work(txn, *args, **kw):
gax_api.commit.assert_called_once_with(
request=request, metadata=[("google-cloud-resource-prefix", database.name)],
)
database.logger.info.assert_called_once_with(commit_stats)
database.logger.info.assert_called_once_with(
"CommitStats: mutation_count: 4\n", extra={"commit_stats": commit_stats}
)

def test_delay_helper_w_no_delay(self):
from google.cloud.spanner_v1.session import _delay_until_retry
Expand Down

0 comments on commit 11f62bc

Please sign in to comment.