Skip to content

Commit 8505097

Browse files
authored
Add configuration for gearmand integration (#860)
This PR enables the opportunity to handle the gearmand integration via Chef. It's possible to define every parameter described within the integration documentation example file. Because all parameters for the gearmand integration are optional, the possibility of optional node attributes has been enabled within the configuration deployment, too. Means, if a parameter is not defined, it will be written with default parameters. Well known that the example config file doesn't need that, because these parameters are set by default behind the scenes, the behavior has been set anyway in case of visibility. So the user has the chance to see what he does. If the user would only add the following to his recipe node.default['datadog']['gearmand']['instances'] = [{}], the basic standard config would be written as well. If only a view parameters will not be set but others, too - the default settings will be written in case of visibility as well.
1 parent 03f7b32 commit 8505097

File tree

4 files changed

+234
-0
lines changed

4 files changed

+234
-0
lines changed

recipes/gearmand.rb

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
include_recipe '::dd-agent'
2+
3+
# All attributes are optional.
4+
# node.default['datadog']['gearmand']['instances'] = [
5+
# {
6+
# # Defaults to 127.0.0.1 if not set
7+
# 'server' => '127.0.0.1',
8+
# # Defaults to 4730 if not set
9+
# 'port' => '4730',
10+
# 'tasks' => [
11+
# 'TASK_1',
12+
# 'TASK_2',
13+
# ],
14+
# 'tags' => [
15+
# '<KEY_1>:<VALUE_1>',
16+
# '<KEY_2>:<VALUE_2>'
17+
# ],
18+
# 'service' => '<SERVICE>',
19+
# # Defaults to 15 if not set
20+
# 'min_collection_interval' => 60,
21+
# # Defaults to false if not set
22+
# 'empty_default_hostname' => true,
23+
# 'metric_patterns' => {
24+
# 'include' => [
25+
# '<INCLUDE_REGEX>'
26+
# ],
27+
# 'exclude' => [
28+
# '<EXCLUDE_REGEX>'
29+
# ]
30+
# }
31+
# }
32+
# ]
33+
34+
datadog_monitor 'gearmand' do
35+
instances node['datadog']['gearmand']['instances']
36+
logs node['datadog']['gearmand']['logs']
37+
action :add
38+
notifies :restart, 'service[datadog-agent]' if node['datadog']['agent_start']
39+
end

spec/integrations/gearmand_spec.rb

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
describe 'datadog::gearmand' do
2+
expected_yaml = <<-EOF
3+
logs:
4+
5+
instances:
6+
- server: 127.0.0.1
7+
port: 4730
8+
tasks:
9+
- TASK_1
10+
- TASK_2
11+
tags:
12+
- <KEY_1>:<VALUE_1>
13+
- <KEY_2>:<VALUE_2>
14+
service: <SERVICE>
15+
min_collection_interval: 60
16+
empty_default_hostname: true
17+
metric_patterns:
18+
include:
19+
- <INCLUDE_REGEX>
20+
exclude:
21+
- <EXCLUDE_REGEX>
22+
23+
init_config:
24+
# No init_config details needed
25+
EOF
26+
27+
cached(:chef_run) do
28+
ChefSpec::SoloRunner.new(
29+
platform: 'ubuntu',
30+
version: '16.04',
31+
step_into: ['datadog_monitor']
32+
) do |node|
33+
node.automatic['languages'] = { 'python' => { 'version' => '2.7.2' } }
34+
node.normal['datadog'] = {
35+
api_key: 'someapikey',
36+
gearmand: {
37+
instances: [
38+
{
39+
server: '127.0.0.1',
40+
port: '4730',
41+
tasks: [
42+
'TASK_1',
43+
'TASK_2',
44+
],
45+
tags: [
46+
'<KEY_1>:<VALUE_1>',
47+
'<KEY_2>:<VALUE_2>'
48+
],
49+
service: '<SERVICE>',
50+
# Defaults to 15 if not set
51+
min_collection_interval: 60,
52+
# Defaults to false if not set
53+
empty_default_hostname: true,
54+
metric_patterns: {
55+
include: [
56+
'<INCLUDE_REGEX>'
57+
],
58+
exclude: [
59+
'<EXCLUDE_REGEX>'
60+
]
61+
}
62+
}
63+
]
64+
}
65+
}
66+
end.converge(described_recipe)
67+
end
68+
69+
subject { chef_run }
70+
71+
it_behaves_like 'datadog-agent'
72+
73+
it { is_expected.to include_recipe('datadog::dd-agent') }
74+
75+
it { is_expected.to add_datadog_monitor('gearmand') }
76+
77+
it 'renders expected YAML config file' do
78+
expect(chef_run).to(render_file('/etc/datadog-agent/conf.d/gearmand.d/conf.yaml').with_content { |content|
79+
expect(YAML.safe_load(content).to_json).to be_json_eql(YAML.safe_load(expected_yaml).to_json)
80+
})
81+
end
82+
end

templates/default/gearmand.yaml.erb

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<%= JSON.parse(({'logs' => @logs }).to_json).to_yaml -%>
2+
3+
instances:
4+
<% @instances.each do |i| %>
5+
<% if i['server'] -%>
6+
<% ip = i['server'] %>
7+
<% else %>
8+
<% ip = '127.0.0.1' %>
9+
<% end -%>
10+
- server: <%= ip %>
11+
<% if i['port'] -%>
12+
<% port = i['port'] %>
13+
<% else %>
14+
<% port = '4730' %>
15+
<% end -%>
16+
port: <%= port %>
17+
<% if i['tasks'] %>
18+
tasks:
19+
<% i['tasks'].each do |task| -%>
20+
- <%= task %>
21+
<% end %>
22+
<% end %>
23+
<% if i['tags'] %>
24+
tags:
25+
<% i['tags'].each do |tag| -%>
26+
- <%= tag %>
27+
<% end %>
28+
<% end %>
29+
<% if i['service'] %>
30+
service: <%= i['service'] %>
31+
<% end %>
32+
<% if i['min_collection_interval'] %>
33+
<% seconds = i['min_collection_interval'] %>
34+
<% else %>
35+
<% seconds = 15 %>
36+
<% end %>
37+
min_collection_interval: <%= seconds %>
38+
<% if i['empty_default_hostname'] %>
39+
<% boolean = i['empty_default_hostname'] %>
40+
<% else %>
41+
<% boolean = false %>
42+
<% end %>
43+
empty_default_hostname: <%= boolean %>
44+
<% if i['metric_patterns'] %>
45+
metric_patterns:
46+
<% if i['metric_patterns'].include?('include') %>
47+
include:
48+
<% i['metric_patterns']['include'].each do |regex| %>
49+
- <%= regex %>
50+
<% end %>
51+
<% end %>
52+
<% if i['metric_patterns'].include?('exclude') %>
53+
exclude:
54+
<% i['metric_patterns']['exclude'].each do |regex| %>
55+
- <%= regex %>
56+
<% end %>
57+
<% end %>
58+
<% end %>
59+
<% end -%>
60+
61+
init_config:
62+
# No init_config details needed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Encoding: utf-8
2+
3+
require 'spec_helper'
4+
5+
AGENT_CONFIG = File.join(@agent_config_dir, 'conf.d/gearmand.d/conf.yaml')
6+
7+
describe service(@agent_service_name) do
8+
it { should be_running }
9+
end
10+
11+
describe file(AGENT_CONFIG) do
12+
it { should be_a_file }
13+
14+
it 'is valid yaml matching input values' do
15+
generated = YAML.load_file(AGENT_CONFIG)
16+
17+
expected = {
18+
'instances' => [
19+
{
20+
'server' => '127.0.0.1',
21+
'port' => '4730',
22+
'tasks' => [
23+
'TASK_1',
24+
'TASK_2',
25+
],
26+
'tags' => [
27+
'<KEY_1>:<VALUE_1>',
28+
'<KEY_2>:<VALUE_2>'
29+
],
30+
'service' => '<SERVICE>',
31+
# Defaults to 15 if not set
32+
'min_collection_interval' => 60,
33+
# Defaults to false if not set
34+
'empty_default_hostname' => true,
35+
'metric_patterns' => {
36+
'include' => [
37+
'<INCLUDE_REGEX>'
38+
],
39+
'exclude' => [
40+
'<EXCLUDE_REGEX>'
41+
]
42+
}
43+
}
44+
],
45+
'logs' => nil,
46+
'init_config' => nil
47+
}
48+
49+
expect(generated.to_json).to be_json_eql expected.to_json
50+
end
51+
end

0 commit comments

Comments
 (0)