Skip to content

Commit

Permalink
[Mega-Linter] Apply linters fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
TimPansino authored and github-actions[bot] committed Jul 26, 2023
1 parent 9d411e0 commit fa5f39a
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 42 deletions.
25 changes: 18 additions & 7 deletions newrelic/hooks/datastore_firestore.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from newrelic.common.object_wrapper import wrap_function_wrapper
from newrelic.api.datastore_trace import wrap_datastore_trace
from newrelic.api.datastore_trace import DatastoreTrace, wrap_datastore_trace
from newrelic.api.function_trace import wrap_function_trace
from newrelic.common.async_wrapper import generator_wrapper
from newrelic.api.datastore_trace import DatastoreTrace
from newrelic.common.object_wrapper import wrap_function_wrapper


def _get_object_id(obj, *args, **kwargs):
Expand Down Expand Up @@ -46,7 +45,7 @@ def _wrapper(wrapped, instance, args, kwargs):
trace = DatastoreTrace(product="Firestore", target=target_, operation=method_name)
wrapped = generator_wrapper(wrapped, trace)
return wrapped(*args, **kwargs)

class_ = getattr(module, class_name)
if class_ is not None:
if hasattr(class_, method_name):
Expand Down Expand Up @@ -74,7 +73,11 @@ def instrument_google_cloud_firestore_v1_collection(module):
for method in ("add", "get"):
if hasattr(class_, method):
wrap_datastore_trace(
module, "CollectionReference.%s" % method, product="Firestore", target=_get_object_id, operation=method
module,
"CollectionReference.%s" % method,
product="Firestore",
target=_get_object_id,
operation=method,
)

for method in ("stream", "list_documents"):
Expand All @@ -88,7 +91,11 @@ def instrument_google_cloud_firestore_v1_document(module):
for method in ("create", "delete", "get", "set", "update"):
if hasattr(class_, method):
wrap_datastore_trace(
module, "DocumentReference.%s" % method, product="Firestore", target=_get_object_id, operation=method
module,
"DocumentReference.%s" % method,
product="Firestore",
target=_get_object_id,
operation=method,
)

for method in ("collections",):
Expand Down Expand Up @@ -116,7 +123,11 @@ def instrument_google_cloud_firestore_v1_aggregation(module):
for method in ("get",):
if hasattr(class_, method):
wrap_datastore_trace(
module, "AggregationQuery.%s" % method, product="Firestore", target=_get_collection_ref_id, operation=method
module,
"AggregationQuery.%s" % method,
product="Firestore",
target=_get_collection_ref_id,
operation=method,
)

for method in ("stream",):
Expand Down
36 changes: 20 additions & 16 deletions tests/datastore_firestore/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,43 @@
import uuid

import pytest

from google.cloud.firestore import Client

from newrelic.api.time_trace import current_trace
from newrelic.api.datastore_trace import DatastoreTrace
from testing_support.db_settings import firestore_settings
from testing_support.fixtures import collector_agent_registration_fixture, collector_available_fixture # noqa: F401; pylint: disable=W0611
from testing_support.fixtures import ( # noqa: F401; pylint: disable=W0611
collector_agent_registration_fixture,
collector_available_fixture,
)

from newrelic.api.datastore_trace import DatastoreTrace
from newrelic.api.time_trace import current_trace

DB_SETTINGS = firestore_settings()[0]
FIRESTORE_HOST = DB_SETTINGS["host"]
FIRESTORE_PORT = DB_SETTINGS["port"]

_default_settings = {
'transaction_tracer.explain_threshold': 0.0,
'transaction_tracer.transaction_threshold': 0.0,
'transaction_tracer.stack_trace_threshold': 0.0,
'debug.log_data_collector_payloads': True,
'debug.record_transaction_failure': True,
'debug.log_explain_plan_queries': True
"transaction_tracer.explain_threshold": 0.0,
"transaction_tracer.transaction_threshold": 0.0,
"transaction_tracer.stack_trace_threshold": 0.0,
"debug.log_data_collector_payloads": True,
"debug.record_transaction_failure": True,
"debug.log_explain_plan_queries": True,
}

collector_agent_registration = collector_agent_registration_fixture(
app_name='Python Agent Test (datastore_firestore)',
default_settings=_default_settings,
linked_applications=['Python Agent Test (datastore)'])
app_name="Python Agent Test (datastore_firestore)",
default_settings=_default_settings,
linked_applications=["Python Agent Test (datastore)"],
)


@pytest.fixture(scope="session")
def client():
os.environ["FIRESTORE_EMULATOR_HOST"] = "%s:%d" % (FIRESTORE_HOST, FIRESTORE_PORT)
client = Client()
client.collection("healthcheck").document("healthcheck").set({}, retry=None, timeout=5) # Ensure connection is available
client.collection("healthcheck").document("healthcheck").set(
{}, retry=None, timeout=5
) # Ensure connection is available
return client


Expand Down Expand Up @@ -75,4 +79,4 @@ def _assert_trace_for_generator(generator_func, *args, **kwargs):
assert _trace_check and all(_trace_check) # All checks are True, and at least 1 is present.
assert current_trace() is txn # Generator trace has exited.

return _assert_trace_for_generator
return _assert_trace_for_generator
14 changes: 11 additions & 3 deletions tests/datastore_firestore/test_batching.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@
# 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,
)
from testing_support.validators.validate_transaction_metrics import (
validate_transaction_metrics,
)

from newrelic.api.background_task import background_task

# ===== WriteBatch =====


def _exercise_write_batch(client, collection):
docs = [collection.document(str(x)) for x in range(1, 4)]
batch = client.batch()
Expand All @@ -38,6 +42,7 @@ def test_firestore_write_batch(client, collection):
("Datastore/all", 1),
("Datastore/allOther", 1),
]

@validate_database_duration()
@validate_transaction_metrics(
"test_firestore_write_batch",
Expand All @@ -51,11 +56,13 @@ def _test():

_test()


# ===== BulkWriteBatch =====


def _exercise_bulk_write_batch(client, collection):
from google.cloud.firestore_v1.bulk_batch import BulkWriteBatch

docs = [collection.document(str(x)) for x in range(1, 4)]
batch = BulkWriteBatch(client)
for doc in docs:
Expand All @@ -73,6 +80,7 @@ def test_firestore_bulk_write_batch(client, collection):
("Datastore/all", 1),
("Datastore/allOther", 1),
]

@validate_database_duration()
@validate_transaction_metrics(
"test_firestore_bulk_write_batch",
Expand Down
8 changes: 5 additions & 3 deletions tests/datastore_firestore/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import pytest

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,
)
from testing_support.validators.validate_transaction_metrics import (
validate_transaction_metrics,
)

from newrelic.api.background_task import background_task


@pytest.fixture()
Expand Down
12 changes: 8 additions & 4 deletions tests/datastore_firestore/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@
# 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,
)
from testing_support.validators.validate_transaction_metrics import (
validate_transaction_metrics,
)

from newrelic.api.background_task import background_task


def _exercise_collections(collection):
collection.document("DoesNotExist")
collection.add({"capital": "Rome", "currency": "Euro", "language": "Italian"}, "Italy")
collection.add({"capital": "Mexico City", "currency": "Peso", "language": "Spanish"}, "Mexico")

documents_get = collection.get()
assert len(documents_get) == 2
documents_stream = [_ for _ in collection.stream()]
Expand All @@ -48,6 +51,7 @@ def test_firestore_collections(collection):
("Datastore/all", 5),
("Datastore/allOther", 5),
]

@validate_database_duration()
@validate_transaction_metrics(
"test_firestore_collections",
Expand All @@ -67,6 +71,6 @@ def test_firestore_collections_generators(collection, assert_trace_for_generator
collection.add({})
collection.add({})
assert len([_ for _ in collection.list_documents()]) == 2

assert_trace_for_generator(collection.stream)
assert_trace_for_generator(collection.list_documents)
10 changes: 7 additions & 3 deletions tests/datastore_firestore/test_documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
# 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,
)
from testing_support.validators.validate_transaction_metrics import (
validate_transaction_metrics,
)

from newrelic.api.background_task import background_task


def _exercise_documents(collection):
Expand Down Expand Up @@ -57,6 +60,7 @@ def test_firestore_documents(collection):
("Datastore/all", 7),
("Datastore/allOther", 7),
]

@validate_database_duration()
@validate_transaction_metrics(
"test_firestore_documents",
Expand All @@ -78,5 +82,5 @@ def test_firestore_documents_generators(collection, assert_trace_for_generator):
subcollection_doc.collection("collection1").add({})
subcollection_doc.collection("collection2").add({})
assert len([_ for _ in subcollection_doc.collections()]) == 2

assert_trace_for_generator(subcollection_doc.collections)
14 changes: 11 additions & 3 deletions tests/datastore_firestore/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
# limitations under the License.

import pytest

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,
)
from testing_support.validators.validate_transaction_metrics import (
validate_transaction_metrics,
)

from newrelic.api.background_task import background_task


@pytest.fixture(autouse=True)
Expand All @@ -27,8 +29,10 @@ def sample_data(collection, reset_firestore):
for x in range(1, 6):
collection.add({"x": x})


# ===== Query =====


def _exercise_query(collection):
query = collection.select("x").limit(10).order_by("x").where(field_path="x", op_string="<=", value=3)
assert len(query.get()) == 3
Expand All @@ -47,6 +51,7 @@ def test_firestore_query(collection):
("Datastore/all", 2),
("Datastore/allOther", 2),
]

@validate_database_duration()
@validate_transaction_metrics(
"test_firestore_query",
Expand All @@ -66,8 +71,10 @@ def test_firestore_query_generators(collection, assert_trace_for_generator):
query = collection.select("x").where(field_path="x", op_string="<=", value=3)
assert_trace_for_generator(query.stream)


# ===== AggregationQuery =====


def _exercise_aggregation_query(collection):
aggregation_query = collection.select("x").where(field_path="x", op_string="<=", value=3).count()
assert aggregation_query.get()[0][0].value == 3
Expand All @@ -86,6 +93,7 @@ def test_firestore_aggregation_query(collection):
("Datastore/all", 2),
("Datastore/allOther", 2),
]

@validate_database_duration()
@validate_transaction_metrics(
"test_firestore_aggregation_query",
Expand Down
10 changes: 7 additions & 3 deletions tests/datastore_firestore/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import pytest

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,
)
from testing_support.validators.validate_transaction_metrics import (
validate_transaction_metrics,
)

from newrelic.api.background_task import background_task


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -80,6 +82,7 @@ def test_firestore_transaction_commit(client, collection):
("Datastore/all", 5),
("Datastore/allOther", 5),
]

@validate_database_duration()
@validate_transaction_metrics(
"test_firestore_transaction",
Expand All @@ -105,6 +108,7 @@ def test_firestore_transaction_rollback(client, collection):
("Datastore/all", 2),
("Datastore/allOther", 2),
]

@validate_database_duration()
@validate_transaction_metrics(
"test_firestore_transaction",
Expand Down

0 comments on commit fa5f39a

Please sign in to comment.