standard_circuit 0.1.2 → 0.3.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 +44 -0
- data/LICENSE +22 -0
- data/README.md +140 -3
- data/lib/generators/standard_circuit/install/install_generator.rb +83 -0
- data/lib/generators/standard_circuit/install/templates/health_initializer.rb.tt +25 -0
- data/lib/generators/standard_circuit/install/templates/initializer.rb.tt +104 -0
- data/lib/standard_circuit/config.rb +71 -8
- data/lib/standard_circuit/engine.rb +36 -0
- data/lib/standard_circuit/event_emitter.rb +35 -0
- data/lib/standard_circuit/notifier_bridge.rb +49 -0
- data/lib/standard_circuit/notifiers/logger.rb +26 -6
- data/lib/standard_circuit/notifiers/metrics.rb +11 -7
- data/lib/standard_circuit/notifiers/sentry.rb +75 -11
- data/lib/standard_circuit/rspec.rb +13 -0
- data/lib/standard_circuit/runner.rb +35 -9
- data/lib/standard_circuit/subscribers.rb +102 -0
- data/lib/standard_circuit/version.rb +1 -1
- data/lib/standard_circuit.rb +9 -0
- metadata +52 -3
- data/MIT-LICENSE +0 -21
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ac355e3881c42900c272ba14d433216f4d8e6019e003e4b5bd52f84545c11c79
|
|
4
|
+
data.tar.gz: ffdb732b389d5b752deef02d6cdc82fd148af38e3e861d046876cce329e50d36
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e2ffa97cfc68660ca8e32bd606df2251700f619f728969ce0e27ed485b6887eb6d8ecd6f1b641a0cea9d22c4842c3a5041a25b00bac97a84c37ea00ca78adf48
|
|
7
|
+
data.tar.gz: 8f245331cafaeb355b33e0ddd515bb23824713826d908544533959cd1f2dcb6c21d0160887370dadeef566a495d0474f66603095c0e1d93d1275f67c35f9f27d
|
data/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,49 @@ All notable changes to this project will be documented in this file.
|
|
|
4
4
|
|
|
5
5
|
## [Unreleased]
|
|
6
6
|
|
|
7
|
+
## [0.3.0] - 2026-07-30
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
- `config.sentry_criticality_levels` — opt in to criticality-aware Sentry reporting for the built-in Sentry subscriber. Accepts `true` (the recommended map `{ critical: :error, standard: :warning, optional: :info }`), a partial Hash merged over that map, or `nil` / `false` for the previous flat behaviour. In criticality-aware mode a circuit-open report also gains `circuit` / `circuit_criticality` tags and a stable `["circuit-open", <circuit>]` fingerprint, so Sentry alert rules can page on `circuit_criticality:critical` and group one issue per breaker. Invalid criticalities and non-symbolizable levels raise `ArgumentError` at configure time rather than failing silently at alert time.
|
|
11
|
+
|
|
12
|
+
**This is opt-in, and deliberately not the new default.** Both the level and the fingerprint feed Sentry's alerting and issue grouping, so flipping the map on at gem-upgrade time would silently re-page and re-group live issues in apps that never asked for it. Apps that leave `sentry_criticality_levels` unset get the 0.2.x report byte-for-byte — same `:warning` level, same message, no tags, no fingerprint. Host apps that hand-rolled this by setting `sentry_enabled = false` and registering their own alerter can now delete that class and set `sentry_enabled = true` + `sentry_criticality_levels = true` instead.
|
|
13
|
+
- `isolate_namespace StandardCircuit` on the engine, bringing it in line with every other engine gem in the family. Verified non-breaking for the aggregate health route every consumer draws (`get "/health", to: "standard_circuit/health#show"`): that path is resolved by the *application's* route set through constant lookup, which isolation does not touch, and the new `spec/integration/health_route_boot_spec.rb` boots a real Rails app and requests the route end to end so a regression fails here rather than in a consumer after release. Safe specifically because this engine is library-only — no `config/routes.rb` for the isolated `default_scope` to scope, and no ActiveRecord models for the `standard_circuit_` `table_name_prefix` to apply to.
|
|
14
|
+
|
|
15
|
+
One consequence to know about: `StandardCircuit::HealthController` now picks up the engine's (empty) url helpers instead of the application's, so app path helpers used inside it — or inside a host subclass of it — need a `main_app.` prefix. The gem's controller only renders JSON, so nothing in-gem is affected.
|
|
16
|
+
- README section on `data_store`, previously undocumented. Spells out that the `Stoplight::DataStore::Memory` default is **per-process** — thresholds count per worker, `/health` reports the serving process's view, and `force_open` / `reset!` are process-local — plus the shared-store alternative for apps that want cross-process state.
|
|
17
|
+
- README section on Sentry reporting covering `sentry_enabled`, the new `sentry_criticality_levels` opt-in, and the "subscribe yourself" escape hatch.
|
|
18
|
+
|
|
19
|
+
### Changed
|
|
20
|
+
- The install generator now warns that the aggregate `get "/health", to: "standard_circuit/health#show"` route must be drawn **before** `mount StandardHealth::Engine => "/health"`. `StandardHealth::Engine` registers sub-paths only (`/alive`, `/ready`, `/diagnostics/env`) and never serves the aggregate tier itself, so an app that mounts it and assumes `/health` is covered silently has no aggregate tier — with no boot error and no failing route spec to reveal it. The warning appears in the initializer template, in the `--with-health-endpoint` health initializer next to the route line, and in the hint the generator prints. Same note added to the README's health-endpoint section.
|
|
21
|
+
- `.github/workflows/ci.yml` follows the shared reusable workflow at `@v2` again, matching the rest of the gem family. The previous SHA pin carried a stale rationale: it claimed to restore pre-`rarebit-one/.github#14` job names for branch protection, but `main`'s protection now requires the *post*-#14 names (`ci / lint`, `ci / test-matrix (4.0.x)`) and the pinned revision's `reusable-gem-ci.yml` is byte-identical to `@v2` — so it was neither restoring old names nor changing any check name. It did honour `extra-lint-commands`, so the brakeman / bundler-audit gate was live throughout.
|
|
22
|
+
|
|
23
|
+
## [0.2.0] - 2026-04-28
|
|
24
|
+
|
|
25
|
+
### Added
|
|
26
|
+
- Rails event emission for every circuit-breaker lifecycle moment. The `StandardCircuit::Runner` (via a small internal `NotifierBridge` registered with Stoplight) now emits five events as host apps' breakers change state:
|
|
27
|
+
- `standard_circuit.circuit.opened` — RED transition (the "alert me" event)
|
|
28
|
+
- `standard_circuit.circuit.closed` — GREEN transition (recovery)
|
|
29
|
+
- `standard_circuit.circuit.degraded` — YELLOW transition (half-open probe)
|
|
30
|
+
- `standard_circuit.circuit.fallback_invoked` — Runner returned a fallback rather than raising RedLight
|
|
31
|
+
- `standard_circuit.circuit.registered` — `Config#register` / `register_prefix` was called
|
|
32
|
+
|
|
33
|
+
Payloads carry `circuit:`, `from_color:`, `to_color:`, `criticality:`, and (when applicable) `error_class:` / `error_message:` / `reason:`.
|
|
34
|
+
- `standard_circuit.run.completed` event. Per-call complement to the lifecycle events above — fires once per wrapped `StandardCircuit.run` invocation with `circuit:`, `status:` (`:success` / `:failure` / `:circuit_open`), `duration_ms:`, `criticality:`, `error_class:`, and `error_message:`. The right hook for cost-tracking, p95 latency, and per-circuit success-rate dashboards. (`force_closed` runs are intentionally not emitted — that path bypasses the runner.) Routed through the same `EventEmitter` as the lifecycle events, so subscribers get it on `Rails.event` (8.1+) or `ActiveSupport::Notifications` automatically; `duration_ms` is in the payload (not `event.duration`) so backend choice doesn't matter.
|
|
35
|
+
- Dual-backend dispatch in `StandardCircuit::EventEmitter`: emits through `Rails.event.notify` on Rails 8.1+ and falls back to `ActiveSupport::Notifications.instrument` on older Rails. Detection happens at call time, so the gem still loads cleanly before Rails has booted and before `railties` is even required.
|
|
36
|
+
- `StandardCircuit::Engine` Railtie that registers the internal subscribers (Logger / Sentry / Metrics) plus any `extra_notifiers` at boot via the `standard_circuit.subscribers` initializer.
|
|
37
|
+
- `StandardCircuit.subscribers` accessor + `Subscribers#setup!` / `#teardown!` for tests and host apps that need to re-register listeners after mutating config.
|
|
38
|
+
- `rails g standard_circuit:install` — Rails install generator. Writes `config/initializers/standard_circuit.rb` with commented-out examples covering the public Config DSL (`register`, `register_prefix`, notifiers, data store, criticality). Idempotent: re-running on an existing initializer skips with a clear message; pass `--force` to overwrite. Pass `--with-health-endpoint` to also write `config/initializers/standard_circuit_health.rb` (which `require`s the opt-in `HealthController`) and print the route line to add to `config/routes.rb`. The generator does not auto-edit `routes.rb` — too invasive — so consumers paste the printed line themselves.
|
|
39
|
+
- README section on streaming responses and non-controller contexts: shows the recipe for catching `Stoplight::Error::RedLight` inside a `Live` controller's streaming proc (where `circuit_open_fallback` can't render over an open response), and notes the equivalent pattern for background jobs.
|
|
40
|
+
|
|
41
|
+
### Changed
|
|
42
|
+
- **BREAKING.** `StandardCircuit::Notifiers::{Logger,Sentry,Metrics}` are no longer Stoplight-shaped notifiers. Each now exposes `call(event_name, payload)` and is registered as an event subscriber by the gem's Railtie. They are still considered an internal implementation detail — host apps that want their own behaviour should subscribe to the `standard_circuit.*` namespace directly rather than instantiating these classes.
|
|
43
|
+
- **BREAKING.** `Config#add_notifier` now requires the supplied object to respond to `call(event_name, payload)`. Stoplight-shaped 4-arg notifiers from 0.1.x are rejected with `ArgumentError`. Callers should subscribe via `Rails.event.subscribe` / `ActiveSupport::Notifications.subscribe("standard_circuit.*")` for full control, or pass a lambda to `add_notifier` for the simple case.
|
|
44
|
+
- **BREAKING.** Stoplight only sees a single internal `StandardCircuit::NotifierBridge` notifier now; host apps that previously read `StandardCircuit.config.notifiers` to build their own Stoplight light will need to register against the new event namespace instead.
|
|
45
|
+
- README "Quick start" and the `rails g standard_circuit:install` initializer template now use `ErrorTaxonomies::*.tracked` consistently (the S3 example in the template still showed the pre-0.1.2 `AdapterErrors::Aws.server_errors` form).
|
|
46
|
+
- The install template's "Extra notifiers" example now uses `add_notifier` with a 2-arg `call(name, payload)` callable, matching the contract `Config#add_notifier` enforces. The previous example wrote directly to `extra_notifiers <<` with a 1-arg lambda — both bypassing validation and using the wrong arity, so any consumer who uncommented it would get `ArgumentError: wrong number of arguments` on the first emitted event.
|
|
47
|
+
- `lib/standard_circuit/rspec.rb` now also tears down event subscribers between examples so a spec that subscribes manually doesn't leak listeners into the next.
|
|
48
|
+
- CI and release workflows migrated to the shared `rarebit-one/.github` reusable workflows (`reusable-gem-ci.yml@v1`, `reusable-gem-release.yml@v1`); `.github/workflows/ci.yml` and `release.yml` are now thin shims.
|
|
49
|
+
|
|
7
50
|
## [0.1.2] - 2026-04-27
|
|
8
51
|
|
|
9
52
|
### Added
|
|
@@ -37,6 +80,7 @@ All notable changes to this project will be documented in this file.
|
|
|
37
80
|
- `StandardCircuit.force_open`, `force_closed`, `reset_force!` + `require "standard_circuit/rspec"` for auto-cleanup.
|
|
38
81
|
|
|
39
82
|
### Changed
|
|
83
|
+
- Minimum Ruby version is now `>= 4.0` (was `>= 3.4`). CI tests all four published 4.0.x patches.
|
|
40
84
|
- Removed redundant `defined?(::Sentry::Metrics)` guards in `Runner`, `ControllerSupport`, and `Notifiers::Metrics`. `sentry-ruby` is a hard runtime dependency; the guards were dead code.
|
|
41
85
|
- Tightened `sentry-ruby` lower bound from `>= 5.0` to `>= 5.17`. `Sentry::Metrics` was introduced in 5.17; the previous floor let Bundler resolve a version where the metrics API does not exist.
|
|
42
86
|
|
data/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Rarebit One
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
|
@@ -6,7 +6,7 @@ Wraps the upstream `stoplight` gem with:
|
|
|
6
6
|
|
|
7
7
|
- Opinionated default error taxonomy (network errors track; caller/config errors do not)
|
|
8
8
|
- SDK-specific adapter error bundles (Stripe, AWS, Faraday, SMTP)
|
|
9
|
-
-
|
|
9
|
+
- Rails event emission (`standard_circuit.circuit.{opened,closed,degraded,fallback_invoked,registered}`) with built-in Logger, Sentry, and Sentry::Metrics subscribers
|
|
10
10
|
- ActiveStorage S3 adapter with per-bucket circuit keying
|
|
11
11
|
- Generic ActionMailer delivery-method wrapper (supports both instance and symbol `underlying:` forms)
|
|
12
12
|
- Controller concern for standardized 503 responses on `Stoplight::Error::RedLight`
|
|
@@ -19,6 +19,22 @@ Wraps the upstream `stoplight` gem with:
|
|
|
19
19
|
gem "standard_circuit", git: "https://github.com/rarebit-one/standard_circuit", ref: "<sha>"
|
|
20
20
|
```
|
|
21
21
|
|
|
22
|
+
Then run the install generator to drop a commented-out initializer into
|
|
23
|
+
`config/initializers/standard_circuit.rb`:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
bundle add standard_circuit
|
|
27
|
+
rails g standard_circuit:install
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Pass `--with-health-endpoint` to also generate
|
|
31
|
+
`config/initializers/standard_circuit_health.rb` (which requires the opt-in
|
|
32
|
+
health controller); the generator prints the matching route line for you to
|
|
33
|
+
add to `config/routes.rb`.
|
|
34
|
+
|
|
35
|
+
The generator is idempotent — re-running skips an existing initializer
|
|
36
|
+
unless you pass `--force`.
|
|
37
|
+
|
|
22
38
|
## Quick start
|
|
23
39
|
|
|
24
40
|
```ruby
|
|
@@ -30,7 +46,7 @@ StandardCircuit.configure do |c|
|
|
|
30
46
|
c.register(:stripe,
|
|
31
47
|
threshold: 5,
|
|
32
48
|
cool_off_time: 30,
|
|
33
|
-
tracked_errors: StandardCircuit::
|
|
49
|
+
tracked_errors: StandardCircuit::ErrorTaxonomies::Stripe.tracked,
|
|
34
50
|
skipped_errors: StandardCircuit::AdapterErrors::Stripe.caller_errors)
|
|
35
51
|
end
|
|
36
52
|
```
|
|
@@ -42,6 +58,120 @@ StandardCircuit.run(:stripe) do
|
|
|
42
58
|
end
|
|
43
59
|
```
|
|
44
60
|
|
|
61
|
+
## Circuit state storage (`data_store`)
|
|
62
|
+
|
|
63
|
+
Circuit state (failure counts, colors, locks) lives in a Stoplight data store. StandardCircuit defaults to `Stoplight::DataStore::Memory.new`, which is **per-process**: each Puma worker, Sidekiq/SolidQueue worker, and console gets its own independent view of every circuit.
|
|
64
|
+
|
|
65
|
+
That is the deliberate default for a Redis-free deployment, and it is usually the right one — a circuit exists to stop *this* process from hammering a dead upstream, and per-process thresholds mean one unlucky worker can't trip the breaker for everyone. But be explicit about what it implies:
|
|
66
|
+
|
|
67
|
+
- Thresholds are counted per process, so an app with 4 web workers tolerates roughly 4× the configured `threshold` in aggregate before every worker has tripped.
|
|
68
|
+
- `/health` reports the circuit colors of **the process that served the request**, so two consecutive probes can legitimately disagree while a circuit is tripping.
|
|
69
|
+
- `force_open` / `force_closed` and `reset!` affect only the calling process — they are test and console tools, not an operational kill switch.
|
|
70
|
+
|
|
71
|
+
Point `data_store` at a shared store if you want cross-process state instead:
|
|
72
|
+
|
|
73
|
+
```ruby
|
|
74
|
+
StandardCircuit.configure do |c|
|
|
75
|
+
# Default — per-process, no external dependency.
|
|
76
|
+
c.data_store = Stoplight::DataStore::Memory.new
|
|
77
|
+
|
|
78
|
+
# Shared across processes and hosts (requires the redis gem + a Redis server).
|
|
79
|
+
# c.data_store = Stoplight::DataStore::Redis.new(Redis.new(url: ENV["REDIS_URL"]))
|
|
80
|
+
end
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Sentry reporting
|
|
84
|
+
|
|
85
|
+
The built-in Sentry subscriber is on by default (`c.sentry_enabled = true`) and reports every circuit-open transition at a flat `:warning`, with the circuit name, colors, and error in `extra`.
|
|
86
|
+
|
|
87
|
+
Set `sentry_criticality_levels` to derive the level from the circuit's registered `criticality` instead. That also adds `circuit` / `circuit_criticality` tags and a stable `["circuit-open", <circuit>]` fingerprint, so Sentry alert rules can route on criticality (e.g. page on `circuit_criticality:critical`) and group per circuit:
|
|
88
|
+
|
|
89
|
+
```ruby
|
|
90
|
+
StandardCircuit.configure do |c|
|
|
91
|
+
# { critical: :error, standard: :warning, optional: :info }
|
|
92
|
+
c.sentry_criticality_levels = true
|
|
93
|
+
|
|
94
|
+
# Or override part of that map — unlisted criticalities keep the default.
|
|
95
|
+
# c.sentry_criticality_levels = { optional: :debug }
|
|
96
|
+
end
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
This is **opt-in, not the default**. Both the level and the fingerprint feed Sentry's alerting and issue grouping, so turning it on for existing apps at gem-upgrade time would silently change what pages and re-group open issues. Leaving `sentry_criticality_levels` unset keeps the flat `:warning` shape byte-for-byte.
|
|
100
|
+
|
|
101
|
+
If you want something else entirely, set `c.sentry_enabled = false` and subscribe to `standard_circuit.circuit.opened` yourself — the payload carries `criticality`.
|
|
102
|
+
|
|
103
|
+
## Events
|
|
104
|
+
|
|
105
|
+
Every circuit lifecycle moment is emitted as a Rails event. On Rails 8.1+ the canonical bus is `Rails.event`; on older Rails versions the gem transparently falls back to `ActiveSupport::Notifications`. Detection happens per-emit, so subscribers do not need to care which backend is live.
|
|
106
|
+
|
|
107
|
+
| Event | When it fires | Payload |
|
|
108
|
+
|-------|---------------|---------|
|
|
109
|
+
| `standard_circuit.circuit.opened` | RED transition (circuit tripped) | `circuit:, from_color:, to_color:, criticality:, error_class:, error_message:` |
|
|
110
|
+
| `standard_circuit.circuit.closed` | GREEN transition (recovered) | `circuit:, from_color:, to_color:, criticality:` |
|
|
111
|
+
| `standard_circuit.circuit.degraded` | YELLOW transition (half-open probe) | `circuit:, from_color:, to_color:, criticality:` |
|
|
112
|
+
| `standard_circuit.circuit.fallback_invoked` | Runner returned a fallback instead of raising RedLight | `circuit:, reason: (:circuit_open\|:forced_open), criticality:` |
|
|
113
|
+
| `standard_circuit.circuit.registered` | `Config#register` / `register_prefix` was called (see note below) | `circuit:, criticality:, scope: (:name\|:prefix)` |
|
|
114
|
+
| `standard_circuit.run.completed` | Every wrapped `StandardCircuit.run` call (success, failure, or circuit_open) | `circuit:, status: (:success\|:failure\|:circuit_open), duration_ms:, criticality:, error_class:, error_message:` |
|
|
115
|
+
|
|
116
|
+
> **Note on `standard_circuit.run.completed`:** the per-call event for cost / latency / success-rate dashboards. Fires on every `Runner#execute` invocation and on `force_open` runs; **not** emitted for `force_closed` runs (which intentionally bypass the runner). All payload keys are always present — `error_class` and `error_message` are `nil` on `:success`. Payload duration uses `duration_ms` (numeric), not `event.duration`, so subscribers work identically on the `Rails.event` and `ActiveSupport::Notifications` backends.
|
|
117
|
+
|
|
118
|
+
> **Note on `standard_circuit.circuit.registered`:** subscribers are wired up *after* the `StandardCircuit.configure` block yields, so any `c.register` calls inside that block fire before any subscriber can hear them. This event is reliable only for post-boot, dynamic `register` / `register_prefix` calls — do not rely on it for a boot-time circuit inventory.
|
|
119
|
+
|
|
120
|
+
Built-in subscribers (Logger / Sentry / Metrics) are registered automatically by the gem's Railtie. Host apps can subscribe to the namespace however they like:
|
|
121
|
+
|
|
122
|
+
```ruby
|
|
123
|
+
# Rails 8.1+
|
|
124
|
+
class MyAuditSubscriber
|
|
125
|
+
def emit(event)
|
|
126
|
+
return unless event[:name].start_with?("standard_circuit.")
|
|
127
|
+
Rails.logger.info("circuit event: #{event[:name]} #{event[:payload].inspect}")
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
Rails.event.subscribe(MyAuditSubscriber.new)
|
|
131
|
+
|
|
132
|
+
# Older Rails
|
|
133
|
+
ActiveSupport::Notifications.subscribe(/\Astandard_circuit\./) do |name, _start, _finish, _id, payload|
|
|
134
|
+
Rails.logger.info("circuit event: #{name} #{payload.inspect}")
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# Quick host-supplied callable (auto-wired at boot via the Railtie)
|
|
138
|
+
StandardCircuit.configure do |c|
|
|
139
|
+
c.add_notifier(->(name, payload) { MyAlerting.notify(name, payload) })
|
|
140
|
+
end
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## Streaming and non-controller contexts
|
|
144
|
+
|
|
145
|
+
`ControllerSupport.circuit_open_fallback` only works for non-streaming responses — once a `Live` controller has flushed any output, Rails can't render an error template over the wire. For a streaming controller, catch `Stoplight::Error::RedLight` *inside* the streaming proc and write a degraded payload before the stream closes:
|
|
146
|
+
|
|
147
|
+
```ruby
|
|
148
|
+
class Api::MessagesController < ApplicationController
|
|
149
|
+
include ActionController::Live
|
|
150
|
+
|
|
151
|
+
def stream
|
|
152
|
+
response.headers["Content-Type"] = "application/x-ndjson"
|
|
153
|
+
|
|
154
|
+
StandardCircuit.run(:openai) do
|
|
155
|
+
llm.stream do |chunk|
|
|
156
|
+
response.stream.write({ delta: chunk }.to_json + "\n")
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
rescue Stoplight::Error::RedLight
|
|
160
|
+
# Only reachable when the circuit was already open at call time —
|
|
161
|
+
# Stoplight raises RedLight before executing the block, not mid-stream.
|
|
162
|
+
# Errors raised mid-stream propagate as their original class through the
|
|
163
|
+
# `ensure` below; add a broader rescue if you also need to write a
|
|
164
|
+
# terminal NDJSON line for those.
|
|
165
|
+
response.stream.write({ error: "service_unavailable" }.to_json + "\n")
|
|
166
|
+
ensure
|
|
167
|
+
response.stream.close
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Same pattern applies in background jobs (where `circuit_open_fallback` doesn't help): wrap the work in `StandardCircuit.run` and rescue `Stoplight::Error::RedLight` to either `discard_on` (avoid thundering retries) or `retry_on` with backoff (defer until cool-off), depending on whether eventual delivery is required.
|
|
173
|
+
|
|
174
|
+
|
|
45
175
|
## Health endpoint
|
|
46
176
|
|
|
47
177
|
StandardCircuit ships an opt-in controller that renders `StandardCircuit.health_report` as JSON. It returns 503 when the rolled-up status is `:critical` (so orchestrators pull the instance out of rotation) and 200 otherwise.
|
|
@@ -59,7 +189,14 @@ end
|
|
|
59
189
|
|
|
60
190
|
The controller inherits from `ActionController::API` to sidestep app-level filters (authentication, bootstrap redirects, etc.) so probes can call it anonymously.
|
|
61
191
|
|
|
62
|
-
|
|
192
|
+
**If your app also mounts `StandardHealth::Engine` at `/health`, draw the aggregate route first:**
|
|
193
|
+
|
|
194
|
+
```ruby
|
|
195
|
+
get "/health", to: "standard_circuit/health#show" # aggregate — FIRST
|
|
196
|
+
mount StandardHealth::Engine => "/health", as: :standard_health
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
`StandardHealth::Engine` registers sub-paths only (`/alive`, `/ready`, `/diagnostics/env`) — it never serves the aggregate tier itself. An app that mounts the engine and assumes `/health` is covered silently has no aggregate tier at all, with no boot error and no failing route spec to reveal it. The ordering is load-bearing; draw the aggregate route explicitly, first.
|
|
63
200
|
|
|
64
201
|
## License
|
|
65
202
|
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
require "rails/generators"
|
|
2
|
+
|
|
3
|
+
module StandardCircuit
|
|
4
|
+
module Generators
|
|
5
|
+
# Installs StandardCircuit in a host Rails application.
|
|
6
|
+
#
|
|
7
|
+
# By default, writes config/initializers/standard_circuit.rb with
|
|
8
|
+
# commented-out examples covering the public Config DSL.
|
|
9
|
+
#
|
|
10
|
+
# When +--with-health-endpoint+ is passed, also writes
|
|
11
|
+
# config/initializers/standard_circuit_health.rb (which requires the
|
|
12
|
+
# opt-in HealthController) and prints the route line the host should
|
|
13
|
+
# add to config/routes.rb to expose the endpoint.
|
|
14
|
+
#
|
|
15
|
+
# Idempotent: re-running on an existing initializer logs and skips. Pass
|
|
16
|
+
# +--force+ to overwrite.
|
|
17
|
+
class InstallGenerator < Rails::Generators::Base
|
|
18
|
+
source_root File.expand_path("templates", __dir__)
|
|
19
|
+
|
|
20
|
+
desc <<~DESC
|
|
21
|
+
Installs StandardCircuit. By default this writes
|
|
22
|
+
config/initializers/standard_circuit.rb with commented-out examples
|
|
23
|
+
covering circuit registration, prefix registration, and notifier
|
|
24
|
+
wiring.
|
|
25
|
+
|
|
26
|
+
Pass --with-health-endpoint to also write
|
|
27
|
+
config/initializers/standard_circuit_health.rb (which requires the
|
|
28
|
+
opt-in HealthController) and print the route line to add to
|
|
29
|
+
config/routes.rb.
|
|
30
|
+
|
|
31
|
+
The generator is idempotent — already-installed initializers are
|
|
32
|
+
skipped with a clear message. Pass --force to overwrite.
|
|
33
|
+
DESC
|
|
34
|
+
|
|
35
|
+
class_option :with_health_endpoint, type: :boolean, default: false,
|
|
36
|
+
desc: "Also create config/initializers/standard_circuit_health.rb and print the route hint"
|
|
37
|
+
|
|
38
|
+
def create_initializer_file
|
|
39
|
+
path = "config/initializers/standard_circuit.rb"
|
|
40
|
+
if File.exist?(File.join(destination_root, path)) && !options[:force]
|
|
41
|
+
say_status("skip", "#{path} already present, skipping (use --force to overwrite)", :yellow)
|
|
42
|
+
return
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
template "initializer.rb.tt", path
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def create_health_initializer_file
|
|
49
|
+
return unless options[:with_health_endpoint]
|
|
50
|
+
|
|
51
|
+
path = "config/initializers/standard_circuit_health.rb"
|
|
52
|
+
if File.exist?(File.join(destination_root, path)) && !options[:force]
|
|
53
|
+
say_status("skip", "#{path} already present, skipping (use --force to overwrite)", :yellow)
|
|
54
|
+
else
|
|
55
|
+
template "health_initializer.rb.tt", path
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def print_health_route_hint
|
|
60
|
+
return unless options[:with_health_endpoint]
|
|
61
|
+
|
|
62
|
+
say ""
|
|
63
|
+
say "=" * 79
|
|
64
|
+
say "StandardCircuit health endpoint installed."
|
|
65
|
+
say ""
|
|
66
|
+
say "Add the following to config/routes.rb to expose the endpoint:"
|
|
67
|
+
say ""
|
|
68
|
+
say ' get "/health", to: "standard_circuit/health#show"'
|
|
69
|
+
say ""
|
|
70
|
+
say "If you also mount StandardHealth::Engine at \"/health\", draw the"
|
|
71
|
+
say "line above BEFORE the mount. That engine serves sub-paths only"
|
|
72
|
+
say "(/alive, /ready, /diagnostics/env), never the aggregate tier — so"
|
|
73
|
+
say "an app that mounts it and assumes \"/health\" is covered silently has"
|
|
74
|
+
say "no aggregate tier, with no boot error to warn you."
|
|
75
|
+
say ""
|
|
76
|
+
say "The controller returns 503 when the rolled-up circuit health is"
|
|
77
|
+
say ":critical and 200 otherwise — wire it up to your load balancer."
|
|
78
|
+
say "=" * 79
|
|
79
|
+
say ""
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# StandardCircuit health-check controller
|
|
2
|
+
# Generated by: rails g standard_circuit:install --with-health-endpoint
|
|
3
|
+
#
|
|
4
|
+
# The controller is opt-in: this require pulls it onto the autoload path so
|
|
5
|
+
# the route below resolves. Add the matching route to config/routes.rb:
|
|
6
|
+
#
|
|
7
|
+
# get "/health", to: "standard_circuit/health#show"
|
|
8
|
+
#
|
|
9
|
+
# ORDERING IS LOAD-BEARING: if your app also mounts StandardHealth::Engine at
|
|
10
|
+
# "/health", draw this aggregate route BEFORE the mount —
|
|
11
|
+
#
|
|
12
|
+
# get "/health", to: "standard_circuit/health#show" # aggregate — FIRST
|
|
13
|
+
# mount StandardHealth::Engine => "/health", as: :standard_health
|
|
14
|
+
#
|
|
15
|
+
# StandardHealth::Engine registers sub-paths only ("/alive", "/ready",
|
|
16
|
+
# "/diagnostics/env") — it never serves the aggregate tier itself. So an app
|
|
17
|
+
# that mounts the engine and assumes "/health" is covered has no aggregate tier
|
|
18
|
+
# at all, and nothing says so: no boot error, and no failing route spec unless
|
|
19
|
+
# one already exists. Draw this route explicitly, first.
|
|
20
|
+
#
|
|
21
|
+
# The endpoint renders StandardCircuit.health_report as JSON and returns
|
|
22
|
+
# 503 when the rolled-up status is :critical (so orchestrators pull the
|
|
23
|
+
# instance out of rotation) and 200 otherwise.
|
|
24
|
+
|
|
25
|
+
require "standard_circuit/health_controller"
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# StandardCircuit configuration
|
|
2
|
+
# Generated by: rails g standard_circuit:install
|
|
3
|
+
#
|
|
4
|
+
# StandardCircuit wraps the upstream `stoplight` gem with an opinionated
|
|
5
|
+
# error taxonomy, Sentry/metrics notifiers, ActiveStorage and ActionMailer
|
|
6
|
+
# adapters, and a Rails-aware health rollup.
|
|
7
|
+
#
|
|
8
|
+
# All commented-out lines below show the public DSL — uncomment and edit
|
|
9
|
+
# the ones that apply to your app.
|
|
10
|
+
|
|
11
|
+
StandardCircuit.configure do |config|
|
|
12
|
+
# ---------------------------------------------------------------------------
|
|
13
|
+
# Global settings
|
|
14
|
+
# ---------------------------------------------------------------------------
|
|
15
|
+
|
|
16
|
+
# Send Sentry warnings on GREEN -> RED transitions.
|
|
17
|
+
# Default: true
|
|
18
|
+
# config.sentry_enabled = true
|
|
19
|
+
|
|
20
|
+
# Report circuit-open events at a Sentry level chosen from the circuit's
|
|
21
|
+
# criticality, and add `circuit` / `circuit_criticality` tags plus a stable
|
|
22
|
+
# ["circuit-open", <circuit>] fingerprint so Sentry alert rules can route and
|
|
23
|
+
# group by circuit. Opt-in: leaving this unset keeps the flat :warning
|
|
24
|
+
# reporting (no tags, no fingerprint) that earlier versions used.
|
|
25
|
+
# true -> { critical: :error, standard: :warning, optional: :info }
|
|
26
|
+
# Hash -> the above, merged with your overrides
|
|
27
|
+
# Default: nil (flat :warning)
|
|
28
|
+
# config.sentry_criticality_levels = true
|
|
29
|
+
# config.sentry_criticality_levels = { optional: :debug }
|
|
30
|
+
|
|
31
|
+
# Prefix used for emitted metrics (e.g. "<prefix>.circuit_breaker",
|
|
32
|
+
# "<prefix>.request"). Default: "external"
|
|
33
|
+
# config.metric_prefix = "external"
|
|
34
|
+
|
|
35
|
+
# Logger used by the built-in Logger notifier. Falls back to nil (no
|
|
36
|
+
# logging) when unset; assign Rails.logger or a custom logger to enable.
|
|
37
|
+
# Default: nil
|
|
38
|
+
# config.logger = Rails.logger
|
|
39
|
+
|
|
40
|
+
# Stoplight data store. Default: in-memory (per-process). Use a shared
|
|
41
|
+
# store (e.g. Stoplight::DataStore::Redis) when you want circuit state
|
|
42
|
+
# shared across processes/hosts.
|
|
43
|
+
# Default: Stoplight::DataStore::Memory.new
|
|
44
|
+
# config.data_store = Stoplight::DataStore::Redis.new(Redis.new)
|
|
45
|
+
|
|
46
|
+
# ---------------------------------------------------------------------------
|
|
47
|
+
# Circuit registration
|
|
48
|
+
# ---------------------------------------------------------------------------
|
|
49
|
+
# Each call to `register` declares a named circuit. Supported options:
|
|
50
|
+
#
|
|
51
|
+
# threshold — failures before tripping (default: 3)
|
|
52
|
+
# cool_off_time — seconds before half-open retry (default: 30)
|
|
53
|
+
# window_size — seconds for the rolling failure window (default: 60)
|
|
54
|
+
# tracked_errors — error classes that count toward the threshold
|
|
55
|
+
# (default: StandardCircuit::NetworkErrors.defaults)
|
|
56
|
+
# skipped_errors — error classes that bypass the circuit (re-raised
|
|
57
|
+
# without counting — typically caller/validation errors)
|
|
58
|
+
# criticality — :critical | :standard | :optional
|
|
59
|
+
# (affects health rollup; default: :standard)
|
|
60
|
+
|
|
61
|
+
# config.register(:stripe,
|
|
62
|
+
# threshold: 5,
|
|
63
|
+
# cool_off_time: 60,
|
|
64
|
+
# tracked_errors: StandardCircuit::ErrorTaxonomies::Stripe.tracked,
|
|
65
|
+
# skipped_errors: StandardCircuit::AdapterErrors::Stripe.caller_errors,
|
|
66
|
+
# criticality: :critical)
|
|
67
|
+
|
|
68
|
+
# config.register(:smtp,
|
|
69
|
+
# tracked_errors: StandardCircuit::ErrorTaxonomies::Smtp.tracked,
|
|
70
|
+
# criticality: :standard)
|
|
71
|
+
|
|
72
|
+
# ---------------------------------------------------------------------------
|
|
73
|
+
# Prefix registration
|
|
74
|
+
# ---------------------------------------------------------------------------
|
|
75
|
+
# Use `register_prefix` to dynamically name circuits matching a pattern.
|
|
76
|
+
# The first matching prefix wins; the circuit name is "<prefix>_<suffix>".
|
|
77
|
+
|
|
78
|
+
# config.register_prefix(:s3,
|
|
79
|
+
# threshold: 10,
|
|
80
|
+
# tracked_errors: StandardCircuit::ErrorTaxonomies::Aws.tracked,
|
|
81
|
+
# criticality: :standard)
|
|
82
|
+
|
|
83
|
+
# ---------------------------------------------------------------------------
|
|
84
|
+
# Extra notifiers
|
|
85
|
+
# ---------------------------------------------------------------------------
|
|
86
|
+
# Built-in subscribers (Logger / Sentry / Metrics) are wired up automatically.
|
|
87
|
+
# For most cases, prefer subscribing to the `standard_circuit.*` namespace
|
|
88
|
+
# directly via `Rails.event.subscribe` (Rails 8.1+) or
|
|
89
|
+
# `ActiveSupport::Notifications.subscribe`. `add_notifier` is a convenience
|
|
90
|
+
# for registering a callable that follows the same `call(event_name, payload)`
|
|
91
|
+
# contract as the built-in subscribers.
|
|
92
|
+
|
|
93
|
+
# config.add_notifier(->(name, payload) { MyTracer.record(name, payload) })
|
|
94
|
+
|
|
95
|
+
# ---------------------------------------------------------------------------
|
|
96
|
+
# Health endpoint (see --with-health-endpoint)
|
|
97
|
+
# ---------------------------------------------------------------------------
|
|
98
|
+
# The aggregate route is `get "/health", to: "standard_circuit/health#show"`.
|
|
99
|
+
# If your app also mounts StandardHealth::Engine at "/health", draw the
|
|
100
|
+
# aggregate route BEFORE the mount. That engine registers sub-paths only
|
|
101
|
+
# ("/alive", "/ready", "/diagnostics/env") and never serves the aggregate tier
|
|
102
|
+
# itself, so an app that mounts it and assumes "/health" is covered silently
|
|
103
|
+
# has no aggregate tier — no boot error, no failing route spec.
|
|
104
|
+
end
|
|
@@ -34,10 +34,11 @@ module StandardCircuit
|
|
|
34
34
|
end
|
|
35
35
|
|
|
36
36
|
attr_accessor :sentry_enabled, :metric_prefix, :data_store, :logger
|
|
37
|
-
attr_reader :circuits, :prefixes, :extra_notifiers
|
|
37
|
+
attr_reader :circuits, :prefixes, :extra_notifiers, :sentry_criticality_levels
|
|
38
38
|
|
|
39
39
|
def initialize
|
|
40
40
|
@sentry_enabled = true
|
|
41
|
+
@sentry_criticality_levels = nil
|
|
41
42
|
@metric_prefix = "external"
|
|
42
43
|
@data_store = Stoplight::DataStore::Memory.new
|
|
43
44
|
@logger = nil
|
|
@@ -46,11 +47,20 @@ module StandardCircuit
|
|
|
46
47
|
@extra_notifiers = []
|
|
47
48
|
end
|
|
48
49
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
50
|
+
# Opt in to criticality-aware Sentry reporting for the built-in Sentry
|
|
51
|
+
# subscriber. Accepts:
|
|
52
|
+
#
|
|
53
|
+
# nil / false — (default) flat :warning for every circuit, no tags and
|
|
54
|
+
# no fingerprint. The 0.2.x behaviour.
|
|
55
|
+
# true — Notifiers::Sentry::DEFAULT_LEVELS
|
|
56
|
+
# ({ critical: :error, standard: :warning, optional: :info })
|
|
57
|
+
# Hash — DEFAULT_LEVELS merged with the given criticality => level
|
|
58
|
+
# pairs, so a partial map is enough.
|
|
59
|
+
#
|
|
60
|
+
# Stored normalized: the reader returns either nil or a frozen, complete
|
|
61
|
+
# criticality => level Hash.
|
|
62
|
+
def sentry_criticality_levels=(value)
|
|
63
|
+
@sentry_criticality_levels = normalize_sentry_criticality_levels(value)
|
|
54
64
|
end
|
|
55
65
|
|
|
56
66
|
def reset_registry!
|
|
@@ -60,14 +70,34 @@ module StandardCircuit
|
|
|
60
70
|
end
|
|
61
71
|
|
|
62
72
|
def register(name, **opts)
|
|
63
|
-
|
|
73
|
+
spec = CircuitSpec.build(**opts)
|
|
74
|
+
@circuits[name.to_sym] = spec
|
|
75
|
+
EventEmitter.emit("standard_circuit.circuit.registered",
|
|
76
|
+
circuit: name.to_s,
|
|
77
|
+
criticality: spec.criticality,
|
|
78
|
+
scope: :name)
|
|
79
|
+
spec
|
|
64
80
|
end
|
|
65
81
|
|
|
66
82
|
def register_prefix(prefix, **opts)
|
|
67
|
-
|
|
83
|
+
spec = CircuitSpec.build(**opts)
|
|
84
|
+
@prefixes[prefix.to_s] = spec
|
|
85
|
+
EventEmitter.emit("standard_circuit.circuit.registered",
|
|
86
|
+
circuit: prefix.to_s,
|
|
87
|
+
criticality: spec.criticality,
|
|
88
|
+
scope: :prefix)
|
|
89
|
+
spec
|
|
68
90
|
end
|
|
69
91
|
|
|
92
|
+
# Register a host-supplied subscriber. Subscribers must respond to
|
|
93
|
+
# `call(event_name, payload)` — Stoplight-shaped 4-arg notifiers from the
|
|
94
|
+
# 0.1.x API are no longer accepted as extras (Logger / Sentry / Metrics
|
|
95
|
+
# demonstrate the new shape).
|
|
70
96
|
def add_notifier(notifier)
|
|
97
|
+
unless notifier.respond_to?(:call)
|
|
98
|
+
raise ArgumentError,
|
|
99
|
+
"extra notifiers must respond to `call(event_name, payload)`; got #{notifier.class}"
|
|
100
|
+
end
|
|
71
101
|
@extra_notifiers << notifier
|
|
72
102
|
end
|
|
73
103
|
|
|
@@ -77,6 +107,39 @@ module StandardCircuit
|
|
|
77
107
|
|
|
78
108
|
private
|
|
79
109
|
|
|
110
|
+
def normalize_sentry_criticality_levels(value)
|
|
111
|
+
case value
|
|
112
|
+
when nil, false then nil
|
|
113
|
+
when true then Notifiers::Sentry::DEFAULT_LEVELS
|
|
114
|
+
when Hash then merge_sentry_criticality_levels(value)
|
|
115
|
+
else
|
|
116
|
+
raise ArgumentError,
|
|
117
|
+
"sentry_criticality_levels must be nil, true, false, or a Hash of " \
|
|
118
|
+
"criticality => Sentry level; got #{value.class}"
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def merge_sentry_criticality_levels(map)
|
|
123
|
+
normalized = map.to_h do |criticality, level|
|
|
124
|
+
# Check symbolizability before coercing: a key like 42 would otherwise
|
|
125
|
+
# raise NoMethodError instead of the ArgumentError this setter promises.
|
|
126
|
+
criticality = criticality.to_sym if criticality.respond_to?(:to_sym)
|
|
127
|
+
unless CRITICALITIES.include?(criticality)
|
|
128
|
+
raise ArgumentError,
|
|
129
|
+
"invalid criticality #{criticality.inspect} in sentry_criticality_levels; " \
|
|
130
|
+
"must be one of #{CRITICALITIES.inspect}"
|
|
131
|
+
end
|
|
132
|
+
unless level.respond_to?(:to_sym)
|
|
133
|
+
raise ArgumentError,
|
|
134
|
+
"invalid Sentry level #{level.inspect} for criticality #{criticality.inspect}; " \
|
|
135
|
+
"must be a Symbol or String"
|
|
136
|
+
end
|
|
137
|
+
[ criticality, level.to_sym ]
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
Notifiers::Sentry::DEFAULT_LEVELS.merge(normalized).freeze
|
|
141
|
+
end
|
|
142
|
+
|
|
80
143
|
def spec_for_prefix(name)
|
|
81
144
|
key = name.to_s
|
|
82
145
|
_matched_prefix, spec = @prefixes.find { |prefix, _| key.start_with?("#{prefix}_") }
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
module StandardCircuit
|
|
2
|
+
# Boot hook: register the internal Logger / Sentry / Metrics subscribers (and
|
|
3
|
+
# any `extra_notifiers` the host configured) against whichever event bus is
|
|
4
|
+
# live in this Rails version.
|
|
5
|
+
#
|
|
6
|
+
# We hook `after: :load_config_initializers` so any host-side
|
|
7
|
+
# `StandardCircuit.configure` block in `config/initializers/*` has finished
|
|
8
|
+
# running and `extra_notifiers` / `metric_prefix` / `logger` are in their
|
|
9
|
+
# final state when the subscriber set is built. This is independent of
|
|
10
|
+
# ActiveRecord — apps that don't load AR still need observability.
|
|
11
|
+
class Engine < ::Rails::Engine
|
|
12
|
+
# Brings StandardCircuit in line with the other engine gems in the family.
|
|
13
|
+
# Safe here specifically because this engine is library-only:
|
|
14
|
+
#
|
|
15
|
+
# * No `config/routes.rb`, so the `default_scope` isolate_namespace applies
|
|
16
|
+
# to the engine's own route set has nothing to scope.
|
|
17
|
+
# * No ActiveRecord models, so the `standard_circuit_` table_name_prefix it
|
|
18
|
+
# defines on the StandardCircuit module applies to nothing.
|
|
19
|
+
# * The host-drawn route the convention prescribes —
|
|
20
|
+
# `get "/health", to: "standard_circuit/health#show"` — resolves through
|
|
21
|
+
# the *application's* route set by constant lookup, which isolation does
|
|
22
|
+
# not touch. Verified by booting a real Rails app both ways (see
|
|
23
|
+
# spec/integration/health_route_boot_spec.rb, which fails if this ever
|
|
24
|
+
# regresses).
|
|
25
|
+
#
|
|
26
|
+
# One real consequence: `StandardCircuit::HealthController` now picks up the
|
|
27
|
+
# engine's (empty) url_helpers instead of the application's, so app path
|
|
28
|
+
# helpers inside it — or inside a host subclass of it — need a `main_app.`
|
|
29
|
+
# prefix. The controller only renders JSON, so nothing in-gem is affected.
|
|
30
|
+
isolate_namespace StandardCircuit
|
|
31
|
+
|
|
32
|
+
initializer "standard_circuit.subscribers", after: :load_config_initializers do
|
|
33
|
+
StandardCircuit.subscribers.setup!
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
module StandardCircuit
|
|
2
|
+
# Internal helper that emits StandardCircuit lifecycle events through whichever
|
|
3
|
+
# event reporter is live in the host process.
|
|
4
|
+
#
|
|
5
|
+
# - On Rails 8.1+, `Rails.event.notify(name, **payload)` is the canonical bus.
|
|
6
|
+
# - On older Rails (or any host without the structured reporter), we fall back
|
|
7
|
+
# to `ActiveSupport::Notifications.instrument(name, payload)`.
|
|
8
|
+
#
|
|
9
|
+
# Detection is performed at *call time* — the gem is required before Rails has
|
|
10
|
+
# finished booting, so we cannot cache the decision at load time.
|
|
11
|
+
#
|
|
12
|
+
# @api private
|
|
13
|
+
module EventEmitter
|
|
14
|
+
module_function
|
|
15
|
+
|
|
16
|
+
# Emit a single event. Both backends are best-effort: any exception raised
|
|
17
|
+
# by a subscriber is swallowed so circuit-breaker observability never takes
|
|
18
|
+
# down a circuit-protected request.
|
|
19
|
+
def emit(event_name, payload)
|
|
20
|
+
if rails_event_available?
|
|
21
|
+
::Rails.event.notify(event_name, **payload)
|
|
22
|
+
else
|
|
23
|
+
::ActiveSupport::Notifications.instrument(event_name, payload)
|
|
24
|
+
end
|
|
25
|
+
rescue => e
|
|
26
|
+
warn "[StandardCircuit] event emit for #{event_name.inspect} failed: #{e.class}: #{e.message}"
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def rails_event_available?
|
|
30
|
+
defined?(::Rails) &&
|
|
31
|
+
::Rails.respond_to?(:event) &&
|
|
32
|
+
::Rails.event.respond_to?(:notify)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
module StandardCircuit
|
|
2
|
+
# Stoplight-shaped notifier whose only job is to translate the upstream
|
|
3
|
+
# `notifier.notify(light, from_color, to_color, error)` callback into a
|
|
4
|
+
# StandardCircuit Rails event.
|
|
5
|
+
#
|
|
6
|
+
# Stoplight calls notifiers on every color transition (GREEN<->RED, RED->YELLOW
|
|
7
|
+
# for half-open recovery). We map each transition to a stable event name and
|
|
8
|
+
# forward a uniform payload to whichever event bus is live.
|
|
9
|
+
#
|
|
10
|
+
# This is the single Stoplight notifier StandardCircuit registers — Logger,
|
|
11
|
+
# Sentry, and Metrics are now subscribers, not direct notifiers.
|
|
12
|
+
#
|
|
13
|
+
# @api private
|
|
14
|
+
class NotifierBridge
|
|
15
|
+
EVENT_FOR_COLOR = {
|
|
16
|
+
Stoplight::Color::RED => "standard_circuit.circuit.opened",
|
|
17
|
+
Stoplight::Color::GREEN => "standard_circuit.circuit.closed",
|
|
18
|
+
Stoplight::Color::YELLOW => "standard_circuit.circuit.degraded"
|
|
19
|
+
}.freeze
|
|
20
|
+
|
|
21
|
+
def initialize(config)
|
|
22
|
+
@config = config
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def notify(light, from_color, to_color, error)
|
|
26
|
+
event_name = EVENT_FOR_COLOR[to_color]
|
|
27
|
+
return unless event_name
|
|
28
|
+
|
|
29
|
+
EventEmitter.emit(event_name, payload_for(light, from_color, to_color, error))
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def payload_for(light, from_color, to_color, error)
|
|
35
|
+
spec = @config.spec_for(light.name)
|
|
36
|
+
payload = {
|
|
37
|
+
circuit: light.name,
|
|
38
|
+
from_color: from_color,
|
|
39
|
+
to_color: to_color,
|
|
40
|
+
criticality: spec&.criticality
|
|
41
|
+
}
|
|
42
|
+
if error
|
|
43
|
+
payload[:error_class] = error.class.name
|
|
44
|
+
payload[:error_message] = error.message
|
|
45
|
+
end
|
|
46
|
+
payload
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -2,23 +2,43 @@ require "logger"
|
|
|
2
2
|
|
|
3
3
|
module StandardCircuit
|
|
4
4
|
module Notifiers
|
|
5
|
+
# Subscribes to standard_circuit.circuit.* events and writes a human-readable
|
|
6
|
+
# log line for each transition. Always-on by default; pass a custom logger
|
|
7
|
+
# via `StandardCircuit.config.logger=`.
|
|
5
8
|
class Logger
|
|
9
|
+
TRANSITION_EVENTS = %w[
|
|
10
|
+
standard_circuit.circuit.opened
|
|
11
|
+
standard_circuit.circuit.closed
|
|
12
|
+
standard_circuit.circuit.degraded
|
|
13
|
+
].freeze
|
|
14
|
+
|
|
6
15
|
def initialize(logger = nil)
|
|
7
16
|
@logger = logger || ::Logger.new($stdout)
|
|
8
17
|
end
|
|
9
18
|
|
|
10
|
-
def
|
|
11
|
-
|
|
12
|
-
|
|
19
|
+
def call(event_name, payload)
|
|
20
|
+
return unless TRANSITION_EVENTS.include?(event_name)
|
|
21
|
+
|
|
22
|
+
message = build_message(event_name, payload)
|
|
23
|
+
level = event_name == "standard_circuit.circuit.opened" ? :warn : :info
|
|
13
24
|
@logger.public_send(level, message)
|
|
14
25
|
message
|
|
15
26
|
end
|
|
16
27
|
|
|
17
28
|
private
|
|
18
29
|
|
|
19
|
-
def build_message(
|
|
20
|
-
words = [
|
|
21
|
-
|
|
30
|
+
def build_message(event_name, payload)
|
|
31
|
+
words = [
|
|
32
|
+
"Stoplight",
|
|
33
|
+
payload[:circuit],
|
|
34
|
+
"switched from",
|
|
35
|
+
payload[:from_color],
|
|
36
|
+
"to",
|
|
37
|
+
payload[:to_color]
|
|
38
|
+
]
|
|
39
|
+
if payload[:error_class]
|
|
40
|
+
words += [ "because", payload[:error_class], payload[:error_message] ]
|
|
41
|
+
end
|
|
22
42
|
words.join(" ")
|
|
23
43
|
end
|
|
24
44
|
end
|
|
@@ -1,22 +1,26 @@
|
|
|
1
1
|
module StandardCircuit
|
|
2
2
|
module Notifiers
|
|
3
|
+
# Subscribes to all standard_circuit.circuit.{opened,closed,degraded} events
|
|
4
|
+
# and emits a Sentry::Metrics counter with the canonical state name.
|
|
3
5
|
class Metrics
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
STATE_FOR_EVENT = {
|
|
7
|
+
"standard_circuit.circuit.opened" => "opened",
|
|
8
|
+
"standard_circuit.circuit.closed" => "closed",
|
|
9
|
+
"standard_circuit.circuit.degraded" => "half_open"
|
|
8
10
|
}.freeze
|
|
9
11
|
|
|
10
12
|
def initialize(metric_prefix: "external")
|
|
11
13
|
@metric_prefix = metric_prefix
|
|
12
14
|
end
|
|
13
15
|
|
|
14
|
-
def
|
|
15
|
-
state =
|
|
16
|
+
def call(event_name, payload)
|
|
17
|
+
state = STATE_FOR_EVENT[event_name]
|
|
18
|
+
return unless state
|
|
19
|
+
|
|
16
20
|
::Sentry::Metrics.count(
|
|
17
21
|
"#{@metric_prefix}.circuit_breaker",
|
|
18
22
|
value: 1,
|
|
19
|
-
attributes: { service:
|
|
23
|
+
attributes: { service: payload[:circuit], state: state }
|
|
20
24
|
)
|
|
21
25
|
end
|
|
22
26
|
end
|
|
@@ -1,24 +1,88 @@
|
|
|
1
1
|
module StandardCircuit
|
|
2
2
|
module Notifiers
|
|
3
|
+
# Subscribes to standard_circuit.circuit.opened and forwards a message to
|
|
4
|
+
# Sentry. Other transitions are ignored — only RED matters for alerting.
|
|
5
|
+
#
|
|
6
|
+
# Two reporting shapes:
|
|
7
|
+
#
|
|
8
|
+
# * **Flat** (default, and the only 0.2.x behaviour): every circuit reports
|
|
9
|
+
# at `:warning` with the circuit's colors and error in `extra`. No tags,
|
|
10
|
+
# no fingerprint.
|
|
11
|
+
# * **Criticality-aware** (opt in via `config.sentry_criticality_levels`):
|
|
12
|
+
# the level is chosen from the circuit's registered criticality, and the
|
|
13
|
+
# report gains `circuit` / `circuit_criticality` tags plus a stable
|
|
14
|
+
# `["circuit-open", circuit]` fingerprint so Sentry alert rules can route
|
|
15
|
+
# and group by circuit.
|
|
16
|
+
#
|
|
17
|
+
# Flat stays the default deliberately. Both the level and the fingerprint
|
|
18
|
+
# feed Sentry's alerting and grouping, so a gem bump must not silently
|
|
19
|
+
# re-page or re-group a host app's existing issues — see CHANGELOG.
|
|
3
20
|
class Sentry
|
|
4
|
-
|
|
5
|
-
|
|
21
|
+
OPENED_EVENT = "standard_circuit.circuit.opened".freeze
|
|
22
|
+
|
|
23
|
+
# Level used in flat mode, and the fallback for an unrecognised
|
|
24
|
+
# criticality in criticality-aware mode.
|
|
25
|
+
DEFAULT_LEVEL = :warning
|
|
26
|
+
|
|
27
|
+
# The recommended criticality -> Sentry level mapping, used when
|
|
28
|
+
# `config.sentry_criticality_levels = true`. :critical is loud enough to
|
|
29
|
+
# page; :optional stays informational so a flapping nice-to-have
|
|
30
|
+
# upstream doesn't cry wolf.
|
|
31
|
+
DEFAULT_LEVELS = {
|
|
32
|
+
critical: :error,
|
|
33
|
+
standard: :warning,
|
|
34
|
+
optional: :info
|
|
35
|
+
}.freeze
|
|
36
|
+
|
|
37
|
+
# @param levels [Hash{Symbol=>Symbol}, nil] criticality -> level map.
|
|
38
|
+
# nil (the default) selects flat :warning reporting.
|
|
39
|
+
def initialize(levels: nil)
|
|
40
|
+
@levels = levels
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def call(event_name, payload)
|
|
44
|
+
return unless event_name == OPENED_EVENT
|
|
6
45
|
return unless defined?(::Sentry) && ::Sentry.respond_to?(:capture_message)
|
|
7
46
|
|
|
8
|
-
|
|
47
|
+
@levels ? capture_criticality_aware(payload) : capture_flat(payload)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
def capture_flat(payload)
|
|
53
|
+
message = "Circuit breaker opened: #{payload[:circuit]}"
|
|
9
54
|
::Sentry.capture_message(
|
|
10
55
|
message,
|
|
11
|
-
level:
|
|
12
|
-
extra:
|
|
13
|
-
circuit: light.name,
|
|
14
|
-
from_color: from_color,
|
|
15
|
-
to_color: to_color,
|
|
16
|
-
error_class: error&.class&.name,
|
|
17
|
-
error_message: error&.message
|
|
18
|
-
}.compact
|
|
56
|
+
level: DEFAULT_LEVEL,
|
|
57
|
+
extra: base_extra(payload)
|
|
19
58
|
)
|
|
20
59
|
message
|
|
21
60
|
end
|
|
61
|
+
|
|
62
|
+
def capture_criticality_aware(payload)
|
|
63
|
+
circuit = payload[:circuit].to_s
|
|
64
|
+
criticality = (payload[:criticality] || Config::DEFAULT_CRITICALITY).to_sym
|
|
65
|
+
message = "Circuit breaker opened: #{circuit} (#{criticality})"
|
|
66
|
+
|
|
67
|
+
::Sentry.capture_message(
|
|
68
|
+
message,
|
|
69
|
+
level: @levels.fetch(criticality, DEFAULT_LEVEL),
|
|
70
|
+
tags: { circuit: circuit, circuit_criticality: criticality.to_s },
|
|
71
|
+
fingerprint: [ "circuit-open", circuit ],
|
|
72
|
+
extra: base_extra(payload).merge(circuit: circuit, criticality: criticality)
|
|
73
|
+
)
|
|
74
|
+
message
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def base_extra(payload)
|
|
78
|
+
{
|
|
79
|
+
circuit: payload[:circuit],
|
|
80
|
+
from_color: payload[:from_color],
|
|
81
|
+
to_color: payload[:to_color],
|
|
82
|
+
error_class: payload[:error_class],
|
|
83
|
+
error_message: payload[:error_message]
|
|
84
|
+
}.compact
|
|
85
|
+
end
|
|
22
86
|
end
|
|
23
87
|
end
|
|
24
88
|
end
|
|
@@ -5,14 +5,27 @@ require "standard_circuit"
|
|
|
5
5
|
# - Clears the light cache so rebuilt lights pick up fresh config if a spec
|
|
6
6
|
# mutates it.
|
|
7
7
|
# - Clears forced states (force_open / force_closed).
|
|
8
|
+
# - Tears down all event subscribers so a spec that subscribes manually doesn't
|
|
9
|
+
# leak listeners into the next example. The internal Logger/Sentry/Metrics
|
|
10
|
+
# subscribers are re-registered if a spec calls `StandardCircuit.configure`
|
|
11
|
+
# or `StandardCircuit.subscribers.setup!`.
|
|
8
12
|
# - Swaps a fresh Stoplight::DataStore::Memory into the Config when the
|
|
9
13
|
# current store is already Memory, so failure counters from one spec don't
|
|
10
14
|
# leak into the next. Redis stores are left alone.
|
|
11
15
|
#
|
|
16
|
+
# Circuit registrations are intentionally NOT cleared. Host apps usually
|
|
17
|
+
# define circuits once in a `config/initializers/standard_circuit.rb`
|
|
18
|
+
# initializer; clearing the registry between examples would force every spec
|
|
19
|
+
# to re-`configure`. Specs that register circuits in `before(:each)` are
|
|
20
|
+
# unaffected by leftover registrations from prior examples (the new register
|
|
21
|
+
# call wins). If you do need a clean registry, call
|
|
22
|
+
# `StandardCircuit.config.reset_registry!` explicitly in your own hook.
|
|
23
|
+
#
|
|
12
24
|
# This is intentionally `before(:each)` rather than `after(:each)` so the
|
|
13
25
|
# setup happens even when a previous example aborted in an after hook.
|
|
14
26
|
RSpec.configure do |config|
|
|
15
27
|
config.before(:each) do
|
|
16
28
|
StandardCircuit.reset!
|
|
29
|
+
StandardCircuit.subscribers.teardown!
|
|
17
30
|
end
|
|
18
31
|
end
|
|
@@ -86,21 +86,32 @@ module StandardCircuit
|
|
|
86
86
|
light = light_for(name)
|
|
87
87
|
|
|
88
88
|
result = fallback ? light.run(fallback, &block) : light.run(&block)
|
|
89
|
-
|
|
89
|
+
duration = duration_ms(started_at)
|
|
90
|
+
emit_request_metric(name, :success, duration)
|
|
91
|
+
emit_run_completed(name, status: :success, duration_ms: duration)
|
|
90
92
|
result
|
|
91
93
|
rescue Stoplight::Error::RedLight => e
|
|
92
|
-
|
|
94
|
+
duration = duration_ms(started_at)
|
|
95
|
+
emit_request_metric(name, :circuit_open, duration)
|
|
96
|
+
emit_run_completed(name, status: :circuit_open, duration_ms: duration, error: e)
|
|
93
97
|
raise e unless fallback
|
|
94
98
|
|
|
99
|
+
emit_fallback_invoked(name, reason: :circuit_open)
|
|
95
100
|
fallback.call(nil)
|
|
96
101
|
rescue StandardError => e
|
|
97
|
-
|
|
102
|
+
duration = duration_ms(started_at)
|
|
103
|
+
emit_request_metric(name, :failure, duration)
|
|
104
|
+
emit_run_completed(name, status: :failure, duration_ms: duration, error: e)
|
|
98
105
|
raise e
|
|
99
106
|
end
|
|
100
107
|
|
|
101
108
|
def run_forced_open(name, fallback)
|
|
102
109
|
emit_request_metric(name, :circuit_open, 0)
|
|
103
|
-
|
|
110
|
+
emit_run_completed(name, status: :circuit_open, duration_ms: 0)
|
|
111
|
+
if fallback
|
|
112
|
+
emit_fallback_invoked(name, reason: :forced_open)
|
|
113
|
+
return fallback.call(nil)
|
|
114
|
+
end
|
|
104
115
|
|
|
105
116
|
spec = @config.spec_for(name)
|
|
106
117
|
raise Stoplight::Error::RedLight.new(
|
|
@@ -138,14 +149,10 @@ module StandardCircuit
|
|
|
138
149
|
tracked_errors: spec.tracked_errors,
|
|
139
150
|
skipped_errors: spec.skipped_errors,
|
|
140
151
|
data_store: @config.data_store,
|
|
141
|
-
notifiers:
|
|
152
|
+
notifiers: [ NotifierBridge.new(@config) ]
|
|
142
153
|
)
|
|
143
154
|
end
|
|
144
155
|
|
|
145
|
-
def notifiers
|
|
146
|
-
@config.notifiers
|
|
147
|
-
end
|
|
148
|
-
|
|
149
156
|
def emit_request_metric(name, status, duration)
|
|
150
157
|
prefix = @config.metric_prefix
|
|
151
158
|
attrs = { service: name.to_s, status: status.to_s }
|
|
@@ -153,6 +160,25 @@ module StandardCircuit
|
|
|
153
160
|
::Sentry::Metrics.distribution("#{prefix}.request.duration", duration, unit: "millisecond", attributes: attrs)
|
|
154
161
|
end
|
|
155
162
|
|
|
163
|
+
def emit_fallback_invoked(name, reason:)
|
|
164
|
+
spec = @config.spec_for(name)
|
|
165
|
+
EventEmitter.emit("standard_circuit.circuit.fallback_invoked",
|
|
166
|
+
circuit: name.to_s,
|
|
167
|
+
reason: reason,
|
|
168
|
+
criticality: spec&.criticality)
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
def emit_run_completed(name, status:, duration_ms:, error: nil)
|
|
172
|
+
spec = @config.spec_for(name)
|
|
173
|
+
EventEmitter.emit("standard_circuit.run.completed",
|
|
174
|
+
circuit: name.to_s,
|
|
175
|
+
status: status,
|
|
176
|
+
duration_ms: duration_ms,
|
|
177
|
+
criticality: spec&.criticality,
|
|
178
|
+
error_class: error&.class&.name,
|
|
179
|
+
error_message: error&.message)
|
|
180
|
+
end
|
|
181
|
+
|
|
156
182
|
def monotonic_now
|
|
157
183
|
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
158
184
|
end
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
module StandardCircuit
|
|
2
|
+
# Registers internal and user-supplied subscribers against whichever event
|
|
3
|
+
# bus is live (Rails.event on 8.1+, ActiveSupport::Notifications elsewhere).
|
|
4
|
+
#
|
|
5
|
+
# Each subscriber must respond to `call(event_name, payload)`. Internal
|
|
6
|
+
# subscribers (Logger / Sentry / Metrics) are built from the live config so
|
|
7
|
+
# changes to `metric_prefix` / `logger` propagate when `setup!` is re-run.
|
|
8
|
+
#
|
|
9
|
+
# Subscriptions cover the namespace prefix `standard_circuit.`, so a single
|
|
10
|
+
# registration on each backend listens for every lifecycle event the gem
|
|
11
|
+
# emits — bridge color transitions plus runner-side fallback / registration.
|
|
12
|
+
#
|
|
13
|
+
# @api private
|
|
14
|
+
class Subscribers
|
|
15
|
+
EVENT_PATTERN = "standard_circuit."
|
|
16
|
+
EVENT_REGEXP = /\A#{Regexp.escape(EVENT_PATTERN)}/
|
|
17
|
+
|
|
18
|
+
def initialize
|
|
19
|
+
@rails_event_subscribers = []
|
|
20
|
+
@as_subscribers = []
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def setup!
|
|
24
|
+
teardown!
|
|
25
|
+
register(internal_subscribers + extra_subscribers)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Tear down both backends. Rails.event subscribers are unsubscribed
|
|
29
|
+
# unconditionally — we recorded them at registration time, so we must
|
|
30
|
+
# remove them even if Rails.event has since become unavailable (e.g. test
|
|
31
|
+
# `hide_const("Rails")`). Otherwise the wrappers would remain live in the
|
|
32
|
+
# bus while we believe they are gone.
|
|
33
|
+
def teardown!
|
|
34
|
+
@rails_event_subscribers.each do |subscriber|
|
|
35
|
+
::Rails.event.unsubscribe(subscriber) if EventEmitter.rails_event_available?
|
|
36
|
+
rescue StandardError
|
|
37
|
+
# If Rails.event is gone (test isolation), we can do nothing more —
|
|
38
|
+
# clearing the array still releases our reference.
|
|
39
|
+
end
|
|
40
|
+
@rails_event_subscribers.clear
|
|
41
|
+
|
|
42
|
+
@as_subscribers.each do |subscriber|
|
|
43
|
+
::ActiveSupport::Notifications.unsubscribe(subscriber)
|
|
44
|
+
end
|
|
45
|
+
@as_subscribers.clear
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
private
|
|
49
|
+
|
|
50
|
+
def register(subscribers)
|
|
51
|
+
subscribers.each do |subscriber|
|
|
52
|
+
register_one(subscriber)
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Register on whichever backend is live. We do not double-subscribe — if
|
|
57
|
+
# `Rails.event` is present, every emit goes through it (see EventEmitter),
|
|
58
|
+
# so the AS::Notifications side would never receive anything.
|
|
59
|
+
def register_one(subscriber)
|
|
60
|
+
if EventEmitter.rails_event_available?
|
|
61
|
+
wrapper = RailsEventAdapter.new(subscriber)
|
|
62
|
+
::Rails.event.subscribe(wrapper)
|
|
63
|
+
@rails_event_subscribers << wrapper
|
|
64
|
+
else
|
|
65
|
+
handle = ::ActiveSupport::Notifications.subscribe(EVENT_REGEXP) do |name, _start, _finish, _id, payload|
|
|
66
|
+
subscriber.call(name, payload)
|
|
67
|
+
end
|
|
68
|
+
@as_subscribers << handle
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def internal_subscribers
|
|
73
|
+
config = StandardCircuit.config
|
|
74
|
+
list = [ Notifiers::Logger.new(config.logger) ]
|
|
75
|
+
list << Notifiers::Sentry.new(levels: config.sentry_criticality_levels) if config.sentry_enabled
|
|
76
|
+
list << Notifiers::Metrics.new(metric_prefix: config.metric_prefix)
|
|
77
|
+
list
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Config#add_notifier already enforces that every entry responds to :call,
|
|
81
|
+
# so we can hand the array straight through to register/1.
|
|
82
|
+
def extra_subscribers
|
|
83
|
+
StandardCircuit.config.extra_notifiers
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Adapts a `call(name, payload)` subscriber to the Rails.event#subscribe
|
|
87
|
+
# contract, which delivers a Hash event with :name / :payload / :context /
|
|
88
|
+
# :tags / :source_location.
|
|
89
|
+
class RailsEventAdapter
|
|
90
|
+
def initialize(subscriber)
|
|
91
|
+
@subscriber = subscriber
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def emit(event)
|
|
95
|
+
name = event[:name]
|
|
96
|
+
return unless name&.start_with?(EVENT_PATTERN)
|
|
97
|
+
|
|
98
|
+
@subscriber.call(name, event[:payload] || {})
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
data/lib/standard_circuit.rb
CHANGED
|
@@ -9,15 +9,19 @@ require "standard_circuit/adapter_errors/aws"
|
|
|
9
9
|
require "standard_circuit/adapter_errors/faraday"
|
|
10
10
|
require "standard_circuit/adapter_errors/smtp"
|
|
11
11
|
require "standard_circuit/error_taxonomies"
|
|
12
|
+
require "standard_circuit/event_emitter"
|
|
13
|
+
require "standard_circuit/notifier_bridge"
|
|
12
14
|
require "standard_circuit/notifiers/logger"
|
|
13
15
|
require "standard_circuit/notifiers/sentry"
|
|
14
16
|
require "standard_circuit/notifiers/metrics"
|
|
17
|
+
require "standard_circuit/subscribers"
|
|
15
18
|
require "standard_circuit/config"
|
|
16
19
|
require "standard_circuit/health"
|
|
17
20
|
require "standard_circuit/runner"
|
|
18
21
|
require "standard_circuit/mailer/circuit_open_error"
|
|
19
22
|
require "standard_circuit/mailer/delivery_method"
|
|
20
23
|
require "standard_circuit/controller_support"
|
|
24
|
+
require "standard_circuit/engine" if defined?(::Rails::Engine)
|
|
21
25
|
|
|
22
26
|
module StandardCircuit
|
|
23
27
|
class Error < StandardError; end
|
|
@@ -27,6 +31,7 @@ module StandardCircuit
|
|
|
27
31
|
def configure
|
|
28
32
|
yield config
|
|
29
33
|
runner.apply_config!(config)
|
|
34
|
+
subscribers.setup!
|
|
30
35
|
config
|
|
31
36
|
end
|
|
32
37
|
|
|
@@ -38,6 +43,10 @@ module StandardCircuit
|
|
|
38
43
|
@runner ||= Runner.new
|
|
39
44
|
end
|
|
40
45
|
|
|
46
|
+
def subscribers
|
|
47
|
+
@subscribers ||= Subscribers.new
|
|
48
|
+
end
|
|
49
|
+
|
|
41
50
|
def run(name, fallback: nil, &block)
|
|
42
51
|
runner.run(name, fallback: fallback, &block)
|
|
43
52
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: standard_circuit
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Jaryl Sim
|
|
@@ -65,6 +65,48 @@ dependencies:
|
|
|
65
65
|
- - ">="
|
|
66
66
|
- !ruby/object:Gem::Version
|
|
67
67
|
version: '8.0'
|
|
68
|
+
- !ruby/object:Gem::Dependency
|
|
69
|
+
name: brakeman
|
|
70
|
+
requirement: !ruby/object:Gem::Requirement
|
|
71
|
+
requirements:
|
|
72
|
+
- - ">="
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: '0'
|
|
75
|
+
type: :development
|
|
76
|
+
prerelease: false
|
|
77
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
78
|
+
requirements:
|
|
79
|
+
- - ">="
|
|
80
|
+
- !ruby/object:Gem::Version
|
|
81
|
+
version: '0'
|
|
82
|
+
- !ruby/object:Gem::Dependency
|
|
83
|
+
name: bundler-audit
|
|
84
|
+
requirement: !ruby/object:Gem::Requirement
|
|
85
|
+
requirements:
|
|
86
|
+
- - ">="
|
|
87
|
+
- !ruby/object:Gem::Version
|
|
88
|
+
version: '0'
|
|
89
|
+
type: :development
|
|
90
|
+
prerelease: false
|
|
91
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
92
|
+
requirements:
|
|
93
|
+
- - ">="
|
|
94
|
+
- !ruby/object:Gem::Version
|
|
95
|
+
version: '0'
|
|
96
|
+
- !ruby/object:Gem::Dependency
|
|
97
|
+
name: simplecov
|
|
98
|
+
requirement: !ruby/object:Gem::Requirement
|
|
99
|
+
requirements:
|
|
100
|
+
- - ">="
|
|
101
|
+
- !ruby/object:Gem::Version
|
|
102
|
+
version: '0'
|
|
103
|
+
type: :development
|
|
104
|
+
prerelease: false
|
|
105
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
106
|
+
requirements:
|
|
107
|
+
- - ">="
|
|
108
|
+
- !ruby/object:Gem::Version
|
|
109
|
+
version: '0'
|
|
68
110
|
description: StandardCircuit wraps the stoplight gem with opinionated error taxonomy,
|
|
69
111
|
Sentry notifiers, ActiveStorage S3 and ActionMailer adapters, and test helpers shared
|
|
70
112
|
across Rails apps.
|
|
@@ -75,10 +117,13 @@ extensions: []
|
|
|
75
117
|
extra_rdoc_files: []
|
|
76
118
|
files:
|
|
77
119
|
- CHANGELOG.md
|
|
78
|
-
-
|
|
120
|
+
- LICENSE
|
|
79
121
|
- README.md
|
|
80
122
|
- Rakefile
|
|
81
123
|
- lib/active_storage/service/standard_circuit_s3_service.rb
|
|
124
|
+
- lib/generators/standard_circuit/install/install_generator.rb
|
|
125
|
+
- lib/generators/standard_circuit/install/templates/health_initializer.rb.tt
|
|
126
|
+
- lib/generators/standard_circuit/install/templates/initializer.rb.tt
|
|
82
127
|
- lib/standard_circuit.rb
|
|
83
128
|
- lib/standard_circuit/active_storage/s3_service.rb
|
|
84
129
|
- lib/standard_circuit/adapter_errors/aws.rb
|
|
@@ -87,17 +132,21 @@ files:
|
|
|
87
132
|
- lib/standard_circuit/adapter_errors/stripe.rb
|
|
88
133
|
- lib/standard_circuit/config.rb
|
|
89
134
|
- lib/standard_circuit/controller_support.rb
|
|
135
|
+
- lib/standard_circuit/engine.rb
|
|
90
136
|
- lib/standard_circuit/error_taxonomies.rb
|
|
137
|
+
- lib/standard_circuit/event_emitter.rb
|
|
91
138
|
- lib/standard_circuit/health.rb
|
|
92
139
|
- lib/standard_circuit/health_controller.rb
|
|
93
140
|
- lib/standard_circuit/mailer/circuit_open_error.rb
|
|
94
141
|
- lib/standard_circuit/mailer/delivery_method.rb
|
|
95
142
|
- lib/standard_circuit/network_errors.rb
|
|
143
|
+
- lib/standard_circuit/notifier_bridge.rb
|
|
96
144
|
- lib/standard_circuit/notifiers/logger.rb
|
|
97
145
|
- lib/standard_circuit/notifiers/metrics.rb
|
|
98
146
|
- lib/standard_circuit/notifiers/sentry.rb
|
|
99
147
|
- lib/standard_circuit/rspec.rb
|
|
100
148
|
- lib/standard_circuit/runner.rb
|
|
149
|
+
- lib/standard_circuit/subscribers.rb
|
|
101
150
|
- lib/standard_circuit/version.rb
|
|
102
151
|
homepage: https://github.com/rarebit-one/standard_circuit
|
|
103
152
|
licenses:
|
|
@@ -114,7 +163,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
114
163
|
requirements:
|
|
115
164
|
- - ">="
|
|
116
165
|
- !ruby/object:Gem::Version
|
|
117
|
-
version: '
|
|
166
|
+
version: '4.0'
|
|
118
167
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
119
168
|
requirements:
|
|
120
169
|
- - ">="
|
data/MIT-LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 Jaryl Sim
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in
|
|
13
|
-
all copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
-
THE SOFTWARE.
|