-
Notifications
You must be signed in to change notification settings - Fork 2
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 #1203 from pulibrary/health_endpoint
Add Health Endpoint
- Loading branch information
Showing
7 changed files
with
75 additions
and
1 deletion.
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,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 |
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,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 |
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,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 |
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