Skip to content

Commit

Permalink
[postgres] Allow disable postgresql.database_size
Browse files Browse the repository at this point in the history
Adds a new "collect_database_size_metrics" flag that defaults to true.

Resolves #3034
  • Loading branch information
jstotz committed Nov 16, 2016
1 parent 709a2e8 commit 9e697ba
Show file tree
Hide file tree
Showing 2 changed files with 209 additions and 181 deletions.
18 changes: 13 additions & 5 deletions checks.d/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ class PostgreSql(AgentCheck):
'tup_inserted' : ('postgresql.rows_inserted', RATE),
'tup_updated' : ('postgresql.rows_updated', RATE),
'tup_deleted' : ('postgresql.rows_deleted', RATE),
}

DATABASE_SIZE_METRICS = {
'pg_database_size(datname) as pg_database_size' : ('postgresql.database_size', GAUGE),
}

Expand Down Expand Up @@ -349,7 +352,7 @@ def _is_9_1_or_above(self, key, db):
def _is_9_2_or_above(self, key, db):
return self._is_above(key, db, [9,2,0])

def _get_instance_metrics(self, key, db):
def _get_instance_metrics(self, key, db, database_size_metrics):
"""Use either COMMON_METRICS or COMMON_METRICS + NEWER_92_METRICS
depending on the postgres version.
Uses a dictionnary to save the result for each instance
Expand All @@ -374,6 +377,10 @@ def _get_instance_metrics(self, key, db):
self.instance_metrics[key] = dict(self.COMMON_METRICS, **self.NEWER_92_METRICS)
else:
self.instance_metrics[key] = dict(self.COMMON_METRICS)

if database_size_metrics:
self.instance_metrics[key] = dict(self.instance_metrics[key], **self.DATABASE_SIZE_METRICS)

metrics = self.instance_metrics.get(key)
return metrics

Expand Down Expand Up @@ -438,7 +445,7 @@ def _build_relations_config(self, yamlconfig):
self.log.warn('Failed to parse config element=%s, check syntax' % str(element))
return config

def _collect_stats(self, key, db, instance_tags, relations, custom_metrics, function_metrics, count_metrics, interface_error, programming_error):
def _collect_stats(self, key, db, instance_tags, relations, custom_metrics, function_metrics, count_metrics, database_size_metrics, interface_error, programming_error):
"""Query pg_stat_* for various metrics
If relations is not an empty list, gather per-relation metrics
on top of that.
Expand All @@ -457,7 +464,7 @@ def _collect_stats(self, key, db, instance_tags, relations, custom_metrics, func
metric_scope.append(self.COUNT_METRICS)

# These are added only once per PG server, thus the test
db_instance_metrics = self._get_instance_metrics(key, db)
db_instance_metrics = self._get_instance_metrics(key, db, database_size_metrics)
bgw_instance_metrics = self._get_bgw_metrics(key, db)

if db_instance_metrics is not None:
Expand Down Expand Up @@ -668,6 +675,7 @@ def check(self, instance):
function_metrics = _is_affirmative(instance.get('collect_function_metrics', False))
# Default value for `count_metrics` is True for backward compatibility
count_metrics = _is_affirmative(instance.get('collect_count_metrics', True))
database_size_metrics = _is_affirmative(instance.get('collect_database_size_metrics', True))

if relations and not dbname:
self.warning('"dbname" parameter must be set when using the "relations" parameter.')
Expand Down Expand Up @@ -702,11 +710,11 @@ def check(self, instance):
db = self.get_connection(key, host, port, user, password, dbname, ssl, connect_fct)
version = self._get_version(key, db)
self.log.debug("Running check against version %s" % version)
self._collect_stats(key, db, tags, relations, custom_metrics, function_metrics, count_metrics, interface_error, programming_error)
self._collect_stats(key, db, tags, relations, custom_metrics, function_metrics, count_metrics, database_size_metrics, interface_error, programming_error)
except ShouldRestartException:
self.log.info("Resetting the connection")
db = self.get_connection(key, host, port, user, password, dbname, ssl, connect_fct, use_cached=False)
self._collect_stats(key, db, tags, relations, custom_metrics, function_metrics, count_metrics, interface_error, programming_error)
self._collect_stats(key, db, tags, relations, custom_metrics, function_metrics, count_metrics, database_size_metrics, interface_error, programming_error)

if db is not None:
service_check_tags = self._get_service_check_tags(host, port, dbname)
Expand Down
Loading

0 comments on commit 9e697ba

Please sign in to comment.