-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Sidekiq queue size and queue latency checks
- Loading branch information
1 parent
0f9c330
commit 671596b
Showing
9 changed files
with
262 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
lib/govuk_app_config/govuk_healthcheck/sidekiq_queue_check.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
module GovukHealthcheck | ||
class SidekiqQueueCheck | ||
def status | ||
queues.each do |name, value| | ||
if value >= critical_threshold(queue: name) | ||
return :critical | ||
elsif value >= warning_threshold(queue: name) | ||
return :warning | ||
end | ||
end | ||
|
||
:ok | ||
end | ||
|
||
def message | ||
messages = queues.map do |name, value| | ||
critical = critical_threshold(queue: name) | ||
warning = warning_threshold(queue: name) | ||
|
||
if value >= critical | ||
"#{name} (#{value}) is above the critical threshold (#{critical})" | ||
elsif value >= warning | ||
"#{name} (#{value}) is above the warning threshold (#{warning})" | ||
end | ||
end | ||
|
||
messages = messages.compact | ||
|
||
if messages.empty? | ||
"all queues are below the critical and warning thresholds" | ||
else | ||
messages.join("\n") | ||
end | ||
end | ||
|
||
def details | ||
{ | ||
queues: queues.each_with_object({}) do |(name, value), hash| | ||
hash[name] = { | ||
value: value, | ||
thresholds: { | ||
critical: critical_threshold(queue: name), | ||
warning: warning_threshold(queue: name), | ||
}, | ||
} | ||
end, | ||
} | ||
end | ||
|
||
def queues | ||
raise "This method must be overriden to be a hash of queue names and data." | ||
end | ||
|
||
def critical_threshold(queue:) | ||
raise "This method must be overriden to be the critical threshold." | ||
end | ||
|
||
def warning_threshold(queue:) | ||
raise "This method must be overriden to be the warning threshold." | ||
end | ||
end | ||
end |
13 changes: 13 additions & 0 deletions
13
lib/govuk_app_config/govuk_healthcheck/sidekiq_queue_latency_check.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module GovukHealthcheck | ||
class SidekiqQueueLatencyCheck < SidekiqQueueCheck | ||
def name | ||
:sidekiq_queue_latency | ||
end | ||
|
||
def queues | ||
@queues ||= Sidekiq::Stats.new.queues.keys.each_with_object({}) do |name, hash| | ||
hash[name] = Sidekiq::Queue.new(name).latency | ||
end | ||
end | ||
end | ||
end |
11 changes: 11 additions & 0 deletions
11
lib/govuk_app_config/govuk_healthcheck/sidekiq_queue_size_check.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module GovukHealthcheck | ||
class SidekiqQueueSizeCheck < SidekiqQueueCheck | ||
def name | ||
:sidekiq_queue_size | ||
end | ||
|
||
def queues | ||
@queues ||= Sidekiq::Stats.new.queues | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
require "spec_helper" | ||
require "govuk_app_config/govuk_healthcheck" | ||
require_relative "shared_interface" | ||
|
||
RSpec.describe GovukHealthcheck::SidekiqQueueCheck do | ||
context "an ok check" do | ||
subject { TestQueueCheck.new({ queue: 0 }, 10, 20) } | ||
|
||
it_behaves_like "a healthcheck" | ||
|
||
its(:status) { is_expected.to eq(:ok) } | ||
its(:message) { is_expected.to match(/below the critical and warning thresholds/) } | ||
its(:details) do | ||
is_expected.to match( | ||
queues: { | ||
queue: hash_including(value: 0, thresholds: { warning: 10, critical: 20 }) | ||
} | ||
) | ||
end | ||
end | ||
|
||
context "a warning check" do | ||
subject { TestQueueCheck.new({ queue: 11 }, 10, 20) } | ||
|
||
it_behaves_like "a healthcheck" | ||
|
||
its(:status) { is_expected.to eq(:warning) } | ||
its(:message) { is_expected.to match(/above the warning threshold/) } | ||
its(:details) do | ||
is_expected.to match( | ||
queues: { | ||
queue: hash_including(value: 11, thresholds: { warning: 10, critical: 20 }) | ||
} | ||
) | ||
end | ||
end | ||
|
||
context "a critical check" do | ||
subject { TestQueueCheck.new({ queue: 21 }, 10, 20) } | ||
|
||
it_behaves_like "a healthcheck" | ||
|
||
its(:status) { is_expected.to eq(:critical) } | ||
its(:message) { is_expected.to match(/above the critical threshold/) } | ||
its(:details) do | ||
is_expected.to match( | ||
queues: { | ||
queue: hash_including(value: 21, thresholds: { warning: 10, critical: 20 }) | ||
} | ||
) | ||
end | ||
end | ||
|
||
class TestQueueCheck < GovukHealthcheck::SidekiqQueueCheck | ||
def initialize(queues, warning_threshold, critical_threshold, name = :test) | ||
@queues = queues | ||
@warning_threshold = warning_threshold | ||
@critical_threshold = critical_threshold | ||
@name = name | ||
end | ||
|
||
attr_reader :queues, :name | ||
|
||
def warning_threshold(queue:) | ||
@warning_threshold | ||
end | ||
|
||
def critical_threshold(queue:) | ||
@critical_threshold | ||
end | ||
end | ||
end |
33 changes: 33 additions & 0 deletions
33
spec/lib/govuk_healthcheck/sidekiq_queue_latency_check_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require "spec_helper" | ||
require "govuk_app_config/govuk_healthcheck" | ||
require_relative "shared_interface" | ||
|
||
RSpec.describe GovukHealthcheck::SidekiqQueueLatencyCheck do | ||
subject { TestQueueLatencyCheck.new } | ||
|
||
let(:sidekiq_stats) { double(queues: { test: 10 }) } | ||
let(:sidekiq_queue) { double(latency: 5) } | ||
|
||
let(:sidekiq_stats_class) { double } | ||
let(:sidekiq_queue_class) { double } | ||
|
||
before do | ||
allow(sidekiq_stats_class).to receive(:new).and_return(sidekiq_stats) | ||
allow(sidekiq_queue_class).to receive(:new).with(:test).and_return(sidekiq_queue) | ||
|
||
stub_const("Sidekiq::Stats", sidekiq_stats_class) | ||
stub_const("Sidekiq::Queue", sidekiq_queue_class) | ||
end | ||
|
||
it_behaves_like "a healthcheck" | ||
|
||
class TestQueueLatencyCheck < GovukHealthcheck::SidekiqQueueLatencyCheck | ||
def warning_threshold(queue:) | ||
10 | ||
end | ||
|
||
def critical_threshold(queue:) | ||
20 | ||
end | ||
end | ||
end |
27 changes: 27 additions & 0 deletions
27
spec/lib/govuk_healthcheck/sidekiq_queue_size_check_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require "spec_helper" | ||
require "govuk_app_config/govuk_healthcheck" | ||
require_relative "shared_interface" | ||
|
||
RSpec.describe GovukHealthcheck::SidekiqQueueSizeCheck do | ||
subject { TestQueueSizeCheck.new } | ||
|
||
let(:sidekiq_stats) { double(queues: { test: 10 }) } | ||
let(:sidekiq_stats_class) { double } | ||
|
||
before do | ||
allow(sidekiq_stats_class).to receive(:new).and_return(sidekiq_stats) | ||
stub_const("Sidekiq::Stats", sidekiq_stats_class) | ||
end | ||
|
||
it_behaves_like "a healthcheck" | ||
|
||
class TestQueueSizeCheck < GovukHealthcheck::SidekiqQueueSizeCheck | ||
def warning_threshold(queue:) | ||
100 | ||
end | ||
|
||
def critical_threshold(queue:) | ||
200 | ||
end | ||
end | ||
end |