-
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.
Merge pull request #47 from alphagov/healthcheck-classes
Health check convenience classes
- Loading branch information
Showing
19 changed files
with
578 additions
and
50 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
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,156 @@ | ||
# Health Checks | ||
|
||
## Check interface | ||
|
||
A check is expected to be a class with the following methods: | ||
|
||
```ruby | ||
class CustomCheck | ||
def name | ||
:the_name_of_the_check | ||
end | ||
|
||
def status | ||
if critical_condition? | ||
:critical | ||
elsif warning_condition? | ||
:warning | ||
else | ||
:ok | ||
end | ||
end | ||
|
||
# Optional | ||
def message | ||
"This is an optional custom message" | ||
end | ||
|
||
# Optional | ||
def details | ||
{ | ||
extra: "This is an optional details hash", | ||
} | ||
end | ||
|
||
# Optional | ||
def enabled? | ||
true # false if the check is not relevant at this time | ||
end | ||
end | ||
``` | ||
|
||
It is expected that these methods may cache their results for performance | ||
reasons, if a user wants to ensure they have the latest value they should | ||
create a new instance of the check first. | ||
|
||
## Including checks in your app | ||
|
||
Set up a route in your rack-compatible Ruby application, and pick the built-in | ||
or custom checks you wish to perform. | ||
|
||
For Rails apps: | ||
|
||
```ruby | ||
get "/healthcheck", to: GovukHealthcheck.rack_response( | ||
GovukHealthcheck::SidekiqRedis, | ||
GovukHealthcheck::ActiveRecord, | ||
CustomCheck, | ||
) | ||
``` | ||
|
||
## Built-in Checks | ||
|
||
A convention used when naming these classes is that it should end with `Check` | ||
if it must be subclassed to work, but a concrete class which works on its own | ||
doesn't need that suffix. You should aim to follow this convention in your own | ||
apps, ideally putting custom health checks into a `Healthcheck` module. | ||
|
||
### `SidekiqRedis` | ||
|
||
This checks that the app has a connection to Redis via Sidekiq. | ||
|
||
### `ActiveRecord` | ||
|
||
This checks that the app has a connection to the database via ActiveRecord. | ||
|
||
### `ThresholdCheck` | ||
|
||
This class is the basis for a check which compares a value with a warning or a | ||
critical threshold. | ||
|
||
```ruby | ||
class MyThresholdCheck < GovukHealthcheck::ThresholdCheck | ||
def name | ||
:my_threshold_check | ||
end | ||
|
||
def value | ||
# get the value to be checked | ||
end | ||
|
||
def total | ||
# (optional) get the total value to be included in the details as extra | ||
# information | ||
end | ||
|
||
def warning_threshold | ||
# if the value is above this threshold, its status is warning | ||
end | ||
|
||
def critical_threshold | ||
# if the value is above this threshold, its status is critical | ||
end | ||
end | ||
``` | ||
|
||
### `SidekiqQueueLatencyCheck` | ||
|
||
This class is the basis for a check which compares the Sidekiq queue latencies | ||
with warning or critical thresholds. | ||
|
||
```ruby | ||
class MySidekiqQueueLatencyCheck < GovukHealthcheck::SidekiqQueueLatencyCheck | ||
def warning_threshold(queue:) | ||
# the warning threshold for a particular queue | ||
end | ||
|
||
def critical_threshold(queue:) | ||
# the critical threshold for a particular queue | ||
end | ||
end | ||
``` | ||
|
||
### `SidekiqQueueSizeCheck` | ||
|
||
This class is the basis for a check which compares the Sidekiq queue sizes | ||
with warning or critical thresholds. | ||
|
||
```ruby | ||
class MySidekiqQueueSizeCheck < GovukHealthcheck::SidekiqQueueSizeCheck | ||
def warning_threshold(queue:) | ||
# the warning threshold for a particular queue | ||
end | ||
|
||
def critical_threshold(queue:) | ||
# the critical threshold for a particular queue | ||
end | ||
end | ||
``` | ||
|
||
|
||
### `SidekiqRetrySizeCheck` | ||
|
||
Similar to `SidekiqQueueSizeCheck`, this class is the basis for a check which | ||
compares the Sidekiq retry set size with a warning and critical threshold. | ||
|
||
```ruby | ||
class MySidekiqRetrySizeCheck < GovukHealthcheck::SidekiqRetrySizeCheck | ||
def warning_threshold | ||
# the warning threshold for the retry set | ||
end | ||
|
||
def critical_threshold | ||
# the critical threshold for the retry set | ||
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
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 |
11 changes: 11 additions & 0 deletions
11
lib/govuk_app_config/govuk_healthcheck/sidekiq_retry_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 SidekiqRetrySizeCheck < ThresholdCheck | ||
def name | ||
:sidekiq_retry_size | ||
end | ||
|
||
def value | ||
Sidekiq::Stats.new.retry_size | ||
end | ||
end | ||
end |
Oops, something went wrong.