Class: DatadogChefTags
- Inherits:
-
Object
- Object
- DatadogChefTags
- Defined in:
- lib/chef/handler/datadog_chef_tags.rb
Overview
helper class for sending datadog tags from chef runs
Instance Method Summary collapse
-
#combined_host_tags ⇒ Array
return a combined array of tags that should be sent to Datadog.
-
#initialize ⇒ DatadogChefTags
constructor
A new instance of DatadogChefTags.
-
#send_update_to_datadog(dog) ⇒ Object
send updated chef run generated tags to Datadog.
-
#with_hostname(hostname) ⇒ DatadogChefTags
set the target hostname (chef node name).
-
#with_policy_tags_enabled(enabled) ⇒ DatadogChefTags
enable policy tags.
-
#with_retries(retries) ⇒ DatadogChefTags
set the number of retries when sending tags, when the host is not yet present on Datadog.
-
#with_run_status(run_status) ⇒ DatadogChefTags
set the chef run status used for the report.
-
#with_scope_prefix(scope_prefix) ⇒ DatadogChefTags
set the prefix to be added to Datadog tags (Role, Env).
-
#with_tag_blacklist(tags_blacklist_regex) ⇒ DatadogChefTags
set the blacklist regexp, node tags matching this regex won’t be sent.
-
#with_tag_prefix(tag_prefix) ⇒ DatadogChefTags
set the prefix to be added to all Chef tags.
Constructor Details
#initialize ⇒ DatadogChefTags
Returns a new instance of DatadogChefTags.
9 10 11 12 13 14 15 16 17 18 19 |
# 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 |
Instance Method Details
#combined_host_tags ⇒ Array
return a combined array of tags that should be sent to Datadog
125 126 127 128 |
# File 'lib/chef/handler/datadog_chef_tags.rb', line 125 def # Combine (union) all arrays. Removes duplicates if found. node_env.split | node_roles | | end |
#send_update_to_datadog(dog) ⇒ Object
send updated chef run generated tags to Datadog
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/chef/handler/datadog_chef_tags.rb', line 92 def send_update_to_datadog(dog) = retries = @retries rc = [] begin loop do should_retry = false rc = dog.(@hostname, , '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 for #{@hostname} to Datadog: #{rc}") else Chef::Log.debug("Successfully updated #{@hostname}'s tags to #{.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 |
#with_hostname(hostname) ⇒ DatadogChefTags
set the target hostname (chef node name)
38 39 40 41 |
# File 'lib/chef/handler/datadog_chef_tags.rb', line 38 def with_hostname(hostname) @hostname = hostname self end |
#with_policy_tags_enabled(enabled) ⇒ DatadogChefTags
enable policy tags
84 85 86 87 |
# File 'lib/chef/handler/datadog_chef_tags.rb', line 84 def (enabled) @policy_tags_enabled = enabled unless enabled.nil? self end |
#with_retries(retries) ⇒ DatadogChefTags
set the number of retries when sending tags, when the host is not yet present on Datadog
57 58 59 60 |
# File 'lib/chef/handler/datadog_chef_tags.rb', line 57 def with_retries(retries) @retries = retries unless retries.nil? self end |
#with_run_status(run_status) ⇒ DatadogChefTags
set the chef run status used for the report
25 26 27 28 29 30 31 32 |
# 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 |
#with_scope_prefix(scope_prefix) ⇒ DatadogChefTags
set the prefix to be added to Datadog tags (Role, Env)
75 76 77 78 |
# 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 |
#with_tag_blacklist(tags_blacklist_regex) ⇒ DatadogChefTags
set the blacklist regexp, node tags matching this regex won’t be sent
66 67 68 69 |
# File 'lib/chef/handler/datadog_chef_tags.rb', line 66 def with_tag_blacklist() @regex_black_list = Regexp.new(, Regexp::IGNORECASE) unless .nil? || .empty? self end |
#with_tag_prefix(tag_prefix) ⇒ DatadogChefTags
set the prefix to be added to all Chef tags
47 48 49 50 |
# 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 |