Module: TalkToYourApp::Health
- Defined in:
- lib/talk_to_your_app/plugins/health/registry.rb
Overview
The operator-facing health-check registry. Host developers register named checks in Ruby; the Health plugin exposes them over MCP. Checks are plain callables owned by the host app — the gem does no scheduling, aggregation, alerting, or historical storage.
Put one check per file under app/talk_to_your_app/health/ (loaded automatically by the :health plugin); each file calls register:
TalkToYourApp::Health.register(:video_pipeline, description: "Encoder lag") do
{ status: :pass, value: encoder_lag_ratio }
end
Defined Under Namespace
Classes: Check
Class Method Summary collapse
- .checks ⇒ Object
- .clear! ⇒ Object
-
.normalize(result) ⇒ Object
Accepts a bare boolean or a { status:, value:, message: } hash.
-
.register(name, callable = nil, description: nil, &block) ⇒ Object
Registers a check by block or callable.
- .registered?(name) ⇒ Boolean
-
.run(name) ⇒ Object
Runs a check and returns a normalized result hash.
Class Method Details
.checks ⇒ Object
31 32 33 |
# File 'lib/talk_to_your_app/plugins/health/registry.rb', line 31 def checks @checks ||= {} end |
.clear! ⇒ Object
39 40 41 |
# File 'lib/talk_to_your_app/plugins/health/registry.rb', line 39 def clear! @checks = {} end |
.normalize(result) ⇒ Object
Accepts a bare boolean or a { status:, value:, message: } hash.
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/talk_to_your_app/plugins/health/registry.rb', line 55 def normalize(result) case result when true then { status: "pass" } when false then { status: "fail" } when Hash normalized = result.transform_keys(&:to_sym) normalized[:status] = normalized[:status].to_s if normalized[:status] normalized else { status: "pass", value: result } end end |
.register(name, callable = nil, description: nil, &block) ⇒ Object
Registers a check by block or callable. Re-registering a name replaces it and warns, so a typo’d duplicate is visible rather than silent.
22 23 24 25 26 27 28 29 |
# File 'lib/talk_to_your_app/plugins/health/registry.rb', line 22 def register(name, callable = nil, description: nil, &block) runnable = block || callable raise ArgumentError, "health check #{name.inspect} needs a block or callable" unless runnable key = name.to_sym warn("talk_to_your_app: health check #{key.inspect} was already registered; replacing it.") if checks.key?(key) checks[key] = Check.new(key, description, runnable) end |
.registered?(name) ⇒ Boolean
35 36 37 |
# File 'lib/talk_to_your_app/plugins/health/registry.rb', line 35 def registered?(name) checks.key?(name.to_sym) end |
.run(name) ⇒ Object
Runs a check and returns a normalized result hash. A raised exception is caught and reported as status “error” rather than crashing the request.
45 46 47 48 49 50 51 52 |
# File 'lib/talk_to_your_app/plugins/health/registry.rb', line 45 def run(name) check = checks[name.to_sym] return nil unless check normalize(check.callable.call) rescue StandardError => e { status: "error", error_class: e.class.name, message: e. } end |