Skip to content

Commit

Permalink
Include db tag with postgresql.locks metrics (#2567)
Browse files Browse the repository at this point in the history
* Include db tag with postgresql.locks metrics

* Fix flake8 complains

* Remove unused ungranted_lock metric
  • Loading branch information
sj26 authored and ChristineTChen committed Nov 20, 2018
1 parent e02e63c commit bab268c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
6 changes: 4 additions & 2 deletions postgres/datadog_checks/postgres/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,23 @@ class PostgreSql(AgentCheck):
LOCK_METRICS = {
'descriptors': [
('mode', 'lock_mode'),
('datname', 'db'),
('relname', 'table'),
],
'metrics': {
'lock_count': ('postgresql.locks', GAUGE),
'ungranted_locks': ('postgresql.ungranted_locks'),
},
'query': """
SELECT mode,
pd.datname,
pc.relname,
count(*) AS %s
FROM pg_locks l
JOIN pg_database pd ON (l.database = pd.oid)
JOIN pg_class pc ON (l.relation = pc.oid)
WHERE l.mode IS NOT NULL
AND pc.relname NOT LIKE 'pg_%%'
GROUP BY pc.relname, mode""",
GROUP BY pd.datname, pc.relname, mode""",
'relation': False,
}

Expand Down
13 changes: 13 additions & 0 deletions postgres/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Licensed under Simplified BSD License (see LICENSE)
import os

import psycopg2
import pytest

from datadog_checks.postgres import PostgreSql
Expand Down Expand Up @@ -122,6 +123,18 @@ def test_connections_metrics(aggregator, check, pg_instance):
aggregator.assert_metric('postgresql.connections', count=1, tags=pg_instance['tags']+['db:datadog_test'])


@pytest.mark.integration
@pytest.mark.usefixtures('dd_environment')
def test_locks_metrics(aggregator, check, pg_instance):
with psycopg2.connect(host=HOST, dbname=DB_NAME, user="postgres") as conn:
with conn.cursor() as cur:
cur.execute('LOCK persons')
check.check(pg_instance)

tags = pg_instance['tags'] + ['lock_mode:AccessExclusiveLock', 'table:persons', 'db:datadog_test']
aggregator.assert_metric('postgresql.locks', count=1, tags=tags)


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

0 comments on commit bab268c

Please sign in to comment.