Module: StandardCircuit::Health

Defined in:
lib/standard_circuit/health.rb

Overview

Health-reporting helpers that inspect the Runner’s Stoplight Light cache and the Config’s registered circuits / prefixes and return structured snapshots plus an overall health status.

Intended for mounting in a Rails HealthController. Prefer health_report over calling health_snapshot and health_overall separately — the combined call takes a single atomic snapshot, so the rendered status and circuits always describe the same moment:

report = StandardCircuit.health_report
render json: report, status: (report[:status] == :critical ? 503 : 200)

Class Method Summary collapse

Class Method Details

.overall(snapshot) ⇒ Object

Roll the snapshot up to :ok | :degraded | :critical.

  • any :critical circuit RED -> :critical

  • any :critical circuit YELLOW -> :degraded

  • any :standard circuit RED -> :degraded

  • otherwise -> :ok

:optional circuits never elevate the overall state.



41
42
43
44
45
46
47
48
49
50
# File 'lib/standard_circuit/health.rb', line 41

def overall(snapshot)
  return :critical if snapshot.any? { |e| e[:criticality] == :critical && e[:color] == "red" }

  degraded = snapshot.any? do |e|
    (e[:criticality] == :critical && e[:color] == "yellow") ||
      (e[:criticality] == :standard && e[:color] == "red")
  end

  degraded ? :degraded : :ok
end

.snapshot(runner, config) ⇒ Object

Build a snapshot of every relevant circuit.

Includes: every named circuit registered via Config#register (lights are eagerly built if not yet cached so the snapshot reflects real state instead of “never exercised”); plus every circuit already present in the runner’s light cache that was matched via a prefix registration.

Prefix-registered circuits that have never been exercised are not enumerable and are therefore omitted.



26
27
28
29
30
31
# File 'lib/standard_circuit/health.rb', line 26

def snapshot(runner, config)
  entries = named_entries(runner, config) + prefix_entries(runner, config)
  # Dedupe by name — a named circuit might also match a prefix; the named
  # registration wins and we keep its entry.
  entries.uniq { |entry| entry[:name] }
end