Beispiel #1
0
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_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,
    )
Beispiel #3
0
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_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)
Beispiel #6
0
def check():
    c = PostgreSql('postgres', {}, [{
        'dbname': 'dbname',
        'host': 'localhost',
        'port': '5432'
    }])
    c._is_9_2_or_above = mock.MagicMock()
    return c
Beispiel #7
0
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, [])
Beispiel #8
0
def check():
    c = PostgreSql('postgres', {}, [{
        'dbname': 'dbname',
        'host': 'localhost',
        'port': '5432'
    }])
    c._version = VersionInfo(9, 2, 0)
    return c
Beispiel #9
0
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)
Beispiel #10
0
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_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_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)
Beispiel #13
0
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']
                                    ])
Beispiel #14
0
def test_get_instance_with_default(pg_instance, collect_default_database):
    """
    Test the contents of the query string with different `collect_default_database` values
    """
    pg_instance['collect_default_database'] = collect_default_database
    check = PostgreSql('postgres', {}, [pg_instance])
    check._version = VersionInfo(9, 2, 0)
    res = check.metrics_cache.get_instance_metrics(check._version)
    dbfilter = " AND psd.datname not ilike 'postgres'"
    if collect_default_database:
        assert dbfilter not in res['query']
    else:
        assert dbfilter in res['query']
Beispiel #15
0
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])
Beispiel #16
0
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'])
Beispiel #17
0
def test_resolved_hostname(disable_generic_tags, expected_hostname,
                           pg_instance):
    instance = copy.deepcopy(pg_instance)
    instance['disable_generic_tags'] = disable_generic_tags
    check = PostgreSql('test_instance', {}, [instance])

    with mock.patch('datadog_checks.postgres.PostgreSql.resolve_db_host',
                    return_value='resolved.hostname') as resolve_db_host:
        assert check.resolved_hostname == expected_hostname
        assert resolve_db_host.called == disable_generic_tags, 'Expected resolve_db_host.called to be ' + str(
            disable_generic_tags)
Beispiel #18
0
def check():
    check = PostgreSql('postgres', {}, {})
    check._is_9_2_or_above = mock.MagicMock()
    PostgreSql._known_servers = set()  # reset the global state
    return check
Beispiel #19
0
 def _check(instance):
     c = PostgreSql('postgres', {}, [instance])
     return c
Beispiel #20
0
def check():
    check = PostgreSql('postgres', {}, {})
    check._is_9_2_or_above = mock.MagicMock()
    return check
Beispiel #21
0
 def _check(instance):
     c = PostgreSql('postgres', {}, [instance])
     c._is_9_2_or_above = mock.MagicMock()
     return c
Beispiel #22
0
def test_server_tag_(disable_generic_tags, expected_tags, pg_instance):
    instance = copy.deepcopy(pg_instance)
    instance['disable_generic_tags'] = disable_generic_tags
    check = PostgreSql('test_instance', {}, [instance])
    tags = check._get_service_check_tags()
    assert set(tags) == expected_tags