Skip to content

Commit 0b842d3

Browse files
Diogo Kisstruthbk
authored andcommitted
Make it possible to enable PostgreSQL extra metrics collection (#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.
1 parent af72537 commit 0b842d3

3 files changed

Lines changed: 72 additions & 21 deletions

File tree

manifests/integrations/postgres.pp

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# Parameters:
66
# $password
77
# The password for the datadog user
8-
# $host:
8+
# $host
99
# The host postgres is running on
1010
# $dbname
1111
# The postgres database name
@@ -18,6 +18,18 @@
1818
# $use_psycopg2
1919
# Boolean to flag connecting to postgres with psycopg2 instead of pg8000.
2020
# Warning, psycopg2 doesn't support ssl mode.
21+
# $collect_function_metrics
22+
# Boolean to enable collecting metrics regarding PL/pgSQL functions from pg_stat_user_functions.
23+
# $collect_count_metrics
24+
# Boolean to enable collecting count metrics, default value is True for backward compatibility but they might be slow,
25+
# suggested value is False.
26+
# $collect_activity_metrics
27+
# Boolean to enable collecting metrics regarding transactions from pg_stat_activity, default value is False.
28+
# Please make sure the user has sufficient privileges to read from pg_stat_activity before enabling this option.
29+
# $collect_database_size_metrics
30+
# Boolean to enable collecting database size metrics. Default value is True but they might be slow with large databases
31+
# $collect_default_database
32+
# Boolean to enable collecting statistics from the default database 'postgres' in the check metrics, default to false
2133
# $tags
2234
# Optional array of tags
2335
# $tables
@@ -56,16 +68,21 @@
5668
#
5769
class datadog_agent::integrations::postgres(
5870
String $password,
59-
String $host = 'localhost',
60-
String $dbname = 'postgres',
61-
Variant[String, Integer] $port = '5432',
62-
String $username = 'datadog',
63-
Boolean $ssl = false,
64-
Boolean $use_psycopg2 = false,
65-
Array[String] $tags = [],
66-
Array[String] $tables = [],
67-
Hash $custom_metrics = {},
68-
Optional[Array] $instances = undef,
71+
String $host = 'localhost',
72+
String $dbname = 'postgres',
73+
Variant[String, Integer] $port = '5432',
74+
String $username = 'datadog',
75+
Boolean $ssl = false,
76+
Boolean $use_psycopg2 = false,
77+
Boolean $collect_function_metrics = false,
78+
Boolean $collect_count_metrics = false,
79+
Boolean $collect_activity_metrics = false,
80+
Boolean $collect_database_size_metrics = false,
81+
Boolean $collect_default_database = false,
82+
Array[String] $tags = [],
83+
Array[String] $tables = [],
84+
Hash $custom_metrics = {},
85+
Optional[Array] $instances = undef,
6986
) inherits datadog_agent::params {
7087
include datadog_agent
7188

@@ -85,16 +102,21 @@
85102

86103
if !$instances and $host {
87104
$_instances = [{
88-
'host' => $host,
89-
'password' => $password,
90-
'dbname' => $dbname,
91-
'port' => $port,
92-
'username' => $username,
93-
'ssl' => $ssl,
94-
'use_psycopg2' => $use_psycopg2,
95-
'tags' => $tags,
96-
'tables' => $tables,
97-
'custom_metrics' => $custom_metrics,
105+
'host' => $host,
106+
'password' => $password,
107+
'dbname' => $dbname,
108+
'port' => $port,
109+
'username' => $username,
110+
'ssl' => $ssl,
111+
'use_psycopg2' => $use_psycopg2,
112+
'tags' => $tags,
113+
'tables' => $tables,
114+
'custom_metrics' => $custom_metrics,
115+
'collect_function_metrics' => $collect_function_metrics,
116+
'collect_count_metrics' => $collect_count_metrics,
117+
'collect_activity_metrics' => $collect_activity_metrics,
118+
'collect_database_size_metrics' => $collect_database_size_metrics,
119+
'collect_default_database' => $collect_default_database,
98120
}]
99121
} elsif !$instances{
100122
$_instances = []

spec/classes/datadog_agent_integrations_postgres_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,34 @@
4848
it { should contain_file(conf_file).with_content(%r{port: 5432}) }
4949
it { should contain_file(conf_file).with_content(%r{username: datadog}) }
5050
it { should contain_file(conf_file).without_content(%r{^\s*use_psycopg2: }) }
51+
it { should contain_file(conf_file).with_content(%r{collect_function_metrics: false}) }
52+
it { should contain_file(conf_file).with_content(%r{collect_count_metrics: false}) }
53+
it { should contain_file(conf_file).with_content(%r{collect_activity_metrics: false}) }
54+
it { should contain_file(conf_file).with_content(%r{collect_database_size_metrics: false}) }
55+
it { should contain_file(conf_file).with_content(%r{collect_default_database: false}) }
5156
it { should contain_file(conf_file).without_content(%r{tags: })}
5257
it { should contain_file(conf_file).without_content(%r{^[^#]*relations: }) }
5358
end
5459

60+
context 'with extra metrics collection enabled' do
61+
let(:params) {{
62+
password: 'abc123',
63+
collect_function_metrics: true,
64+
collect_count_metrics: true,
65+
collect_activity_metrics: true,
66+
collect_database_size_metrics: true,
67+
collect_default_database: true,
68+
}}
69+
it {
70+
should contain_file(conf_file)
71+
.with_content(%r{collect_function_metrics: true})
72+
.with_content(%r{collect_count_metrics: true})
73+
.with_content(%r{collect_activity_metrics: true})
74+
.with_content(%r{collect_database_size_metrics: true})
75+
.with_content(%r{collect_default_database: true})
76+
}
77+
end
78+
5579
context 'with use_psycopg2 enabled' do
5680
let(:params) {{
5781
use_psycopg2: true,

templates/agent-conf.d/postgres.yaml.erb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ instances:
2020
username: <%= instance['username'] %>
2121
password: <%= instance['password'] %>
2222
dbname: <%= instance['dbname'] %>
23+
collect_function_metrics: <%= instance['collect_function_metrics'] %>
24+
collect_count_metrics: <%= instance['collect_count_metrics'] %>
25+
collect_activity_metrics: <%= instance['collect_activity_metrics'] %>
26+
collect_database_size_metrics: <%= instance['collect_database_size_metrics'] %>
27+
collect_default_database: <%= instance['collect_default_database'] %>
2328
<% if instance['ssl'] == true -%>
2429
ssl: true
2530
<% end -%>

0 commit comments

Comments
 (0)