Skip to content

Commit ebf44e8

Browse files
ajvbtruthbk
authored andcommitted
Ajvb/support multiple pgbouncers (DataDog#361)
* Support for multiple pgbouncers + pgbouncer spec This adds support for an optional 'pgbouncers' parameter that works exactly like the processes parameter in the process integration. This commit also includes specs for the pgbouncer integration. * Minor style/doc changes + spec updates * Fix ruby version differences within specs
1 parent 5e53996 commit ebf44e8

3 files changed

Lines changed: 149 additions & 22 deletions

File tree

manifests/integrations/pgbouncer.pp

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,57 @@
33
# This class will install the necessary configuration for the pgbouncer integration
44
#
55
# Parameters:
6-
# $password
6+
# $password:
77
# The password for the datadog user
88
# $host:
99
# The host pgbouncer is listening on
10-
# $port
10+
# $port:
1111
# The pgbouncer port number
12-
# $username
12+
# $username:
1313
# The username for the datadog user
14-
# $tags
14+
# $tags:
1515
# Optional array of tags
16+
# $pgbouncers:
17+
# Optional array of pgbouncer hashes. See example
1618
#
1719
# Sample Usage:
1820
#
1921
# class { 'datadog_agent::integrations::pgbouncer' :
20-
# host => 'localhost',
21-
# username => 'datadog',
22-
# port => '6432',
23-
# password => 'some_pass',
22+
# host => 'localhost',
23+
# username => 'datadog',
24+
# port => '6432',
25+
# password => 'some_pass',
2426
# }
2527
#
28+
# class { 'datadog_agent::integrations::pgbouncer' :
29+
# pgbouncers => [
30+
# {
31+
# 'host' => 'localhost',
32+
# 'username' => 'datadog',
33+
# 'port' => '6432',
34+
# 'password' => 'some_pass',
35+
# 'tags' => ['instance:one'],
36+
# },
37+
# {
38+
# 'host' => 'localhost',
39+
# 'username' => 'datadog2',
40+
# 'port' => '6433',
41+
# 'password' => 'some_pass2',
42+
# },
43+
# ],
44+
# }
2645
#
2746
class datadog_agent::integrations::pgbouncer(
28-
$password,
47+
$password = '',
2948
$host = 'localhost',
3049
$port = '6432',
3150
$username = 'datadog',
3251
$tags = [],
52+
$pgbouncers = [],
3353
) inherits datadog_agent::params {
3454

3555
validate_array($tags)
56+
validate_array($pgbouncers)
3657

3758
if $::datadog_agent::agent6_enable {
3859
$dst = "${datadog_agent::conf6_dir}/pgbouncer.yaml"
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
require 'spec_helper'
2+
3+
describe 'datadog_agent::integrations::pgbouncer' do
4+
context 'supported agents - v5 and v6' do
5+
agents = { '5' => false, '6' => true }
6+
agents.each do |_, enabled|
7+
let(:pre_condition) { "class {'::datadog_agent': agent6_enable => #{enabled}}" }
8+
let(:facts) {{
9+
operatingsystem: 'Ubuntu',
10+
}}
11+
if !enabled
12+
let(:conf_dir) { '/etc/dd-agent/conf.d' }
13+
else
14+
let(:conf_dir) { '/etc/datadog-agent/conf.d' }
15+
end
16+
let(:dd_user) { 'dd-agent' }
17+
let(:dd_group) { 'root' }
18+
let(:dd_package) { 'datadog-agent' }
19+
let(:dd_service) { 'datadog-agent' }
20+
let(:conf_file) { "#{conf_dir}/pgbouncer.yaml" }
21+
22+
23+
context 'with default parameters' do
24+
let(:params) {{
25+
password: 'foobar',
26+
}}
27+
it { should contain_file(conf_file).with_content(%r{host: localhost}) }
28+
it { should contain_file(conf_file).with_content(%r{port: 6432}) }
29+
it { should contain_file(conf_file).with_content(%r{password: foobar}) }
30+
31+
it { should compile.with_all_deps }
32+
it { should contain_file(conf_file).with(
33+
owner: dd_user,
34+
group: dd_group,
35+
mode: '0600',
36+
)}
37+
it { should contain_file(conf_file).that_requires("Package[#{dd_package}]") }
38+
it { should contain_file(conf_file).that_notifies("Service[#{dd_service}]") }
39+
end
40+
41+
context 'with one pgbouncer config parameters' do
42+
let(:params) {{
43+
host: 'localhost',
44+
username: 'foo',
45+
port: '1234',
46+
password: 'bar',
47+
tags: ['foo:bar'],
48+
}}
49+
it { should contain_file(conf_file).with_content(%r{host: localhost}) }
50+
it { should contain_file(conf_file).with_content(%r{username: foo}) }
51+
it { should contain_file(conf_file).with_content(%r{port: ("|')?1234("|')?}) }
52+
it { should contain_file(conf_file).with_content(%r{password: bar}) }
53+
it { should contain_file(conf_file).with_content(%r{- ("|')?foo:bar("|')?}) }
54+
end
55+
56+
context 'with multiple pgbouncers configured' do
57+
let(:params) {{
58+
pgbouncers: [
59+
{
60+
'host' => 'localhost',
61+
'username' => 'datadog',
62+
'port' => '6432',
63+
'password' => 'some_pass',
64+
'tags' => ['instance:one'],
65+
},
66+
{
67+
'host' => 'localhost',
68+
'username' => 'datadog2',
69+
'port' => '6433',
70+
'password' => 'some_pass2',
71+
'tags' => ['instance:two'],
72+
}
73+
]
74+
}}
75+
it { should contain_file(conf_file).with_content(%r{host: localhost}) }
76+
it { should contain_file(conf_file).with_content(%r{port: ("|')?6432("|')?}) }
77+
it { should contain_file(conf_file).with_content(%r{password: some_pass}) }
78+
it { should contain_file(conf_file).with_content(%r{- ("|')?instance:one("|')?}) }
79+
it { should contain_file(conf_file).with_content(%r{port: ("|')?6433("|')?}) }
80+
it { should contain_file(conf_file).with_content(%r{password: some_pass2}) }
81+
it { should contain_file(conf_file).with_content(%r{- ("|')?instance:two("|')?}) }
82+
end
83+
end
84+
end
85+
end
Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,44 @@
1+
<%
2+
require 'yaml'
3+
4+
if RUBY_VERSION < "1.9"
5+
# Hack to avoid ruby 1.8 always reordering the hashes and
6+
# changing this template resource at every run
7+
# See https://github.com/DataDog/puppet-datadog-agent/issues/43
8+
# Taken from http://www.dzone.com/snippets/generating-yaml-hashes-sorted
9+
class Hash
10+
# Replacing the to_yaml function so it'll serialize hashes sorted (by their keys)
11+
#
12+
# Original function is in /usr/lib/ruby/1.8/yaml/rubytypes.rb
13+
def to_yaml( opts = {} )
14+
YAML::quick_emit( object_id, opts ) do |out|
15+
out.map( taguri, to_yaml_style ) do |map|
16+
sort.each do |k, v| # <-- here's my addition (the 'sort')
17+
map.add( k, v )
18+
end
19+
end
20+
end
21+
end
22+
end
23+
end
24+
%>
25+
26+
<%- if @pgbouncers.empty? -%>
127
init_config:
228

329
instances:
4-
# - host: localhost
5-
# port: 6432
6-
# username: my_username
7-
# password: my_password
8-
# tags:
9-
# - optional_tag1
10-
# - optional_tag2
11-
#
1230
- host: <%= @host %>
1331
port: <%= @port %>
1432
username: <%= @username %>
1533
password: <%= @password %>
16-
<% if @tags and ! @tags.empty? -%>
34+
<% if @tags and ! @tags.empty? -%>
1735
tags:
18-
<%- Array(@tags).each do |tag| -%>
19-
<%- if tag != '' -%>
36+
<%- Array(@tags).each do |tag| -%>
37+
<%- if tag != '' -%>
2038
- <%= tag %>
39+
<%- end -%>
2140
<%- end -%>
22-
<%- end -%>
23-
<% end -%>
41+
<% end -%>
42+
<%- else -%>
43+
<%= {'init_config'=>nil, 'instances'=>@pgbouncers}.to_yaml %>
44+
<%- end -%>

0 commit comments

Comments
 (0)