Skip to content

Commit 1343656

Browse files
larkeebusunkim96skuruppu
authored
feat: add sample for commit stats (#241)
* feat: add sample for commit stats * fix: use correct kwarg * fix: correct super call * fix: add missing super init call * fix: update super init call * fix: use correct key * refactor: remove testing file * test: fix typo * Apply suggestions from code review Co-authored-by: Bu Sun Kim <[email protected]> Co-authored-by: skuruppu <[email protected]> * refactor: make last_commit_stats public Co-authored-by: larkee <[email protected]> Co-authored-by: Bu Sun Kim <[email protected]> Co-authored-by: skuruppu <[email protected]>
1 parent da146b7 commit 1343656

File tree

3 files changed

+49
-53
lines changed

3 files changed

+49
-53
lines changed

samples/samples/snippets.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import base64
2525
import datetime
2626
import decimal
27+
import logging
2728

2829
from google.cloud import spanner
2930
from google.cloud.spanner_v1 import param_types
@@ -969,6 +970,44 @@ def insert_singers(transaction):
969970
# [END spanner_dml_standard_insert]
970971

971972

973+
# [START spanner_get_commit_stats]
974+
def log_commit_stats(instance_id, database_id):
975+
"""Inserts sample data using DML and displays the commit statistics. """
976+
# By default, commit statistics are logged via stdout at level Info.
977+
# This sample uses a custom logger to access the commit statistics.
978+
class CommitStatsSampleLogger(logging.Logger):
979+
def __init__(self):
980+
self.last_commit_stats = None
981+
super().__init__("commit_stats_sample")
982+
983+
def info(self, msg, *args, **kwargs):
984+
if kwargs["extra"] and "commit_stats" in kwargs["extra"]:
985+
self.last_commit_stats = kwargs["extra"]["commit_stats"]
986+
super().info(msg)
987+
988+
spanner_client = spanner.Client()
989+
instance = spanner_client.instance(instance_id)
990+
database = instance.database(database_id, logger=CommitStatsSampleLogger())
991+
database.log_commit_stats = True
992+
993+
def insert_singers(transaction):
994+
row_ct = transaction.execute_update(
995+
"INSERT Singers (SingerId, FirstName, LastName) "
996+
" VALUES (110, 'Virginia', 'Watson')"
997+
)
998+
999+
print("{} record(s) inserted.".format(row_ct))
1000+
1001+
database.run_in_transaction(insert_singers)
1002+
commit_stats = database.logger.last_commit_stats
1003+
print(
1004+
"{} mutation(s) in transaction.".format(
1005+
commit_stats.mutation_count
1006+
)
1007+
)
1008+
# [END spanner_get_commit_stats]
1009+
1010+
9721011
def update_data_with_dml(instance_id, database_id):
9731012
"""Updates sample data from the database using a DML statement. """
9741013
# [START spanner_dml_standard_update]
@@ -1710,6 +1749,7 @@ def create_client_with_query_options(instance_id, database_id):
17101749
"query_nested_struct_field", help=query_nested_struct_field.__doc__
17111750
)
17121751
subparsers.add_parser("insert_data_with_dml", help=insert_data_with_dml.__doc__)
1752+
subparsers.add_parser("log_commit_stats", help=log_commit_stats.__doc__)
17131753
subparsers.add_parser("update_data_with_dml", help=update_data_with_dml.__doc__)
17141754
subparsers.add_parser("delete_data_with_dml", help=delete_data_with_dml.__doc__)
17151755
subparsers.add_parser(
@@ -1820,6 +1860,8 @@ def create_client_with_query_options(instance_id, database_id):
18201860
query_nested_struct_field(args.instance_id, args.database_id)
18211861
elif args.command == "insert_data_with_dml":
18221862
insert_data_with_dml(args.instance_id, args.database_id)
1863+
elif args.command == "log_commit_stats":
1864+
log_commit_stats(args.instance_id, args.database_id)
18231865
elif args.command == "update_data_with_dml":
18241866
update_data_with_dml(args.instance_id, args.database_id)
18251867
elif args.command == "delete_data_with_dml":

samples/samples/snippets_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,13 @@ def test_insert_data_with_dml(capsys):
236236
assert "1 record(s) inserted." in out
237237

238238

239+
def test_log_commit_stats(capsys):
240+
snippets.log_commit_stats(INSTANCE_ID, DATABASE_ID)
241+
out, _ = capsys.readouterr()
242+
assert "1 record(s) inserted." in out
243+
assert "3 mutation(s) in transaction." in out
244+
245+
239246
def test_update_data_with_dml(capsys):
240247
snippets.update_data_with_dml(INSTANCE_ID, DATABASE_ID)
241248
out, _ = capsys.readouterr()

test.py

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)