exceptbox 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +170 -0
- data/lib/exceptbox/async_queue.rb +103 -0
- data/lib/exceptbox/breadcrumbs.rb +73 -0
- data/lib/exceptbox/cleaner.rb +79 -0
- data/lib/exceptbox/configuration.rb +165 -0
- data/lib/exceptbox/context.rb +53 -0
- data/lib/exceptbox/delivery.rb +116 -0
- data/lib/exceptbox/event.rb +138 -0
- data/lib/exceptbox/integrations/active_job.rb +50 -0
- data/lib/exceptbox/integrations/at_exit.rb +32 -0
- data/lib/exceptbox/integrations/error_subscriber.rb +37 -0
- data/lib/exceptbox/integrations/rack.rb +37 -0
- data/lib/exceptbox/integrations/railtie.rb +63 -0
- data/lib/exceptbox/integrations/sidekiq.rb +49 -0
- data/lib/exceptbox/request_data.rb +66 -0
- data/lib/exceptbox/stacktrace.rb +78 -0
- data/lib/exceptbox/tasks/exceptbox.rake +43 -0
- data/lib/exceptbox/version.rb +5 -0
- data/lib/exceptbox.rb +193 -0
- data/lib/generators/exceptbox/install/install_generator.rb +25 -0
- data/lib/generators/exceptbox/install/templates/exceptbox.rb.tt +33 -0
- metadata +67 -0
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "net/http"
|
|
5
|
+
require "uri"
|
|
6
|
+
require "zlib"
|
|
7
|
+
require "stringio"
|
|
8
|
+
|
|
9
|
+
module Exceptbox
|
|
10
|
+
# Sends a batch of events to the Exceptbox API:
|
|
11
|
+
#
|
|
12
|
+
# POST {endpoint}/v1/events
|
|
13
|
+
# Exceptbox-Api-Key: <project api key>
|
|
14
|
+
# Content-Type: application/json, Content-Encoding: gzip
|
|
15
|
+
# { "payload_version": 1, "notifier": {...}, "events": [ ... ] }
|
|
16
|
+
#
|
|
17
|
+
# Never raises. Returns the HTTP status (or nil when the request failed).
|
|
18
|
+
module Delivery
|
|
19
|
+
PAYLOAD_VERSION = 1
|
|
20
|
+
NOTIFIER = {
|
|
21
|
+
"name" => "exceptbox-ruby",
|
|
22
|
+
"version" => Exceptbox::VERSION,
|
|
23
|
+
"url" => "https://github.com/exceptbox/exceptbox-ruby"
|
|
24
|
+
}.freeze
|
|
25
|
+
|
|
26
|
+
class << self
|
|
27
|
+
# Replaceable for tests: ->(url, body, headers) { status }
|
|
28
|
+
attr_writer :transport
|
|
29
|
+
|
|
30
|
+
def deliver(events, config)
|
|
31
|
+
return nil if events.empty?
|
|
32
|
+
return nil if backing_off?
|
|
33
|
+
|
|
34
|
+
body = gzip(JSON.generate(
|
|
35
|
+
"payload_version" => PAYLOAD_VERSION,
|
|
36
|
+
"notifier" => NOTIFIER,
|
|
37
|
+
"events" => events
|
|
38
|
+
))
|
|
39
|
+
headers = {
|
|
40
|
+
"Content-Type" => "application/json",
|
|
41
|
+
"Content-Encoding" => "gzip",
|
|
42
|
+
"Exceptbox-Api-Key" => config.api_key.to_s,
|
|
43
|
+
"Exceptbox-Payload-Version" => PAYLOAD_VERSION.to_s,
|
|
44
|
+
"User-Agent" => "exceptbox-ruby/#{Exceptbox::VERSION} ruby/#{RUBY_VERSION}"
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
status, retry_after = transport.call(config.events_url, body, headers, config)
|
|
48
|
+
handle_status(status, retry_after, events.size, config)
|
|
49
|
+
status
|
|
50
|
+
rescue StandardError => e
|
|
51
|
+
config.logger&.warn("Failed to send #{events.size} event(s): #{e.class}: #{e.message}")
|
|
52
|
+
nil
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def transport
|
|
56
|
+
@transport ||= method(:http_post)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def reset!
|
|
60
|
+
@transport = nil
|
|
61
|
+
@backoff_until = nil
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
private
|
|
65
|
+
|
|
66
|
+
def http_post(url, body, headers, config)
|
|
67
|
+
uri = URI.parse(url)
|
|
68
|
+
proxy = config.proxy && URI.parse(config.proxy)
|
|
69
|
+
http = Net::HTTP.new(uri.host, uri.port, proxy&.host, proxy&.port, proxy&.user, proxy&.password)
|
|
70
|
+
http.use_ssl = uri.scheme == "https"
|
|
71
|
+
http.open_timeout = config.timeout
|
|
72
|
+
http.read_timeout = config.timeout
|
|
73
|
+
http.write_timeout = config.timeout if http.respond_to?(:write_timeout=)
|
|
74
|
+
|
|
75
|
+
request = Net::HTTP::Post.new(uri.request_uri, headers)
|
|
76
|
+
request.body = body
|
|
77
|
+
response = http.request(request)
|
|
78
|
+
[response.code.to_i, response["Retry-After"]]
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def handle_status(status, retry_after, count, config)
|
|
82
|
+
case status
|
|
83
|
+
when 200..299 then nil
|
|
84
|
+
when 401, 403
|
|
85
|
+
config.logger&.error("Events rejected: invalid or revoked API key (check config.api_key)")
|
|
86
|
+
when 413
|
|
87
|
+
config.logger&.warn("#{count} event(s) rejected: payload too large")
|
|
88
|
+
when 429
|
|
89
|
+
# Over the plan's quota or rate limit: wait as long as the API asks
|
|
90
|
+
seconds = retry_after.to_i.positive? ? retry_after.to_i : 60
|
|
91
|
+
@backoff_until = now + [seconds, 3600].min
|
|
92
|
+
config.logger&.warn("Rate limited, pausing delivery for #{seconds}s (#{count} event(s) dropped)")
|
|
93
|
+
when 500..599
|
|
94
|
+
@backoff_until = now + 10
|
|
95
|
+
config.logger&.warn("Server error #{status}, #{count} event(s) dropped")
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def backing_off?
|
|
100
|
+
@backoff_until && now < @backoff_until
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def now
|
|
104
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def gzip(string)
|
|
108
|
+
io = StringIO.new
|
|
109
|
+
gz = Zlib::GzipWriter.new(io)
|
|
110
|
+
gz.write(string)
|
|
111
|
+
gz.close
|
|
112
|
+
io.string
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "time"
|
|
4
|
+
|
|
5
|
+
module Exceptbox
|
|
6
|
+
# One occurrence of an error, as it will be sent. before_notify callbacks
|
|
7
|
+
# (and the block given to Exceptbox.notify) receive it and can change it:
|
|
8
|
+
#
|
|
9
|
+
# config.before_notify do |event|
|
|
10
|
+
# event.ignore! if event.exception.message.include?("timeout")
|
|
11
|
+
# event.severity = "warning" if event.exception.is_a?(Net::ReadTimeout)
|
|
12
|
+
# event.add_metadata(:account, plan: Current.account&.plan)
|
|
13
|
+
# event.grouping_hash = "payments-timeout" # force a custom group
|
|
14
|
+
# end
|
|
15
|
+
class Event
|
|
16
|
+
SEVERITIES = %w[error warning info].freeze
|
|
17
|
+
MAX_CAUSES = 5
|
|
18
|
+
|
|
19
|
+
attr_reader :exception, :metadata, :breadcrumbs
|
|
20
|
+
attr_accessor :severity, :handled, :source, :context, :user, :grouping_hash,
|
|
21
|
+
:request, :job, :app_type
|
|
22
|
+
|
|
23
|
+
def initialize(exception, config:, handled:, severity:, source:)
|
|
24
|
+
@exception = exception
|
|
25
|
+
@config = config
|
|
26
|
+
@handled = handled
|
|
27
|
+
@severity = SEVERITIES.include?(severity.to_s) ? severity.to_s : "error"
|
|
28
|
+
@source = source
|
|
29
|
+
@metadata = {}
|
|
30
|
+
@breadcrumbs = []
|
|
31
|
+
@ignored = false
|
|
32
|
+
@timestamp = Time.now.utc
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def add_metadata(section, data = nil, value = nil)
|
|
36
|
+
data = { data => value } unless data.is_a?(Hash)
|
|
37
|
+
(@metadata[section.to_s] ||= {}).merge!(data.transform_keys(&:to_s))
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def add_breadcrumbs(crumbs)
|
|
41
|
+
@breadcrumbs.concat(crumbs)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def ignore!
|
|
45
|
+
@ignored = true
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def ignored?
|
|
49
|
+
@ignored
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# The JSON-ready hash sent to the API (payload version 1).
|
|
53
|
+
def to_h
|
|
54
|
+
filters = @config.filters
|
|
55
|
+
{
|
|
56
|
+
"exceptions" => exceptions,
|
|
57
|
+
"severity" => SEVERITIES.include?(@severity.to_s) ? @severity.to_s : "error",
|
|
58
|
+
"handled" => !!@handled,
|
|
59
|
+
"source" => @source,
|
|
60
|
+
"context" => @context,
|
|
61
|
+
"grouping_hash" => @grouping_hash,
|
|
62
|
+
"timestamp" => @timestamp.iso8601(3),
|
|
63
|
+
"app" => {
|
|
64
|
+
"release_stage" => @config.release_stage,
|
|
65
|
+
"version" => @config.app_version,
|
|
66
|
+
"type" => @app_type || @config.app_type
|
|
67
|
+
}.compact,
|
|
68
|
+
"device" => {
|
|
69
|
+
"hostname" => @config.hostname,
|
|
70
|
+
"runtime_versions" => runtime_versions
|
|
71
|
+
}.compact,
|
|
72
|
+
"user" => user_hash && Cleaner.clean(user_hash, []),
|
|
73
|
+
"request" => @request && Cleaner.clean(@request, filters),
|
|
74
|
+
"job" => @job && Cleaner.clean(@job, filters),
|
|
75
|
+
"breadcrumbs" => Cleaner.clean(@breadcrumbs, filters),
|
|
76
|
+
"metadata" => Cleaner.clean(@metadata, filters)
|
|
77
|
+
}.reject { |_, v| v.nil? || (v.respond_to?(:empty?) && v.empty?) }
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
private
|
|
81
|
+
|
|
82
|
+
# Outermost first, then its causes ("raised while handling ...").
|
|
83
|
+
def exceptions
|
|
84
|
+
list = []
|
|
85
|
+
current = @exception
|
|
86
|
+
seen = {}
|
|
87
|
+
while current && list.size <= MAX_CAUSES && !seen[current.object_id]
|
|
88
|
+
seen[current.object_id] = true
|
|
89
|
+
list << {
|
|
90
|
+
"error_class" => current.class.name || current.class.to_s,
|
|
91
|
+
"message" => Cleaner.truncate(current.message.to_s.dup.force_encoding(Encoding::UTF_8).scrub),
|
|
92
|
+
"stacktrace" => Stacktrace.frames(current.backtrace, @config)
|
|
93
|
+
}
|
|
94
|
+
current = current.cause
|
|
95
|
+
end
|
|
96
|
+
list
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def user_hash
|
|
100
|
+
case @user
|
|
101
|
+
when nil then nil
|
|
102
|
+
when Hash
|
|
103
|
+
@user.transform_keys(&:to_s).slice("id", "email", "name")
|
|
104
|
+
else
|
|
105
|
+
{
|
|
106
|
+
"id" => try_call(@user, :id),
|
|
107
|
+
"email" => try_call(@user, :email),
|
|
108
|
+
"name" => user_name(@user)
|
|
109
|
+
}.compact
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def user_name(user)
|
|
114
|
+
full = [try_call(user, :first_name), try_call(user, :last_name)].compact.map(&:to_s).reject(&:empty?).join(" ")
|
|
115
|
+
return full unless full.empty?
|
|
116
|
+
|
|
117
|
+
%i[full_name name display_name username].each do |field|
|
|
118
|
+
value = try_call(user, field)
|
|
119
|
+
return value.to_s if value && !value.to_s.empty?
|
|
120
|
+
end
|
|
121
|
+
nil
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def try_call(object, method)
|
|
125
|
+
object.respond_to?(method) ? object.public_send(method) : nil
|
|
126
|
+
rescue StandardError
|
|
127
|
+
nil
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def runtime_versions
|
|
131
|
+
versions = { "ruby" => RUBY_VERSION }
|
|
132
|
+
versions["rails"] = ::Rails.version if defined?(::Rails) && ::Rails.respond_to?(:version)
|
|
133
|
+
versions["rack"] = ::Rack.release if defined?(::Rack) && ::Rack.respond_to?(:release)
|
|
134
|
+
versions["sidekiq"] = ::Sidekiq::VERSION if defined?(::Sidekiq::VERSION)
|
|
135
|
+
versions
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Exceptbox
|
|
4
|
+
module Integrations
|
|
5
|
+
# Reports exceptions raised inside ActiveJob jobs, with the job details.
|
|
6
|
+
# Included into ActiveJob::Base by the Railtie.
|
|
7
|
+
module ActiveJob
|
|
8
|
+
def self.included(base)
|
|
9
|
+
base.around_perform do |job, block|
|
|
10
|
+
Context.isolate do
|
|
11
|
+
Context.current.job = Exceptbox::Integrations::ActiveJob.details(job)
|
|
12
|
+
Context.current.context_name = job.class.name
|
|
13
|
+
begin
|
|
14
|
+
block.call
|
|
15
|
+
rescue Exception => e # rubocop:disable Lint/RescueException -- re-raised below
|
|
16
|
+
unless e.is_a?(SignalException) || e.is_a?(SystemExit)
|
|
17
|
+
Exceptbox.notify(e, severity: "error", handled: false, source: "job")
|
|
18
|
+
end
|
|
19
|
+
raise
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Only standard ActiveJob attributes; arguments are serialized the way
|
|
26
|
+
# ActiveJob stores them (records become GlobalIDs, never full rows).
|
|
27
|
+
def self.details(job)
|
|
28
|
+
{
|
|
29
|
+
"class" => job.class.name,
|
|
30
|
+
"id" => job.job_id,
|
|
31
|
+
"provider_id" => job.provider_job_id,
|
|
32
|
+
"queue" => job.queue_name,
|
|
33
|
+
"priority" => job.priority,
|
|
34
|
+
"attempt" => job.executions,
|
|
35
|
+
"enqueued_at" => job.enqueued_at,
|
|
36
|
+
"arguments" => arguments(job),
|
|
37
|
+
"adapter" => job.class.queue_adapter_name
|
|
38
|
+
}.compact
|
|
39
|
+
rescue StandardError
|
|
40
|
+
{ "class" => job.class.name }
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def self.arguments(job)
|
|
44
|
+
::ActiveJob::Arguments.serialize(job.arguments)
|
|
45
|
+
rescue StandardError
|
|
46
|
+
Array(job.arguments).map { |arg| arg.inspect[0, 200] }
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Exceptbox
|
|
4
|
+
module Integrations
|
|
5
|
+
# Reports the exception that is about to crash the process: a failing rake
|
|
6
|
+
# task, `rails runner` script, or any Ruby script. Sent synchronously,
|
|
7
|
+
# because the process is about to end.
|
|
8
|
+
module AtExit
|
|
9
|
+
module_function
|
|
10
|
+
|
|
11
|
+
def install!
|
|
12
|
+
return if @installed
|
|
13
|
+
|
|
14
|
+
@installed = true
|
|
15
|
+
at_exit { report($!) }
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def report(exception)
|
|
19
|
+
return if exception.nil? || exception.is_a?(SystemExit) || exception.is_a?(SignalException)
|
|
20
|
+
|
|
21
|
+
config = Exceptbox.configuration
|
|
22
|
+
return unless config.report_crashes
|
|
23
|
+
|
|
24
|
+
async = config.async
|
|
25
|
+
config.async = false
|
|
26
|
+
Exceptbox.notify(exception, severity: "error", handled: false, source: "process")
|
|
27
|
+
ensure
|
|
28
|
+
config.async = async if config
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Exceptbox
|
|
4
|
+
module Integrations
|
|
5
|
+
# Receives everything reported through Rails' error reporter:
|
|
6
|
+
#
|
|
7
|
+
# Rails.error.handle { ... } # swallowed errors, sent as handled
|
|
8
|
+
# Rails.error.report(error) # explicitly reported errors
|
|
9
|
+
#
|
|
10
|
+
# Unhandled request/job errors also pass through here; the Rack and job
|
|
11
|
+
# integrations have usually reported them already (they are sent once).
|
|
12
|
+
class ErrorSubscriber
|
|
13
|
+
def report(error, handled:, severity:, context:, source: nil)
|
|
14
|
+
return if handled && !Exceptbox.configuration.capture_handled_errors
|
|
15
|
+
|
|
16
|
+
Exceptbox.notify(
|
|
17
|
+
error,
|
|
18
|
+
severity: severity.to_s,
|
|
19
|
+
handled: handled,
|
|
20
|
+
source: source || "rails.error",
|
|
21
|
+
metadata: metadata_for(context)
|
|
22
|
+
)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
# Rails passes live objects (controller, job) in the context: keep the
|
|
28
|
+
# plain data only.
|
|
29
|
+
def metadata_for(context)
|
|
30
|
+
data = context.to_h.reject { |key, _| %i[controller job].include?(key.to_sym) }
|
|
31
|
+
data.empty? ? nil : { "context" => data }
|
|
32
|
+
rescue StandardError
|
|
33
|
+
nil
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Exceptbox
|
|
4
|
+
module Integrations
|
|
5
|
+
# Rack middleware: reports exceptions raised while handling a request,
|
|
6
|
+
# with the request details and user. Added automatically in Rails; for
|
|
7
|
+
# Sinatra/Hanami/plain Rack: use Exceptbox::Integrations::Rack
|
|
8
|
+
class Rack
|
|
9
|
+
def initialize(app)
|
|
10
|
+
@app = app
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def call(env)
|
|
14
|
+
Context.clear!
|
|
15
|
+
Context.current.env = env
|
|
16
|
+
response = @app.call(env)
|
|
17
|
+
|
|
18
|
+
# Frameworks that rescue and render the error themselves leave it here
|
|
19
|
+
if (error = env["sinatra.error"] || env["rack.exception"])
|
|
20
|
+
Exceptbox.notify(error, severity: "error", handled: false, source: "request")
|
|
21
|
+
end
|
|
22
|
+
response
|
|
23
|
+
rescue Exception => e # rubocop:disable Lint/RescueException -- re-raised below
|
|
24
|
+
Exceptbox.notify(e, severity: "error", handled: false, source: "request") unless skip?(e)
|
|
25
|
+
raise
|
|
26
|
+
ensure
|
|
27
|
+
Context.clear!
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
def skip?(exception)
|
|
33
|
+
exception.is_a?(SignalException) || exception.is_a?(SystemExit)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rails/railtie"
|
|
4
|
+
|
|
5
|
+
module Exceptbox
|
|
6
|
+
module Integrations
|
|
7
|
+
# Wires Exceptbox into a Rails app: request middleware, ActiveJob,
|
|
8
|
+
# Rails.error, Sidekiq, breadcrumbs, crash reporting, and sensible
|
|
9
|
+
# defaults (project root, release stage, the app's filter_parameters).
|
|
10
|
+
class Railtie < ::Rails::Railtie
|
|
11
|
+
initializer "exceptbox.middleware" do |app|
|
|
12
|
+
app.config.middleware.use Exceptbox::Integrations::Rack
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
initializer "exceptbox.active_job" do
|
|
16
|
+
ActiveSupport.on_load(:active_job) do
|
|
17
|
+
require "exceptbox/integrations/active_job"
|
|
18
|
+
include Exceptbox::Integrations::ActiveJob
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
initializer "exceptbox.defaults", before: :load_config_initializers do |app|
|
|
23
|
+
config = Exceptbox.configuration
|
|
24
|
+
config.project_root = ::Rails.root.to_s
|
|
25
|
+
config.release_stage = ENV["EXCEPTBOX_RELEASE_STAGE"] || ::Rails.env.to_s
|
|
26
|
+
config.app_type ||= "rails"
|
|
27
|
+
config.logger = ::Rails.logger if ::Rails.logger
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
config.after_initialize do |app|
|
|
31
|
+
config = Exceptbox.configuration
|
|
32
|
+
|
|
33
|
+
# The app's own filter list (passwords, tokens...) applies too
|
|
34
|
+
extra = Array(app.config.filter_parameters).select { |f| f.is_a?(String) || f.is_a?(Symbol) || f.is_a?(Regexp) }
|
|
35
|
+
config.filters = (Array(config.filters) + extra.map { |f| f.is_a?(Regexp) ? f : f.to_s }).uniq
|
|
36
|
+
|
|
37
|
+
::Rails.error.subscribe(Exceptbox::Integrations::ErrorSubscriber.new) if ::Rails.respond_to?(:error)
|
|
38
|
+
Exceptbox::Breadcrumbs.subscribe_to_rails! if config.breadcrumbs
|
|
39
|
+
|
|
40
|
+
# Gems load in Gemfile order: Sidekiq may load after Exceptbox
|
|
41
|
+
if defined?(::Sidekiq)
|
|
42
|
+
require "exceptbox/integrations/sidekiq"
|
|
43
|
+
Exceptbox::Integrations::Sidekiq.install!
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
rake_tasks do
|
|
48
|
+
Exceptbox::Integrations::AtExit.install!
|
|
49
|
+
load File.expand_path("../tasks/exceptbox.rake", __dir__)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
runner do
|
|
53
|
+
Exceptbox::Integrations::AtExit.install!
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
generators do
|
|
57
|
+
require "generators/exceptbox/install/install_generator"
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
require "exceptbox/integrations/error_subscriber"
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Exceptbox
|
|
4
|
+
module Integrations
|
|
5
|
+
# Sidekiq server middleware: reports exceptions raised by Sidekiq jobs
|
|
6
|
+
# (including ActiveJob jobs running on Sidekiq; those are sent once).
|
|
7
|
+
class Sidekiq
|
|
8
|
+
def call(_worker, job, queue)
|
|
9
|
+
Context.isolate do
|
|
10
|
+
Context.current.job = details(job, queue)
|
|
11
|
+
Context.current.context_name = job["wrapped"] || job["class"]
|
|
12
|
+
begin
|
|
13
|
+
yield
|
|
14
|
+
rescue Exception => e # rubocop:disable Lint/RescueException -- re-raised below
|
|
15
|
+
unless e.is_a?(SignalException) || e.is_a?(SystemExit)
|
|
16
|
+
Exceptbox.notify(e, severity: "error", handled: false, source: "sidekiq")
|
|
17
|
+
end
|
|
18
|
+
raise
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def self.install!
|
|
24
|
+
return if @installed || !defined?(::Sidekiq)
|
|
25
|
+
|
|
26
|
+
@installed = true
|
|
27
|
+
::Sidekiq.configure_server do |config|
|
|
28
|
+
config.server_middleware { |chain| chain.add(Exceptbox::Integrations::Sidekiq) }
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def details(job, queue)
|
|
35
|
+
{
|
|
36
|
+
"class" => job["wrapped"] || job["class"],
|
|
37
|
+
"id" => job["jid"],
|
|
38
|
+
"queue" => queue,
|
|
39
|
+
"attempt" => job["retry_count"].to_i + 1,
|
|
40
|
+
"retry" => job["retry"],
|
|
41
|
+
"enqueued_at" => job["enqueued_at"],
|
|
42
|
+
"arguments" => job["args"]
|
|
43
|
+
}.compact
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
Exceptbox::Integrations::Sidekiq.install!
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Exceptbox
|
|
4
|
+
# Request details from a Rack env, without depending on Rails. Values are
|
|
5
|
+
# filtered later by the Cleaner (params, headers) and here (URLs).
|
|
6
|
+
module RequestData
|
|
7
|
+
# Headers never worth sending, even filtered
|
|
8
|
+
SKIP_HEADERS = %w[HTTP_COOKIE HTTP_AUTHORIZATION HTTP_PROXY_AUTHORIZATION].freeze
|
|
9
|
+
|
|
10
|
+
module_function
|
|
11
|
+
|
|
12
|
+
def from_env(env, config)
|
|
13
|
+
return nil unless env.is_a?(Hash)
|
|
14
|
+
|
|
15
|
+
{
|
|
16
|
+
"url" => Cleaner.clean_url(url(env), config.filters),
|
|
17
|
+
"http_method" => env["REQUEST_METHOD"],
|
|
18
|
+
"params" => params(env),
|
|
19
|
+
"headers" => headers(env),
|
|
20
|
+
"client_ip" => client_ip(env),
|
|
21
|
+
"referer" => Cleaner.clean_url(env["HTTP_REFERER"], config.filters),
|
|
22
|
+
"request_id" => env["action_dispatch.request_id"] || env["HTTP_X_REQUEST_ID"]
|
|
23
|
+
}.compact
|
|
24
|
+
rescue StandardError
|
|
25
|
+
nil
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def url(env)
|
|
29
|
+
scheme = env["HTTP_X_FORWARDED_PROTO"]&.split(",")&.first || env["rack.url_scheme"] || "http"
|
|
30
|
+
host = env["HTTP_X_FORWARDED_HOST"]&.split(",")&.last&.strip || env["HTTP_HOST"] || env["SERVER_NAME"]
|
|
31
|
+
path = "#{env['SCRIPT_NAME']}#{env['PATH_INFO']}"
|
|
32
|
+
query = env["QUERY_STRING"].to_s
|
|
33
|
+
"#{scheme}://#{host}#{path}#{"?#{query}" unless query.empty?}"
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Rails has already parsed (and filtered) params; for plain Rack apps use
|
|
37
|
+
# the query string and form body, which Rack has usually parsed already.
|
|
38
|
+
def params(env)
|
|
39
|
+
if env["action_dispatch.parameter_filter"] && env["action_dispatch.request.parameters"]
|
|
40
|
+
env["action_dispatch.request.parameters"]
|
|
41
|
+
elsif defined?(::Rack::Request)
|
|
42
|
+
request = ::Rack::Request.new(env)
|
|
43
|
+
request.GET.merge(request.POST)
|
|
44
|
+
end
|
|
45
|
+
rescue StandardError
|
|
46
|
+
nil
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def headers(env)
|
|
50
|
+
env.each_with_object({}) do |(key, value), out|
|
|
51
|
+
next unless key.start_with?("HTTP_") && value.is_a?(String)
|
|
52
|
+
next if SKIP_HEADERS.include?(key)
|
|
53
|
+
|
|
54
|
+
name = key.delete_prefix("HTTP_").split("_").map(&:capitalize).join("-")
|
|
55
|
+
out[name] = value
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def client_ip(env)
|
|
60
|
+
return env["action_dispatch.remote_ip"].to_s if env["action_dispatch.remote_ip"]
|
|
61
|
+
|
|
62
|
+
forwarded = env["HTTP_X_FORWARDED_FOR"].to_s.split(",").map(&:strip).reject(&:empty?)
|
|
63
|
+
forwarded.first || env["REMOTE_ADDR"]
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Exceptbox
|
|
4
|
+
# Turns a Ruby backtrace into structured frames:
|
|
5
|
+
# { "file", "line", "method", "in_project", "code" => { "41" => "..." } }
|
|
6
|
+
#
|
|
7
|
+
# File paths under the project root are sent relative to it, so a new
|
|
8
|
+
# release directory (Capistrano, Kamal) doesn't change how errors group.
|
|
9
|
+
module Stacktrace
|
|
10
|
+
# "path/to/file.rb:12:in `method'" and Ruby 3.4's "in 'Klass#method'"
|
|
11
|
+
FRAME = /\A(?<file>.+?):(?<line>\d+)(?::in [`'](?<method>.*)')?\z/
|
|
12
|
+
MAX_FRAMES = 200
|
|
13
|
+
CODE_FRAMES = 10 # project frames that get source lines
|
|
14
|
+
CODE_RADIUS = 4 # lines before and after
|
|
15
|
+
MAX_LINE = 200 # characters per source line
|
|
16
|
+
|
|
17
|
+
module_function
|
|
18
|
+
|
|
19
|
+
def frames(backtrace, config)
|
|
20
|
+
root = "#{config.project_root.to_s.chomp('/')}/"
|
|
21
|
+
code_left = config.send_code ? CODE_FRAMES : 0
|
|
22
|
+
|
|
23
|
+
Array(backtrace).first(MAX_FRAMES).filter_map do |line|
|
|
24
|
+
match = FRAME.match(line.to_s)
|
|
25
|
+
next { "file" => line.to_s, "line" => nil, "method" => nil, "in_project" => false } unless match
|
|
26
|
+
|
|
27
|
+
file = absolute(match[:file])
|
|
28
|
+
in_project = file.start_with?(root) && !vendored?(file)
|
|
29
|
+
frame = {
|
|
30
|
+
"file" => in_project ? file.delete_prefix(root) : gem_path(file),
|
|
31
|
+
"line" => match[:line].to_i,
|
|
32
|
+
"method" => match[:method],
|
|
33
|
+
"in_project" => in_project
|
|
34
|
+
}
|
|
35
|
+
if in_project && code_left.positive?
|
|
36
|
+
code = code_around(file, match[:line].to_i)
|
|
37
|
+
if code
|
|
38
|
+
frame["code"] = code
|
|
39
|
+
code_left -= 1
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
frame
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def absolute(file)
|
|
47
|
+
return file if file.start_with?("/", "<") || file.match?(%r{\A[A-Za-z]:[\\/]})
|
|
48
|
+
|
|
49
|
+
File.expand_path(file)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def vendored?(file)
|
|
53
|
+
file.include?("/vendor/") || file.include?("/node_modules/") || file.include?("/gems/")
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# "/usr/lib/ruby/gems/3.4.0/gems/actionpack-8.1.0/lib/x.rb" -> "actionpack-8.1.0/lib/x.rb"
|
|
57
|
+
def gem_path(file)
|
|
58
|
+
if (m = file.match(%r{/gems/([^/]+-\d[^/]*/.*)\z}))
|
|
59
|
+
m[1]
|
|
60
|
+
elsif (m = file.match(%r{/lib/ruby/\d[^/]*/(.*)\z}))
|
|
61
|
+
"ruby/#{m[1]}"
|
|
62
|
+
else
|
|
63
|
+
file
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def code_around(file, line_no)
|
|
68
|
+
return nil unless File.file?(file) && File.size(file) < 1_000_000
|
|
69
|
+
|
|
70
|
+
lines = File.readlines(file)
|
|
71
|
+
first = [line_no - CODE_RADIUS, 1].max
|
|
72
|
+
last = [line_no + CODE_RADIUS, lines.size].min
|
|
73
|
+
(first..last).to_h { |n| [n.to_s, lines[n - 1].to_s.chomp[0, MAX_LINE]] }
|
|
74
|
+
rescue StandardError
|
|
75
|
+
nil
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|