Class: Lens::Rails::ErrorExporter
- Inherits:
-
Object
- Object
- Lens::Rails::ErrorExporter
- Defined in:
- lib/lens/rails/error_exporter.rb
Overview
Implements the Rails.error subscriber interface. Buffers error reports on a SizedQueue and ships them in batches to POST /v1/errors via a persistent HTTP connection on a dedicated worker thread.
Constant Summary collapse
- BATCH_SIZE =
200
Instance Method Summary collapse
- #ensure_worker! ⇒ Object
-
#initialize(url:, token:, service_name:, ignore_classes: [], max_buffer: 1_000, open_timeout: 2, read_timeout: 2) ⇒ ErrorExporter
constructor
A new instance of ErrorExporter.
-
#report(error, handled:, severity: :error, context: {}, source: nil) ⇒ Object
Rails.error subscriber interface.
- #restart_flush_thread ⇒ Object
- #shutdown(timeout:) ⇒ Object
Constructor Details
#initialize(url:, token:, service_name:, ignore_classes: [], max_buffer: 1_000, open_timeout: 2, read_timeout: 2) ⇒ ErrorExporter
Returns a new instance of ErrorExporter.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/lens/rails/error_exporter.rb', line 16 def initialize(url:, token:, service_name:, ignore_classes: [], max_buffer: 1_000, open_timeout: 2, read_timeout: 2) @uri = URI("#{url}/v1/errors") @token = token @service_name = service_name @ignore_classes = ignore_classes @max_buffer = max_buffer @open_timeout = open_timeout @read_timeout = read_timeout @queue = SizedQueue.new(max_buffer) @http = nil @fork_mutex = Mutex.new start_worker Lens::Rails.register_flushable(self) end |
Instance Method Details
#ensure_worker! ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/lens/rails/error_exporter.rb', line 60 def ensure_worker! return if @worker&.alive? @fork_mutex.synchronize do return if @worker&.alive? restart_flush_thread end end |
#report(error, handled:, severity: :error, context: {}, source: nil) ⇒ Object
Rails.error subscriber interface.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/lens/rails/error_exporter.rb', line 35 def report(error, handled:, severity: :error, context: {}, source: nil) return if @ignore_classes.include?(error.class.name) rec = { "class" => error.class.name, "message" => error..to_s.first(2_000), "backtrace" => error.backtrace&.first(50) || [], "context" => Sanitizer.sanitize(context || {}), "severity" => severity.to_s, "source" => source, "occurred_at" => Time.now.iso8601(3), }.compact ensure_worker! @queue.push(rec, true) rescue ThreadError # Buffer full — drop silently end |
#restart_flush_thread ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/lens/rails/error_exporter.rb', line 68 def restart_flush_thread begin @worker&.kill rescue nil end close_http @queue = SizedQueue.new(@max_buffer) start_worker end |
#shutdown(timeout:) ⇒ Object
54 55 56 57 58 |
# File 'lib/lens/rails/error_exporter.rb', line 54 def shutdown(timeout:) @queue.close @worker&.join(timeout) rescue end |