Skip to content

Commit

Permalink
Merge pull request #161 from alphagov/new-healthchecks
Browse files Browse the repository at this point in the history
Add a couple of new healthchecks
  • Loading branch information
benthorner authored Oct 14, 2020
2 parents c7246db + 1b2e76a commit dba8f8d
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 2.4.0

* Add new GovukHealthcheck::Mongoid check
* Add new GovukHealthcheck::RailsCache check

# 2.3.0

* Remove unused SidekiqQueueSizeCheck healthcheck base class
Expand Down
9 changes: 9 additions & 0 deletions docs/healthchecks.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ 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.

### `RailsCache`

This checks that the Rails cache store, such as Memcached, is acessible by
writing and reading back a cache entry called "healthcheck-cache".

### `Mongoid`

This checks that the app has a connection to its Mongo database via Mongoid.

### `SidekiqRedis`

This checks that the app has a connection to Redis via Sidekiq.
Expand Down
1 change: 0 additions & 1 deletion govuk_app_config.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ Gem::Specification.new do |spec|
spec.add_dependency "statsd-ruby", "~> 1.4.0"
spec.add_dependency "unicorn", ">= 5.4", "< 5.8"

spec.add_development_dependency "bundler", "~> 1.15"
spec.add_development_dependency "climate_control"
spec.add_development_dependency "rack-test", "~> 1.1.0"
spec.add_development_dependency "rails", "~> 6"
Expand Down
2 changes: 2 additions & 0 deletions lib/govuk_app_config/govuk_healthcheck.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require "govuk_app_config/govuk_healthcheck/checkup"
require "govuk_app_config/govuk_healthcheck/active_record"
require "govuk_app_config/govuk_healthcheck/mongoid"
require "govuk_app_config/govuk_healthcheck/rails_cache"
require "govuk_app_config/govuk_healthcheck/sidekiq_redis"
require "govuk_app_config/govuk_healthcheck/threshold_check"
require "govuk_app_config/govuk_healthcheck/sidekiq_queue_check"
Expand Down
14 changes: 14 additions & 0 deletions lib/govuk_app_config/govuk_healthcheck/mongoid.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module GovukHealthcheck
class Mongoid
def name
:database_connectivity
end

def status
::Mongoid.default_client.database_names.any?
GovukHealthcheck::OK
rescue StandardError
GovukHealthcheck::CRITICAL
end
end
end
16 changes: 16 additions & 0 deletions lib/govuk_app_config/govuk_healthcheck/rails_cache.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module GovukHealthcheck
class RailsCache
def name
:rails_cache
end

def status
::Rails.cache.write("healthcheck-cache", true)
raise unless ::Rails.cache.read("healthcheck-cache")

GovukHealthcheck::OK
rescue StandardError
GovukHealthcheck::CRITICAL
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def self.should_monkey_patch_log_error?(clazz = ::ActionDispatch::DebugException

def self.monkey_patch_log_error(clazz = ::ActionDispatch::DebugExceptions)
clazz.class_eval do
private
private

def log_error(request, wrapper)
logger = logger(request)
Expand Down
2 changes: 1 addition & 1 deletion lib/govuk_app_config/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module GovukAppConfig
VERSION = "2.3.0".freeze
VERSION = "2.4.0".freeze
end
31 changes: 31 additions & 0 deletions spec/lib/govuk_healthcheck/mongoid_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require "spec_helper"
require "govuk_app_config/govuk_healthcheck"
require_relative "shared_interface"

RSpec.describe GovukHealthcheck::Mongoid do
let(:client) { double(:client, database_names: %w[db]) }
let(:mongoid) { double(:mongoid, default_client: client) }
before { stub_const("Mongoid", mongoid) }

describe ".status" do
context "when the database is connected" do
it_behaves_like "a healthcheck"

it "returns OK" do
expect(subject.status).to eq(GovukHealthcheck::OK)
end
end

context "when the database is not connected" do
before do
allow(client).to receive(:database_names) { raise }
end

it_behaves_like "a healthcheck"

it "returns CRITICAL" do
expect(subject.status).to eq(GovukHealthcheck::CRITICAL)
end
end
end
end
43 changes: 43 additions & 0 deletions spec/lib/govuk_healthcheck/rails_cache_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require "spec_helper"
require "govuk_app_config/govuk_healthcheck"
require_relative "shared_interface"

RSpec.describe GovukHealthcheck::RailsCache do
let(:cache) { double(:cache, write: true, read: true) }
let(:rails) { double(:mongoid, cache: cache) }
before { stub_const("Rails", rails) }

describe ".status" do
context "when the cache is available" do
it_behaves_like "a healthcheck"

it "returns OK" do
expect(subject.status).to eq(GovukHealthcheck::OK)
end
end

context "when the cache is silently unavailable" do
before do
allow(cache).to receive(:read) { false }
end

it_behaves_like "a healthcheck"

it "returns CRITICAL" do
expect(subject.status).to eq(GovukHealthcheck::CRITICAL)
end
end

context "when the cache is loudly unavailable" do
before do
allow(cache).to receive(:read) { raise }
end

it_behaves_like "a healthcheck"

it "returns CRITICAL" do
expect(subject.status).to eq(GovukHealthcheck::CRITICAL)
end
end
end
end

0 comments on commit dba8f8d

Please sign in to comment.