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