Module: Snare

Defined in:
lib/snare.rb,
lib/snare/queue.rb,
lib/snare/capture.rb,
lib/snare/transport.rb

Overview

Snare error-monitoring SDK. See README.md for usage.

Defined Under Namespace

Modules: Capture, Transport Classes: Queue

Constant Summary collapse

DEFAULT_API_BASE =
"https://intake.snare.dev"
FLUSH_SIZE =
5
FLUSH_INTERVAL_SECONDS =
10

Class Method Summary collapse

Class Method Details

.capture_exception(exception = $!) ⇒ Object

For the host app's own rescue blocks. exception defaults to $! (the exception actively propagating in a live rescue block) so this can also be called bare from inside one — a nice-to-have since Ruby has no equivalent of Python's ambient sys.exc_info() idiom.



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/snare.rb', line 37

def capture_exception(exception = $!)
  unless @queue
    warn "[snare-sdk] Snare.capture_exception called before Snare.init"
    return nil
  end
  unless exception
    warn "[snare-sdk] Snare.capture_exception called with no exception and no active $!"
    return nil
  end

  @queue.enqueue(Snare::Capture.build_captured_event(exception: exception, now: (Time.now.to_f * 1000).to_i))
  nil
end

.flushObject

Forces immediate delivery of whatever is queued. Used by the at_exit hook (at_exit runs right before process death — a background timer can't be relied on to fire after that point) and available for a host app to call explicitly, e.g. in a graceful-shutdown handler.



55
56
57
58
# File 'lib/snare.rb', line 55

def flush
  @queue&.flush
  nil
end

.init(project_id:, api_key:, api_base: DEFAULT_API_BASE) ⇒ Object

Registers the at_exit crash-capture handler and starts the batching queue. Keyword arguments, idiomatic Ruby. Safe to call more than once — each call re-initializes cleanly, discarding anything queued under the previous configuration rather than flushing it to stale settings.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/snare.rb', line 18

def init(project_id:, api_key:, api_base: DEFAULT_API_BASE)
  @project_id = project_id
  @api_key = api_key
  @api_base = api_base

  @queue&.drain

  @queue = Snare::Queue.new(flush_size: FLUSH_SIZE, flush_interval: FLUSH_INTERVAL_SECONDS) do |events|
    Snare::Transport.send_batch(api_base: @api_base, project_id: @project_id, api_key: @api_key, events: events)
  end

  register_at_exit_hook
  nil
end