Skip to content

Commit

Permalink
Multiple endpoints: easier configuration
Browse files Browse the repository at this point in the history
For backward compatibility purpose, we keep url, api_key &
application_key, and add a extra_endpoints attributes to handle the
other endpoints.
  • Loading branch information
degemer committed Sep 9, 2016
1 parent 4819c38 commit d54c9a2
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 27 deletions.
12 changes: 7 additions & 5 deletions attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
# Create an application key on the Account Settings page
default['datadog']['application_key'] = nil

# Use this attribute to send data to multiple accounts
# The key can be anything you want, 'prod' is used there as an example
default['datadog']['extra_endpoints']['prod']['enabled'] = nil
default['datadog']['extra_endpoints']['prod']['api_key'] = nil
default['datadog']['extra_endpoints']['prod']['application_key'] = nil
default['datadog']['extra_endpoints']['prod']['url'] = nil # optional

# Add this prefix to all Chef tags sent to Datadog: "#{tag_prefix}#{tag}"
# This makes it easy to group hosts in Datadog by their Chef tags, but might be counterproductive
# if your Chef tags are already in the "#{tag_group}:#{value}" form.
Expand Down Expand Up @@ -201,11 +208,6 @@
# extra_packages to install
default['datadog']['extra_packages'] = {}

# Multiple endpoints/api_keys settings
default['datadog']['other_dd_urls'] = nil # list of endpoints
default['datadog']['other_api_keys'] = nil # list of api_keys
default['datadog']['other_application_keys'] = nil # list of application_keys

# For service-specific configuration, use the integration recipes included
# in this cookbook, and apply them to the appropirate node's run list.
# Read more at http://docs.datadoghq.com/
Expand Down
16 changes: 14 additions & 2 deletions recipes/dd-agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@
#
raise "Add a ['datadog']['api_key'] attribute to configure this node's Datadog Agent." if node['datadog'] && node['datadog']['api_key'].nil?

api_keys = [node['datadog']['api_key']]
dd_urls = [node['datadog']['url']]
node['datadog']['extra_endpoints'].each do |_, endpoint|
next unless endpoint['enabled']
api_keys << endpoint['api_key']
dd_urls << if endpoint['url']
endpoint['url']
else
node['datadog']['url']
end
end

template agent_config_file do
if is_windows
owner 'Administrators'
Expand All @@ -64,8 +76,8 @@
mode 0640
end
variables(
:api_key => node['datadog']['api_key'],
:dd_url => node['datadog']['url']
:api_keys => api_keys,
:dd_urls => dd_urls
)
sensitive true if Chef::Resource.instance_methods(false).include?(:sensitive)
end
Expand Down
12 changes: 9 additions & 3 deletions recipes/dd-handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,20 @@
ENV['DATADOG_PROXY'] = proxy_url.to_s
end

extra_endpoints = []
node['datadog']['extra_endpoints'].each do |_, endpoint|
next unless endpoint['enabled']
endpoint.delete('enabled')
extra_endpoints << endpoint
end

handler_config = {
:api_key => node['datadog']['api_key'],
:application_key => node['datadog']['application_key'],
:use_ec2_instance_id => node['datadog']['use_ec2_instance_id'],
:tag_prefix => node['datadog']['tag_prefix'],
:other_dd_urls => node['datadog']['other_dd_urls'],
:other_api_keys => node['datadog']['other_api_keys'],
:other_application_keys => node['datadog']['other_application_keys']
:url => node['datadog']['url'],
:extra_endpoints => extra_endpoints
}

unless node['datadog']['use_ec2_instance_id']
Expand Down
49 changes: 42 additions & 7 deletions spec/dd-agent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -297,27 +297,62 @@ def set_env_var(name, value)
end
end

context 'does accept other_api_keys' do
context 'does accept extra endpoints' do
cached(:chef_run) do
ChefSpec::SoloRunner.new(
platform: 'ubuntu',
version: '12.04'
) do |node|
node.set['datadog'] = {
'api_key' => 'somethingnotnil',
'other_dd_urls' => ['http://app.example.com', 'http://app.datadoghq.com'],
'other_api_keys' => ['something1', 'something2']
'api_key' => 'something1',
'url' => 'https://app.example.com',
'extra_endpoints' => {
'example' => {
'enabled' => true,
'api_key' => 'something2',
'url' => 'https://app.datadoghq.com'
}
}
}
node.set['languages'] = { 'python' => { 'version' => '2.6.2' } }
end.converge described_recipe
end

it_behaves_like 'common linux resources'

it 'sets tags from the tags attribute' do
it 'uses the multiples apikeys and urls' do
expect(chef_run).to render_file('/etc/dd-agent/datadog.conf')
.with_content(/^api_key: something1,something2$/)
.with_content(%r{^dd_url: https://app.example.com,https://app.datadoghq.com$})
end
end

context 'does accept extra api_key' do
cached(:chef_run) do
ChefSpec::SoloRunner.new(
platform: 'ubuntu',
version: '12.04'
) do |node|
node.set['datadog'] = {
'api_key' => 'something1',
'url' => 'https://app.example.com',
'extra_endpoints' => {
'example' => {
'enabled' => true,
'api_key' => 'something2'
}
}
}
node.set['languages'] = { 'python' => { 'version' => '2.6.2' } }
end.converge described_recipe
end

it_behaves_like 'common linux resources'

it 'uses the multiples apikeys and urls' do
expect(chef_run).to render_file('/etc/dd-agent/datadog.conf')
.with_content(/^other_api_keys: something1,something2$/)
.with_content(%r{^other_dd_urls: http://app.example.com,http://app.datadoghq.com$})
.with_content(/^api_key: something1,something2$/)
.with_content(%r{^dd_url: https://app.example.com,https://app.example.com$})
end
end
end
Expand Down
12 changes: 2 additions & 10 deletions templates/default/datadog.conf.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Generated by Chef, local modifications will be overwritten

[Main]
dd_url: <%= @dd_url %>
api_key: <%= @api_key %>
dd_url: <%= @dd_urls.join(',') %>
api_key: <%= @api_keys.join(',') %>
check_freq: <%= node['datadog']['check_freq'] %>
hostname: <%= node['datadog']['hostname'] %>
use_mount: <%= node['datadog']['use_mount'] ? "yes" : "no" %>
Expand Down Expand Up @@ -61,14 +61,6 @@ graphite_listen_port: <%= node['datadog']['graphite_port'] %>
histogram_aggregates: <%= node['datadog']['histogram_aggregates'] %>
histogram_percentiles: <%= node['datadog']['histogram_percentiles'] %>

<% if node['datadog']['other_api_keys'] %>
## Multiple endpoints/api_keys (since 5.9)
<% if node['datadog']['other_dd_urls'] %>
other_dd_urls: <%= node['datadog']['other_dd_urls'].join(',') %>
<% end -%>
other_api_keys: <%= node['datadog']['other_api_keys'].join(',') %>
<% end -%>

<% if node['datadog']['dogstatsd'] -%>
# ========================================================================== #
# DogStatsd configuration #
Expand Down

0 comments on commit d54c9a2

Please sign in to comment.