Skip to content

Commit

Permalink
Add async batch instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
TimPansino committed Jul 26, 2023
1 parent c392e78 commit 6b7fc79
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
5 changes: 5 additions & 0 deletions newrelic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2329,6 +2329,11 @@ def _process_module_builtin_defaults():
"newrelic.hooks.datastore_firestore",
"instrument_google_cloud_firestore_v1_batch",
)
_process_module_definition(
"google.cloud.firestore_v1.async_batch",
"newrelic.hooks.datastore_firestore",
"instrument_google_cloud_firestore_v1_async_batch",
)
_process_module_definition(
"google.cloud.firestore_v1.bulk_batch",
"newrelic.hooks.datastore_firestore",
Expand Down
10 changes: 10 additions & 0 deletions newrelic/hooks/datastore_firestore.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,16 @@ def instrument_google_cloud_firestore_v1_batch(module):
)


def instrument_google_cloud_firestore_v1_async_batch(module):
if hasattr(module, "AsyncWriteBatch"):
class_ = module.AsyncWriteBatch
for method in ("commit",):
if hasattr(class_, method):
wrap_datastore_trace(
module, "AsyncWriteBatch.%s" % method, product="Firestore", target=None, operation=method
)


def instrument_google_cloud_firestore_v1_bulk_batch(module):
if hasattr(module, "BulkWriteBatch"):
class_ = module.BulkWriteBatch
Expand Down
50 changes: 50 additions & 0 deletions tests/datastore_firestore/test_async_batching.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright 2010 New Relic, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from testing_support.validators.validate_transaction_metrics import validate_transaction_metrics
from newrelic.api.background_task import background_task
from testing_support.validators.validate_database_duration import (
validate_database_duration,
)

async def _exercise_async_write_batch(async_client, async_collection):
docs = [async_collection.document(str(x)) for x in range(1, 4)]
async_batch = async_client.batch()
for doc in docs:
async_batch.set(doc, {})

await async_batch.commit()


def test_firestore_async_write_batch(loop, async_client, async_collection):
_test_scoped_metrics = [
("Datastore/operation/Firestore/commit", 1),
]

_test_rollup_metrics = [
("Datastore/all", 1),
("Datastore/allOther", 1),
]
@validate_database_duration()
@validate_transaction_metrics(
"test_firestore_async_write_batch",
scoped_metrics=_test_scoped_metrics,
rollup_metrics=_test_rollup_metrics,
background_task=True,
)
@background_task(name="test_firestore_async_write_batch")
def _test():
loop.run_until_complete(_exercise_async_write_batch(async_client, async_collection))

_test()

0 comments on commit 6b7fc79

Please sign in to comment.