Class: WatCatcher::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/wat_catcher/report.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exception, user: nil, request: nil, sidekiq: nil) ⇒ Report

Returns a new instance of Report.



5
6
7
8
9
10
11
12
13
# File 'lib/wat_catcher/report.rb', line 5

def initialize(exception, user: nil, request: nil, sidekiq: nil)
  self.exception = exception
  self.request = request
  self.sidekiq = sidekiq
  self.user = user
  send_report
  log_report
  instrument_report unless metrics_disabled?
end

Instance Attribute Details

#exceptionObject

Returns the value of attribute exception.



3
4
5
# File 'lib/wat_catcher/report.rb', line 3

def exception
  @exception
end

#requestObject

Returns the value of attribute request.



3
4
5
# File 'lib/wat_catcher/report.rb', line 3

def request
  @request
end

#sidekiqObject

Returns the value of attribute sidekiq.



3
4
5
# File 'lib/wat_catcher/report.rb', line 3

def sidekiq
  @sidekiq
end

#userObject

Returns the value of attribute user.



3
4
5
# File 'lib/wat_catcher/report.rb', line 3

def user
  @user
end

Instance Method Details

#base_descriptionObject



71
72
73
74
75
76
77
78
79
80
# File 'lib/wat_catcher/report.rb', line 71

def base_description
  {
    app_env: ::Rails.env.to_s,
    app_name: ::Rails.application.class.parent_name,
    language: "ruby",
    captured_at: Time.zone.now,
    hostname: Socket.gethostname,
    rails_root: Rails.root.to_s
  }
end

#exception_descriptionObject



90
91
92
93
94
95
96
97
# File 'lib/wat_catcher/report.rb', line 90

def exception_description
  return {} unless exception
  {
    backtrace: exception.backtrace.to_a,
    message: exception.message,
    error_class: exception.class.to_s,
  }
end

#headersObject



113
114
115
# File 'lib/wat_catcher/report.rb', line 113

def headers
  Hash[*request.headers.select { |x, y| y.instance_of? String }.sort_by(&:first).flatten]
end

#instrument_reportObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/wat_catcher/report.rb', line 42

def instrument_report
  return if WatCatcher.configuration.disabled

  # increment graphite counter, ':' is used to seperate metric from metric value -- therefore, replace ':' with '_'
  #   e.g. kairos.staging.exceptions.NoMethodError.frequency is count of `NoMethodError`s during sample period (60s)
  #
  # for application-aggregated
  metrics_reporter.increment "#{metrics_namespace}.wat.#{exception_description[:error_class].gsub ':', '_'}.frequency"
  # for server-aggregated
  metrics_reporter.increment "#{metrics_namespace}.#{base_description[:hostname]}.wat.#{exception_description[:error_class].gsub ':', '_'}.frequency"

  # emit an event that an exception was raised
  metrics_reporter.set "#{metrics_namespace}.#{base_description[:hostname]}.wat.#{base_description[:error_class]}.occurance", base_description[:captured_at]
end

#log_reportObject



20
21
22
23
# File 'lib/wat_catcher/report.rb', line 20

def log_report
  return if WatCatcher.configuration.disabled
  Rails.logger.error( "WatCatcher::error: " + base_description.tap {|x| x.delete(:rails_root) }.to_json )
end

#metrics_disabled?Boolean

Returns:

  • (Boolean)


25
26
27
28
# File 'lib/wat_catcher/report.rb', line 25

def metrics_disabled?
  WatCatcher.configuration.metrics_disabled = true if WatCatcher.configuration.metrics_disabled.nil?
  WatCatcher.configuration.metrics_disabled
end

#metrics_namespaceObject



38
39
40
# File 'lib/wat_catcher/report.rb', line 38

def metrics_namespace
  "#{base_description[:app_name]}.#{base_description[:app_env]}".downcase
end

#metrics_reporterObject



30
31
32
33
34
35
36
# File 'lib/wat_catcher/report.rb', line 30

def metrics_reporter
  @reporter ||= ::WatCatcher::Metrics.new
  @reporter.host = WatCatcher.configuration.statsd_host
  @reporter.port = WatCatcher.configuration.statsd_port

  @reporter
end

#param_exception_descriptionObject



66
67
68
69
# File 'lib/wat_catcher/report.rb', line 66

def param_exception_description
  return {} if exception || request.blank?
  request.params[:wat].merge(request_params: nil)
end

#paramsObject



57
58
59
60
61
62
63
64
# File 'lib/wat_catcher/report.rb', line 57

def params
  { wat: base_description
    .merge(user_description)
    .merge(exception_description)
    .merge(request_description)
    .merge(worker_description)
    .merge(param_exception_description) }
end

#request_descriptionObject



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/wat_catcher/report.rb', line 99

def request_description
  return {} unless request
  request_params = request.filtered_parameters
  session = request.session.as_json
  page_url = request.url

  {
    page_url: page_url,
    request_headers: headers,
    request_params: request_params,
    session: session,
  }
end

#send_reportObject



15
16
17
18
# File 'lib/wat_catcher/report.rb', line 15

def send_report
  return if WatCatcher.configuration.disabled
  ::WatCatcher::Poster.perform_later("#{WatCatcher.configuration.host}/wats", params.as_json)
end

#user_descriptionObject



82
83
84
85
86
87
88
# File 'lib/wat_catcher/report.rb', line 82

def user_description
  u = nil
  begin
    u = user.as_json
  rescue; end
  { app_user: u }
end

#worker_descriptionObject



117
118
119
120
121
122
# File 'lib/wat_catcher/report.rb', line 117

def worker_description
  return {} unless sidekiq
  {
    sidekiq_msg: sidekiq
  }
end