Skip to content

Commit 4374d8a

Browse files
author
Seth Cleveland
committed
- Added custom metrics example and specs
1 parent fc575c1 commit 4374d8a

4 files changed

Lines changed: 62 additions & 3 deletions

File tree

manifests/integrations/postgres.pp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@
3333
# dbname => 'postgres'
3434
# username => 'datadog',
3535
# password => 'some_pass',
36+
# custom_metrics => {
37+
# a_custom_query => {
38+
# query => "select tag_column, %s from table",
39+
# relation => false,
40+
# metrics => {
41+
# value_column => ["value_column.datadog.tag", "GAUGE"]
42+
# },
43+
# descriptors => [
44+
# ["tag_column", "tag_column.datadog.tag"]
45+
# ]
46+
# }
47+
# }
3648
# }
3749
#
3850
#

manifests/integrations/postgres_custom_metric.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
$relation = false,
2222
$descriptors = [],
2323
) {
24-
validate_re($query, '^.*%s.*$')
24+
validate_re($query, '^.*%s.*$', 'custom_metrics require %s for metric substitution')
2525
validate_hash($metrics)
2626
validate_array($descriptors)
2727
}

spec/classes/datadog_agent_integrations_postgres_spec.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,53 @@
5555
it { should contain_file(conf_file).with_content(%r{username: monitoring}) }
5656
it { should contain_file(conf_file).with_content(%r{^[^#]*tags:\s+- foo\s+- bar\s+- baz}) }
5757
it { should contain_file(conf_file).with_content(%r{^[^#]*relations:\s+- furry\s+- fuzzy\s+- funky}) }
58+
59+
context 'with custom metric query missing %s' do
60+
let(:params) {{
61+
host: 'postgres1',
62+
dbname: 'cats',
63+
port: 4142,
64+
username: 'monitoring',
65+
password: 'abc123',
66+
custom_metrics: {
67+
'query_is_missing_%s' => {
68+
'query' => 'select * from fuzz',
69+
'metrics' => { },
70+
}
71+
}
72+
}}
73+
it do
74+
expect {
75+
is_expected.to compile
76+
}.to raise_error(/custom_metrics require %s for metric substitution/)
77+
end
78+
end
79+
80+
context 'with custom metric query' do
81+
let(:params) {{
82+
host: 'postgres1',
83+
dbname: 'cats',
84+
port: 4142,
85+
username: 'monitoring',
86+
password: 'abc123',
87+
custom_metrics: {
88+
'foo_gooo_bar_query' => {
89+
'query' => 'select foo, %s from bar',
90+
'metrics' => {
91+
"gooo" => ["custom_metric.tag.gooo", "GAUGE"]
92+
},
93+
'descriptors' => [["foo", "custom_metric.tag.foo"]]
94+
}
95+
}
96+
}}
97+
it { is_expected.to compile }
98+
it { should contain_file(conf_file).with_content(%r{^[^#]*custom_metrics:}) }
99+
it { should contain_file(conf_file).with_content(%r{\s+query:\s*['"]?select foo, %s from bar['"]?}) }
100+
it { should contain_file(conf_file).with_content(%r{\s+metrics:}) }
101+
it { should contain_file(conf_file).with_content(%r{\s+"gooo":\s+\[custom_metric.tag.gooo, GAUGE\]}) }
102+
it { should contain_file(conf_file).with_content(%r{\s+query.*\n\s+relation:\s*false}) }
103+
it { should contain_file(conf_file).with_content(%r{\s+descriptors.*\n\s+-\s+\[foo, custom_metric.tag.foo\]}) }
104+
end
58105
end
59106
end
60107

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ instances:
5555
#
5656
<% if @custom_metrics and ! @custom_metrics.empty? -%>
5757
custom_metrics:
58-
<%- Array(@custom_metrics).each do |custom_metric| -%>
58+
<%- Array(@custom_metrics).each do |n, custom_metric| -%>
5959
- query: <%= custom_metric["query"] %>
60-
relation: <%= custom_metric["relation"] %>
60+
relation: <%= custom_metric["relation"]? "true" : "false" %>
6161
metrics:
6262
<%- Array(custom_metric["metrics"]).each do |metric_name, metric_descriptor| -%>
6363
"<%= metric_name %>": [<%= metric_descriptor[0] %>, <%= metric_descriptor[1] %>]

0 commit comments

Comments
 (0)