Skip to content

Commit

Permalink
Add some metrics to the postgres check
Browse files Browse the repository at this point in the history
  • Loading branch information
DorianZaccaria committed Feb 26, 2015
1 parent 243bf8b commit 9be2fec
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
21 changes: 19 additions & 2 deletions checks.d/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,26 @@ class PostgreSql(AgentCheck):
relname = ANY(%s)"""
}

COUNT_METRICS = {
'descriptors': [
('schemaname', 'schema')
],
'metrics': {
'pg_stat_user_tables': ('postgresql.total_tables', GAUGE),
},
'relation': False,
'query': """
SELECT schemaname, count(*)
FROM %s
GROUP BY schemaname
"""
}

REPLICATION_METRICS = {
'descriptors': [],
'metrics': {
'GREATEST(0, EXTRACT(EPOCH FROM now() - pg_last_xact_replay_timestamp())) AS replication_delay': ('postgresql.replication_delay', GAUGE),
'CASE WHEN pg_last_xlog_receive_location() = pg_last_xlog_replay_location() THEN 0 ELSE GREATEST (0, EXTRACT (EPOCH FROM now() - pg_last_xact_replay_timestamp())) END': ('postgresql.replication_delay', GAUGE),
'abs(pg_xlog_location_diff(pg_last_xlog_receive_location(), pg_last_xlog_replay_location())) AS replication_delay_bytes': ('postgres.replication_delay_bytes', GAUGE)
},
'relation': False,
'query': """
Expand Down Expand Up @@ -274,7 +290,8 @@ def _collect_stats(self, key, db, instance_tags, relations, custom_metrics):
self.DB_METRICS,
self.CONNECTION_METRICS,
self.BGW_METRICS,
self.LOCK_METRICS
self.LOCK_METRICS,
self.COUNT_METRICS
]

# Do we need relation-specific metrics?
Expand Down
1 change: 1 addition & 0 deletions tests/test_postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def test_checks(self):
self.assertTrue(len([m for m in metrics if m[0] == u'postgresql.total_size']) >= 1, pprint(metrics))
self.assertTrue(len([m for m in metrics if m[0] == u'postgresql.max_connections']) >= 1, pprint(metrics))
self.assertTrue(len([m for m in metrics if m[0] == u'postgresql.percent_usage_connections']) >= 1, pprint(metrics))
self.assertTrue(len([m for m in metrics if m[0] == u'postgresql.total_tables']) >= 1, pprint(metrics))
self.assertTrue(len([m for m in metrics if m[0] == u'postgresql.db.count']) == 1, pprint(metrics))
# Don't test for locks
# self.assertTrue(len([m for m in metrics if m[0] == u'postgresql.locks']) >= 1, pprint(metrics))
Expand Down

0 comments on commit 9be2fec

Please sign in to comment.