Skip to content

Commit

Permalink
Merge pull request #81 from mstepniowski/marek/optional-tag-prefix
Browse files Browse the repository at this point in the history
Allow users to specify custom tag prefix with config[:tag_prefix]
  • Loading branch information
miketheman committed Mar 22, 2016
2 parents 896834e + 5934a14 commit 956be6e
Show file tree
Hide file tree
Showing 6 changed files with 572 additions and 30 deletions.
3 changes: 2 additions & 1 deletion lib/chef/handler/datadog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ def prepare_report_for_datadog
.with_hostname(hostname)
.with_run_status(run_status)
.with_application_key(config[:application_key])
.with_retries(@config[:tags_submission_retries])
.with_tag_prefix(config[:tag_prefix])
.with_retries(config[:tags_submission_retries])

# Build the chef event information
@event =
Expand Down
40 changes: 24 additions & 16 deletions lib/chef/handler/datadog_chef_tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def initialize
@node = nil
@run_status = nil
@application_key = nil
@tag_prefix = 'tag:'
@retries = 0
@combined_host_tags = nil
end
Expand All @@ -23,11 +24,6 @@ def with_dogapi_client(dogapi_client)
self
end

# attribute accessor for combined array of tags
#
# @return [Array] the set of host tags based off the chef run
attr_reader :combined_host_tags

# set the chef run status used for the report
#
# @param run_status [Chef::RunStatus] current chef run status
Expand All @@ -38,13 +34,6 @@ def with_run_status(run_status)
# Selects all [env, roles, tags] from the Node's object and reformats
# them to `key:value` e.g. `role:database-master`.
@node = run_status.node
# generate the combined tags
chef_env = node_env.split # converts a string into an array
chef_roles = node_roles
chef_tags = node_tags

# Combine (union) all arrays. Removes duplicates if found.
@combined_host_tags = chef_env | chef_roles | chef_tags
self
end

Expand Down Expand Up @@ -77,6 +66,15 @@ def with_application_key(application_key)
self
end

# set the prefix to be added to all Chef tags
#
# @param tag_prefix [String] prefix to be added to all Chef tags
# @return [DatadogChefTags] instance reference to self enabling method chaining
def with_tag_prefix(tag_prefix)
@tag_prefix = tag_prefix unless tag_prefix.nil?
self
end

# set the number of retries when sending tags, when the host is not yet present
# on Datadog
#
Expand All @@ -89,11 +87,12 @@ def with_retries(retries)

# send updated chef run generated tags to Datadog
def send_update_to_datadog
tags = combined_host_tags
retries = @retries
begin
loop do
should_retry = false
rc = @dog.update_tags(@hostname, combined_host_tags, 'chef')
rc = @dog.update_tags(@hostname, tags, 'chef')
# See FIXME in DatadogChefEvents::emit_to_datadog about why I feel dirty repeating this code here
if rc.length < 2
Chef::Log.warn("Unexpected response from Datadog Tags API: #{rc}")
Expand All @@ -104,9 +103,9 @@ def send_update_to_datadog
retries -= 1
should_retry = true
elsif rc[0].to_i / 100 != 2
Chef::Log.warn("Could not submit #{combined_host_tags} tags for #{@hostname} to Datadog: #{rc}")
Chef::Log.warn("Could not submit #{tags} tags for #{@hostname} to Datadog: #{rc}")
else
Chef::Log.debug("Successfully updated #{@hostname}'s tags to #{combined_host_tags.join(', ')}")
Chef::Log.debug("Successfully updated #{@hostname}'s tags to #{tags.join(', ')}")
end
end
break unless should_retry
Expand All @@ -116,6 +115,14 @@ def send_update_to_datadog
end
end

# return a combined array of tags that should be sent to Datadog
#
# @return [Array] the set of host tags based off the chef run
def combined_host_tags
# Combine (union) all arrays. Removes duplicates if found.
node_env.split | node_roles | node_tags
end

private

def node_roles
Expand All @@ -127,6 +134,7 @@ def node_env
end

def node_tags
@node.tags ? @node.tags.map! { |tag| 'tag:' + tag } : []
return [] unless @node.tags
@node.tags.map { |tag| "#{@tag_prefix}#{tag}" }
end
end # end class DatadogChefTags
37 changes: 34 additions & 3 deletions spec/datadog_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
Chef::Handler::Datadog.new(
'api_key' => API_KEY,
'application_key' => APPLICATION_KEY,
'tag_prefix' => 'tag',
)
end
end
Expand Down Expand Up @@ -203,18 +204,48 @@

describe 'when specified' do
it 'sets the role and env and tags' do
@node.normal.tags = ['the_one_and_only']
@node.normal.tags = ['the_one_and_only', 'datacenter:my-cloud']
@handler.run_report_unsafe(@run_status)

expect(a_request(:put, HOST_TAG_ENDPOINT + @node.name).with(
:query => { 'api_key' => @handler.config[:api_key],
'application_key' => @handler.config[:application_key],
'source' => 'chef' },
:body => hash_including(:tags => [
'env:hostile', 'role:highlander', 'tag:the_one_and_only'
'env:hostile', 'role:highlander', 'tag:the_one_and_only', 'tag:datacenter:my-cloud'
]),
)).to have_been_made.times(1)
end

it 'allows for user-specified tag prefix' do
@node.normal.tags = ['the_one_and_only', 'datacenter:my-cloud']
@handler.config[:tag_prefix] = 'custom-prefix-'
@handler.run_report_unsafe(@run_status)

expect(a_request(:put, HOST_TAG_ENDPOINT + @node.name).with(
:query => { 'api_key' => @handler.config[:api_key],
'application_key' => @handler.config[:application_key],
'source' => 'chef' },
:body => hash_including(:tags => [
'env:hostile', 'role:highlander', 'custom-prefix-the_one_and_only', 'custom-prefix-datacenter:my-cloud'
]),
)).to have_been_made.times(1)
end

it 'allows for empty tag prefix' do
@node.normal.tags = ['the_one_and_only', 'datacenter:my-cloud']
@handler.config[:tag_prefix] = ''
@handler.run_report_unsafe(@run_status)

expect(a_request(:put, HOST_TAG_ENDPOINT + @node.name).with(
:query => { 'api_key' => @handler.config[:api_key],
'application_key' => @handler.config[:application_key],
'source' => 'chef' },
:body => hash_including(:tags => [
'env:hostile', 'role:highlander', 'the_one_and_only', 'datacenter:my-cloud'
]),
)).to have_been_made.times(1)
end
end

describe 'when unspecified' do
Expand Down Expand Up @@ -457,7 +488,7 @@
)).to have_been_made.times(1)
end
end
end
end

# TODO: test failures:
# @run_status.exception = Exception.new('Boy howdy!')
Expand Down
Loading

0 comments on commit 956be6e

Please sign in to comment.