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
|
@@ -1,23 +1,37 @@
|
|
|
1
1
|
module StandardCircuit
|
|
2
2
|
class Runner
|
|
3
|
+
# Name of the Stoplight system that owns every light this gem builds. It
|
|
4
|
+
# namespaces Redis keys (`stoplight:v6:<system id>:...`), so it must stay
|
|
5
|
+
# stable across processes and deploys.
|
|
6
|
+
SYSTEM_NAME = "standard_circuit"
|
|
7
|
+
|
|
3
8
|
def initialize
|
|
4
9
|
@lights = Concurrent::Map.new
|
|
5
10
|
@forced_states = Concurrent::Map.new
|
|
6
11
|
@config = Config.new
|
|
12
|
+
@system_mutex = Mutex.new
|
|
13
|
+
@system_entry = nil
|
|
7
14
|
end
|
|
8
15
|
|
|
9
16
|
def apply_config!(config)
|
|
10
17
|
@config = config
|
|
11
|
-
|
|
18
|
+
discard_system!
|
|
12
19
|
end
|
|
13
20
|
|
|
14
|
-
|
|
21
|
+
# +name+ is a dependency or a circuit (see Config#run_target). Forced
|
|
22
|
+
# states and the light are keyed by the resolved circuit, so purposes that
|
|
23
|
+
# share a breaker share its forced state too.
|
|
24
|
+
def run(name, purpose: nil, fallback: nil, &block)
|
|
15
25
|
fallback = accept_error_argument(fallback)
|
|
16
|
-
|
|
17
|
-
|
|
26
|
+
target = @config.run_target(name, purpose)
|
|
27
|
+
spec = @config.spec_for(target.circuit)
|
|
28
|
+
return execute_metrics_only(target, spec, fallback, &block) if spec&.metrics_only
|
|
29
|
+
|
|
30
|
+
forced = @forced_states[target.circuit]
|
|
31
|
+
return run_forced_open(target, fallback) if forced == :open
|
|
18
32
|
return yield if forced == :closed
|
|
19
33
|
|
|
20
|
-
execute(
|
|
34
|
+
execute(target, fallback, &block)
|
|
21
35
|
end
|
|
22
36
|
|
|
23
37
|
def force_open(name, &block)
|
|
@@ -39,16 +53,42 @@ module StandardCircuit
|
|
|
39
53
|
# into later specs. We only swap the store when the existing one is already
|
|
40
54
|
# a Memory store, because replacing a Redis store (production config) would
|
|
41
55
|
# silently defeat shared-process coordination.
|
|
56
|
+
#
|
|
57
|
+
# Under stoplight 6 a Memory store is only a marker: each light keeps its
|
|
58
|
+
# own counters, and a Stoplight system hands back the same cached light for
|
|
59
|
+
# a name forever. So a fresh store also means a fresh system (see #system).
|
|
42
60
|
def reset!
|
|
43
|
-
@lights.clear
|
|
44
61
|
@forced_states.clear
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
@config.data_store = Stoplight::DataStore::Memory.new
|
|
62
|
+
@config.data_store = Stoplight::DataStore::Memory.new if @config.data_store.is_a?(Stoplight::DataStore::Memory)
|
|
63
|
+
discard_system!
|
|
48
64
|
end
|
|
49
65
|
|
|
50
66
|
def light_for(name)
|
|
51
|
-
|
|
67
|
+
key = name.to_sym
|
|
68
|
+
current_system = system
|
|
69
|
+
@lights.compute_if_absent(key) { build_light(current_system, key) }
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# One circuit's current state: the light's own +color+ and +locked+ (as
|
|
73
|
+
# health_report shows them) plus any +forced+ override (:open / :closed)
|
|
74
|
+
# from force_open / force_closed.
|
|
75
|
+
def circuit_state(name)
|
|
76
|
+
key = name.to_sym
|
|
77
|
+
light = light_for(key)
|
|
78
|
+
{ name: key, color: light.color.to_s, locked: Health.locked?(light, @config), forced: @forced_states[key] }
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Lock a circuit's light open (red) or closed (green) until #unlock. The
|
|
82
|
+
# lock lives in the data store: per process with Memory, shared by every
|
|
83
|
+
# process with Redis.
|
|
84
|
+
# A no-op for a metrics-only circuit, whose light is never consulted.
|
|
85
|
+
def lock(name, state)
|
|
86
|
+
color = { open: Stoplight::Color::RED, closed: Stoplight::Color::GREEN }.fetch(state)
|
|
87
|
+
light_for(name).lock(color) unless metrics_only?(name)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def unlock(name)
|
|
91
|
+
light_for(name).unlock unless metrics_only?(name)
|
|
52
92
|
end
|
|
53
93
|
|
|
54
94
|
# Snapshot Hash of the current cached lights. Used by Health to discover
|
|
@@ -88,35 +128,82 @@ module StandardCircuit
|
|
|
88
128
|
->(_error) { fallback.call }
|
|
89
129
|
end
|
|
90
130
|
|
|
91
|
-
def execute(
|
|
131
|
+
def execute(target, fallback, &block)
|
|
132
|
+
name = target.circuit
|
|
92
133
|
started_at = monotonic_now
|
|
93
134
|
light = light_for(name)
|
|
135
|
+
spec = @config.spec_for(name)
|
|
94
136
|
|
|
95
|
-
|
|
137
|
+
# The spec's error lists go in per run, which stoplight 6 still accepts
|
|
138
|
+
# for any `===` matcher (procs, anonymous classes) — at registration it
|
|
139
|
+
# only takes named classes/modules. See #build_light.
|
|
140
|
+
result = light.run(fallback, tracked_errors: spec.tracked_errors, skipped_errors: spec.skipped_errors, &block)
|
|
96
141
|
duration = duration_ms(started_at)
|
|
97
142
|
emit_request_metric(name, :success, duration)
|
|
98
|
-
emit_run_completed(
|
|
143
|
+
emit_run_completed(target, status: :success, duration_ms: duration)
|
|
99
144
|
result
|
|
100
145
|
rescue Stoplight::Error::RedLight => e
|
|
101
146
|
duration = duration_ms(started_at)
|
|
102
147
|
emit_request_metric(name, :circuit_open, duration)
|
|
103
|
-
emit_run_completed(
|
|
148
|
+
emit_run_completed(target, status: :circuit_open, duration_ms: duration, error: e)
|
|
104
149
|
raise e unless fallback
|
|
105
150
|
|
|
106
|
-
emit_fallback_invoked(
|
|
151
|
+
emit_fallback_invoked(target, reason: :circuit_open)
|
|
107
152
|
fallback.call(nil)
|
|
108
153
|
rescue StandardError => e
|
|
109
154
|
duration = duration_ms(started_at)
|
|
110
155
|
emit_request_metric(name, :failure, duration)
|
|
111
|
-
emit_run_completed(
|
|
156
|
+
emit_run_completed(target, status: :failure, duration_ms: duration, error: e)
|
|
157
|
+
raise e
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# A metrics-only breaker never blocks a call by any route: the light is
|
|
161
|
+
# never consulted, and forced states and locks don't apply. Everything
|
|
162
|
+
# else matches a breaker that is closed: a tracked error goes to the
|
|
163
|
+
# fallback when there is one, the same events and request metrics are
|
|
164
|
+
# emitted, with the same statuses.
|
|
165
|
+
#
|
|
166
|
+
# The light is still built, though not run through, because the light
|
|
167
|
+
# cache is how prefix-matched circuits (`s3_uploads`) become known to
|
|
168
|
+
# Health.snapshot and Dependency#circuit_names.
|
|
169
|
+
def execute_metrics_only(target, spec, fallback)
|
|
170
|
+
name = target.circuit
|
|
171
|
+
light_for(name)
|
|
172
|
+
started_at = monotonic_now
|
|
173
|
+
result = begin
|
|
174
|
+
yield
|
|
175
|
+
rescue StandardError => e
|
|
176
|
+
raise unless fallback && tracked_error?(spec, e)
|
|
177
|
+
|
|
178
|
+
fallback.call(e)
|
|
179
|
+
end
|
|
180
|
+
duration = duration_ms(started_at)
|
|
181
|
+
emit_request_metric(name, :success, duration)
|
|
182
|
+
emit_run_completed(target, status: :success, duration_ms: duration)
|
|
183
|
+
result
|
|
184
|
+
rescue StandardError => e
|
|
185
|
+
duration = duration_ms(started_at)
|
|
186
|
+
emit_request_metric(name, :failure, duration)
|
|
187
|
+
emit_run_completed(target, status: :failure, duration_ms: duration, error: e)
|
|
112
188
|
raise e
|
|
113
189
|
end
|
|
114
190
|
|
|
115
|
-
def
|
|
191
|
+
def metrics_only?(name)
|
|
192
|
+
@config.spec_for(name)&.metrics_only
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
# Stoplight's rule: skipped wins over tracked.
|
|
196
|
+
def tracked_error?(spec, error)
|
|
197
|
+
spec.skipped_errors.none? { |matcher| matcher === error } &&
|
|
198
|
+
spec.tracked_errors.any? { |matcher| matcher === error }
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
def run_forced_open(target, fallback)
|
|
202
|
+
name = target.circuit
|
|
116
203
|
emit_request_metric(name, :circuit_open, 0)
|
|
117
|
-
emit_run_completed(
|
|
204
|
+
emit_run_completed(target, status: :circuit_open, duration_ms: 0)
|
|
118
205
|
if fallback
|
|
119
|
-
emit_fallback_invoked(
|
|
206
|
+
emit_fallback_invoked(target, reason: :forced_open)
|
|
120
207
|
return fallback.call(nil)
|
|
121
208
|
end
|
|
122
209
|
|
|
@@ -145,21 +232,71 @@ module StandardCircuit
|
|
|
145
232
|
@forced_states[key] = state
|
|
146
233
|
end
|
|
147
234
|
|
|
148
|
-
|
|
235
|
+
# The Stoplight system for the current config and data store, built on
|
|
236
|
+
# first use. A data store swapped in after `configure` (as #reset! does,
|
|
237
|
+
# or a host assigning `config.data_store` directly) gets a new system, the
|
|
238
|
+
# same way 5.x lights picked up `@config.data_store` when they were built.
|
|
239
|
+
def system
|
|
240
|
+
entry = @system_entry
|
|
241
|
+
return entry.last if entry && entry.first.equal?(@config.data_store)
|
|
242
|
+
|
|
243
|
+
@system_mutex.synchronize do
|
|
244
|
+
store = @config.data_store
|
|
245
|
+
unless @system_entry&.first.equal?(store)
|
|
246
|
+
@lights.clear
|
|
247
|
+
@system_entry = [ store, build_system(store) ].freeze
|
|
248
|
+
end
|
|
249
|
+
@system_entry.last
|
|
250
|
+
end
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
def discard_system!
|
|
254
|
+
@system_mutex.synchronize do
|
|
255
|
+
@system_entry = nil
|
|
256
|
+
@lights.clear
|
|
257
|
+
end
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
# Stoplight 6 caches one light per name inside a system and refuses to
|
|
261
|
+
# re-register a name with different settings, and its public
|
|
262
|
+
# `register_system` refuses to reuse a system name. Neither fits a runner
|
|
263
|
+
# that is reconfigured on every code reload and reset between specs, so
|
|
264
|
+
# each system gets its own Stoplight state rather than living in the
|
|
265
|
+
# process-global one. That also keeps it apart from a host's own
|
|
266
|
+
# `Stoplight.configure`.
|
|
267
|
+
def build_system(data_store)
|
|
268
|
+
state = Stoplight::Wiring::GlobalState.new(
|
|
269
|
+
default_config: Stoplight::Wiring::DefaultConfiguration.new.to_config!
|
|
270
|
+
)
|
|
271
|
+
state.register_system(SYSTEM_NAME, data_store: data_store, notifiers: [ NotifierBridge.new(@config) ])
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
def build_light(system, name)
|
|
149
275
|
spec = @config.spec_for(name) or raise UnknownCircuit, "no circuit registered for #{name.inspect}"
|
|
150
276
|
|
|
151
|
-
|
|
277
|
+
system.register(
|
|
152
278
|
name.to_s,
|
|
153
279
|
threshold: spec.threshold,
|
|
154
|
-
cool_off_time: spec.cool_off_time,
|
|
155
|
-
window_size: spec.window_size,
|
|
156
|
-
|
|
157
|
-
skipped_errors: spec.skipped_errors,
|
|
158
|
-
data_store: @config.data_store,
|
|
159
|
-
notifiers: [ NotifierBridge.new(@config) ]
|
|
280
|
+
cool_off_time: whole_seconds(spec.cool_off_time),
|
|
281
|
+
window_size: whole_seconds(spec.window_size),
|
|
282
|
+
**registrable_errors(spec)
|
|
160
283
|
)
|
|
161
284
|
end
|
|
162
285
|
|
|
286
|
+
# Stoplight 6 takes only whole seconds, at least 1, for both durations.
|
|
287
|
+
def whole_seconds(value)
|
|
288
|
+
value && [ value.ceil, 1 ].max
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
# Registration accepts named classes/modules only. Lists that qualify are
|
|
292
|
+
# registered so a light used directly (`light_for(name).run`) still sees
|
|
293
|
+
# them; #execute passes both lists on every run regardless.
|
|
294
|
+
def registrable_errors(spec)
|
|
295
|
+
{ tracked_errors: spec.tracked_errors, skipped_errors: spec.skipped_errors }.select do |_, errors|
|
|
296
|
+
errors.all? { |matcher| matcher.is_a?(Module) && matcher.name }
|
|
297
|
+
end
|
|
298
|
+
end
|
|
299
|
+
|
|
163
300
|
def emit_request_metric(name, status, duration)
|
|
164
301
|
prefix = @config.metric_prefix
|
|
165
302
|
attrs = { service: name.to_s, status: status.to_s }
|
|
@@ -167,23 +304,26 @@ module StandardCircuit
|
|
|
167
304
|
::Sentry::Metrics.distribution("#{prefix}.request.duration", duration, unit: "millisecond", attributes: attrs)
|
|
168
305
|
end
|
|
169
306
|
|
|
170
|
-
|
|
171
|
-
|
|
307
|
+
# Per-call events carry the purpose's criticality: one shared breaker can
|
|
308
|
+
# serve purposes of different weight. For a circuit registered with
|
|
309
|
+
# `register`/`register_prefix` that is the circuit's own criticality.
|
|
310
|
+
def emit_fallback_invoked(target, reason:)
|
|
172
311
|
EventEmitter.emit("standard_circuit.circuit.fallback_invoked",
|
|
173
|
-
circuit:
|
|
312
|
+
circuit: target.circuit.to_s,
|
|
174
313
|
reason: reason,
|
|
175
|
-
criticality:
|
|
314
|
+
criticality: target.criticality)
|
|
176
315
|
end
|
|
177
316
|
|
|
178
|
-
def emit_run_completed(
|
|
179
|
-
spec = @config.spec_for(name)
|
|
317
|
+
def emit_run_completed(target, status:, duration_ms:, error: nil)
|
|
180
318
|
EventEmitter.emit("standard_circuit.run.completed",
|
|
181
|
-
circuit:
|
|
319
|
+
circuit: target.circuit.to_s,
|
|
182
320
|
status: status,
|
|
183
321
|
duration_ms: duration_ms,
|
|
184
|
-
criticality:
|
|
322
|
+
criticality: target.criticality,
|
|
185
323
|
error_class: error&.class&.name,
|
|
186
|
-
error_message: error&.message
|
|
324
|
+
error_message: error&.message,
|
|
325
|
+
dependency: target.dependency,
|
|
326
|
+
purpose: target.purpose)
|
|
187
327
|
end
|
|
188
328
|
|
|
189
329
|
def monotonic_now
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
module StandardCircuit
|
|
2
|
+
# Rolling, in-process call statistics per dependency and per purpose, fed by
|
|
3
|
+
# `standard_circuit.run.completed` (see Stats::Subscriber).
|
|
4
|
+
#
|
|
5
|
+
# Each window holds the last +window_size+ calls in a fixed ring, so memory
|
|
6
|
+
# is bounded by (dependencies + purposes) × window_size however much traffic
|
|
7
|
+
# flows. Nothing is shared between processes: like the default Memory data
|
|
8
|
+
# store, every worker has its own view.
|
|
9
|
+
module Stats
|
|
10
|
+
DEFAULT_WINDOW_SIZE = 100
|
|
11
|
+
STATUSES = [ :success, :failure, :circuit_open ].freeze
|
|
12
|
+
|
|
13
|
+
Sample = Struct.new(:status, :duration_ms)
|
|
14
|
+
|
|
15
|
+
# The last +size+ calls for one dependency or purpose, plus a few
|
|
16
|
+
# running values (last success/failure, consecutive failures) that
|
|
17
|
+
# outlive the ring. Thread-safe.
|
|
18
|
+
class Window
|
|
19
|
+
def initialize(size, clock: -> { Time.now })
|
|
20
|
+
@size = size
|
|
21
|
+
@clock = clock
|
|
22
|
+
@mutex = Mutex.new
|
|
23
|
+
@samples = Array.new(size)
|
|
24
|
+
@next = 0
|
|
25
|
+
@count = 0
|
|
26
|
+
@last_success_at = nil
|
|
27
|
+
@last_failure_at = nil
|
|
28
|
+
@consecutive_failures = 0
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def record(status, duration_ms)
|
|
32
|
+
now = @clock.call
|
|
33
|
+
@mutex.synchronize do
|
|
34
|
+
@samples[@next] = Sample.new(status, duration_ms)
|
|
35
|
+
@next = (@next + 1) % @size
|
|
36
|
+
@count += 1 if @count < @size
|
|
37
|
+
|
|
38
|
+
case status
|
|
39
|
+
when :success
|
|
40
|
+
@last_success_at = now
|
|
41
|
+
@consecutive_failures = 0
|
|
42
|
+
when :failure
|
|
43
|
+
@last_failure_at = now
|
|
44
|
+
@consecutive_failures += 1
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# A Hash describing the window. A window with no calls reports
|
|
50
|
+
# `calls: 0` and nil rates, never zeros that would read as healthy.
|
|
51
|
+
def snapshot
|
|
52
|
+
samples, last_success_at, last_failure_at, consecutive_failures = @mutex.synchronize do
|
|
53
|
+
[ @samples.first(@count).compact, @last_success_at, @last_failure_at, @consecutive_failures ]
|
|
54
|
+
end
|
|
55
|
+
Stats.summarize(samples, window_size: @size, last_success_at: last_success_at,
|
|
56
|
+
last_failure_at: last_failure_at, consecutive_failures: consecutive_failures)
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# +samples+ in any order. Error rate and latency count only attempted
|
|
61
|
+
# calls (success + failure): a :circuit_open call never reached the
|
|
62
|
+
# upstream, so it is no evidence about it either way.
|
|
63
|
+
def self.summarize(samples, window_size:, last_success_at: nil, last_failure_at: nil, consecutive_failures: 0)
|
|
64
|
+
counts = STATUSES.to_h { |status| [ status, 0 ] }
|
|
65
|
+
samples.each { |sample| counts[sample.status] += 1 if counts.key?(sample.status) }
|
|
66
|
+
attempted = samples.select { |sample| sample.status == :success || sample.status == :failure }
|
|
67
|
+
durations = attempted.map(&:duration_ms).compact.sort
|
|
68
|
+
|
|
69
|
+
{
|
|
70
|
+
calls: samples.size,
|
|
71
|
+
window_size: window_size,
|
|
72
|
+
counts: counts,
|
|
73
|
+
error_rate: attempted.empty? ? nil : (counts[:failure].to_f / attempted.size).round(4),
|
|
74
|
+
latency_ms: durations.empty? ? nil : { p50: percentile(durations, 50), p95: percentile(durations, 95) },
|
|
75
|
+
last_success_at: last_success_at,
|
|
76
|
+
last_failure_at: last_failure_at,
|
|
77
|
+
consecutive_failures: consecutive_failures
|
|
78
|
+
}
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Nearest-rank percentile of a sorted, non-empty Array.
|
|
82
|
+
def self.percentile(sorted, pct)
|
|
83
|
+
sorted[((pct / 100.0) * sorted.size).ceil.clamp(1, sorted.size) - 1]
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Every window, keyed by dependency name or [dependency, purpose].
|
|
87
|
+
class Store
|
|
88
|
+
attr_reader :window_size
|
|
89
|
+
|
|
90
|
+
def initialize(window_size: DEFAULT_WINDOW_SIZE, clock: -> { Time.now })
|
|
91
|
+
@window_size = window_size
|
|
92
|
+
@clock = clock
|
|
93
|
+
@windows = Concurrent::Map.new
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Changing the size drops every window: a ring can't be resized in place
|
|
97
|
+
# without inventing or discarding history.
|
|
98
|
+
def window_size=(size)
|
|
99
|
+
return if size == @window_size
|
|
100
|
+
|
|
101
|
+
@window_size = size
|
|
102
|
+
clear!
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def record(dependency, purpose, status, duration_ms)
|
|
106
|
+
window(dependency).record(status, duration_ms)
|
|
107
|
+
window([ dependency, purpose ]).record(status, duration_ms)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def for_dependency(dependency)
|
|
111
|
+
snapshot(dependency)
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def for_purpose(dependency, purpose)
|
|
115
|
+
snapshot([ dependency, purpose ])
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def clear!
|
|
119
|
+
@windows.clear
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
private
|
|
123
|
+
|
|
124
|
+
def window(key)
|
|
125
|
+
@windows.compute_if_absent(key) { Window.new(@window_size, clock: @clock) }
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def snapshot(key)
|
|
129
|
+
@windows[key]&.snapshot || Stats.summarize([], window_size: @window_size)
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# Internal subscriber (see Subscribers#internal_subscribers). Records
|
|
134
|
+
# calls that resolved to a dependency; never raises into the emitter.
|
|
135
|
+
class Subscriber
|
|
136
|
+
EVENT = "standard_circuit.run.completed".freeze
|
|
137
|
+
|
|
138
|
+
def initialize(store)
|
|
139
|
+
@store = store
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def call(event_name, payload)
|
|
143
|
+
return unless event_name == EVENT
|
|
144
|
+
|
|
145
|
+
dependency = payload[:dependency]
|
|
146
|
+
return if dependency.nil?
|
|
147
|
+
|
|
148
|
+
@store.record(dependency, payload[:purpose], payload[:status], payload[:duration_ms])
|
|
149
|
+
rescue StandardError => e
|
|
150
|
+
warn "[StandardCircuit] stats recording failed: #{e.class}: #{e.message}"
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
end
|
|
@@ -3,7 +3,7 @@ module StandardCircuit
|
|
|
3
3
|
# bus is live (Rails.event on 8.1+, ActiveSupport::Notifications elsewhere).
|
|
4
4
|
#
|
|
5
5
|
# Each subscriber must respond to `call(event_name, payload)`. Internal
|
|
6
|
-
# subscribers (Logger / Sentry / Metrics) are built from the live config so
|
|
6
|
+
# subscribers (Logger / Sentry / Metrics / Stats) are built from the live config so
|
|
7
7
|
# changes to `metric_prefix` / `logger` propagate when `setup!` is re-run.
|
|
8
8
|
#
|
|
9
9
|
# Subscriptions cover the namespace prefix `standard_circuit.`, so a single
|
|
@@ -74,6 +74,7 @@ module StandardCircuit
|
|
|
74
74
|
list = [ Notifiers::Logger.new(config.logger) ]
|
|
75
75
|
list << Notifiers::Sentry.new(levels: config.sentry_criticality_levels) if config.sentry_enabled
|
|
76
76
|
list << Notifiers::Metrics.new(metric_prefix: config.metric_prefix)
|
|
77
|
+
list << Stats::Subscriber.new(StandardCircuit.stats)
|
|
77
78
|
list
|
|
78
79
|
end
|
|
79
80
|
|
data/lib/standard_circuit.rb
CHANGED
|
@@ -17,8 +17,11 @@ require "standard_circuit/notifier_bridge"
|
|
|
17
17
|
require "standard_circuit/notifiers/logger"
|
|
18
18
|
require "standard_circuit/notifiers/sentry"
|
|
19
19
|
require "standard_circuit/notifiers/metrics"
|
|
20
|
+
require "standard_circuit/stats"
|
|
20
21
|
require "standard_circuit/subscribers"
|
|
22
|
+
require "standard_circuit/dependency_spec"
|
|
21
23
|
require "standard_circuit/config"
|
|
24
|
+
require "standard_circuit/dependency"
|
|
22
25
|
require "standard_circuit/health"
|
|
23
26
|
require "standard_circuit/runner"
|
|
24
27
|
require "standard_circuit/mailer/circuit_open_error"
|
|
@@ -30,11 +33,17 @@ require "standard_circuit/engine" if defined?(::Rails::Engine)
|
|
|
30
33
|
module StandardCircuit
|
|
31
34
|
class Error < StandardError; end
|
|
32
35
|
class UnknownCircuit < Error; end
|
|
36
|
+
class UnknownDependency < UnknownCircuit; end
|
|
37
|
+
# `run` named a purpose the dependency does not declare.
|
|
38
|
+
class UnknownPurpose < Error; end
|
|
39
|
+
# `run` gave no purpose for a dependency with several and no default.
|
|
40
|
+
class PurposeRequired < Error; end
|
|
33
41
|
|
|
34
42
|
class << self
|
|
35
43
|
def configure
|
|
36
44
|
yield config
|
|
37
45
|
runner.apply_config!(config)
|
|
46
|
+
stats.window_size = config.stats_window_size
|
|
38
47
|
subscribers.setup!
|
|
39
48
|
Mailer::Retry.install_on_load if config.mailer_retry
|
|
40
49
|
config
|
|
@@ -52,8 +61,24 @@ module StandardCircuit
|
|
|
52
61
|
@subscribers ||= Subscribers.new
|
|
53
62
|
end
|
|
54
63
|
|
|
55
|
-
|
|
56
|
-
|
|
64
|
+
# Rolling per-dependency / per-purpose call stats (see Stats), read
|
|
65
|
+
# through `dependency(name).stats`.
|
|
66
|
+
def stats
|
|
67
|
+
@stats ||= Stats::Store.new(window_size: config.stats_window_size)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def run(name, purpose: nil, fallback: nil, &block)
|
|
71
|
+
runner.run(name, purpose: purpose, fallback: fallback, &block)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# The dependency +name+ (see Config#dependency), or the one a
|
|
75
|
+
# `register` / `register_prefix` call created. Raises UnknownDependency.
|
|
76
|
+
def dependency(name)
|
|
77
|
+
Dependency.new(config.dependency_spec(name), runner, config)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def dependencies
|
|
81
|
+
config.dependency_specs.map { |spec| Dependency.new(spec, runner, config) }
|
|
57
82
|
end
|
|
58
83
|
|
|
59
84
|
def force_open(name, &block)
|
|
@@ -70,10 +95,18 @@ module StandardCircuit
|
|
|
70
95
|
|
|
71
96
|
def reset!
|
|
72
97
|
runner.reset!
|
|
98
|
+
stats.clear!
|
|
73
99
|
end
|
|
74
100
|
|
|
75
101
|
def health_report
|
|
76
102
|
runner.health_report
|
|
77
103
|
end
|
|
104
|
+
|
|
105
|
+
# One row per dependency, for a later standard_health contribution and
|
|
106
|
+
# for dashboards. health_report is unchanged and stays what
|
|
107
|
+
# standard_health reads today.
|
|
108
|
+
def dependency_report
|
|
109
|
+
dependencies.map(&:report)
|
|
110
|
+
end
|
|
78
111
|
end
|
|
79
112
|
end
|
metadata
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: standard_circuit
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.7.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
|
-
-
|
|
7
|
+
- Rarebit One
|
|
8
8
|
bindir: bin
|
|
9
9
|
cert_chain: []
|
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
|
@@ -15,14 +15,20 @@ dependencies:
|
|
|
15
15
|
requirements:
|
|
16
16
|
- - "~>"
|
|
17
17
|
- !ruby/object:Gem::Version
|
|
18
|
-
version: '
|
|
18
|
+
version: '6.0'
|
|
19
|
+
- - ">="
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: 6.0.1
|
|
19
22
|
type: :runtime
|
|
20
23
|
prerelease: false
|
|
21
24
|
version_requirements: !ruby/object:Gem::Requirement
|
|
22
25
|
requirements:
|
|
23
26
|
- - "~>"
|
|
24
27
|
- !ruby/object:Gem::Version
|
|
25
|
-
version: '
|
|
28
|
+
version: '6.0'
|
|
29
|
+
- - ">="
|
|
30
|
+
- !ruby/object:Gem::Version
|
|
31
|
+
version: 6.0.1
|
|
26
32
|
- !ruby/object:Gem::Dependency
|
|
27
33
|
name: concurrent-ruby
|
|
28
34
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -161,6 +167,8 @@ files:
|
|
|
161
167
|
- lib/standard_circuit/adapter_errors/stripe.rb
|
|
162
168
|
- lib/standard_circuit/config.rb
|
|
163
169
|
- lib/standard_circuit/controller_support.rb
|
|
170
|
+
- lib/standard_circuit/dependency.rb
|
|
171
|
+
- lib/standard_circuit/dependency_spec.rb
|
|
164
172
|
- lib/standard_circuit/deprecator.rb
|
|
165
173
|
- lib/standard_circuit/engine.rb
|
|
166
174
|
- lib/standard_circuit/error_taxonomies.rb
|
|
@@ -177,6 +185,7 @@ files:
|
|
|
177
185
|
- lib/standard_circuit/presets.rb
|
|
178
186
|
- lib/standard_circuit/rspec.rb
|
|
179
187
|
- lib/standard_circuit/runner.rb
|
|
188
|
+
- lib/standard_circuit/stats.rb
|
|
180
189
|
- lib/standard_circuit/subscribers.rb
|
|
181
190
|
- lib/standard_circuit/version.rb
|
|
182
191
|
homepage: https://github.com/rarebit-one/standard_circuit
|
|
@@ -201,7 +210,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
201
210
|
- !ruby/object:Gem::Version
|
|
202
211
|
version: '0'
|
|
203
212
|
requirements: []
|
|
204
|
-
rubygems_version: 4.0.
|
|
213
|
+
rubygems_version: 4.0.20
|
|
205
214
|
specification_version: 4
|
|
206
215
|
summary: Circuit breaker primitives for Rails apps, built on stoplight.
|
|
207
216
|
test_files: []
|