Skip to content

Commit

Permalink
Added toast metric tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bonnefoa committed Mar 14, 2023
1 parent c5e1d5e commit 91f77e7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
5 changes: 5 additions & 0 deletions postgres/tests/compose/resources/03_load_data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" datadog_test <<-EOSQL
INSERT INTO pgtable (lastname, firstname, address, city) VALUES ('Cavaille', 'Leo', 'Midtown', 'New York'), ('Someveryveryveryveryveryveryveryveryveryverylongname', 'something', 'Avenue des Champs Elysees', 'Beautiful city of lights');
CREATE TABLE pg_newtable (personid SERIAL, lastname VARCHAR(255), firstname VARCHAR(255), address VARCHAR(255), city VARCHAR(255));
INSERT INTO pg_newtable (lastname, firstname, address, city) VALUES ('Cavaille', 'Leo', 'Midtown', 'New York'), ('Someveryveryveryveryveryveryveryveryveryverylongname', 'something', 'Avenue des Champs Elysees', 'Beautiful city of lights');
CREATE TABLE test_toast (id SERIAL, txt TEXT);
insert into test_toast (txt) select string_agg (md5(random()::text),'') as dummy from generate_series(1,5000);
update test_toast set txt = txt;
SELECT * FROM test_toast;
SELECT * FROM test_toast;
SELECT * FROM persons;
SELECT * FROM persons;
SELECT * FROM persons;
Expand Down
48 changes: 41 additions & 7 deletions postgres/tests/test_relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pytest

from datadog_checks.base import ConfigurationError
from datadog_checks.dev.utils import get_metadata_metrics
from datadog_checks.postgres.relationsmanager import (
IDX_METRICS,
REL_METRICS,
Expand All @@ -15,7 +16,6 @@

from .common import DB_NAME, HOST, PORT


INDEX_FROM_REL_METRICS = ['postgresql.index_rel_scans', 'postgresql.index_rel_rows_fetched']
INDEX_FROM_STATIO_METRICS = ['postgresql.index_blocks_read', 'postgresql.index_blocks_hit']

Expand All @@ -25,6 +25,15 @@ def _get_metric_names(scope):
yield metric_name


def _get_oid_of_relation(relation):
oid = 0
with psycopg2.connect(host=HOST, dbname=DB_NAME, user="postgres", password="datad0g") as conn:
with conn.cursor() as cur:
cur.execute("select oid from pg_class where relname='{}'".format(relation))
oid = cur.fetchall()[0][0]
return oid


def _check_relation_metrics(aggregator, pg_instance, relation):
relation = relation.lower()
base_tags = pg_instance['tags'] + ['port:{}'.format(pg_instance['port']), 'db:%s' % pg_instance['dbname']]
Expand All @@ -33,12 +42,7 @@ def _check_relation_metrics(aggregator, pg_instance, relation):
'schema:public',
]

oid = 0
with psycopg2.connect(host=HOST, dbname=DB_NAME, user="postgres", password="datad0g") as conn:
with conn.cursor() as cur:
cur.execute("select oid from pg_class where relname='{}'".format(relation))
oid = cur.fetchall()[0][0]

oid = _get_oid_of_relation(relation)
expected_toast_tags = base_tags + [
'toast_of:{}'.format(relation),
'table:pg_toast_{}'.format(oid),
Expand Down Expand Up @@ -83,6 +87,7 @@ def test_relations_metrics(aggregator, integration_check, pg_instance):
posgres_check = integration_check(pg_instance)
posgres_check.check(pg_instance)
_check_relation_metrics(aggregator, pg_instance, 'persons')
aggregator.assert_metrics_using_metadata(get_metadata_metrics())


@pytest.mark.integration
Expand Down Expand Up @@ -132,6 +137,35 @@ def test_relations_metrics_regex(aggregator, integration_check, pg_instance):
_check_relation_metrics(aggregator, pg_instance, relation)


@pytest.mark.integration
@pytest.mark.usefixtures('dd_environment')
def test_toast_relation_metrics(aggregator, integration_check, pg_instance):
pg_instance['relations'] = ['test_toast']
posgres_check = integration_check(pg_instance)
posgres_check.check(pg_instance)

oid = _get_oid_of_relation('test_toast')
expected_toast_tags = pg_instance['tags'] + [
'port:{}'.format(pg_instance['port']),
'db:%s' % pg_instance['dbname'],
'toast_of:test_toast',
'table:pg_toast_{}'.format(oid),
'schema:pg_toast',
]
expected_toast_index_tags = expected_toast_tags + [
'index:pg_toast_{}_index'.format(oid),
]
# 5000 iterations * 32 bytes / 1996 bytes per page = 80.16032064128257
aggregator.assert_metric('postgresql.seq_scans', count=1, value=1, tags=expected_toast_tags)
aggregator.assert_metric('postgresql.index_rel_scans', count=1, value=3, tags=expected_toast_tags)
aggregator.assert_metric('postgresql.live_rows', count=1, value=81, tags=expected_toast_tags)
aggregator.assert_metric('postgresql.rows_inserted', count=1, value=81, tags=expected_toast_tags)
aggregator.assert_metric('postgresql.index_rel_rows_fetched', count=1, value=81 * 2, tags=expected_toast_tags)

aggregator.assert_metric('postgresql.index_rows_read', count=1, value=81 * 2, tags=expected_toast_index_tags)
aggregator.assert_metric('postgresql.index_rows_fetched', count=1, value=81 * 2, tags=expected_toast_index_tags)


@pytest.mark.integration
@pytest.mark.usefixtures('dd_environment')
def test_max_relations(aggregator, integration_check, pg_instance):
Expand Down

0 comments on commit 91f77e7

Please sign in to comment.