class DatadogChefTags
helper class for sending datadog tags from chef runs
Public Class Methods
# File lib/chef/handler/datadog_chef_tags.rb, line 9 def initialize @node = nil @run_status = nil @application_key = nil @tag_prefix = 'tag:' @scope_prefix = nil @retries = 0 @combined_host_tags = nil @regex_black_list = nil @policy_tags_enabled = false end
Public Instance Methods
send updated chef run generated tags to Datadog
@param dog [Dogapi::Client] Dogapi Client to be used
# File lib/chef/handler/datadog_chef_tags.rb, line 92 def send_update_to_datadog(dog) tags = combined_host_tags retries = @retries rc = [] begin loop do should_retry = false 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}") else if retries > 0 && rc[0].to_i == 404 Chef::Log.debug("Host #{@hostname} not yet present on Datadog, re-submitting tags in 2 seconds") sleep 2 retries -= 1 should_retry = true elsif rc[0].to_i / 100 != 2 Chef::Log.warn("Could not submit #{tags} tags for #{@hostname} to Datadog: #{rc}") else Chef::Log.debug("Successfully updated #{@hostname}'s tags to #{tags.join(', ')}") end end break unless should_retry end rescue StandardError => e Chef::Log.warn("Could not determine whether #{@hostname}'s tags were successfully submitted to Datadog: #{rc.inspect}. Error:\n#{e}") end end
set the target hostname (chef node name)
@param hostname [String] hostname to use for the handler report @return [DatadogChefTags] instance reference to self enabling method chaining
# File lib/chef/handler/datadog_chef_tags.rb, line 38 def with_hostname(hostname) @hostname = hostname self end
set the number of retries when sending tags, when the host is not yet present on Datadog
@param retries [Integer] number of retries @return [DatadogChefTags] instance reference to self enabling method chaining
# File lib/chef/handler/datadog_chef_tags.rb, line 57 def with_retries(retries) @retries = retries unless retries.nil? self end
set the chef run status used for the report
@param run_status [Chef::RunStatus] current chef run status @return [DatadogChefTags] instance reference to self enabling method chaining
# File lib/chef/handler/datadog_chef_tags.rb, line 25 def with_run_status(run_status) @run_status = run_status # Build up an array of Chef tags that will be sent back # 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 self end
set the prefix to be added to Datadog tags (Role, Env)
@param scope_prefix [String] prefix to be added to Datadog tags @return [DatadogChefTags] instance reference to self enabling method chaining
# File lib/chef/handler/datadog_chef_tags.rb, line 75 def with_scope_prefix(scope_prefix) @scope_prefix = scope_prefix unless scope_prefix.nil? self end
set the blacklist regexp, node tags matching this regex won't be sent
@param tags_blacklist_regex [String] regexp-formatted string @return [DatadogChefTags] instance reference to self enabling method chaining
# File lib/chef/handler/datadog_chef_tags.rb, line 66 def with_tag_blacklist(tags_blacklist_regex) @regex_black_list = Regexp.new(tags_blacklist_regex, Regexp::IGNORECASE) unless tags_blacklist_regex.nil? || tags_blacklist_regex.empty? 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
# File lib/chef/handler/datadog_chef_tags.rb, line 47 def with_tag_prefix(tag_prefix) @tag_prefix = tag_prefix unless tag_prefix.nil? self end
Private Instance Methods
# File lib/chef/handler/datadog_chef_tags.rb, line 136 def node_env "#{@scope_prefix}env:#{@node.chef_environment}" if @node.respond_to?('chef_environment') end
# File lib/chef/handler/datadog_chef_tags.rb, line 132 def node_roles @node.run_list.roles.map! { |role| "#{@scope_prefix}role:#{role}" } end