Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[postgres] Allow disabling postgresql.database_size metrics #3035

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 4 additions & 0 deletions conf.d/postgres.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,7 @@ instances:
# suggested value is False.
# collect_count_metrics: False
#

# Collect database size metrics. Default value is True but they might be slow with large databases
# collect_database_size_metrics: False
#
Loading