class Capistrano::Datadog::Reporter

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

Attributes

current_task[RW]

Public Class Methods

new() click to toggle source
   # File lib/capistrano/datadog.rb
46 def initialize()
47   @tasks = []
48   @current_task = nil
49   @logging_output = {}
50 end

Public Instance Methods

record_log(message) click to toggle source
   # File lib/capistrano/datadog.rb
62 def record_log(message)
63   if not @logging_output[@current_task]
64     @logging_output[@current_task] = []
65   end
66   @logging_output[@current_task] << message
67 end
record_task(task_name, timing, roles, stage=nil, application_name=nil) click to toggle source
   # File lib/capistrano/datadog.rb
52 def record_task(task_name, timing, roles, stage=nil, application_name=nil)
53   @tasks << {
54     :name   => task_name,
55     :timing => timing,
56     :roles  => roles,
57     :stage  => stage,
58     :application => application_name
59   }
60 end
report() click to toggle source
    # File lib/capistrano/datadog.rb
 69 def report()
 70   hostname = %x[hostname -f].strip
 71   user = Etc.getlogin
 72 
 73   # Lazy randomness
 74   aggregation_key = Digest::MD5.hexdigest "#{Time.new}|#{rand}"
 75 
 76   # Convert the tasks into Datadog events
 77   @tasks.map do |task|
 78     name  = task[:name]
 79     roles = Array(task[:roles]).map(&:to_s).sort
 80     tags  = ["#capistrano"] + (roles.map { |t| '#role:' + t })
 81     if !task[:stage].nil? and !task[:stage].empty? then
 82       tags << "#stage:#{task[:stage]}"
 83     end
 84     application = ''
 85     if !task[:application].nil? and !task[:application].empty? then
 86       application = ' for ' + task[:application]
 87     end
 88     title = "%s@%s ran %s%s on %s with capistrano in %.2f secs" % [user, hostname, name, application, roles.join(', '), task[:timing]]
 89     type  = "deploy"
 90     alert_type = "success"
 91     source_type = "capistrano"
 92     message_content = (@logging_output[name] || []).join('')
 93     message = if !message_content.empty? then
 94       # Strip out color control characters
 95       message_content = sanitize_encoding(message_content).gsub(/\e\[(\d+)m/, '')
 96       "@@@\n#{message_content}@@@" else "" end
 97 
 98     Dogapi::Event.new(message,
 99       :msg_title        => title,
100       :event_type       => type,
101       :event_object     => aggregation_key,
102       :alert_type       => alert_type,
103       :source_type_name => source_type,
104       :tags             => tags
105     )
106   end
107 end
sanitize_encoding(string) click to toggle source
    # File lib/capistrano/datadog.rb
109 def sanitize_encoding(string)
110   return string unless defined?(::Encoding) && string.encoding == Encoding::BINARY
111   string.encode(Encoding::UTF_8, Encoding::BINARY, invalid: :replace, undef: :replace, replace: '')
112 end