Skip to content

Commit

Permalink
[redis] mutli-instance support (#520)
Browse files Browse the repository at this point in the history
* [redis] adding instances support + tests (could be improved)

* [redis][spec] adding multiple instances test

* [redis] fixing lint issues

* [redis] documenting multi-instance option
  • Loading branch information
truthbk authored May 14, 2019
1 parent 0feed8c commit e28c270
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 16 deletions.
35 changes: 35 additions & 0 deletions manifests/integrations/redis.pp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,26 @@
# Optional array of keys to check length
# $command_stats
# Collect INFO COMMANDSTATS output as metrics
# $instances
# Optional array of hashes should you wish to specify multiple instances.
# If this option is specified all other parameters will be overriden.
# This parameter may also be used to specify instances with hiera.
#
# Sample Usage:
#
# class { 'datadog_agent::integrations::redis' :
# host => 'localhost',
# }
#
# Hiera Usage:
#
# datadog_agent::integrations::redis::instances:
# - host: 'localhost'
# password: 'datadog'
# port: 6379
# slowlog_max_len: 1000
# warn_on_missing_keys: true
# command_stats: false
#
class datadog_agent::integrations::redis(
String $host = 'localhost',
Expand All @@ -37,6 +50,7 @@
Array $keys = [],
Boolean $warn_on_missing_keys = true,
Boolean $command_stats = false,
Optional[Array] $instances = undef,

) inherits datadog_agent::params {
include datadog_agent
Expand All @@ -55,6 +69,19 @@

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

$_port_instances = $_ports.map |$instance_port| {
{
'host' => $host,
'password' => $password,
'port' => $instance_port,
'slowlog_max_len' => $slowlog_max_len,
'tags' => $tags,
'keys' => $keys,
'warn_on_missing_keys' => $warn_on_missing_keys,
'command_stats' => $command_stats,
}
}

$legacy_dst = "${datadog_agent::conf_dir}/redisdb.yaml"
if !$::datadog_agent::agent5_enable {
$dst = "${datadog_agent::conf6_dir}/redisdb.d/conf.yaml"
Expand All @@ -65,6 +92,14 @@
$dst = $legacy_dst
}

if !$instances and $host {
$_instances = $_port_instances
} elsif !$instances{
$_instances = []
} else {
$_instances = $instances
}

file { $dst:
ensure => file,
owner => $datadog_agent::params::dd_user,
Expand Down
55 changes: 54 additions & 1 deletion spec/classes/datadog_agent_integrations_redis_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@

context 'with default parameters' do
it { should contain_file(conf_file).with_content(%r{host: localhost}) }
it { should contain_file(conf_file).without_content(%r{^[^#]*password: }) }
it { should contain_file(conf_file).with_content(%r{port: 6379}) }
it { should contain_file(conf_file).without_content(%r{^[^#]*password: }) }
it { should contain_file(conf_file).without_content(%r{^[^#]*slowlog-max-len: }) }
it { should contain_file(conf_file).without_content(%r{tags:}) }
it { should contain_file(conf_file).without_content(%r{\bkeys:}) }
Expand Down Expand Up @@ -63,6 +63,59 @@
it { should contain_file(conf_file).with_content(%r{warn_on_missing_keys: false}) }
it { should contain_file(conf_file).with_content(%r{command_stats: true}) }
end

context 'with ports parameters set' do
let(:params) {{
host: 'redis1',
password: 'hunter2',
ports: %w(2379 2380 2381),
slowlog_max_len: '5309',
tags: %w{foo bar},
keys: %w{baz bat},
warn_on_missing_keys: false,
command_stats: true,
}}
it { should contain_file(conf_file).with_content(%r{host: redis1}) }
it { should contain_file(conf_file).with_content(%r{^[^#]*password: hunter2}) }
it { should contain_file(conf_file).with_content(%r{^[^#]*slowlog-max-len: 5309}) }
it { should contain_file(conf_file).with_content(%r{tags:.*\s+- foo\s+- bar}) }
it { should contain_file(conf_file).with_content(%r{keys:.*\s+- baz\s+- bat}) }
it { should contain_file(conf_file).with_content(%r{warn_on_missing_keys: false}) }
it { should contain_file(conf_file).with_content(%r{command_stats: true}) }
it { should contain_file(conf_file).with_content(%r{port: 2379}) }
it { should contain_file(conf_file).with_content(%r{port: 2380}) }
it { should contain_file(conf_file).with_content(%r{port: 2381}) }
end

context 'with instances set' do
let(:params) {{
instances: [
{
'host' => 'redis1',
'password' => 'hunter2',
'port' => 2379,
'tags' => %w(foo bar),
'keys' => %w(baz bat),
},
{
'host' => 'redis1',
'password' => 'hunter2',
'port' => 2380,
'tags' => %w(foo bar),
'keys' => %w(baz bat),
},
],
}}
it { should contain_file(conf_file).with_content(%r{host: redis1}) }
it { should contain_file(conf_file).with_content(%r{^[^#]*password: hunter2}) }
it { should contain_file(conf_file).with_content(%r{port: 2379}) }
it { should contain_file(conf_file).with_content(%r{port: 2380}) }
it { should contain_file(conf_file).with_content(%r{tags:.*\s+- foo\s+- bar}) }
it { should contain_file(conf_file).with_content(%r{keys:.*\s+- baz\s+- bat}) }
it { should contain_file(conf_file).without_content(%r{^[^#]*slowlog-max-len: 5309}) }
it { should contain_file(conf_file).without_content(%r{warn_on_missing_keys: false}) }
it { should contain_file(conf_file).without_content(%r{command_stats: true}) }
end
end
end
end
45 changes: 30 additions & 15 deletions templates/agent-conf.d/redisdb.yaml.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,39 @@
init_config:

instances:
<% @_ports.each do |port| -%>
- host: <%= @host %>
port: <%= port %>
warn_on_missing_keys: <%= @warn_on_missing_keys %>
command_stats: <%= @command_stats %>
<% if @password.empty? %># <%end %>password: <%= @password %>
<%- (Array(@_instances)).each do |instance| -%>
- host: <%= instance['host'] %>
port: <%= instance['port'] %>
<% if instance['password'] and ! instance['password'].empty? -%>
password: <%= instance['password'] %>
<% end -%>
<% if instance['slowlog_max_len'] and ! instance['slowlog_max_len'].empty? -%>
# unix_socket_path: /var/run/redis/redis.sock # optional, can be used in lieu of host/port
<% if @slowlog_max_len.empty? %># <%end %>slowlog-max-len: <%= @slowlog_max_len %>
<% unless @tags.empty? -%>
tags: # Optional
<% @tags.each do |tag| -%>
- <%= tag %>
slowlog-max-len: <%= instance['slowlog_max_len'] %>
<% end -%>
<% if !instance['warn_on_missing_keys'].nil? -%>
warn_on_missing_keys: <%= instance['warn_on_missing_keys'] %>
<% end -%>
<% if !instance['command_stats'].nil? -%>
command_stats: <%= instance['command_stats'] %>
<% end -%>
<% if instance['keys'] and unless instance['keys'].empty? -%>
keys:
<%- Array(instance['keys'] ).each do |key| -%>
<%- if key != '' -%>
- <%= key %>
<%- end -%>
<% end -%>
<% end -%>
<% unless @keys.empty? -%>
keys: # check the length of these keys
<% @keys.each do |key| -%>
- <%= key %>
<% if instance['tags'] and unless instance['tags'].empty? -%>
tags:
<%- Array(instance['tags'] ).each do |tag| -%>
<%- if tag != '' -%>
- <%= tag %>
<%- end -%>
<% end -%>
<% end -%>
<% end -%>

<% end -%>
<% end -%>

0 comments on commit e28c270

Please sign in to comment.