From 6b7fc79b2466bc729d07878193643f989f95bf04 Mon Sep 17 00:00:00 2001 From: Tim Pansino Date: Wed, 26 Jul 2023 16:56:04 -0700 Subject: [PATCH] Add async batch instrumentation --- newrelic/config.py | 5 ++ newrelic/hooks/datastore_firestore.py | 10 ++++ .../test_async_batching.py | 50 +++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 tests/datastore_firestore/test_async_batching.py diff --git a/newrelic/config.py b/newrelic/config.py index 1921bcc9b..042f22461 100644 --- a/newrelic/config.py +++ b/newrelic/config.py @@ -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", diff --git a/newrelic/hooks/datastore_firestore.py b/newrelic/hooks/datastore_firestore.py index b9993350a..5dc8f6286 100644 --- a/newrelic/hooks/datastore_firestore.py +++ b/newrelic/hooks/datastore_firestore.py @@ -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 diff --git a/tests/datastore_firestore/test_async_batching.py b/tests/datastore_firestore/test_async_batching.py new file mode 100644 index 000000000..0d51e9952 --- /dev/null +++ b/tests/datastore_firestore/test_async_batching.py @@ -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()