standard_circuit 0.6.0 → 0.7.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 +4 -4
- data/CHANGELOG.md +81 -0
- data/README.md +181 -3
- data/lib/standard_circuit/config.rb +163 -3
- data/lib/standard_circuit/dependency.rb +272 -0
- data/lib/standard_circuit/dependency_spec.rb +165 -0
- data/lib/standard_circuit/health.rb +19 -13
- data/lib/standard_circuit/notifier_bridge.rb +8 -3
- data/lib/standard_circuit/runner.rb +176 -36
- data/lib/standard_circuit/stats.rb +154 -0
- data/lib/standard_circuit/subscribers.rb +2 -1
- data/lib/standard_circuit/version.rb +1 -1
- data/lib/standard_circuit.rb +35 -2
- metadata +14 -5
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
module StandardCircuit
|
|
2
|
+
# Runtime view of one dependency, returned by `StandardCircuit.dependency`.
|
|
3
|
+
# Cheap to build: it keeps the definition it was built from and reads
|
|
4
|
+
# breaker state from the live runner on every call. After a `configure`
|
|
5
|
+
# that changes the dependency, fetch a fresh handle; one kept from before
|
|
6
|
+
# still describes the old purposes and breakers.
|
|
7
|
+
#
|
|
8
|
+
# State is whatever the data store holds: per process with the default
|
|
9
|
+
# Memory store, shared across processes with Redis (see README).
|
|
10
|
+
class Dependency
|
|
11
|
+
STATUS_RANK = { ok: 0, degraded: 1, unavailable: 2 }.freeze
|
|
12
|
+
COLOR_RANK = { "green" => 0, "yellow" => 1, "red" => 2 }.freeze
|
|
13
|
+
|
|
14
|
+
# A forced override wins over the light's color, as it does for `run`.
|
|
15
|
+
def self.effective_color(state)
|
|
16
|
+
case state[:forced]
|
|
17
|
+
when :open then "red"
|
|
18
|
+
when :closed then "green"
|
|
19
|
+
else state[:color]
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def self.worst_color(states)
|
|
24
|
+
states.map { |state| effective_color(state) }.max_by { |color| COLOR_RANK.fetch(color, 0) } || "green"
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
attr_reader :spec, :runner, :config, :stats_store
|
|
28
|
+
|
|
29
|
+
def initialize(spec, runner, config, stats_store: StandardCircuit.stats)
|
|
30
|
+
@spec = spec
|
|
31
|
+
@runner = runner
|
|
32
|
+
@config = config
|
|
33
|
+
@stats_store = stats_store
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def name
|
|
37
|
+
@spec.name
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# The worst criticality among the purposes.
|
|
41
|
+
def criticality
|
|
42
|
+
@spec.criticality
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def prefix?
|
|
46
|
+
@spec.prefix?
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def purposes
|
|
50
|
+
@spec.purposes.map { |purpose_spec| Purpose.new(self, purpose_spec) }
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Raises UnknownPurpose for a purpose the dependency does not declare.
|
|
54
|
+
def purpose(name)
|
|
55
|
+
Purpose.new(self, @spec.purpose(name))
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Names of the circuits (breakers) behind this dependency. For a prefix
|
|
59
|
+
# registration these are the `<prefix>_*` circuits used so far in this
|
|
60
|
+
# process — they can't be enumerated any other way (see Health).
|
|
61
|
+
def circuit_names
|
|
62
|
+
return @spec.circuits.keys unless prefix?
|
|
63
|
+
|
|
64
|
+
@runner.cached_lights.keys.select do |circuit|
|
|
65
|
+
!@config.circuits.key?(circuit) && @config.run_target(circuit).dependency == name
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Current state of every circuit behind the dependency:
|
|
70
|
+
# `{ name:, color:, locked:, forced: }` (see Runner#circuit_state).
|
|
71
|
+
def circuits
|
|
72
|
+
circuit_names.map { |circuit| @runner.circuit_state(circuit) }
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# True when every purpose is metrics-only (see Config#dependency).
|
|
76
|
+
def metrics_only?
|
|
77
|
+
purposes.all?(&:metrics_only?)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Rolling stats over the dependency's recent calls, all purposes
|
|
81
|
+
# together. See Stats.summarize for the shape; `calls: 0` and nil rates
|
|
82
|
+
# mean no evidence yet, not health.
|
|
83
|
+
def stats
|
|
84
|
+
@stats_store.for_dependency(name)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# One row of StandardCircuit.dependency_report. Circuit state is read
|
|
88
|
+
# once and every field is derived from that read, so status, open and
|
|
89
|
+
# the purposes can't describe different moments.
|
|
90
|
+
def report
|
|
91
|
+
states = circuits
|
|
92
|
+
by_name = states.to_h { |state| [ state[:name], state ] }
|
|
93
|
+
purpose_rows = purposes.map do |purpose|
|
|
94
|
+
purpose.report(prefix? ? states : by_name.values_at(*purpose.circuit_names).compact)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
{
|
|
98
|
+
name: name,
|
|
99
|
+
status: purpose_rows.map { |row| row[:status] }.max_by { |status| STATUS_RANK.fetch(status) },
|
|
100
|
+
criticality: criticality,
|
|
101
|
+
open: purpose_rows.all? { |row| row[:open] },
|
|
102
|
+
metrics_only: purpose_rows.all? { |row| row[:metrics_only] },
|
|
103
|
+
stats: stats,
|
|
104
|
+
purposes: purpose_rows,
|
|
105
|
+
circuits: states
|
|
106
|
+
}
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# True when no purpose can be served: every purpose's breaker is open
|
|
110
|
+
# (red, or forced open). For the usual one-breaker dependency that is
|
|
111
|
+
# simply "its breaker is open". Half-open (yellow) is not open: calls go
|
|
112
|
+
# through as recovery probes.
|
|
113
|
+
def open?
|
|
114
|
+
purposes.all?(&:open?)
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# :ok | :degraded | :unavailable — the worst purpose's status. Each
|
|
118
|
+
# purpose applies the health_report rule to its criticality and breaker
|
|
119
|
+
# color: critical + red is unavailable; critical + yellow or standard +
|
|
120
|
+
# red is degraded; optional never elevates.
|
|
121
|
+
def status
|
|
122
|
+
purposes.map(&:status).max_by { |status| STATUS_RANK.fetch(status) }
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# Lock every breaker behind the dependency open (calls raise RedLight, or
|
|
126
|
+
# return their fallback) or closed (calls always go through) until
|
|
127
|
+
# #unlock!. For a prefix dependency that covers the sub-circuits used so
|
|
128
|
+
# far in this process.
|
|
129
|
+
def lock_open!
|
|
130
|
+
circuit_names.each { |circuit| @runner.lock(circuit, :open) }
|
|
131
|
+
self
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def lock_closed!
|
|
135
|
+
circuit_names.each { |circuit| @runner.lock(circuit, :closed) }
|
|
136
|
+
self
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def unlock!
|
|
140
|
+
circuit_names.each { |circuit| @runner.unlock(circuit) }
|
|
141
|
+
self
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def inspect
|
|
145
|
+
"#<#{self.class.name} #{name.inspect} purposes=#{@spec.purpose_names.inspect}>"
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# One declared reason to call a dependency.
|
|
149
|
+
class Purpose
|
|
150
|
+
attr_reader :dependency, :spec
|
|
151
|
+
|
|
152
|
+
def initialize(dependency, spec)
|
|
153
|
+
@dependency = dependency
|
|
154
|
+
@spec = spec
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
def name
|
|
158
|
+
@spec.name
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def criticality
|
|
162
|
+
@spec.criticality
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# The circuit guarding calls for this purpose: the dependency's shared
|
|
166
|
+
# breaker, or the purpose's own when isolated. nil for a prefix
|
|
167
|
+
# dependency, whose circuit depends on the name each call uses.
|
|
168
|
+
def circuit_name
|
|
169
|
+
@spec.circuit
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
def isolated?
|
|
173
|
+
@spec.isolated?
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def default?
|
|
177
|
+
@spec.default?
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# The purpose's breaker; for a prefix dependency, its sub-circuits.
|
|
181
|
+
def circuit_names
|
|
182
|
+
@spec.circuit ? [ @spec.circuit ] : dependency.circuit_names
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def circuits
|
|
186
|
+
circuit_names.map { |circuit| dependency.runner.circuit_state(circuit) }
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
# Calls are timed and recorded but never blocked (`metrics_only: true`
|
|
190
|
+
# on the dependency, or on an isolated purpose).
|
|
191
|
+
def metrics_only?
|
|
192
|
+
spec = dependency.prefix? ? dependency.config.prefixes[dependency.spec.prefix] : dependency.config.spec_for(circuit_name)
|
|
193
|
+
spec&.metrics_only || false
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
# The breaker's color with forced overrides applied: "green" | "yellow"
|
|
197
|
+
# | "red". The worst sub-circuit's for a prefix dependency. Always
|
|
198
|
+
# "green" when metrics-only: such a breaker never blocks a call.
|
|
199
|
+
def color
|
|
200
|
+
color_for(circuits)
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
# True while the breaker is open (red, or forced open). A prefix
|
|
204
|
+
# dependency's purpose is open only when every sub-circuit used so far
|
|
205
|
+
# is, and never before one has been used. Never true when metrics-only.
|
|
206
|
+
def open?
|
|
207
|
+
open_for(circuits)
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
# Rolling stats over this purpose's recent calls (see Dependency#stats).
|
|
211
|
+
def stats
|
|
212
|
+
dependency.stats_store.for_purpose(dependency.name, name)
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
# +states+: this purpose's circuit states, when the caller already has them.
|
|
216
|
+
def report(states = circuits)
|
|
217
|
+
{
|
|
218
|
+
name: name,
|
|
219
|
+
criticality: criticality,
|
|
220
|
+
status: status_for(states),
|
|
221
|
+
open: open_for(states),
|
|
222
|
+
isolated: isolated?,
|
|
223
|
+
metrics_only: metrics_only?,
|
|
224
|
+
circuits: circuit_names,
|
|
225
|
+
stats: stats
|
|
226
|
+
}
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
# :ok | :degraded | :unavailable, from criticality and #color.
|
|
230
|
+
def status
|
|
231
|
+
status_for(circuits)
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
# Locks this purpose's breaker. A purpose that shares the dependency's
|
|
235
|
+
# breaker locks it for every purpose sharing it.
|
|
236
|
+
def lock_open!
|
|
237
|
+
circuit_names.each { |circuit| dependency.runner.lock(circuit, :open) }
|
|
238
|
+
self
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
def lock_closed!
|
|
242
|
+
circuit_names.each { |circuit| dependency.runner.lock(circuit, :closed) }
|
|
243
|
+
self
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
def unlock!
|
|
247
|
+
circuit_names.each { |circuit| dependency.runner.unlock(circuit) }
|
|
248
|
+
self
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
def inspect
|
|
252
|
+
"#<#{self.class.name} #{dependency.name.inspect}/#{name.inspect} criticality=#{criticality.inspect}>"
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
private
|
|
256
|
+
|
|
257
|
+
def color_for(states)
|
|
258
|
+
metrics_only? ? "green" : Dependency.worst_color(states)
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
def open_for(states)
|
|
262
|
+
return false if metrics_only?
|
|
263
|
+
|
|
264
|
+
states.any? && states.all? { |state| Dependency.effective_color(state) == "red" }
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
def status_for(states)
|
|
268
|
+
Health.severity(criticality, color_for(states))
|
|
269
|
+
end
|
|
270
|
+
end
|
|
271
|
+
end
|
|
272
|
+
end
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
module StandardCircuit
|
|
2
|
+
# Config-time definition of one external service the app depends on (see
|
|
3
|
+
# `Config#dependency`). A dependency has one or more *purposes*, the declared
|
|
4
|
+
# reasons the app calls it, and each purpose carries a criticality. Purposes
|
|
5
|
+
# share the dependency's breaker unless declared `isolate: true`, which gives
|
|
6
|
+
# the purpose a breaker of its own, named `<dependency>_<purpose>`.
|
|
7
|
+
#
|
|
8
|
+
# A dependency declared without purposes has one implicit purpose,
|
|
9
|
+
# `:default`, with the dependency's criticality. `register` / `register_prefix`
|
|
10
|
+
# produce exactly that shape, so every circuit registered the old way is a
|
|
11
|
+
# dependency too.
|
|
12
|
+
#
|
|
13
|
+
# Immutable once built; `Config` replaces the whole spec on re-registration.
|
|
14
|
+
class DependencySpec
|
|
15
|
+
DEFAULT_PURPOSE = :default
|
|
16
|
+
BREAKER_OPTIONS = [ :threshold, :cool_off_time, :window_size, :tracked_errors, :skipped_errors, :metrics_only ].freeze
|
|
17
|
+
CRITICALITY_RANK = { optional: 0, standard: 1, critical: 2 }.freeze
|
|
18
|
+
|
|
19
|
+
PurposeSpec = Data.define(:name, :criticality, :circuit, :isolated, :default) do
|
|
20
|
+
def isolated? = isolated
|
|
21
|
+
def default? = default
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
attr_reader :name, :prefix, :purposes, :circuits
|
|
25
|
+
|
|
26
|
+
# Build from the `Config#dependency` DSL. +opts+ are the breaker options
|
|
27
|
+
# `register` takes (threshold, cool_off_time, window_size, tracked_errors,
|
|
28
|
+
# skipped_errors, metrics_only); +criticality+ is the default for every
|
|
29
|
+
# purpose.
|
|
30
|
+
def self.define(name, criticality: Config::DEFAULT_CRITICALITY, **opts)
|
|
31
|
+
name = name.to_sym
|
|
32
|
+
validate_criticality!(criticality, "dependency #{name.inspect}")
|
|
33
|
+
validate_breaker_options!(opts, "dependency #{name.inspect}")
|
|
34
|
+
|
|
35
|
+
builder = Builder.new(name, criticality)
|
|
36
|
+
yield builder if block_given?
|
|
37
|
+
declared = builder.declared
|
|
38
|
+
declared = [ Builder::Declared.new(DEFAULT_PURPOSE, criticality, false, true, {}) ] if declared.empty?
|
|
39
|
+
|
|
40
|
+
shared = declared.reject(&:isolate)
|
|
41
|
+
circuits = {}
|
|
42
|
+
circuits[name] = Config::CircuitSpec.build(**opts, criticality: worst(shared.map(&:criticality))) if shared.any?
|
|
43
|
+
|
|
44
|
+
purposes = declared.map do |purpose|
|
|
45
|
+
circuit = purpose.isolate ? :"#{name}_#{purpose.name}" : name
|
|
46
|
+
if purpose.isolate
|
|
47
|
+
circuits[circuit] = Config::CircuitSpec.build(**opts, **purpose.breaker, criticality: purpose.criticality)
|
|
48
|
+
end
|
|
49
|
+
PurposeSpec.new(name: purpose.name, criticality: purpose.criticality, circuit: circuit,
|
|
50
|
+
isolated: purpose.isolate, default: purpose.default)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
new(name: name, purposes: purposes, circuits: circuits)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# The dependency a plain `register(name, ...)` stands for.
|
|
57
|
+
def self.for_circuit(name, spec)
|
|
58
|
+
name = name.to_sym
|
|
59
|
+
new(name: name, purposes: [ implicit_purpose(spec, name) ], circuits: { name => spec })
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# The dependency a `register_prefix(prefix, ...)` stands for. Its circuits
|
|
63
|
+
# (`<prefix>_<suffix>`) are only known once they have been used.
|
|
64
|
+
def self.for_prefix(prefix, spec)
|
|
65
|
+
new(name: prefix.to_sym, purposes: [ implicit_purpose(spec, nil) ], circuits: {}, prefix: prefix.to_s)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def self.implicit_purpose(spec, circuit)
|
|
69
|
+
PurposeSpec.new(name: DEFAULT_PURPOSE, criticality: spec.criticality, circuit: circuit, isolated: false, default: true)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def self.worst(criticalities)
|
|
73
|
+
criticalities.max_by { |criticality| CRITICALITY_RANK.fetch(criticality) }
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def self.validate_criticality!(criticality, owner)
|
|
77
|
+
return if Config::CRITICALITIES.include?(criticality)
|
|
78
|
+
|
|
79
|
+
raise ArgumentError,
|
|
80
|
+
"invalid criticality #{criticality.inspect} for #{owner}; must be one of #{Config::CRITICALITIES.inspect}"
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def self.validate_breaker_options!(opts, owner)
|
|
84
|
+
unknown = opts.keys - BREAKER_OPTIONS
|
|
85
|
+
return if unknown.empty?
|
|
86
|
+
|
|
87
|
+
raise ArgumentError, "unknown option(s) #{unknown.inspect} for #{owner}; breaker options are #{BREAKER_OPTIONS.inspect}"
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
private_class_method :new, :implicit_purpose
|
|
91
|
+
|
|
92
|
+
def initialize(name:, purposes:, circuits:, prefix: nil)
|
|
93
|
+
@name = name
|
|
94
|
+
@purposes = purposes.freeze
|
|
95
|
+
@circuits = circuits.freeze
|
|
96
|
+
@prefix = prefix
|
|
97
|
+
freeze
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def prefix?
|
|
101
|
+
!@prefix.nil?
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# The worst criticality among the purposes.
|
|
105
|
+
def criticality
|
|
106
|
+
self.class.worst(@purposes.map(&:criticality))
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def purpose_names
|
|
110
|
+
@purposes.map(&:name)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def purpose(name)
|
|
114
|
+
key = name.to_sym
|
|
115
|
+
@purposes.find { |purpose| purpose.name == key } or
|
|
116
|
+
raise UnknownPurpose,
|
|
117
|
+
"dependency #{@name.inspect} has no purpose #{key.inspect}; declared: #{purpose_names.inspect}"
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# The purpose a `run` targets. Without +name+ that is the default purpose:
|
|
121
|
+
# the implicit one, or the declared purpose marked `default: true`.
|
|
122
|
+
def resolve_purpose(name)
|
|
123
|
+
return purpose(name) unless name.nil?
|
|
124
|
+
|
|
125
|
+
@purposes.find(&:default?) or
|
|
126
|
+
raise PurposeRequired,
|
|
127
|
+
"dependency #{@name.inspect} declares purposes #{purpose_names.inspect}; pass one with `purpose:` " \
|
|
128
|
+
"(or mark one `default: true`)"
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# Collects `purpose` calls inside a `Config#dependency` block.
|
|
132
|
+
class Builder
|
|
133
|
+
Declared = Struct.new(:name, :criticality, :isolate, :default, :breaker)
|
|
134
|
+
|
|
135
|
+
attr_reader :declared
|
|
136
|
+
|
|
137
|
+
def initialize(dependency, criticality)
|
|
138
|
+
@dependency = dependency
|
|
139
|
+
@criticality = criticality
|
|
140
|
+
@declared = []
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# +isolate: true+ gives the purpose its own breaker, which takes the
|
|
144
|
+
# dependency's breaker options merged with any given here. Breaker
|
|
145
|
+
# options without +isolate+ are rejected: a shared breaker has one set.
|
|
146
|
+
def purpose(name, criticality: @criticality, isolate: false, default: false, **breaker)
|
|
147
|
+
name = name.to_sym
|
|
148
|
+
owner = "purpose #{name.inspect} of dependency #{@dependency.inspect}"
|
|
149
|
+
DependencySpec.validate_criticality!(criticality, owner)
|
|
150
|
+
DependencySpec.validate_breaker_options!(breaker, owner)
|
|
151
|
+
if breaker.any? && !isolate
|
|
152
|
+
raise ArgumentError, "#{owner} sets breaker options #{breaker.keys.inspect} but shares the " \
|
|
153
|
+
"dependency's breaker; pass `isolate: true` to give it its own"
|
|
154
|
+
end
|
|
155
|
+
raise ArgumentError, "#{owner} is declared twice" if @declared.any? { |d| d.name == name }
|
|
156
|
+
if default && @declared.any?(&:default)
|
|
157
|
+
raise ArgumentError, "#{owner} is marked default, but #{@declared.find(&:default).name.inspect} already is"
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
@declared << Declared.new(name, criticality, isolate, default, breaker)
|
|
161
|
+
name
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
end
|
|
@@ -38,14 +38,25 @@ module StandardCircuit
|
|
|
38
38
|
#
|
|
39
39
|
# :optional circuits never elevate the overall state.
|
|
40
40
|
def overall(snapshot)
|
|
41
|
-
|
|
41
|
+
severities = snapshot.map { |e| severity(e[:criticality], e[:color]) }
|
|
42
|
+
return :critical if severities.include?(:unavailable)
|
|
42
43
|
|
|
43
|
-
degraded
|
|
44
|
-
|
|
45
|
-
(e[:criticality] == :standard && e[:color] == "red")
|
|
46
|
-
end
|
|
44
|
+
severities.include?(:degraded) ? :degraded : :ok
|
|
45
|
+
end
|
|
47
46
|
|
|
48
|
-
|
|
47
|
+
# The per-circuit form of the +overall+ rule, also used for dependency and
|
|
48
|
+
# purpose status: :unavailable | :degraded | :ok.
|
|
49
|
+
def severity(criticality, color)
|
|
50
|
+
return :unavailable if criticality == :critical && color == "red"
|
|
51
|
+
return :degraded if (criticality == :critical && color == "yellow") || (criticality == :standard && color == "red")
|
|
52
|
+
|
|
53
|
+
:ok
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# @api private — also used by Runner#circuit_state.
|
|
57
|
+
def locked?(light, config)
|
|
58
|
+
state = safe_state(light, config)
|
|
59
|
+
state == Stoplight::State::LOCKED_RED || state == Stoplight::State::LOCKED_GREEN
|
|
49
60
|
end
|
|
50
61
|
|
|
51
62
|
# --- internals -------------------------------------------------------
|
|
@@ -72,19 +83,14 @@ module StandardCircuit
|
|
|
72
83
|
def build_entry(light, spec, config)
|
|
73
84
|
{
|
|
74
85
|
name: light.name.to_sym,
|
|
75
|
-
|
|
86
|
+
# Stoplight 6 returns Symbols; the report has always carried Strings.
|
|
87
|
+
color: light.color.to_s,
|
|
76
88
|
locked: locked?(light, config),
|
|
77
89
|
criticality: spec.criticality
|
|
78
90
|
}
|
|
79
91
|
end
|
|
80
92
|
private_class_method :build_entry
|
|
81
93
|
|
|
82
|
-
def locked?(light, config)
|
|
83
|
-
state = safe_state(light, config)
|
|
84
|
-
state == Stoplight::State::LOCKED_RED || state == Stoplight::State::LOCKED_GREEN
|
|
85
|
-
end
|
|
86
|
-
private_class_method :locked?
|
|
87
|
-
|
|
88
94
|
# Reading +light.state+ can raise when the data store is unreachable
|
|
89
95
|
# (e.g. Redis connection dropped). Swallow the failure so the health
|
|
90
96
|
# endpoint still returns the rest of the snapshot, but log via the
|
|
@@ -31,15 +31,20 @@ module StandardCircuit
|
|
|
31
31
|
|
|
32
32
|
private
|
|
33
33
|
|
|
34
|
+
# Colors go out as Strings ("green"), as they did under stoplight 5;
|
|
35
|
+
# stoplight 6 hands us Symbols. The error is attached only to the
|
|
36
|
+
# GREEN -> RED trip: stoplight 5 never passed one for a failed recovery
|
|
37
|
+
# probe (YELLOW -> RED) and stoplight 6 does, which would add keys to the
|
|
38
|
+
# `circuit.opened` payload.
|
|
34
39
|
def payload_for(light, from_color, to_color, error)
|
|
35
40
|
spec = @config.spec_for(light.name)
|
|
36
41
|
payload = {
|
|
37
42
|
circuit: light.name,
|
|
38
|
-
from_color: from_color,
|
|
39
|
-
to_color: to_color,
|
|
43
|
+
from_color: from_color.to_s,
|
|
44
|
+
to_color: to_color.to_s,
|
|
40
45
|
criticality: spec&.criticality
|
|
41
46
|
}
|
|
42
|
-
if error
|
|
47
|
+
if error && from_color.to_s == Stoplight::Color::GREEN.to_s
|
|
43
48
|
payload[:error_class] = error.class.name
|
|
44
49
|
payload[:error_message] = error.message
|
|
45
50
|
end
|