Class: DatadogChefTags

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/handler/datadog_chef_tags.rb

Overview

helper class for sending datadog tags from chef runs

Instance Method Summary collapse

Constructor Details

#initializeDatadogChefTags

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_tagsArray

return a combined array of tags that should be sent to Datadog

Returns:

  • (Array)

    the set of host tags based off the chef run



125
126
127
128
# File 'lib/chef/handler/datadog_chef_tags.rb', line 125

def combined_host_tags
  # Combine (union) all arrays. Removes duplicates if found.
  node_env.split | node_roles | node_policy_tags | node_tags
end

#send_update_to_datadog(dog) ⇒ Object

send updated chef run generated tags to Datadog

Parameters:

  • dog (Dogapi::Client)

    Dogapi Client to be used



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)
  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

#with_hostname(hostname) ⇒ DatadogChefTags

set the target hostname (chef node name)

Parameters:

  • hostname (String)

    hostname to use for the handler report

Returns:

  • (DatadogChefTags)

    instance reference to self enabling method chaining



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

Parameters:

  • enabled (TrueClass, FalseClass)

    enable or disable policy tags

Returns:

  • (DatadogChefTags)

    instance reference to self enabling method chaining



84
85
86
87
# File 'lib/chef/handler/datadog_chef_tags.rb', line 84

def with_policy_tags_enabled(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

Parameters:

  • retries (Integer)

    number of retries

Returns:

  • (DatadogChefTags)

    instance reference to self enabling method chaining



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

Parameters:

  • run_status (Chef::RunStatus)

    current chef run status

Returns:

  • (DatadogChefTags)

    instance reference to self enabling method chaining



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)

Parameters:

  • scope_prefix (String)

    prefix to be added to Datadog tags

Returns:

  • (DatadogChefTags)

    instance reference to self enabling method chaining



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

Parameters:

  • tags_blacklist_regex (String)

    regexp-formatted string

Returns:

  • (DatadogChefTags)

    instance reference to self enabling method chaining



66
67
68
69
# 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

#with_tag_prefix(tag_prefix) ⇒ DatadogChefTags

set the prefix to be added to all Chef tags

Parameters:

  • tag_prefix (String)

    prefix to be added to all Chef tags

Returns:

  • (DatadogChefTags)

    instance reference to self enabling method chaining



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