sidekiq-monitoring 0.5.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +6 -0
  3. data/VERSION +1 -1
  4. data/lib/sidekiq-monitoring.rb +96 -23
  5. metadata +6 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b415451ae88923236fa1ec3b2371e5a14c9510d6
4
- data.tar.gz: 317a56005f5d0366cc8c2757d87a6bd82bf01b98
3
+ metadata.gz: ff9875b4a8c594e350b7fa0011370e3e6f2b85f5
4
+ data.tar.gz: 26b491d59ff14b95e8cc7d990bc3708cfc96c445
5
5
  SHA512:
6
- metadata.gz: df938fb0ed1ec27f90029ea5feccb36bc7d95b2441b79efe147256095abce6bc3accf66490abbbe7785ba79b2518aad278ec87ac70e87fb4fb25069cfe60a6e8
7
- data.tar.gz: 65251acce94105c8110ea045f5694797a985a40c8e3d70e1ef8dc9bb23b08c14295badb5b3310247c2cf83af021d16759cfb6b9045b5934829a6fba13b74d90c
6
+ metadata.gz: 4d8dd80f523151bc5dad5c48a2316e4cb70e8523dcc91ffbc9e55607af70792af081bc36afdf136c327ca49ab176a9b4e46e8103fd9757e326e025bc95801943
7
+ data.tar.gz: 63003606a81842ec0d2e8d6780f387dd3cfc0cc6a75901888721d8a2d8eb859fd4b709bf7db4f9afaa7fe3bb180cd3a7ee411a391643ddf315b283469e60c2c4
data/README.md CHANGED
@@ -28,6 +28,12 @@ SidekiqMonitoring.thresholds = {
28
28
  'queue_name_2' => [warning, critical],
29
29
  'queue_name_3' => [warning, critical]
30
30
  }
31
+
32
+ SidekiqMonitoring.latency_thresholds = {
33
+ 'queue_name_1' => [warning, critical],
34
+ 'queue_name_2' => [warning, critical],
35
+ 'queue_name_3' => [warning, critical]
36
+ }
31
37
  ```
32
38
 
33
39
  Or if you want to override the default threshold in case of the queue name
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.2
1
+ 1.0.0
@@ -3,20 +3,24 @@ require 'multi_json'
3
3
 
4
4
  class SidekiqMonitoring < Sinatra::Base
5
5
 
6
- # Set yours down thresholds configuration
6
+ # Set your down thresholds configuration
7
7
  # {'default' => [ 1_000, 2_000 ], 'low' => [ 10_000, 20_000 ] }
8
8
  def self.thresholds=(thresholds)
9
9
  @@thresholds = thresholds
10
10
  end
11
11
  @@thresholds = {}
12
12
 
13
+ def self.latency_thresholds=(latency_thresholds)
14
+ @@latency_thresholds = latency_thresholds
15
+ end
16
+ @@latency_thresholds = {}
17
+
13
18
  get '/sidekiq_queues' do
14
19
  content_type :json
15
20
  MultiJson.dump SidekiqMonitoring::Global.new(@@thresholds)
16
21
  end
17
22
 
18
- class Queue
19
-
23
+ module Monitorable
20
24
  ALERT_STATUS = {
21
25
  'OK' => 0,
22
26
  'WARNING' => 1,
@@ -24,34 +28,83 @@ class SidekiqMonitoring < Sinatra::Base
24
28
  'UNKNOWN' => 3
25
29
  }
26
30
 
27
- DEFAULT_THRESHOLD = [ 1_000, 2_000 ]
31
+ attr_accessor :warning_threshold, :critical_threshold, :status
32
+
33
+ def <=>(other)
34
+ ALERT_STATUS[status] <=> ALERT_STATUS[other.status]
35
+ end
28
36
 
29
- attr_accessor :name, :size, :warning_threshold, :critical_threshold, :status
37
+ end
30
38
 
31
- def as_json(options = {})
39
+ class Worker
40
+ include Monitorable
41
+
42
+ DEFAULT_THRESHOLD = [ 60, 120 ]
43
+
44
+ attr_accessor :process_id, :jid, :run_at, :queue, :worker_class
45
+
46
+ def initialize(process_id, jid, run_at, queue, worker_class, thresholds = nil)
47
+ @process_id = process_id
48
+ @jid = jid
49
+ @run_at = run_at
50
+ @queue = queue
51
+ @worker_class = worker_class
52
+ @warning_threshold, @critical_threshold = thresholds ? thresholds : DEFAULT_THRESHOLD
53
+ end
54
+
55
+ def as_json
32
56
  {
33
- 'name' => name,
34
- 'size' => size,
35
- 'warning_threshold' => warning_threshold,
36
- 'critical_threshold' => critical_threshold,
37
- 'status' => status
57
+ 'queue' => queue,
58
+ 'jid' => jid,
59
+ 'worker_class' => worker_class,
60
+ 'elapsed_time' => elapsed_time,
61
+ 'status' => status,
62
+ 'process_id' => process_id
38
63
  }
39
64
  end
40
65
 
41
- def initialize(name, size, thresholds = nil)
66
+ def elapsed_time
67
+ @elapsed_time ||= Time.now.to_i - run_at
68
+ end
69
+
70
+ def status
71
+ return 'CRITICAL' if elapsed_time >= critical_threshold
72
+ return 'WARNING' if elapsed_time >= warning_threshold
73
+ 'OK'
74
+ end
75
+
76
+ end
77
+
78
+ class Queue
79
+ include Monitorable
80
+
81
+ DEFAULT_THRESHOLD = [ 1_000, 2_000 ]
82
+ DEFAULT_LATENCY_THRESHOLD = [ 300, 900 ]
83
+
84
+ attr_accessor :name, :size, :latency, :warning_latency_threshold, :critical_latency_threshold
85
+
86
+ def initialize(name, size, latency, thresholds = nil, latency_thresholds = nil)
42
87
  @name = name
43
88
  @size = size
89
+ @latency = latency
44
90
  @warning_threshold, @critical_threshold = (thresholds ? thresholds : DEFAULT_THRESHOLD)
91
+ @warning_latency_threshold, @critical_latency_threshold = (latency_thresholds ? latency_thresholds : DEFAULT_LATENCY_THRESHOLD)
45
92
  @status = monitoring_status
46
93
  end
47
94
 
48
- def <=>(other)
49
- ALERT_STATUS[status] <=> ALERT_STATUS[other.status]
95
+ def as_json
96
+ {
97
+ 'name' => name,
98
+ 'size' => size,
99
+ 'warning_threshold' => warning_threshold,
100
+ 'critical_threshold' => critical_threshold,
101
+ 'status' => status
102
+ }
50
103
  end
51
104
 
52
105
  def monitoring_status
53
- return 'CRITICAL' if size >= critical_threshold
54
- return 'WARNING' if size >= warning_threshold
106
+ return 'CRITICAL' if size >= critical_threshold || latency >= critical_latency_threshold
107
+ return 'WARNING' if size >= warning_threshold || latency >= warning_latency_threshold
55
108
  'OK'
56
109
  end
57
110
 
@@ -59,33 +112,53 @@ class SidekiqMonitoring < Sinatra::Base
59
112
 
60
113
  class Global
61
114
 
62
- attr_accessor :thresholds
115
+ attr_accessor :thresholds, :latency_thresholds
63
116
 
64
117
  def as_json(options = {})
65
118
  {
66
119
  'global_status' => global_status,
67
- 'queues' => queues.map(&:as_json)
120
+ 'queues' => queues.map(&:as_json),
121
+ 'workers' => workers.map(&:as_json)
68
122
  }
69
123
  end
70
124
 
71
125
  def global_status
72
- @global_status ||= (queues.sort.last && queues.sort.last.status) || 'UNKNOWN'
126
+ queue_status = (queues.sort.last && queues.sort.last.status) || 'UNKNOWN'
127
+ worker_status = (workers.sort.last && workers.sort.last.status) || 'UNKNOWN'
128
+ status = if worker_status != 'UNKNOWN' && Monitorable::ALERT_STATUS[worker_status] > Monitorable::ALERT_STATUS[queue_status]
129
+ worker_status
130
+ else
131
+ queue_status
132
+ end
133
+ @global_status ||= status
73
134
  end
74
135
 
75
- def initialize(thresholds = {})
136
+ def initialize(thresholds = {}, latency_thresholds = {})
76
137
  @thresholds = thresholds
138
+ @latency_thresholds = latency_thresholds
77
139
  end
78
140
 
79
141
  def queues
80
- @queues ||= Sidekiq::Queue.all.collect{ |queue|
81
- Queue.new(queue.name, queue.size, thresholds_from_queue(queue.name))
82
- }
142
+ @queues ||= Sidekiq::Queue.all.map do |queue|
143
+ Queue.new(queue.name, queue.size, queue.latency, thresholds_from_queue(queue.name), latency_thresholds_from_queue(queue.name))
144
+ end
145
+ end
146
+
147
+ def workers
148
+ @workers ||= Sidekiq::Workers.new.map do |process_id, thread_id, work|
149
+ payload = work['payload']
150
+ Worker.new(process_id, payload['jid'], work['run_at'], work['queue'], payload['class'], latency_thresholds_from_queue(work['queue']))
151
+ end
83
152
  end
84
153
 
85
154
  def thresholds_from_queue(queue_name)
86
155
  (thresholds || {})[queue_name]
87
156
  end
88
157
 
158
+ def latency_thresholds_from_queue(queue_name)
159
+ (latency_thresholds || {})[queue_name]
160
+ end
161
+
89
162
  end
90
163
 
91
164
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sidekiq-monitoring
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
- - Jérémy Carlier
7
+ - Jeremy Carlier
8
8
  autorequire:
9
9
  bindir: ''
10
10
  cert_chain: []
11
- date: 2014-08-22 00:00:00.000000000 Z
11
+ date: 2016-03-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sinatra
@@ -90,7 +90,8 @@ files:
90
90
  - VERSION
91
91
  - lib/sidekiq-monitoring.rb
92
92
  homepage: http://github.com/dimelo/sidekiq-monitoring
93
- licenses: []
93
+ licenses:
94
+ - MIT
94
95
  metadata: {}
95
96
  post_install_message:
96
97
  rdoc_options: []
@@ -108,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
109
  version: '0'
109
110
  requirements: []
110
111
  rubyforge_project:
111
- rubygems_version: 2.4.1
112
+ rubygems_version: 2.5.1
112
113
  signing_key:
113
114
  specification_version: 4
114
115
  summary: Addons to provide a monitoring API for Sidekiq