sidekiq_queue_status 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 32426fcdf33231d81705398d85a9202f7a172f6a
4
- data.tar.gz: 30bd276ea5ece5a0abfbb330285527d407db13c0
3
+ metadata.gz: cc5fd855d926709a368cc99a1da6e19b2e7979f9
4
+ data.tar.gz: da788b27a27e1fd9572dfe06cdb50a99aa08329f
5
5
  SHA512:
6
- metadata.gz: d4fc67ee170af880100f568e93cbf84f5a2d29a28854495bcaad7ada6295db37f4115275d21ae5b9910d868ec351cd3faea3312b1bfd7b693cf45fa079ffb2a0
7
- data.tar.gz: 2d3b2e1e5011dfa927a1d32d99df7f7ed4c7908a2ea7586a4a1e18911e44812d5b6ebc2ff294fa10257877b64dd39e1e0eca2ca275034ee767f57bbf3d85c136
6
+ metadata.gz: f6f4775f9183af9637b917b8f0fe843473f40af3a6d1b43edecf60858c25ea4f961973b9b76b60d070cce45e94c560637de945d4d17642b88cfc98e69b57dbbc
7
+ data.tar.gz: c9244adf2de5e48c57da03f6bf5d75a4436e352681068edd9f3c4ef76a11df557b5f32f23535460b93398bd35b244a4ced098d0e40dcbf5196e567bf4ff8d588
data/README.md CHANGED
@@ -7,11 +7,21 @@ Lists latency for each sidekiq queue. The latency is the number of
7
7
  seconds the oldest job (next in line) is waiting to be performed.
8
8
 
9
9
  Under normal circumstances returns a `200 OK` status code. When latency
10
- is higher than the 30s default threshold, a `503 Service Unavailable` is
11
- returned.
10
+ is higher than the 30s default threshold, or when the failure rate
11
+ on the current day is higher than 10%, a `503 Service Unavailable`
12
+ is returned.
12
13
 
13
14
  Thresholds are configurable by setting `config.queue_tresholds` hash.
14
15
  Key is queue name, value is latency threshold in seconds.
16
+ Set `config.queue_failure_rate` to override the default failure rate.
17
+
18
+ ```ruby
19
+ # config/application.rb
20
+ Application.configure do
21
+ config.queue_tresholds['batch'] = 10.minutes
22
+ config.queue_failure_rate = 5
23
+ end
24
+ ```
15
25
 
16
26
  Response body is a JSON object, listing the latencies of all queues and
17
27
  an array of error messages explaining which queues triggered a threshold
@@ -21,9 +31,10 @@ error.
21
31
  {
22
32
  "latencies": {
23
33
  "batch": 200,
24
- "default": 1
34
+ "default": 1,
35
+ "failure_percentage": 25
25
36
  },
26
- "errors": ["Queue batch above threshold of 100"]
37
+ "errors": ["Queue batch above threshold of 100", "Failure rate above 5%"]
27
38
  }
28
39
  ```
29
40
 
@@ -3,14 +3,16 @@ require 'sidekiq/api'
3
3
  module SidekiqQueueStatus
4
4
  class Middleware
5
5
  DEFAULT_TRESHOLD = 30
6
+ DEFAULT_FAILURE_RATE_PERC = 10
6
7
  HEADERS = {
7
8
  'Content-Type' => 'application/json',
8
9
  'Cache-Control' => 'no-cache'
9
10
  }
10
11
 
11
- def initialize(app, configured_tresholds)
12
+ def initialize(app, configured_tresholds, failure_rate)
12
13
  @app = app
13
14
  @latency_treshold = Hash.new(DEFAULT_TRESHOLD).merge(configured_tresholds)
15
+ @max_failure_rate = failure_rate || DEFAULT_FAILURE_RATE_PERC
14
16
  end
15
17
 
16
18
  def call(env)
@@ -20,7 +22,9 @@ module SidekiqQueueStatus
20
22
  max_latency = @latency_treshold[name]
21
23
  errors << "Queue #{name} above threshold of #{max_latency}s" if latency > max_latency
22
24
  end
23
- body = { latencies: queues, errors: errors }.to_json
25
+ failure_rate = calculate_failure_rate
26
+ errors << "Failure rate above #{@max_failure_rate}%" if failure_rate > @max_failure_rate
27
+ body = { latencies: queues, failure_percentage: failure_rate, errors: errors }.to_json
24
28
  status = errors.any? ? 503 : 200
25
29
  [status, HEADERS, [body]]
26
30
  else
@@ -31,5 +35,12 @@ module SidekiqQueueStatus
31
35
  def queues
32
36
  Sidekiq::Queue.all.map { |q| [q.name, q.latency.to_i] }.to_h
33
37
  end
38
+
39
+ def calculate_failure_rate
40
+ stats = Sidekiq::Stats::History.new(1)
41
+ processed, failed = [stats.processed, stats.failed].map { |h| h.values.last }
42
+ val = (failed.to_f / processed.to_f) * 100.0
43
+ val > 0 ? val.to_i : 0
44
+ end
34
45
  end
35
46
  end
@@ -3,8 +3,9 @@ require 'rails/railtie'
3
3
  module SidekiqQueueStatus
4
4
  class Railtie < Rails::Railtie
5
5
  config.queue_tresholds = {}
6
+ config.queue_failure_rate = nil
6
7
  initializer "sidekiq_queue_status.configure_rails_initialization" do |app|
7
- app.middleware.use SidekiqQueueStatus::Middleware, app.config.queue_tresholds
8
+ app.middleware.use SidekiqQueueStatus::Middleware, app.config.queue_tresholds, app.config.queue_failure_rate
8
9
  end
9
10
  end
10
11
  end
@@ -1,3 +1,3 @@
1
1
  module SidekiqQueueStatus
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sidekiq_queue_status
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthijs Langenberg