def test_relations_metrics(aggregator, pg_instance): pg_instance['relations'] = ['persons'] posgres_check = PostgreSql('postgres', {}, {}) posgres_check.check(pg_instance) expected_tags = pg_instance['tags'] + [ 'server:{}'.format(pg_instance['host']), 'port:{}'.format(pg_instance['port']), 'db:%s' % pg_instance['dbname'], 'table:persons', 'schema:public', ] expected_size_tags = pg_instance['tags'] + [ 'server:{}'.format(pg_instance['host']), 'port:{}'.format(pg_instance['port']), 'db:%s' % pg_instance['dbname'], 'table:persons', ] for name in RELATION_METRICS: aggregator.assert_metric(name, count=1, tags=expected_tags) # 'persons' db don't have any indexes for name in RELATION_INDEX_METRICS: aggregator.assert_metric(name, count=0, tags=expected_tags) for name in RELATION_SIZE_METRICS: aggregator.assert_metric(name, count=1, tags=expected_size_tags)
def test_custom_metrics(aggregator, pg_instance): pg_instance.update({ 'relations': ['persons'], 'custom_metrics': [{ 'descriptors': [('letter', 'customdb')], 'metrics': { 'num': ['custom.num', 'Gauge'] }, 'query': ("SELECT letter, %s FROM (VALUES (21, 'a'), (22, 'b'), (23, 'c')) AS t (num,letter) LIMIT 1" ), 'relation': False, }], }) postgres_check = PostgreSql('postgres', {}, [pg_instance]) postgres_check.check(pg_instance) tags = [ 'customdb:a', 'server:{}'.format(pg_instance['host']), 'port:{}'.format(pg_instance['port']) ] tags.extend(pg_instance['tags']) aggregator.assert_metric('custom.num', value=21, tags=tags)
def test_common_metrics_without_size(aggregator, postgres_standalone, pg_instance): posgres_check = PostgreSql('postgres', {}, {}) pg_instance['collect_database_size_metrics'] = False posgres_check.check(pg_instance) assert 'postgresql.database_size' not in aggregator.metric_names
def test_correct_hostname(dbm_enabled, reported_hostname, expected_hostname, aggregator, pg_instance): pg_instance['dbm'] = dbm_enabled pg_instance['collect_activity_metrics'] = True pg_instance['disable_generic_tags'] = False # This flag also affects the hostname pg_instance['reported_hostname'] = reported_hostname check = PostgreSql('test_instance', {}, [pg_instance]) with mock.patch( 'datadog_checks.postgres.PostgreSql.resolve_db_host', return_value='resolved.hostname' ) as resolve_db_host: check.check(pg_instance) if reported_hostname: assert resolve_db_host.called is False, 'Expected resolve_db_host.called to be False' else: assert resolve_db_host.called == dbm_enabled, 'Expected resolve_db_host.called to be ' + str(dbm_enabled) expected_tags_no_db = pg_instance['tags'] + ['server:{}'.format(HOST), 'port:{}'.format(PORT)] expected_tags_with_db = expected_tags_no_db + ['db:datadog_test'] c_metrics = COMMON_METRICS if not dbm_enabled: c_metrics = c_metrics + DBM_MIGRATED_METRICS for name in c_metrics + ACTIVITY_METRICS: aggregator.assert_metric(name, count=1, tags=expected_tags_with_db, hostname=expected_hostname) for name in CONNECTION_METRICS: aggregator.assert_metric(name, count=1, tags=expected_tags_no_db, hostname=expected_hostname) aggregator.assert_service_check( 'postgres.can_connect', count=1, status=PostgreSql.OK, tags=expected_tags_with_db, hostname=expected_hostname, )
def test_custom_queries(aggregator, postgres_standalone, pg_instance): pg_instance.update({ 'custom_queries': [{ 'metric_prefix': 'custom', 'query': "SELECT letter, num FROM (VALUES (21, 'a'), (22, 'b'), (23, 'c')) AS t (num,letter) LIMIT 1", 'columns': [{ 'name': 'customtag', 'type': 'tag', }, { 'name': 'num', 'type': 'gauge', }], }, { 'metric_prefix': 'another_custom_one', 'query': "SELECT letter, num FROM (VALUES (21, 'a'), (22, 'b'), (23, 'c')) AS t (num,letter) LIMIT 1", 'columns': [{ 'name': 'customtag', 'type': 'tag', }, { 'name': 'num', 'type': 'gauge', }], }], }) postgres_check = PostgreSql('postgres', {}, {}) postgres_check.check(pg_instance) tags = ['customtag:a', 'db:{}'.format(pg_instance['dbname']) ] + pg_instance['tags'] aggregator.assert_metric('custom.num', value=21, tags=tags)
def test_common_metrics_with_agent6(instance_tag, aggregator, postgres_standalone, pg_instance): posgres_check = PostgreSql('postgres', {}, {}) expected_tags = ['db:%s' % pg_instance['dbname']] posgres_check.check(pg_instance) check_common_metrics(aggregator, expected_tags) check_bgw_metrics(aggregator, [])
def test_connections_metrics(aggregator, postgres_standalone, pg_instance): posgres_check = PostgreSql('postgres', {}, {}) posgres_check.check(pg_instance) for name in CONNECTION_METRICS: aggregator.assert_metric(name, count=1, tags=pg_instance['tags']) aggregator.assert_metric('postgresql.connections', count=1, tags=pg_instance['tags'] + ['db:datadog_test'])
def test_schema_metrics(aggregator, postgres_standalone, pg_instance): posgres_check = PostgreSql('postgres', {}, {}) posgres_check.check(pg_instance) aggregator.assert_metric('postgresql.table.count', value=1, count=1, tags=pg_instance['tags'] + ['schema:public']) aggregator.assert_metric('postgresql.db.count', value=2, count=1)
def test_index_metrics(aggregator, pg_instance): pg_instance['relations'] = ['breed'] pg_instance['dbname'] = 'dogs' posgres_check = PostgreSql('postgres', {}, {}) posgres_check.check(pg_instance) expected_tags = ['db:dogs', 'table:breed', 'index:breed_names', 'schema:public'] expected_tags += pg_instance['tags'] for name in IDX_METRICS: aggregator.assert_metric(name, count=1, tags=expected_tags)
def test_custom_queries(aggregator, pg_instance): pg_instance.update({ 'custom_queries': [ { 'metric_prefix': 'custom', 'query': "SELECT letter, num FROM (VALUES (97, 'a'), (98, 'b'), (99, 'c')) AS t (num,letter)", 'columns': [{ 'name': 'customtag', 'type': 'tag' }, { 'name': 'num', 'type': 'gauge' }], 'tags': ['query:custom'], }, { 'metric_prefix': 'another_custom_one', 'query': "SELECT letter, num FROM (VALUES (97, 'a'), (98, 'b'), (99, 'c')) AS t (num,letter)", 'columns': [{ 'name': 'customtag', 'type': 'tag' }, { 'name': 'num', 'type': 'gauge' }], 'tags': ['query:another_custom_one'], }, ] }) postgres_check = PostgreSql('postgres', {}, [pg_instance]) postgres_check.check(pg_instance) tags = [ 'db:{}'.format(pg_instance['dbname']), 'server:{}'.format(pg_instance['host']), 'port:{}'.format(pg_instance['port']), ] tags.extend(pg_instance['tags']) for tag in ('a', 'b', 'c'): value = ord(tag) custom_tags = ['customtag:{}'.format(tag)] custom_tags.extend(tags) aggregator.assert_metric('custom.num', value=value, tags=custom_tags + ['query:custom']) aggregator.assert_metric('another_custom_one.num', value=value, tags=custom_tags + ['query:another_custom_one'])
def test_can_connect_service_check(aggregator, postgres_standalone, pg_instance): return posgres_check = PostgreSql('postgres', {}, {}) posgres_check.check(pg_instance) aggregator.assert_service_check('postgres.can_connect', count=1, status=PostgreSql.OK, tags=[ 'host:localhost', 'port:15432', 'db:datadog_test', pg_instance['tags'] ])
def test_relations_metrics2(aggregator, pg_instance): pg_instance['relations'] = [ { 'relation_regex': '.*', 'schemas': ['hello', 'hello2'] }, # Empty schemas means all schemas, even though the first relation matches first. { 'relation_regex': r'[pP]ersons[-_]?(dup\d)?' }, ] relations = ['persons', 'personsdup1', 'Personsdup2'] posgres_check = PostgreSql('postgres', {}, {}) posgres_check.check(pg_instance) expected_tags = {} expected_size_tags = {} for relation in relations: expected_tags[relation] = pg_instance['tags'] + [ 'server:{}'.format(pg_instance['host']), 'port:{}'.format(pg_instance['port']), 'db:%s' % pg_instance['dbname'], 'table:{}'.format(relation.lower()), 'schema:public', ] expected_size_tags[relation] = pg_instance['tags'] + [ 'server:{}'.format(pg_instance['host']), 'port:{}'.format(pg_instance['port']), 'db:%s' % pg_instance['dbname'], 'table:{}'.format(relation.lower()), 'schema:public', ] for relation in relations: for name in RELATION_METRICS: aggregator.assert_metric(name, count=1, tags=expected_tags[relation]) # 'persons' db don't have any indexes for name in RELATION_INDEX_METRICS: aggregator.assert_metric(name, count=0, tags=expected_tags[relation]) for name in RELATION_SIZE_METRICS: aggregator.assert_metric(name, count=1, tags=expected_size_tags[relation])
def test_custom_metrics(aggregator, postgres_standalone, pg_instance): pg_instance.update({ 'relations': ['persons'], 'custom_metrics': [{ 'descriptors': [('letter', 'customdb')], 'metrics': { 'num': ['custom.num', 'Gauge'] }, 'query': "SELECT letter, %s FROM (VALUES (21, 'a'), (22, 'b'), (23, 'c')) AS t (num,letter) LIMIT 1", 'relation': False, }], }) postgres_check = PostgreSql('postgres', {}, {}) postgres_check.check(pg_instance) aggregator.assert_metric('custom.num', value=21, tags=['customdb:a'] + pg_instance['tags'])