Skip to content

Commit

Permalink
Make it possible to enable PostgreSQL extra metrics collection (DataD…
Browse files Browse the repository at this point in the history
…og#493)

The current version of the PostgresSQL module integration doesn't
handle the following parameters for the Datadog PostgreSQL integration:
  * collect_function_metrics
  * collect_count_metrics
  * collect_activity_metrics
  * collect_database_size_metrics

This way metrics like postgresql.active_queries and postgresql.waiting_queries,
for example, were not available.
This change aims to make them available whenever desired.
  • Loading branch information
Diogo Kiss authored and truthbk committed Feb 4, 2019
1 parent 9841316 commit 7928d2c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 21 deletions.
64 changes: 43 additions & 21 deletions manifests/integrations/postgres.pp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Parameters:
# $password
# The password for the datadog user
# $host:
# $host
# The host postgres is running on
# $dbname
# The postgres database name
Expand All @@ -18,6 +18,18 @@
# $use_psycopg2
# Boolean to flag connecting to postgres with psycopg2 instead of pg8000.
# Warning, psycopg2 doesn't support ssl mode.
# $collect_function_metrics
# Boolean to enable collecting metrics regarding PL/pgSQL functions from pg_stat_user_functions.
# $collect_count_metrics
# Boolean to enable collecting count metrics, default value is True for backward compatibility but they might be slow,
# suggested value is False.
# $collect_activity_metrics
# Boolean to enable collecting metrics regarding transactions from pg_stat_activity, default value is False.
# Please make sure the user has sufficient privileges to read from pg_stat_activity before enabling this option.
# $collect_database_size_metrics
# Boolean to enable collecting database size metrics. Default value is True but they might be slow with large databases
# $collect_default_database
# Boolean to enable collecting statistics from the default database 'postgres' in the check metrics, default to false
# $tags
# Optional array of tags
# $tables
Expand Down Expand Up @@ -56,16 +68,21 @@
#
class datadog_agent::integrations::postgres(
String $password,
String $host = 'localhost',
String $dbname = 'postgres',
Variant[String, Integer] $port = '5432',
String $username = 'datadog',
Boolean $ssl = false,
Boolean $use_psycopg2 = false,
Array[String] $tags = [],
Array[String] $tables = [],
Hash $custom_metrics = {},
Optional[Array] $instances = undef,
String $host = 'localhost',
String $dbname = 'postgres',
Variant[String, Integer] $port = '5432',
String $username = 'datadog',
Boolean $ssl = false,
Boolean $use_psycopg2 = false,
Boolean $collect_function_metrics = false,
Boolean $collect_count_metrics = false,
Boolean $collect_activity_metrics = false,
Boolean $collect_database_size_metrics = false,
Boolean $collect_default_database = false,
Array[String] $tags = [],
Array[String] $tables = [],
Hash $custom_metrics = {},
Optional[Array] $instances = undef,
) inherits datadog_agent::params {
include datadog_agent

Expand All @@ -85,16 +102,21 @@

if !$instances and $host {
$_instances = [{
'host' => $host,
'password' => $password,
'dbname' => $dbname,
'port' => $port,
'username' => $username,
'ssl' => $ssl,
'use_psycopg2' => $use_psycopg2,
'tags' => $tags,
'tables' => $tables,
'custom_metrics' => $custom_metrics,
'host' => $host,
'password' => $password,
'dbname' => $dbname,
'port' => $port,
'username' => $username,
'ssl' => $ssl,
'use_psycopg2' => $use_psycopg2,
'tags' => $tags,
'tables' => $tables,
'custom_metrics' => $custom_metrics,
'collect_function_metrics' => $collect_function_metrics,
'collect_count_metrics' => $collect_count_metrics,
'collect_activity_metrics' => $collect_activity_metrics,
'collect_database_size_metrics' => $collect_database_size_metrics,
'collect_default_database' => $collect_default_database,
}]
} elsif !$instances{
$_instances = []
Expand Down
24 changes: 24 additions & 0 deletions spec/classes/datadog_agent_integrations_postgres_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,34 @@
it { should contain_file(conf_file).with_content(%r{port: 5432}) }
it { should contain_file(conf_file).with_content(%r{username: datadog}) }
it { should contain_file(conf_file).without_content(%r{^\s*use_psycopg2: }) }
it { should contain_file(conf_file).with_content(%r{collect_function_metrics: false}) }
it { should contain_file(conf_file).with_content(%r{collect_count_metrics: false}) }
it { should contain_file(conf_file).with_content(%r{collect_activity_metrics: false}) }
it { should contain_file(conf_file).with_content(%r{collect_database_size_metrics: false}) }
it { should contain_file(conf_file).with_content(%r{collect_default_database: false}) }
it { should contain_file(conf_file).without_content(%r{tags: })}
it { should contain_file(conf_file).without_content(%r{^[^#]*relations: }) }
end

context 'with extra metrics collection enabled' do
let(:params) {{
password: 'abc123',
collect_function_metrics: true,
collect_count_metrics: true,
collect_activity_metrics: true,
collect_database_size_metrics: true,
collect_default_database: true,
}}
it {
should contain_file(conf_file)
.with_content(%r{collect_function_metrics: true})
.with_content(%r{collect_count_metrics: true})
.with_content(%r{collect_activity_metrics: true})
.with_content(%r{collect_database_size_metrics: true})
.with_content(%r{collect_default_database: true})
}
end

context 'with use_psycopg2 enabled' do
let(:params) {{
use_psycopg2: true,
Expand Down
5 changes: 5 additions & 0 deletions templates/agent-conf.d/postgres.yaml.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ instances:
username: <%= instance['username'] %>
password: <%= instance['password'] %>
dbname: <%= instance['dbname'] %>
collect_function_metrics: <%= instance['collect_function_metrics'] %>
collect_count_metrics: <%= instance['collect_count_metrics'] %>
collect_activity_metrics: <%= instance['collect_activity_metrics'] %>
collect_database_size_metrics: <%= instance['collect_database_size_metrics'] %>
collect_default_database: <%= instance['collect_default_database'] %>
<% if instance['ssl'] == true -%>
ssl: true
<% end -%>
Expand Down

0 comments on commit 7928d2c

Please sign in to comment.