Skip to content

Commit 2ce45c8

Browse files
committed
Instrument Firestore batching
1 parent d17b62f commit 2ce45c8

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

newrelic/config.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2299,6 +2299,16 @@ def _process_module_builtin_defaults():
22992299
"newrelic.hooks.datastore_firestore",
23002300
"instrument_google_cloud_firestore_v1_aggregation",
23012301
)
2302+
_process_module_definition(
2303+
"google.cloud.firestore_v1.batch",
2304+
"newrelic.hooks.datastore_firestore",
2305+
"instrument_google_cloud_firestore_v1_batch",
2306+
)
2307+
_process_module_definition(
2308+
"google.cloud.firestore_v1.bulk_batch",
2309+
"newrelic.hooks.datastore_firestore",
2310+
"instrument_google_cloud_firestore_v1_bulk_batch",
2311+
)
23022312

23032313
_process_module_definition(
23042314
"ariadne.asgi",

newrelic/hooks/datastore_firestore.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,22 @@ def instrument_google_cloud_firestore_v1_aggregation(module):
122122
for method in ("stream",):
123123
if hasattr(class_, method):
124124
wrap_generator_method(module, "AggregationQuery", method, target=_get_collection_ref_id)
125+
126+
127+
def instrument_google_cloud_firestore_v1_batch(module):
128+
if hasattr(module, "WriteBatch"):
129+
class_ = module.WriteBatch
130+
for method in ("commit",):
131+
if hasattr(class_, method):
132+
wrap_datastore_trace(
133+
module, "WriteBatch.%s" % method, product="Firestore", target=None, operation=method
134+
)
135+
136+
def instrument_google_cloud_firestore_v1_bulk_batch(module):
137+
if hasattr(module, "BulkWriteBatch"):
138+
class_ = module.BulkWriteBatch
139+
for method in ("commit",):
140+
if hasattr(class_, method):
141+
wrap_datastore_trace(
142+
module, "BulkWriteBatch.%s" % method, product="Firestore", target=None, operation=method
143+
)
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Copyright 2010 New Relic, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from testing_support.validators.validate_transaction_metrics import validate_transaction_metrics
16+
from newrelic.api.background_task import background_task
17+
from testing_support.validators.validate_database_duration import (
18+
validate_database_duration,
19+
)
20+
21+
# ===== WriteBatch =====
22+
23+
def _exercise_write_batch(client, collection):
24+
docs = [collection.document(str(x)) for x in range(1, 4)]
25+
batch = client.batch()
26+
for doc in docs:
27+
batch.set(doc, {})
28+
29+
batch.commit()
30+
31+
32+
def test_firestore_write_batch(client, collection):
33+
_test_scoped_metrics = [
34+
("Datastore/operation/Firestore/commit", 1),
35+
]
36+
37+
_test_rollup_metrics = [
38+
("Datastore/all", 1),
39+
("Datastore/allOther", 1),
40+
]
41+
@validate_database_duration()
42+
@validate_transaction_metrics(
43+
"test_firestore_write_batch",
44+
scoped_metrics=_test_scoped_metrics,
45+
rollup_metrics=_test_rollup_metrics,
46+
background_task=True,
47+
)
48+
@background_task(name="test_firestore_write_batch")
49+
def _test():
50+
_exercise_write_batch(client, collection)
51+
52+
_test()
53+
54+
# ===== BulkWriteBatch =====
55+
56+
def _exercise_bulk_write_batch(client, collection):
57+
from google.cloud.firestore_v1.bulk_batch import BulkWriteBatch
58+
59+
docs = [collection.document(str(x)) for x in range(1, 4)]
60+
batch = BulkWriteBatch(client)
61+
for doc in docs:
62+
batch.set(doc, {})
63+
64+
batch.commit()
65+
66+
67+
def test_firestore_bulk_write_batch(client, collection):
68+
_test_scoped_metrics = [
69+
("Datastore/operation/Firestore/commit", 1),
70+
]
71+
72+
_test_rollup_metrics = [
73+
("Datastore/all", 1),
74+
("Datastore/allOther", 1),
75+
]
76+
@validate_database_duration()
77+
@validate_transaction_metrics(
78+
"test_firestore_bulk_write_batch",
79+
scoped_metrics=_test_scoped_metrics,
80+
rollup_metrics=_test_rollup_metrics,
81+
background_task=True,
82+
)
83+
@background_task(name="test_firestore_bulk_write_batch")
84+
def _test():
85+
_exercise_bulk_write_batch(client, collection)
86+
87+
_test()

0 commit comments

Comments
 (0)