Skip to content

Commit

Permalink
Updates for the review
Browse files Browse the repository at this point in the history
  • Loading branch information
DorianZaccaria committed Mar 2, 2015
1 parent cb2c335 commit c99437b
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions checks.d/pgbouncer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@

import psycopg2 as pg


class ShouldRestartException(Exception): pass


class PgBouncer(AgentCheck):
"""Collects metrics from pgbouncer
"""
SOURCE_TYPE_NAME = 'pgbouncer'
RATE = AgentCheck.rate
GAUGE = AgentCheck.gauge
DB_NAME = 'pgbouncer'

STATS_METRICS = {
'descriptors': [
Expand Down Expand Up @@ -77,13 +79,13 @@ def _collect_stats(self, key, db, instance_tags):
continue

for row in results:
if row[0] == 'pgbouncer':
if row[0] == self.DB_NAME:
continue

desc = scope['descriptors']
assert len(row) == len(cols) + len(desc)

tags = [t for t in instance_tags]
tags = instance_tags[:]
tags += ["%s:%s" % (d[0][1], d[1]) for d in zip(desc, row[:len(desc)])]

values = zip([scope['metrics'][c] for c in cols], row[len(desc):])
Expand All @@ -98,7 +100,7 @@ def _collect_stats(self, key, db, instance_tags):
self.log.error("Connection error: %s" % str(e))
raise ShouldRestartException

def _get_connection(self, key, host, port, user, password, dbname, use_cached=True):
def _get_connection(self, key, host, port, user, password, use_cached=True):
"Get and memoize connections to instances"
if key in self.dbs and use_cached:
return self.dbs[key]
Expand All @@ -109,18 +111,17 @@ def _get_connection(self, key, host, port, user, password, dbname, use_cached=Tr
"host:%s" % host,
"port:%s" % port
]
if dbname:
service_check_tags.append("db:%s" % dbname)
service_check_tags.append("db:%s" % self.DB_NAME)

if host == 'localhost' and password == '':
# Use ident method
connection = pg.connect("user=%s dbname=%s" % (user, dbname))
connection = pg.connect("user=%s dbname=%s" % (user, self.DB_NAME))
elif port != '':
connection = pg.connect(host=host, port=port, user=user,
password=password, database=dbname)
password=password, database=self.DB_NAME)
else:
connection = pg.connect(host=host, user=user, password=password,
database=dbname)
database=self.DB_NAME)
status = AgentCheck.OK
self.service_check('pgbouncer.can_connect', status, tags=service_check_tags)
self.log.debug('pgbouncer status: %s' % status)
Expand All @@ -137,7 +138,6 @@ def _get_connection(self, key, host, port, user, password, dbname, use_cached=Tr
raise CheckException("Please specify a user to connect to PgBouncer as.")

connection.set_isolation_level(pg.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
# connection.autocommit = True

self.dbs[key] = connection
return connection
Expand All @@ -148,19 +148,18 @@ def check(self, instance):
user = instance.get('username', '')
password = instance.get('password', '')
tags = instance.get('tags', [])
dbname = 'pgbouncer'

key = '%s:%s:%s' % (host, port, dbname)
key = '%s:%s:%s' % (host, port, self.DB_NAME)

if tags is None:
tags = []
else:
tags = list(set(tags))

try:
db = self._get_connection(key, host, port, user, password, dbname)
db = self._get_connection(key, host, port, user, password)
self._collect_stats(key, db, tags)
except ShouldRestartException:
self.log.info("Resetting the connection")
db = self._get_connection(key, host, port, user, password, dbname, use_cached=False)
db = self._get_connection(key, host, port, user, password, use_cached=False)
self._collect_stats(key, db, tags)

0 comments on commit c99437b

Please sign in to comment.