Skip to content

Commit

Permalink
Add Database/instance metric check
Browse files Browse the repository at this point in the history
  • Loading branch information
hmstepanek committed Aug 18, 2023
1 parent 4228128 commit 99fbdbf
Show file tree
Hide file tree
Showing 22 changed files with 734 additions and 512 deletions.
95 changes: 53 additions & 42 deletions tests/datastore_bmemcached/test_memcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,83 +13,94 @@
# limitations under the License.

import os
from testing_support.db_settings import memcached_settings

import bmemcached
from testing_support.db_settings import memcached_settings
from testing_support.validators.validate_transaction_metrics import (
validate_transaction_metrics,
)

from newrelic.api.background_task import background_task
from newrelic.api.transaction import set_background_task

from testing_support.validators.validate_transaction_metrics import validate_transaction_metrics
from testing_support.db_settings import memcached_settings


DB_SETTINGS = memcached_settings()[0]

MEMCACHED_HOST = DB_SETTINGS['host']
MEMCACHED_PORT = DB_SETTINGS['port']
MEMCACHED_HOST = DB_SETTINGS["host"]
MEMCACHED_PORT = DB_SETTINGS["port"]
MEMCACHED_NAMESPACE = str(os.getpid())
MEMCACHED_ADDR = '%s:%s' % (MEMCACHED_HOST, MEMCACHED_PORT)
MEMCACHED_ADDR = "%s:%s" % (MEMCACHED_HOST, MEMCACHED_PORT)

_test_bt_set_get_delete_scoped_metrics = [
('Datastore/operation/Memcached/set', 1),
('Datastore/operation/Memcached/get', 1),
('Datastore/operation/Memcached/delete', 1)]
("Datastore/operation/Memcached/set", 1),
("Datastore/operation/Memcached/get", 1),
("Datastore/operation/Memcached/delete", 1),
]

_test_bt_set_get_delete_rollup_metrics = [
('Datastore/all', 3),
('Datastore/allOther', 3),
('Datastore/Memcached/all', 3),
('Datastore/Memcached/allOther', 3),
('Datastore/operation/Memcached/set', 1),
('Datastore/operation/Memcached/get', 1),
('Datastore/operation/Memcached/delete', 1)]
("Datastore/all", 3),
("Datastore/allOther", 3),
("Datastore/Memcached/all", 3),
("Datastore/Memcached/allOther", 3),
("Datastore/operation/Memcached/set", 1),
("Datastore/operation/Memcached/get", 1),
("Datastore/operation/Memcached/delete", 1),
("Datastore/instance/Memcached/%s/%s" % (MEMCACHED_HOST, MEMCACHED_PORT), 1),
]


@validate_transaction_metrics(
'test_memcache:test_bt_set_get_delete',
scoped_metrics=_test_bt_set_get_delete_scoped_metrics,
rollup_metrics=_test_bt_set_get_delete_rollup_metrics,
background_task=True)
"test_memcache:test_bt_set_get_delete",
scoped_metrics=_test_bt_set_get_delete_scoped_metrics,
rollup_metrics=_test_bt_set_get_delete_rollup_metrics,
background_task=True,
)
@background_task()
def test_bt_set_get_delete():
set_background_task(True)
client = bmemcached.Client([MEMCACHED_ADDR])

key = MEMCACHED_NAMESPACE + 'key'
key = MEMCACHED_NAMESPACE + "key"

client.set(key, 'value')
client.set(key, "value")
value = client.get(key)
client.delete(key)

assert value == 'value'
assert value == "value"


_test_wt_set_get_delete_scoped_metrics = [
('Datastore/operation/Memcached/set', 1),
('Datastore/operation/Memcached/get', 1),
('Datastore/operation/Memcached/delete', 1)]
("Datastore/operation/Memcached/set", 1),
("Datastore/operation/Memcached/get", 1),
("Datastore/operation/Memcached/delete", 1),
]

_test_wt_set_get_delete_rollup_metrics = [
('Datastore/all', 3),
('Datastore/allWeb', 3),
('Datastore/Memcached/all', 3),
('Datastore/Memcached/allWeb', 3),
('Datastore/operation/Memcached/set', 1),
('Datastore/operation/Memcached/get', 1),
('Datastore/operation/Memcached/delete', 1)]
("Datastore/all", 3),
("Datastore/allWeb", 3),
("Datastore/Memcached/all", 3),
("Datastore/Memcached/allWeb", 3),
("Datastore/operation/Memcached/set", 1),
("Datastore/operation/Memcached/get", 1),
("Datastore/operation/Memcached/delete", 1),
("Datastore/instance/MySQL/%s/%s" % (MEMCACHED_HOST, MEMCACHED_PORT), 1),
]


@validate_transaction_metrics(
'test_memcache:test_wt_set_get_delete',
scoped_metrics=_test_wt_set_get_delete_scoped_metrics,
rollup_metrics=_test_wt_set_get_delete_rollup_metrics,
background_task=False)
"test_memcache:test_wt_set_get_delete",
scoped_metrics=_test_wt_set_get_delete_scoped_metrics,
rollup_metrics=_test_wt_set_get_delete_rollup_metrics,
background_task=False,
)
@background_task()
def test_wt_set_get_delete():
set_background_task(False)
client = bmemcached.Client([MEMCACHED_ADDR])

key = MEMCACHED_NAMESPACE + 'key'
key = MEMCACHED_NAMESPACE + "key"

client.set(key, 'value')
client.set(key, "value")
value = client.get(key)
client.delete(key)

assert value == 'value'
assert value == "value"
13 changes: 9 additions & 4 deletions tests/datastore_firestore/test_async_batching.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@
# 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 testing_support.validators.validate_tt_collector_json import (
validate_tt_collector_json,
)

from newrelic.api.background_task import background_task


@pytest.fixture()
def exercise_async_write_batch(async_client, async_collection):
Expand All @@ -33,18 +35,21 @@ async def _exercise_async_write_batch():
async_batch.set(doc, {})

await async_batch.commit()

return _exercise_async_write_batch


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

_test_rollup_metrics = [
("Datastore/all", 1),
("Datastore/allOther", 1),
("Datastore/instance/Firestore/%s/%s" % (instance_info["host"], instance_info["port_path_or_id"]), 1),
]

@validate_database_duration()
@validate_transaction_metrics(
"test_firestore_async_write_batch",
Expand Down
14 changes: 9 additions & 5 deletions tests/datastore_firestore/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@
# 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 testing_support.validators.validate_tt_collector_json import (
validate_tt_collector_json,
)

from newrelic.api.background_task import background_task


@pytest.fixture()
def existing_document(collection):
Expand All @@ -37,10 +39,11 @@ async def _exercise_async_client():
assert len([_ async for _ in async_client.collections()]) >= 1
doc = [_ async for _ in async_client.get_all([existing_document])][0]
assert doc.to_dict()["x"] == 1

return _exercise_async_client


def test_firestore_async_client(loop, exercise_async_client):
def test_firestore_async_client(loop, exercise_async_client, instance_info):
_test_scoped_metrics = [
("Datastore/operation/Firestore/collections", 1),
("Datastore/operation/Firestore/get_all", 1),
Expand All @@ -49,6 +52,7 @@ def test_firestore_async_client(loop, exercise_async_client):
_test_rollup_metrics = [
("Datastore/all", 2),
("Datastore/allOther", 2),
("Datastore/instance/Firestore/%s/%s" % (instance_info["host"], instance_info["port_path_or_id"]), 2),
]

@validate_database_duration()
Expand All @@ -60,7 +64,7 @@ def test_firestore_async_client(loop, exercise_async_client):
)
@background_task(name="test_firestore_async_client")
def _test():
loop.run_until_complete(exercise_async_client())
loop.run_until_complete(exercise_async_client())

_test()

Expand Down
17 changes: 11 additions & 6 deletions tests/datastore_firestore/test_async_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,37 @@
# 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 testing_support.validators.validate_tt_collector_json import (
validate_tt_collector_json,
)

from newrelic.api.background_task import background_task


@pytest.fixture()
def exercise_async_collections(async_collection):
async def _exercise_async_collections():
async_collection.document("DoesNotExist")
await async_collection.add({"capital": "Rome", "currency": "Euro", "language": "Italian"}, "Italy")
await async_collection.add({"capital": "Mexico City", "currency": "Peso", "language": "Spanish"}, "Mexico")

documents_get = await async_collection.get()
assert len(documents_get) == 2
documents_stream = [_ async for _ in async_collection.stream()]
assert len(documents_stream) == 2
documents_list = [_ async for _ in async_collection.list_documents()]
assert len(documents_list) == 2

return _exercise_async_collections


def test_firestore_async_collections(loop, exercise_async_collections, async_collection):
def test_firestore_async_collections(loop, exercise_async_collections, async_collection, instance_info):
_test_scoped_metrics = [
("Datastore/statement/Firestore/%s/stream" % async_collection.id, 1),
("Datastore/statement/Firestore/%s/get" % async_collection.id, 1),
Expand All @@ -55,7 +58,9 @@ def test_firestore_async_collections(loop, exercise_async_collections, async_col
("Datastore/operation/Firestore/list_documents", 1),
("Datastore/all", 5),
("Datastore/allOther", 5),
("Datastore/instance/Firestore/%s/%s" % (instance_info["host"], instance_info["port_path_or_id"]), 5),
]

@validate_database_duration()
@validate_transaction_metrics(
"test_firestore_async_collections",
Expand All @@ -75,7 +80,7 @@ def test_firestore_async_collections_generators(collection, async_collection, as
collection.add({})
collection.add({})
assert len([_ for _ in collection.list_documents()]) == 2

assert_trace_for_async_generator(async_collection.stream)
assert_trace_for_async_generator(async_collection.list_documents)

Expand Down
19 changes: 13 additions & 6 deletions tests/datastore_firestore/test_async_documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@
# 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 testing_support.validators.validate_tt_collector_json import (
validate_tt_collector_json,
)

from newrelic.api.background_task import background_task


@pytest.fixture()
def exercise_async_documents(async_collection):
Expand All @@ -40,10 +42,11 @@ async def _exercise_async_documents():
await usa_doc.update({"president": "Joe Biden"})

await async_collection.document("USA").delete()

return _exercise_async_documents


def test_firestore_async_documents(loop, exercise_async_documents):
def test_firestore_async_documents(loop, exercise_async_documents, instance_info):
_test_scoped_metrics = [
("Datastore/statement/Firestore/Italy/set", 1),
("Datastore/statement/Firestore/Italy/get", 1),
Expand All @@ -64,7 +67,9 @@ def test_firestore_async_documents(loop, exercise_async_documents):
("Datastore/operation/Firestore/delete", 1),
("Datastore/all", 7),
("Datastore/allOther", 7),
("Datastore/instance/Firestore/%s/%s" % (instance_info["host"], instance_info["port_path_or_id"]), 7),
]

@validate_database_duration()
@validate_transaction_metrics(
"test_firestore_async_documents",
Expand All @@ -80,15 +85,17 @@ def _test():


@background_task()
def test_firestore_async_documents_generators(collection, async_collection, assert_trace_for_async_generator):
def test_firestore_async_documents_generators(
collection, async_collection, assert_trace_for_async_generator, instance_info
):
subcollection_doc = collection.document("SubCollections")
subcollection_doc.set({})
subcollection_doc.collection("collection1").add({})
subcollection_doc.collection("collection2").add({})
assert len([_ for _ in subcollection_doc.collections()]) == 2

async_subcollection = async_collection.document(subcollection_doc.id)

assert_trace_for_async_generator(async_subcollection.collections)


Expand Down
Loading

0 comments on commit 99fbdbf

Please sign in to comment.