module Notable

Constants

VERSION

Attributes

enabled[RW]
jobs_enabled[RW]
mask_ips[RW]
requests_enabled[RW]
slow_job_threshold[RW]
slow_request_threshold[RW]
track_job_method[RW]

jobs

track_request_method[RW]

requests

user_method[RW]

Public Class Methods

clear_notes() click to toggle source
# File lib/notable.rb, line 64
def self.clear_notes
  Thread.current[:notable_notes] = nil
end
jobs_enabled?() click to toggle source
# File lib/notable.rb, line 39
def self.jobs_enabled?
  enabled && jobs_enabled
end
mask_ip(ip) click to toggle source
# File lib/notable.rb, line 111
def self.mask_ip(ip)
  addr = IPAddr.new(ip)
  if addr.ipv4?
    # set last octet to 0
    addr.mask(24).to_s
  else
    # set last 80 bits to zeros
    addr.mask(48).to_s
  end
end
monotonic_time() click to toggle source
# File lib/notable.rb, line 122
def self.monotonic_time
  Process.clock_gettime(Process::CLOCK_MONOTONIC)
end
notes() click to toggle source
# File lib/notable.rb, line 60
def self.notes
  Thread.current[:notable_notes] ||= []
end
requests_enabled?() click to toggle source
# File lib/notable.rb, line 35
def self.requests_enabled?
  enabled && requests_enabled
end
track(note_type, note = nil) click to toggle source
# File lib/notable.rb, line 52
def self.track(note_type, note = nil)
  notes << {note_type: note_type, note: note}
end
track_error(e) click to toggle source
# File lib/notable.rb, line 56
def self.track_error(e)
  track "Error", "#{e.class.name}: #{e.message}"
end
track_job(job, job_id, queue, created_at, slow_job_threshold = nil) { || ... } click to toggle source
# File lib/notable.rb, line 68
def self.track_job(job, job_id, queue, created_at, slow_job_threshold = nil)
  slow_job_threshold ||= Notable.slow_job_threshold
  exception = nil
  notes = nil
  started_at = Time.now # wall time
  start_time = monotonic_time
  begin
    yield
  rescue Exception => e
    exception = e
    track_error(e)
  ensure
    notes = Notable.notes
    Notable.clear_notes
  end
  runtime = monotonic_time - start_time

  Safely.safely do
    notes << {note_type: "Slow Job"} if runtime > slow_job_threshold

    if notes.any?
      created_at = Time.parse(created_at) if created_at.is_a?(String)
      queued_time = created_at ? [started_at - created_at, 0].max : nil
    end

    notes.each do |note|
      data = {
        note_type: note[:note_type],
        note: note[:note],
        job: job,
        job_id: job_id,
        queue: queue,
        runtime: runtime,
        queued_time: queued_time
      }

      Notable.track_job_method.call(data)
    end
  end

  raise exception if exception
end