class Capistrano::Datadog::Reporter

Collects info about the tasks that ran in order to submit to Datadog

Attributes

event_filter[RW]

Public Class Methods

new() click to toggle source
   # File lib/capistrano/datadog.rb
58 def initialize()
59   @tasks = []
60   @task_stack = []
61 end

Public Instance Methods

record_hostname(hostname) click to toggle source
   # File lib/capistrano/datadog.rb
90 def record_hostname(hostname)
91   @task_stack.each do |task|
92     task[:hosts] << hostname
93   end
94 end
record_log(message) click to toggle source
   # File lib/capistrano/datadog.rb
83 def record_log(message)
84   current_task = @task_stack.last
85   if current_task
86     current_task[:logs] << message
87   end
88 end
record_task(task_name, roles, stage=nil, application_name=nil) { || ... } click to toggle source
   # File lib/capistrano/datadog.rb
63 def record_task(task_name, roles, stage=nil, application_name=nil)
64   task = {
65     :name   => task_name,
66     :roles  => roles,
67     :stage  => stage,
68     :application => application_name,
69     :logs => [],
70     :hosts => []
71   }
72   @tasks << task
73   @task_stack << task
74   result = nil
75   timing = Benchmark.measure(task_name) do
76     result = yield
77   end
78   task[:timing] = timing.real
79   @task_stack.pop
80   result
81 end
report(use_getlogin=true) click to toggle source
    # File lib/capistrano/datadog.rb
 96 def report(use_getlogin=true)
 97   hostname = Dogapi.find_localhost
 98   user = use_getlogin ? Etc.getlogin : Etc.getpwuid.name
 99 
100   # Lazy randomness
101   aggregation_key = Digest::MD5.hexdigest "#{Time.new}|#{rand}"
102 
103   filter = event_filter || proc { |x| x }
104 
105   # Convert the tasks into Datadog events
106   @tasks.map do |task|
107     name  = task[:name]
108     roles = Array(task[:roles]).map(&:to_s).sort
109     tags  = ['#capistrano'] + (roles.map { |t| '#role:' + t })
110     if !task[:stage].nil? and !task[:stage].empty? then
111       tags << "#stage:#{task[:stage]}"
112     end
113     application = ''
114     if !task[:application].nil? and !task[:application].empty? then
115       application = ' for ' + task[:application]
116     end
117     timing = Float(task[:timing]).round(2) rescue 'n/a'
118     title = "#{user}@#{hostname} ran #{name}#{application} on #{roles.join(', ')} "\
119             "with capistrano in #{timing} secs"
120     type  = 'deploy'
121     alert_type = 'success'
122     source_type = 'capistrano'
123     message_content = task[:logs].join('')
124     message = if !message_content.empty? then
125       # Strip out color control characters
126       message_content = sanitize_encoding(message_content).gsub(/\e\[(\d+)m/, '')
127       "@@@\n#{message_content}@@@" else '' end
128 
129     [Dogapi::Event.new(message,
130       :msg_title        => title,
131       :event_type       => type,
132       :event_object     => aggregation_key,
133       :alert_type       => alert_type,
134       :source_type_name => source_type,
135       :tags             => tags
136     ), task[:hosts]]
137   end.map(&event_filter).reject(&:nil?)
138 end
sanitize_encoding(string) click to toggle source
    # File lib/capistrano/datadog.rb
140 def sanitize_encoding(string)
141   return string unless defined?(::Encoding) && string.encoding == Encoding::BINARY
142   string.encode(Encoding::UTF_8, Encoding::BINARY, invalid: :replace, undef: :replace, replace: '')
143 end