Skip to content

Commit

Permalink
Merge pull request #1203 from pulibrary/health_endpoint
Browse files Browse the repository at this point in the history
Add Health Endpoint
  • Loading branch information
tpendragon authored Mar 7, 2024
2 parents 079057e + d8ca3d6 commit 5805cfd
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ source "https://rubygems.org"

gem "autoprefixer-rails"
gem "ddtrace", "~> 0.54"
gem "health-monitor-rails"
gem "jquery-rails"
gem "lograge"
gem "logstash-event"
Expand Down Expand Up @@ -39,6 +40,7 @@ end

group :test do
gem "axe-core-rspec"
gem "webmock"
end

gem "blacklight", "7.31.0"
Expand Down
12 changes: 11 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ GEM
deep_merge (~> 1.2, >= 1.2.1)
dry-validation (~> 1.0, >= 1.0.0)
connection_pool (2.4.1)
crack (0.4.5)
rexml
crass (1.0.6)
database_cleaner (2.0.1)
database_cleaner-active_record (~> 2.0.0)
Expand Down Expand Up @@ -320,6 +322,8 @@ GEM
tilt (>= 1.2)
hashdiff (1.0.1)
hashie (5.0.0)
health-monitor-rails (11.3.0)
railties (>= 6.1)
honeybadger (5.0.0)
httpclient (2.8.3)
i18n (1.14.1)
Expand Down Expand Up @@ -642,6 +646,10 @@ GEM
nokogiri (~> 1.6)
rubyzip (>= 1.3.0)
selenium-webdriver (~> 4.0, < 4.11)
webmock (3.19.1)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
webrick (1.7.0)
websocket (1.2.10)
websocket-driver (0.7.6)
Expand Down Expand Up @@ -683,6 +691,7 @@ DEPENDENCIES
geoblacklight (= 3.7.0)
geoblacklight_sidecar_images
google-cloud-storage (~> 1.11)
health-monitor-rails
honeybadger
jquery-rails
lograge
Expand Down Expand Up @@ -718,7 +727,8 @@ DEPENDENCIES
vite_rails
web-console
webdrivers
webmock
whenever

BUNDLED WITH
2.3.18
2.4.10
12 changes: 12 additions & 0 deletions app/checks/solr_status.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true
class SolrStatus < HealthMonitor::Providers::Base
def check!
uri = Blacklight.default_index.connection.uri
status_uri = URI(uri.to_s.gsub(uri.path, "/solr/admin/cores?action=STATUS"))
req = Net::HTTP::Get.new(status_uri)
req.basic_auth(uri.user, uri.password) if uri.user && uri.password
response = Net::HTTP.start(uri.hostname, uri.port) { |http| http.request(req) }
json = JSON.parse(response.body)
raise "The solr has an invalid status #{status_uri}" if json["responseHeader"]["status"] != 0
end
end
17 changes: 17 additions & 0 deletions config/initializers/health_monitor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true
Rails.application.config.after_initialize do
HealthMonitor.configure do |config|
config.cache
config.redis unless Rails.env.test?

config.add_custom_provider(SolrStatus)

# Make this health check available at /health
config.path = :health

config.error_callback = proc do |e|
Rails.logger.error "Health check failed with: #{e.message}"
Honeybadger.notify(e)
end
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

Rails.application.routes.draw do
mount HealthMonitor::Engine, at: "/"
concern :range_searchable, BlacklightRangeLimit::Routes::RangeSearchable.new
mount Blacklight::Engine => "/"
mount BlacklightAdvancedSearch::Engine => "/"
Expand Down
25 changes: 25 additions & 0 deletions spec/requests/health_check_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true
require "rails_helper"

RSpec.describe "Health Check", type: :request do
describe "GET /health" do
it "has a health check" do
get "/health.json"
expect(response).to be_successful
end

it "errors when a service is down" do
allow(Blacklight.default_index.connection).to receive(:uri).and_return(URI("http://example.com/bla"))
stub_request(:get, "http://example.com/solr/admin/cores?action=STATUS").to_return(
body: { responseHeader: { status: 500 } }.to_json, headers: { "Content-Type" => "text/json" }
)

get "/health.json"

expect(response).not_to be_successful
expect(response.status).to eq 503
solr_response = JSON.parse(response.body)["results"].find { |x| x["name"] == "SolrStatus" }
expect(solr_response["message"]).to start_with "The solr has an invalid status"
end
end
end
7 changes: 7 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

ENV["RACK_ENV"] = "test"
require "webmock/rspec"
require "simplecov"
require "capybara/rspec"
require "capybara-screenshot/rspec"
Expand Down Expand Up @@ -32,3 +33,9 @@
mocks.verify_partial_doubles = true
end
end

WebMock.disable_net_connect!(allow_localhost: true,
net_http_connect_on_start: true,
allow: ["chromedriver.storage.googleapis.com", "googlechromelabs.github.io",
"storage.googleapis.com",
"edgedl.me.gvt1.com"])

0 comments on commit 5805cfd

Please sign in to comment.