From d54c9a27023374492d35ee94cb3127b4b4cfed7a Mon Sep 17 00:00:00 2001 From: Quentin Madec Date: Mon, 22 Aug 2016 14:29:50 -0400 Subject: [PATCH] Multiple endpoints: easier configuration For backward compatibility purpose, we keep url, api_key & application_key, and add a extra_endpoints attributes to handle the other endpoints. --- attributes/default.rb | 12 +++++--- recipes/dd-agent.rb | 16 ++++++++-- recipes/dd-handler.rb | 12 ++++++-- spec/dd-agent_spec.rb | 49 +++++++++++++++++++++++++----- templates/default/datadog.conf.erb | 12 ++------ 5 files changed, 74 insertions(+), 27 deletions(-) diff --git a/attributes/default.rb b/attributes/default.rb index f84f541c..0e659a92 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -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. @@ -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/ diff --git a/recipes/dd-agent.rb b/recipes/dd-agent.rb index 45eb66cd..8d75dc81 100644 --- a/recipes/dd-agent.rb +++ b/recipes/dd-agent.rb @@ -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' @@ -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 diff --git a/recipes/dd-handler.rb b/recipes/dd-handler.rb index 8674300b..92b7c718 100644 --- a/recipes/dd-handler.rb +++ b/recipes/dd-handler.rb @@ -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'] diff --git a/spec/dd-agent_spec.rb b/spec/dd-agent_spec.rb index 46153119..ac64679f 100644 --- a/spec/dd-agent_spec.rb +++ b/spec/dd-agent_spec.rb @@ -297,16 +297,22 @@ 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 @@ -314,10 +320,39 @@ def set_env_var(name, value) 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 diff --git a/templates/default/datadog.conf.erb b/templates/default/datadog.conf.erb index 83d70c3d..85435929 100644 --- a/templates/default/datadog.conf.erb +++ b/templates/default/datadog.conf.erb @@ -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" %> @@ -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 #