Class: ExceptionNotifier::DatadogNotifier::DatadogExceptionEvent
- Inherits:
-
Object
- Object
- ExceptionNotifier::DatadogNotifier::DatadogExceptionEvent
- Includes:
- BacktraceCleaner
- Defined in:
- lib/exception_notifier/datadog_notifier.rb
Constant Summary collapse
- MAX_TITLE_LENGTH =
120
- MAX_VALUE_LENGTH =
300
- MAX_BACKTRACE_SIZE =
3
- ALERT_TYPE =
'error'
Instance Attribute Summary collapse
-
#exception ⇒ Object
readonly
Returns the value of attribute exception.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #backtrace ⇒ Object
- #controller ⇒ Object
- #event ⇒ Object
- #formatted_backtrace ⇒ Object
- #formatted_body ⇒ Object
- #formatted_key_value(key, value) ⇒ Object
- #formatted_request ⇒ Object
- #formatted_session ⇒ Object
- #formatted_title ⇒ Object
-
#initialize(exception, options) ⇒ DatadogExceptionEvent
constructor
A new instance of DatadogExceptionEvent.
- #inspect_object(object) ⇒ Object
- #request ⇒ Object
- #tags ⇒ Object
- #title_prefix ⇒ Object
- #truncate(string, max) ⇒ Object
Methods included from BacktraceCleaner
Constructor Details
#initialize(exception, options) ⇒ DatadogExceptionEvent
Returns a new instance of DatadogExceptionEvent.
40 41 42 43 |
# File 'lib/exception_notifier/datadog_notifier.rb', line 40 def initialize(exception, ) @exception = exception @options = end |
Instance Attribute Details
#exception ⇒ Object (readonly)
Returns the value of attribute exception.
37 38 39 |
# File 'lib/exception_notifier/datadog_notifier.rb', line 37 def exception @exception end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
37 38 39 |
# File 'lib/exception_notifier/datadog_notifier.rb', line 37 def @options end |
Instance Method Details
#backtrace ⇒ Object
53 54 55 |
# File 'lib/exception_notifier/datadog_notifier.rb', line 53 def backtrace @backtrace ||= exception.backtrace ? clean_backtrace(exception) : [] end |
#controller ⇒ Object
49 50 51 |
# File 'lib/exception_notifier/datadog_notifier.rb', line 49 def controller @controller ||= [:env] && [:env]['action_controller.instance'] end |
#event ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/exception_notifier/datadog_notifier.rb', line 65 def event title = formatted_title body = formatted_body Dogapi::Event.new( body, msg_title: title, alert_type: ALERT_TYPE, tags: , aggregation_key: [title] ) end |
#formatted_backtrace ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/exception_notifier/datadog_notifier.rb', line 124 def formatted_backtrace size = [backtrace.size, MAX_BACKTRACE_SIZE].min text = [] text << '### **Backtrace**' text << '````' size.times { |i| text << backtrace[i] } text << '````' text << '___' text.join("\n") end |
#formatted_body ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/exception_notifier/datadog_notifier.rb', line 85 def formatted_body text = [] text << '%%%' text << formatted_request if request text << formatted_session if request text << formatted_backtrace text << '%%%' text.join("\n") end |
#formatted_key_value(key, value) ⇒ Object
97 98 99 |
# File 'lib/exception_notifier/datadog_notifier.rb', line 97 def formatted_key_value(key, value) "**#{key}:** #{value}" end |
#formatted_request ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/exception_notifier/datadog_notifier.rb', line 101 def formatted_request text = [] text << '### **Request**' text << formatted_key_value('URL', request.url) text << formatted_key_value('HTTP Method', request.request_method) text << formatted_key_value('IP Address', request.remote_ip) text << formatted_key_value('Parameters', request.filtered_parameters.inspect) text << formatted_key_value('Timestamp', Time.current) text << formatted_key_value('Server', Socket.gethostname) text << formatted_key_value('Rails root', Rails.root) if defined?(Rails) && Rails.respond_to?(:root) text << formatted_key_value('Process', $PROCESS_ID) text << '___' text.join("\n") end |
#formatted_session ⇒ Object
116 117 118 119 120 121 122 |
# File 'lib/exception_notifier/datadog_notifier.rb', line 116 def formatted_session text = [] text << '### **Session**' text << formatted_key_value('Data', request.session.to_hash) text << '___' text.join("\n") end |
#formatted_title ⇒ Object
78 79 80 81 82 83 |
# File 'lib/exception_notifier/datadog_notifier.rb', line 78 def formatted_title title = "#{title_prefix}#{controller_subtitle} (#{exception.class}) #{exception..inspect}" truncate(title, MAX_TITLE_LENGTH) end |
#inspect_object(object) ⇒ Object
140 141 142 143 144 145 146 147 |
# File 'lib/exception_notifier/datadog_notifier.rb', line 140 def inspect_object(object) case object when Hash, Array truncate(object.inspect, MAX_VALUE_LENGTH) else object.to_s end end |
#request ⇒ Object
45 46 47 |
# File 'lib/exception_notifier/datadog_notifier.rb', line 45 def request @request ||= ActionDispatch::Request.new([:env]) if [:env] end |
#tags ⇒ Object
57 58 59 |
# File 'lib/exception_notifier/datadog_notifier.rb', line 57 def [:tags] || [] end |
#title_prefix ⇒ Object
61 62 63 |
# File 'lib/exception_notifier/datadog_notifier.rb', line 61 def title_prefix [:title_prefix] || '' end |
#truncate(string, max) ⇒ Object
136 137 138 |
# File 'lib/exception_notifier/datadog_notifier.rb', line 136 def truncate(string, max) string.length > max ? "#{string[0...max]}..." : string end |