rails_health_checks 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d0f595984abf8a7db84183766d61918bdf684642b6ea67b2075afbda8f4cbae6
4
- data.tar.gz: ab35a0deb32d298eae8dadc04c39dbf8ca34b8e77018625ebad8ba81e1901f34
3
+ metadata.gz: 4e3f62376ecb6e658daaf39738dbb50a5eb6885b765af8766a74f748dafe9ac5
4
+ data.tar.gz: da0bcf07fdb7636c810e6656b99084d1554fbd3d4012d69560a8bd8474a05232
5
5
  SHA512:
6
- metadata.gz: 2f2db831794c15dbf59c6763ed65a092e9bc56db1dc87e35d4565cccd63303cfe9555a7b36f738e6a861086c0362f8eb98459e99ea46f5838bfeb3862130ebf5
7
- data.tar.gz: 3d5b057c32506961cf0416d37ea45c70f5218767961ddeed208b37887abd7781187fc1f480aff2e5fb522bfad8400e355fa745a04ec876a064e1aeb938e91c78
6
+ metadata.gz: eccd94c21587daabf39b10f6d98b5be36a632353b698538db2fb8e14be6e0dab9b0f4d175d9f8e896a4cde1faca45824036b4f4618e7bb8303e5e435064584a3
7
+ data.tar.gz: 92ac0ad412e0cc473cccaa16240bcf78b3a2c4cac017f15651386990009cd32b2bf82866d39d58ccd7fa287de88745f6fb84ee99df472c385a2525277dfefa93
data/README.md CHANGED
@@ -15,6 +15,9 @@ A Rails engine providing structured, pluggable health check endpoints for monito
15
15
  - [Configuration](#configuration)
16
16
  - [Authentication](#authentication)
17
17
  - [Built-in Checks](#built-in-checks)
18
+ - [Per-Environment Toggling](#per-environment-toggling)
19
+ - [Check Groups](#check-groups)
20
+ - [Custom Checks](#custom-checks)
18
21
  - [Contributing](#contributing)
19
22
  - [License](#license)
20
23
 
@@ -139,6 +142,69 @@ The block receives the `ActionDispatch::Request` object and must return a truthy
139
142
 
140
143
  ---
141
144
 
145
+ ## Per-Environment Toggling
146
+
147
+ Disable specific checks in specific environments:
148
+
149
+ ```ruby
150
+ RailsHealthChecks.configure do |config|
151
+ config.checks = [:database, :cache, :disk, :memory]
152
+ config.disable :disk, in: :test
153
+ config.disable :memory, in: [:test, :development]
154
+ end
155
+ ```
156
+
157
+ The check is removed from the active list only when `Rails.env` matches. The `in:` option accepts a single symbol or an array.
158
+
159
+ [↑ Back to top](#table-of-contents)
160
+
161
+ ---
162
+
163
+ ## Check Groups
164
+
165
+ Group related checks and expose them at a dedicated endpoint:
166
+
167
+ ```ruby
168
+ RailsHealthChecks.configure do |config|
169
+ config.group :system, [:disk, :memory]
170
+ config.group :workers, [:sidekiq, :good_job]
171
+ end
172
+ ```
173
+
174
+ | Endpoint | Description |
175
+ |----------|-------------|
176
+ | `GET /health/system` | Runs only `:disk` and `:memory`, same JSON shape as `GET /health` |
177
+ | `GET /health/workers` | Runs only `:sidekiq` and `:good_job` |
178
+
179
+ Unknown group names return `404 Not Found`.
180
+
181
+ [↑ Back to top](#table-of-contents)
182
+
183
+ ---
184
+
185
+ ## Custom Checks
186
+
187
+ Define a class inheriting from `RailsHealthChecks::Check` and register it in your initializer:
188
+
189
+ ```ruby
190
+ class MyApiCheck < RailsHealthChecks::Check
191
+ def call
192
+ res = Net::HTTP.get_response(URI("https://api.example.com/status"))
193
+ res.code == "200" ? pass : fail_with("API returned #{res.code}")
194
+ end
195
+ end
196
+
197
+ RailsHealthChecks.configure do |config|
198
+ config.register :my_api, MyApiCheck.new
199
+ end
200
+ ```
201
+
202
+ `config.register` automatically adds the check to the active checks list. Use `pass`, `warn_with`, and `fail_with` (inherited from `Check`) to set status, and `measure { }` to record latency.
203
+
204
+ [↑ Back to top](#table-of-contents)
205
+
206
+ ---
207
+
142
208
  ## Contributing
143
209
 
144
210
  1. Fork the repository
@@ -3,12 +3,12 @@
3
3
  module RailsHealthChecks
4
4
  class HealthController < ApplicationController
5
5
  def show
6
- builder = ResponseBuilder.new(run_checks)
6
+ builder = ResponseBuilder.new(run_checks(RailsHealthChecks.configuration.checks))
7
7
  render json: builder.to_json, status: builder.http_status
8
8
  end
9
9
 
10
10
  def live
11
- builder = ResponseBuilder.new(run_checks)
11
+ builder = ResponseBuilder.new(run_checks(RailsHealthChecks.configuration.checks))
12
12
  if builder.overall_status == "ok"
13
13
  render plain: "OK", status: :ok
14
14
  else
@@ -16,11 +16,20 @@ module RailsHealthChecks
16
16
  end
17
17
  end
18
18
 
19
+ def group
20
+ group_name = params[:group].to_sym
21
+ check_names = RailsHealthChecks.configuration.groups[group_name]
22
+ return render json: { error: "Group '#{group_name}' not found" }, status: :not_found unless check_names
23
+
24
+ builder = ResponseBuilder.new(run_checks(check_names))
25
+ render json: builder.to_json, status: builder.http_status
26
+ end
27
+
19
28
  private
20
29
 
21
- def run_checks
30
+ def run_checks(check_names)
22
31
  config = RailsHealthChecks.configuration
23
- checks = CheckRegistry.build(config.checks)
32
+ checks = CheckRegistry.build(check_names)
24
33
  CheckRegistry.run(checks, timeout: config.timeout)
25
34
  end
26
35
  end
data/config/routes.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  RailsHealthChecks::Engine.routes.draw do
4
- get "/", to: "health#show", as: :health
5
- get "/live", to: "health#live", as: :health_live
4
+ get "/", to: "health#show", as: :health
5
+ get "/live", to: "health#live", as: :health_live
6
+ get "/:group", to: "health#group", as: :health_group
6
7
  end
@@ -24,11 +24,16 @@ module RailsHealthChecks
24
24
  }.freeze
25
25
 
26
26
  def self.build(check_names)
27
+ custom = RailsHealthChecks.configuration.custom_checks
27
28
  check_names.each_with_object({}) do |name, hash|
28
- factory = BUILT_INS.fetch(name) do
29
- raise ArgumentError, "Unknown check: #{name}. Available: #{BUILT_INS.keys.join(', ')}"
29
+ if BUILT_INS.key?(name)
30
+ hash[name] = BUILT_INS[name].call
31
+ elsif custom.key?(name)
32
+ hash[name] = custom[name].dup
33
+ else
34
+ available = (BUILT_INS.keys + custom.keys).join(", ")
35
+ raise ArgumentError, "Unknown check: #{name}. Available: #{available}"
30
36
  end
31
- hash[name] = factory.call
32
37
  end
33
38
  end
34
39
 
@@ -2,10 +2,11 @@
2
2
 
3
3
  module RailsHealthChecks
4
4
  class Configuration
5
- attr_accessor :checks, :timeout, :allowed_ips, :token, :sidekiq_queue_size, :solid_queue_job_count, :good_job_latency,
5
+ attr_writer :checks
6
+ attr_accessor :timeout, :allowed_ips, :token, :sidekiq_queue_size, :solid_queue_job_count, :good_job_latency,
6
7
  :resque_queue_size, :disk_warn_threshold, :disk_critical_threshold, :disk_path,
7
8
  :memory_threshold, :http_url, :http_expected_status
8
- attr_reader :authenticate_block
9
+ attr_reader :authenticate_block, :custom_checks, :groups
9
10
 
10
11
  def initialize
11
12
  @checks = [:database]
@@ -23,10 +24,33 @@ module RailsHealthChecks
23
24
  @memory_threshold = nil
24
25
  @http_url = nil
25
26
  @http_expected_status = 200
27
+ @custom_checks = {}
28
+ @groups = {}
29
+ @disabled_checks = {}
30
+ end
31
+
32
+ def checks
33
+ disabled = @disabled_checks.filter_map { |name, envs| name if envs.include?(Rails.env.to_s) }
34
+ @checks - disabled
26
35
  end
27
36
 
28
37
  def authenticate(&block)
29
38
  @authenticate_block = block
30
39
  end
40
+
41
+ def disable(name, **opts)
42
+ envs = Array(opts.fetch(:in)).map(&:to_s)
43
+ @disabled_checks[name] ||= []
44
+ @disabled_checks[name].concat(envs)
45
+ end
46
+
47
+ def group(name, check_names)
48
+ @groups[name] = check_names
49
+ end
50
+
51
+ def register(name, check)
52
+ @custom_checks[name] = check
53
+ @checks << name unless @checks.include?(name)
54
+ end
31
55
  end
32
56
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsHealthChecks
4
- VERSION = "0.4.0"
4
+ VERSION = "0.5.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_health_checks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chuck Smith