Skip to content

Commit e28c270

Browse files
authored
[redis] mutli-instance support (#520)
* [redis] adding instances support + tests (could be improved) * [redis][spec] adding multiple instances test * [redis] fixing lint issues * [redis] documenting multi-instance option
1 parent 0feed8c commit e28c270

3 files changed

Lines changed: 119 additions & 16 deletions

File tree

manifests/integrations/redis.pp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,26 @@
1919
# Optional array of keys to check length
2020
# $command_stats
2121
# Collect INFO COMMANDSTATS output as metrics
22+
# $instances
23+
# Optional array of hashes should you wish to specify multiple instances.
24+
# If this option is specified all other parameters will be overriden.
25+
# This parameter may also be used to specify instances with hiera.
2226
#
2327
# Sample Usage:
2428
#
2529
# class { 'datadog_agent::integrations::redis' :
2630
# host => 'localhost',
2731
# }
2832
#
33+
# Hiera Usage:
34+
#
35+
# datadog_agent::integrations::redis::instances:
36+
# - host: 'localhost'
37+
# password: 'datadog'
38+
# port: 6379
39+
# slowlog_max_len: 1000
40+
# warn_on_missing_keys: true
41+
# command_stats: false
2942
#
3043
class datadog_agent::integrations::redis(
3144
String $host = 'localhost',
@@ -37,6 +50,7 @@
3750
Array $keys = [],
3851
Boolean $warn_on_missing_keys = true,
3952
Boolean $command_stats = false,
53+
Optional[Array] $instances = undef,
4054

4155
) inherits datadog_agent::params {
4256
include datadog_agent
@@ -55,6 +69,19 @@
5569

5670
validate_legacy('Array', 'validate_array', $_ports)
5771

72+
$_port_instances = $_ports.map |$instance_port| {
73+
{
74+
'host' => $host,
75+
'password' => $password,
76+
'port' => $instance_port,
77+
'slowlog_max_len' => $slowlog_max_len,
78+
'tags' => $tags,
79+
'keys' => $keys,
80+
'warn_on_missing_keys' => $warn_on_missing_keys,
81+
'command_stats' => $command_stats,
82+
}
83+
}
84+
5885
$legacy_dst = "${datadog_agent::conf_dir}/redisdb.yaml"
5986
if !$::datadog_agent::agent5_enable {
6087
$dst = "${datadog_agent::conf6_dir}/redisdb.d/conf.yaml"
@@ -65,6 +92,14 @@
6592
$dst = $legacy_dst
6693
}
6794

95+
if !$instances and $host {
96+
$_instances = $_port_instances
97+
} elsif !$instances{
98+
$_instances = []
99+
} else {
100+
$_instances = $instances
101+
}
102+
68103
file { $dst:
69104
ensure => file,
70105
owner => $datadog_agent::params::dd_user,

spec/classes/datadog_agent_integrations_redis_spec.rb

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434

3535
context 'with default parameters' do
3636
it { should contain_file(conf_file).with_content(%r{host: localhost}) }
37-
it { should contain_file(conf_file).without_content(%r{^[^#]*password: }) }
3837
it { should contain_file(conf_file).with_content(%r{port: 6379}) }
38+
it { should contain_file(conf_file).without_content(%r{^[^#]*password: }) }
3939
it { should contain_file(conf_file).without_content(%r{^[^#]*slowlog-max-len: }) }
4040
it { should contain_file(conf_file).without_content(%r{tags:}) }
4141
it { should contain_file(conf_file).without_content(%r{\bkeys:}) }
@@ -63,6 +63,59 @@
6363
it { should contain_file(conf_file).with_content(%r{warn_on_missing_keys: false}) }
6464
it { should contain_file(conf_file).with_content(%r{command_stats: true}) }
6565
end
66+
67+
context 'with ports parameters set' do
68+
let(:params) {{
69+
host: 'redis1',
70+
password: 'hunter2',
71+
ports: %w(2379 2380 2381),
72+
slowlog_max_len: '5309',
73+
tags: %w{foo bar},
74+
keys: %w{baz bat},
75+
warn_on_missing_keys: false,
76+
command_stats: true,
77+
}}
78+
it { should contain_file(conf_file).with_content(%r{host: redis1}) }
79+
it { should contain_file(conf_file).with_content(%r{^[^#]*password: hunter2}) }
80+
it { should contain_file(conf_file).with_content(%r{^[^#]*slowlog-max-len: 5309}) }
81+
it { should contain_file(conf_file).with_content(%r{tags:.*\s+- foo\s+- bar}) }
82+
it { should contain_file(conf_file).with_content(%r{keys:.*\s+- baz\s+- bat}) }
83+
it { should contain_file(conf_file).with_content(%r{warn_on_missing_keys: false}) }
84+
it { should contain_file(conf_file).with_content(%r{command_stats: true}) }
85+
it { should contain_file(conf_file).with_content(%r{port: 2379}) }
86+
it { should contain_file(conf_file).with_content(%r{port: 2380}) }
87+
it { should contain_file(conf_file).with_content(%r{port: 2381}) }
88+
end
89+
90+
context 'with instances set' do
91+
let(:params) {{
92+
instances: [
93+
{
94+
'host' => 'redis1',
95+
'password' => 'hunter2',
96+
'port' => 2379,
97+
'tags' => %w(foo bar),
98+
'keys' => %w(baz bat),
99+
},
100+
{
101+
'host' => 'redis1',
102+
'password' => 'hunter2',
103+
'port' => 2380,
104+
'tags' => %w(foo bar),
105+
'keys' => %w(baz bat),
106+
},
107+
],
108+
}}
109+
it { should contain_file(conf_file).with_content(%r{host: redis1}) }
110+
it { should contain_file(conf_file).with_content(%r{^[^#]*password: hunter2}) }
111+
it { should contain_file(conf_file).with_content(%r{port: 2379}) }
112+
it { should contain_file(conf_file).with_content(%r{port: 2380}) }
113+
it { should contain_file(conf_file).with_content(%r{tags:.*\s+- foo\s+- bar}) }
114+
it { should contain_file(conf_file).with_content(%r{keys:.*\s+- baz\s+- bat}) }
115+
it { should contain_file(conf_file).without_content(%r{^[^#]*slowlog-max-len: 5309}) }
116+
it { should contain_file(conf_file).without_content(%r{warn_on_missing_keys: false}) }
117+
it { should contain_file(conf_file).without_content(%r{command_stats: true}) }
118+
end
66119
end
67120
end
68121
end

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

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,39 @@
33
init_config:
44

55
instances:
6-
<% @_ports.each do |port| -%>
7-
- host: <%= @host %>
8-
port: <%= port %>
9-
warn_on_missing_keys: <%= @warn_on_missing_keys %>
10-
command_stats: <%= @command_stats %>
11-
<% if @password.empty? %># <%end %>password: <%= @password %>
6+
<%- (Array(@_instances)).each do |instance| -%>
7+
- host: <%= instance['host'] %>
8+
port: <%= instance['port'] %>
9+
<% if instance['password'] and ! instance['password'].empty? -%>
10+
password: <%= instance['password'] %>
11+
<% end -%>
12+
<% if instance['slowlog_max_len'] and ! instance['slowlog_max_len'].empty? -%>
1213
# unix_socket_path: /var/run/redis/redis.sock # optional, can be used in lieu of host/port
13-
<% if @slowlog_max_len.empty? %># <%end %>slowlog-max-len: <%= @slowlog_max_len %>
14-
<% unless @tags.empty? -%>
15-
tags: # Optional
16-
<% @tags.each do |tag| -%>
17-
- <%= tag %>
14+
slowlog-max-len: <%= instance['slowlog_max_len'] %>
15+
<% end -%>
16+
<% if !instance['warn_on_missing_keys'].nil? -%>
17+
warn_on_missing_keys: <%= instance['warn_on_missing_keys'] %>
18+
<% end -%>
19+
<% if !instance['command_stats'].nil? -%>
20+
command_stats: <%= instance['command_stats'] %>
1821
<% end -%>
22+
<% if instance['keys'] and unless instance['keys'].empty? -%>
23+
keys:
24+
<%- Array(instance['keys'] ).each do |key| -%>
25+
<%- if key != '' -%>
26+
- <%= key %>
27+
<%- end -%>
28+
<% end -%>
1929
<% end -%>
20-
<% unless @keys.empty? -%>
21-
keys: # check the length of these keys
22-
<% @keys.each do |key| -%>
23-
- <%= key %>
30+
<% if instance['tags'] and unless instance['tags'].empty? -%>
31+
tags:
32+
<%- Array(instance['tags'] ).each do |tag| -%>
33+
<%- if tag != '' -%>
34+
- <%= tag %>
35+
<%- end -%>
36+
<% end -%>
2437
<% end -%>
38+
<% end -%>
39+
2540
<% end -%>
2641
<% end -%>

0 commit comments

Comments
 (0)