Module: Notable

Defined in:
lib/notable.rb,
lib/notable/engine.rb,
lib/notable/version.rb,
app/models/notable/job.rb,
lib/notable/middleware.rb,
app/models/notable/request.rb,
lib/notable/job_extensions.rb,
lib/notable/debug_exceptions.rb,
lib/notable/validation_errors.rb,
lib/notable/unverified_request.rb,
lib/generators/notable/jobs_generator.rb,
lib/generators/notable/requests_generator.rb
more...

Defined Under Namespace

Modules: DebugExceptions, Generators, JobExtensions, UnverifiedRequest, ValidationErrors Classes: Engine, Job, Middleware, Request

Constant Summary collapse

VERSION =
"0.5.2"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.enabledObject

Returns the value of attribute enabled.


16
17
18
# File 'lib/notable.rb', line 16

def enabled
  @enabled
end

.jobs_enabledObject

Returns the value of attribute jobs_enabled.


18
19
20
# File 'lib/notable.rb', line 18

def jobs_enabled
  @jobs_enabled
end

.mask_ipsObject

Returns the value of attribute mask_ips.


24
25
26
# File 'lib/notable.rb', line 24

def mask_ips
  @mask_ips
end

.requests_enabledObject

Returns the value of attribute requests_enabled.


17
18
19
# File 'lib/notable.rb', line 17

def requests_enabled
  @requests_enabled
end

.slow_job_thresholdObject

Returns the value of attribute slow_job_threshold.


28
29
30
# File 'lib/notable.rb', line 28

def slow_job_threshold
  @slow_job_threshold
end

.slow_request_thresholdObject

Returns the value of attribute slow_request_threshold.


23
24
25
# File 'lib/notable.rb', line 23

def slow_request_threshold
  @slow_request_threshold
end

.track_job_methodObject

jobs


27
28
29
# File 'lib/notable.rb', line 27

def track_job_method
  @track_job_method
end

.track_request_methodObject

requests


21
22
23
# File 'lib/notable.rb', line 21

def track_request_method
  @track_request_method
end

.user_methodObject

Returns the value of attribute user_method.


22
23
24
# File 'lib/notable.rb', line 22

def user_method
  @user_method
end

Class Method Details

.clear_notesObject

[View source]

64
65
66
# File 'lib/notable.rb', line 64

def self.clear_notes
  Thread.current[:notable_notes] = nil
end

.jobs_enabled?Boolean

Returns:

  • (Boolean)
[View source]

39
40
41
# File 'lib/notable.rb', line 39

def self.jobs_enabled?
  enabled && jobs_enabled
end

.mask_ip(ip) ⇒ Object

[View source]

111
112
113
114
115
116
117
118
119
120
# 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_timeObject

[View source]

122
123
124
# File 'lib/notable.rb', line 122

def self.monotonic_time
  Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

.notesObject

[View source]

60
61
62
# File 'lib/notable.rb', line 60

def self.notes
  Thread.current[:notable_notes] ||= []
end

.requests_enabled?Boolean

Returns:

  • (Boolean)
[View source]

35
36
37
# File 'lib/notable.rb', line 35

def self.requests_enabled?
  enabled && requests_enabled
end

.track(note_type, note = nil) ⇒ Object

[View source]

52
53
54
# File 'lib/notable.rb', line 52

def self.track(note_type, note = nil)
  notes << {note_type: note_type, note: note}
end

.track_error(e) ⇒ Object

[View source]

56
57
58
# 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) ⇒ Object

[View source]

68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 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