star_gates 0.3.1 → 0.4.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/.release-please-manifest.json +1 -1
- data/CHANGELOG.md +26 -0
- data/README.md +86 -22
- data/Rakefile +6 -1
- data/lib/star_gates/adapters/base_adapter.rb +2 -0
- data/lib/star_gates/adapters/redis_adapter.rb +15 -10
- data/lib/star_gates/application_star_gate.rb +107 -117
- data/lib/star_gates/circuit.rb +74 -109
- data/lib/star_gates/configuration.rb +26 -9
- data/lib/star_gates/dsl.rb +7 -10
- data/lib/star_gates/errors.rb +15 -3
- data/lib/star_gates/event_store/event_store.rb +6 -16
- data/lib/star_gates/event_store/memory_adapter.rb +7 -1
- data/lib/star_gates/events/base_event.rb +3 -1
- data/lib/star_gates/feature.rb +36 -60
- data/lib/star_gates/flags.rb +102 -25
- data/lib/star_gates/gate.rb +58 -46
- data/lib/star_gates/hexagonal/feature_aggregate.rb +49 -55
- data/lib/star_gates/hexagonal/feature_repository.rb +5 -27
- data/lib/star_gates/hexagonal/feature_service.rb +31 -33
- data/lib/star_gates/jsonrpc_rails_adapter.rb +24 -7
- data/lib/star_gates/projections/feature_projection.rb +40 -13
- data/lib/star_gates/registry.rb +3 -2
- data/lib/star_gates/retry_policy.rb +23 -0
- data/lib/star_gates/rollout.rb +15 -0
- data/lib/star_gates/telemetry/opentelemetry_adapter.rb +71 -75
- data/lib/star_gates/telemetry/prometheus_adapter.rb +82 -74
- data/lib/star_gates/telemetry/telemetry_adapter.rb +16 -15
- data/lib/star_gates/version.rb +1 -1
- data/lib/star_gates.rb +56 -58
- data/sig/star_gates.rbs +832 -0
- metadata +42 -10
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require 'active_support/configurable'
|
|
4
3
|
require 'active_support/core_ext/class/attribute'
|
|
5
4
|
require 'active_support/core_ext/hash/deep_merge'
|
|
6
5
|
require 'active_support/core_ext/hash/indifferent_access'
|
|
6
|
+
require 'active_support/core_ext/string/inflections'
|
|
7
7
|
require 'active_support/concern'
|
|
8
8
|
require 'active_support/notifications'
|
|
9
9
|
require_relative 'configuration'
|
|
10
10
|
|
|
11
11
|
module StarGates
|
|
12
12
|
class ApplicationStarGate < Feature
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
class_attribute :
|
|
13
|
+
# The configured name; nil until `configure(name)`. Instances answer
|
|
14
|
+
# #feature_name, which falls back to the class name, never nil.
|
|
15
|
+
class_attribute :feature_name, instance_accessor: false
|
|
16
|
+
class_attribute :options, default: {}.freeze, instance_accessor: false
|
|
17
17
|
class_attribute :telemetry_tags, default: {}
|
|
18
18
|
class_attribute :state_configs, default: {}
|
|
19
19
|
|
|
@@ -22,7 +22,7 @@ module StarGates
|
|
|
22
22
|
@instance_config = ActiveSupport::HashWithIndifferentAccess.new(options)
|
|
23
23
|
|
|
24
24
|
# Merge with class-level config using deep_merge
|
|
25
|
-
@merged_config = self.class.
|
|
25
|
+
@merged_config = self.class.options.with_indifferent_access.deep_merge(@instance_config)
|
|
26
26
|
|
|
27
27
|
# Use configuration defaults if not provided
|
|
28
28
|
rollout ||= @merged_config[:default_rollout] || 0
|
|
@@ -35,11 +35,9 @@ module StarGates
|
|
|
35
35
|
@default_config = ActiveSupport::HashWithIndifferentAccess.new(state_configs[:disabled] || {})
|
|
36
36
|
@canary_config = ActiveSupport::HashWithIndifferentAccess.new(state_configs[:canary] || {})
|
|
37
37
|
|
|
38
|
-
#
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
# Setup error handling
|
|
42
|
-
setup_error_handling if error_reporting_enabled?
|
|
38
|
+
# Counters exist whatever telemetry says now: it is read per call and
|
|
39
|
+
# may be turned on after construction.
|
|
40
|
+
setup_counters
|
|
43
41
|
end
|
|
44
42
|
|
|
45
43
|
# Enhanced state-specific methods with instrumentation and telemetry
|
|
@@ -67,7 +65,6 @@ module StarGates
|
|
|
67
65
|
end
|
|
68
66
|
end
|
|
69
67
|
|
|
70
|
-
# Configuration access using ActiveSupport::Configurable
|
|
71
68
|
def timeout
|
|
72
69
|
@merged_config[:timeout]
|
|
73
70
|
end
|
|
@@ -102,10 +99,8 @@ module StarGates
|
|
|
102
99
|
}
|
|
103
100
|
end
|
|
104
101
|
|
|
105
|
-
#
|
|
106
|
-
def
|
|
107
|
-
self.class.feature_name || self.class.name.demodulize.underscore
|
|
108
|
-
end
|
|
102
|
+
# The configured name, else Feature's (the class name).
|
|
103
|
+
def feature_name = self.class.feature_name || super
|
|
109
104
|
|
|
110
105
|
# Feature-specific configuration with state awareness
|
|
111
106
|
def feature_config
|
|
@@ -126,8 +121,11 @@ module StarGates
|
|
|
126
121
|
return {} unless performance_monitoring_enabled?
|
|
127
122
|
|
|
128
123
|
{
|
|
129
|
-
average_execution_time:
|
|
130
|
-
|
|
124
|
+
average_execution_time: average_execution_time,
|
|
125
|
+
median_execution_time: latency_quantile(0.5),
|
|
126
|
+
p95_execution_time: latency_quantile(0.95),
|
|
127
|
+
p99_execution_time: latency_quantile(0.99),
|
|
128
|
+
total_executions: @execution_count,
|
|
131
129
|
error_rate: calculate_error_rate,
|
|
132
130
|
last_execution: @last_execution_time
|
|
133
131
|
}
|
|
@@ -140,11 +138,10 @@ module StarGates
|
|
|
140
138
|
configuration: configuration_summary,
|
|
141
139
|
performance: performance_stats,
|
|
142
140
|
environment: StarGates.configuration.environment,
|
|
143
|
-
feature_name:
|
|
141
|
+
feature_name: feature_name
|
|
144
142
|
)
|
|
145
143
|
end
|
|
146
144
|
|
|
147
|
-
# Class methods for configuration using ActiveSupport::Configurable
|
|
148
145
|
class << self
|
|
149
146
|
def configure(name = nil, **options)
|
|
150
147
|
self.feature_name = name if name
|
|
@@ -156,15 +153,12 @@ module StarGates
|
|
|
156
153
|
# Extract telemetry tags
|
|
157
154
|
self.telemetry_tags = extract_telemetry_tags(options)
|
|
158
155
|
|
|
159
|
-
|
|
160
|
-
remaining_options = options.except(*state_specific[:extracted_keys])
|
|
161
|
-
remaining_options.each do |key, value|
|
|
162
|
-
config.send("#{key}=", value)
|
|
163
|
-
end
|
|
156
|
+
self.options = self.options.merge(options.except(*state_specific[:extracted_keys])).freeze
|
|
164
157
|
end
|
|
165
158
|
|
|
166
|
-
|
|
167
|
-
|
|
159
|
+
# `timeout` in Integer nanoseconds.
|
|
160
|
+
def with_timeout(timeout, **options)
|
|
161
|
+
configure(timeout: timeout, **options)
|
|
168
162
|
end
|
|
169
163
|
|
|
170
164
|
def with_circuit_breaker(threshold: 5, **options)
|
|
@@ -172,13 +166,13 @@ module StarGates
|
|
|
172
166
|
end
|
|
173
167
|
|
|
174
168
|
def for_team(team_name, **options)
|
|
175
|
-
self.telemetry_tags = { team: team_name }
|
|
176
169
|
configure(**options)
|
|
170
|
+
self.telemetry_tags = { team: team_name }.merge(telemetry_tags)
|
|
177
171
|
end
|
|
178
172
|
|
|
179
173
|
def critical_feature(**options)
|
|
180
174
|
configure(
|
|
181
|
-
timeout:
|
|
175
|
+
timeout: 1_000_000_000,
|
|
182
176
|
retry_count: 5,
|
|
183
177
|
circuit_breaker_threshold: 10,
|
|
184
178
|
performance_monitoring: true,
|
|
@@ -188,7 +182,7 @@ module StarGates
|
|
|
188
182
|
|
|
189
183
|
def experimental_feature(**options)
|
|
190
184
|
configure(
|
|
191
|
-
timeout:
|
|
185
|
+
timeout: 10_000_000_000,
|
|
192
186
|
retry_count: 1,
|
|
193
187
|
performance_monitoring: true,
|
|
194
188
|
**options
|
|
@@ -230,53 +224,40 @@ module StarGates
|
|
|
230
224
|
@merged_config[:error_reporting]
|
|
231
225
|
end
|
|
232
226
|
|
|
233
|
-
def
|
|
234
|
-
@execution_times = []
|
|
227
|
+
def setup_counters
|
|
235
228
|
@execution_count = 0
|
|
236
229
|
@error_count = 0
|
|
237
230
|
@last_execution_time = nil
|
|
238
231
|
end
|
|
239
232
|
|
|
240
|
-
def setup_error_handling
|
|
241
|
-
# Setup error tracking and reporting
|
|
242
|
-
end
|
|
243
|
-
|
|
244
233
|
def instrument_execution(context)
|
|
245
234
|
return yield unless telemetry_enabled?
|
|
246
235
|
|
|
247
|
-
# Use ActiveSupport::Notifications for instrumentation
|
|
248
236
|
ActiveSupport::Notifications.instrument('star_gates.feature.execute',
|
|
249
|
-
feature:
|
|
250
|
-
status: status,
|
|
237
|
+
feature: feature_name, status: status,
|
|
251
238
|
context: context) do |payload|
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
status,
|
|
275
|
-
!defined?(e),
|
|
276
|
-
payload[:execution_time_ms],
|
|
277
|
-
context
|
|
278
|
-
)
|
|
279
|
-
end
|
|
239
|
+
timed_execution(payload, context) { yield }
|
|
240
|
+
end
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
# Times the block in monotonic Integer nanoseconds and reports it whether
|
|
244
|
+
# it returns or raises.
|
|
245
|
+
def timed_execution(payload, context)
|
|
246
|
+
started = StarGates.monotonic_time
|
|
247
|
+
succeeded = false
|
|
248
|
+
begin
|
|
249
|
+
result = yield
|
|
250
|
+
succeeded = true
|
|
251
|
+
result
|
|
252
|
+
rescue StandardError => e
|
|
253
|
+
payload[:error] = e.class.name
|
|
254
|
+
raise
|
|
255
|
+
ensure
|
|
256
|
+
execution_time = StarGates.monotonic_time - started
|
|
257
|
+
succeeded ? record_successful_execution(execution_time) : record_failed_execution(execution_time)
|
|
258
|
+
payload[:success] = succeeded
|
|
259
|
+
payload[:execution_time] = execution_time
|
|
260
|
+
StarGates.telemetry&.record_feature_execution(feature_name, status, succeeded, execution_time, context)
|
|
280
261
|
end
|
|
281
262
|
end
|
|
282
263
|
|
|
@@ -285,7 +266,7 @@ module StarGates
|
|
|
285
266
|
|
|
286
267
|
# Use ActiveSupport::Notifications for UI access tracking
|
|
287
268
|
ActiveSupport::Notifications.instrument('star_gates.ui_component.access',
|
|
288
|
-
feature:
|
|
269
|
+
feature: feature_name,
|
|
289
270
|
context: context) do |payload|
|
|
290
271
|
result = yield
|
|
291
272
|
payload[:component] = result
|
|
@@ -293,7 +274,7 @@ module StarGates
|
|
|
293
274
|
StarGates.telemetry&.record_counter(
|
|
294
275
|
'star_gates.ui_component.access',
|
|
295
276
|
1,
|
|
296
|
-
tags: telemetry_tags.merge(component: result, feature:
|
|
277
|
+
tags: telemetry_tags.merge(component: result, feature: feature_name)
|
|
297
278
|
)
|
|
298
279
|
|
|
299
280
|
result
|
|
@@ -305,7 +286,7 @@ module StarGates
|
|
|
305
286
|
|
|
306
287
|
# Use ActiveSupport::Notifications for config access tracking
|
|
307
288
|
ActiveSupport::Notifications.instrument('star_gates.config.access',
|
|
308
|
-
feature:
|
|
289
|
+
feature: feature_name,
|
|
309
290
|
config_key: key,
|
|
310
291
|
context: context) do |payload|
|
|
311
292
|
result = yield
|
|
@@ -314,52 +295,88 @@ module StarGates
|
|
|
314
295
|
StarGates.telemetry&.record_counter(
|
|
315
296
|
'star_gates.config.access',
|
|
316
297
|
1,
|
|
317
|
-
tags: telemetry_tags.merge(config_key: key, feature:
|
|
298
|
+
tags: telemetry_tags.merge(config_key: key, feature: feature_name)
|
|
318
299
|
)
|
|
319
300
|
|
|
320
301
|
result
|
|
321
302
|
end
|
|
322
303
|
end
|
|
323
304
|
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
305
|
+
# Runs the block within `timeout` Integer nanoseconds (nil: no limit of
|
|
306
|
+
# its own), narrowed by the ambient deadline. A budget already spent does
|
|
307
|
+
# not run the block: Timeout.timeout(0) would mean no limit at all.
|
|
308
|
+
def with_timeout(timeout, &)
|
|
309
|
+
DeadlineMachines.check!
|
|
310
|
+
budget = timeout.nil? ? DeadlineMachines.remaining : DeadlineMachines.clamp_sleep(timeout)
|
|
311
|
+
return yield if budget.nil?
|
|
312
|
+
raise timed_out(budget) unless budget.positive?
|
|
313
|
+
|
|
314
|
+
DeadlineMachines.with_deadline(budget) { Timeout.timeout(Rational(budget, NS_PER_SECOND), &) }
|
|
330
315
|
rescue Timeout::Error
|
|
331
|
-
raise
|
|
316
|
+
raise timed_out(budget)
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
def timed_out(budget)
|
|
320
|
+
UnavailableError.new(feature_name, :exceeded, "Feature #{feature_name} timed out after #{budget}ns")
|
|
332
321
|
end
|
|
333
322
|
|
|
334
323
|
def record_successful_execution(duration)
|
|
335
|
-
|
|
324
|
+
record_execution(duration)
|
|
325
|
+
end
|
|
336
326
|
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
@last_execution_time = Time.now
|
|
327
|
+
def record_failed_execution(duration)
|
|
328
|
+
return unless performance_monitoring_enabled?
|
|
340
329
|
|
|
341
|
-
|
|
342
|
-
|
|
330
|
+
@error_count += 1
|
|
331
|
+
record_execution(duration)
|
|
343
332
|
end
|
|
344
333
|
|
|
345
|
-
|
|
334
|
+
# Cluster budget for the latency digest; error is about one part in this.
|
|
335
|
+
LATENCY_COMPRESSION = 100
|
|
336
|
+
NS_PER_SECOND = 1_000_000_000
|
|
337
|
+
private_constant :LATENCY_COMPRESSION, :NS_PER_SECOND
|
|
338
|
+
|
|
339
|
+
def record_execution(duration)
|
|
346
340
|
return unless performance_monitoring_enabled?
|
|
347
341
|
|
|
348
|
-
|
|
342
|
+
latency.add(duration)
|
|
343
|
+
@latency_total = latency_total + duration
|
|
349
344
|
@execution_count += 1
|
|
350
|
-
@
|
|
351
|
-
|
|
345
|
+
@last_execution_time = StarGates.monotonic_time
|
|
346
|
+
end
|
|
347
|
+
|
|
348
|
+
def average_execution_time
|
|
349
|
+
return if @execution_count.zero?
|
|
350
|
+
|
|
351
|
+
latency_total / @execution_count
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
# A latency quantile in Integer nanoseconds, nil before any execution.
|
|
355
|
+
def latency_quantile(quantile)
|
|
356
|
+
return if @execution_count.zero?
|
|
357
|
+
|
|
358
|
+
latency.quantile(quantile).round
|
|
359
|
+
end
|
|
360
|
+
|
|
361
|
+
# Telemetry and performance monitoring are separate switches, so the digest
|
|
362
|
+
# is built where it is used rather than in either setup path.
|
|
363
|
+
def latency
|
|
364
|
+
@latency ||= ProbabilisticMachines::TDigest.new(LATENCY_COMPRESSION)
|
|
365
|
+
end
|
|
366
|
+
|
|
367
|
+
def latency_total
|
|
368
|
+
@latency_total ||= 0
|
|
352
369
|
end
|
|
353
370
|
|
|
354
371
|
def calculate_error_rate
|
|
355
|
-
return 0 if @execution_count.
|
|
372
|
+
return 0 if @execution_count.zero?
|
|
356
373
|
|
|
357
374
|
(@error_count.to_f / @execution_count * 100).round(2)
|
|
358
375
|
end
|
|
359
376
|
|
|
360
377
|
def telemetry_tags
|
|
361
378
|
base_tags = {
|
|
362
|
-
feature:
|
|
379
|
+
feature: feature_name,
|
|
363
380
|
state: status,
|
|
364
381
|
environment: StarGates.configuration.environment
|
|
365
382
|
}
|
|
@@ -367,32 +384,5 @@ module StarGates
|
|
|
367
384
|
class_tags = self.class.telemetry_tags || {}
|
|
368
385
|
base_tags.merge(class_tags)
|
|
369
386
|
end
|
|
370
|
-
|
|
371
|
-
# State-specific configuration overrides
|
|
372
|
-
def apply_state_specific_config
|
|
373
|
-
case status
|
|
374
|
-
when 'disabled'
|
|
375
|
-
@current_config = @default_config
|
|
376
|
-
when 'canary'
|
|
377
|
-
@current_config = @canary_config.any? ? @canary_config : @feature_config
|
|
378
|
-
when 'enabled'
|
|
379
|
-
@current_config = @feature_config
|
|
380
|
-
end
|
|
381
|
-
end
|
|
382
|
-
end
|
|
383
|
-
end
|
|
384
|
-
|
|
385
|
-
# Add string extensions for better naming
|
|
386
|
-
unless String.method_defined?(:demodulize)
|
|
387
|
-
class String
|
|
388
|
-
def demodulize
|
|
389
|
-
split('::').last
|
|
390
|
-
end
|
|
391
|
-
|
|
392
|
-
def underscore
|
|
393
|
-
gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
|
|
394
|
-
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
|
|
395
|
-
.downcase
|
|
396
|
-
end
|
|
397
387
|
end
|
|
398
388
|
end
|
data/lib/star_gates/circuit.rb
CHANGED
|
@@ -3,32 +3,53 @@
|
|
|
3
3
|
require 'chrono_machines'
|
|
4
4
|
|
|
5
5
|
module StarGates
|
|
6
|
+
# A breaker_machines circuit with optional chrono_machines retries, wired
|
|
7
|
+
# to a feature so an opening circuit rolls the feature back.
|
|
8
|
+
#
|
|
9
|
+
# `policy` (a BreakerMachines::Policy, spans in Integer nanoseconds) and
|
|
10
|
+
# `retry_policy` (a RetryPolicy or nil) are positional, as the cores take
|
|
11
|
+
# them. The keywords belong to this runtime and have no core counterpart:
|
|
12
|
+
#
|
|
13
|
+
# - `feature:` the feature to roll back when the circuit opens.
|
|
14
|
+
# - `auto_rollback:` whether to roll it back (true).
|
|
15
|
+
#
|
|
16
|
+
# A refused call (the circuit is open, busy or full) is never retried: the
|
|
17
|
+
# refusal is the circuit's answer, and waiting out a cooldown inside a
|
|
18
|
+
# request helps nobody. Exhausted retries re-raise the last exception.
|
|
6
19
|
class Circuit
|
|
7
|
-
|
|
8
|
-
|
|
20
|
+
HALT_ON_REFUSAL = lambda do |exception:, attempt:|
|
|
21
|
+
_ = attempt
|
|
22
|
+
:halt if exception.is_a?(BreakerMachines::CircuitRefusedError)
|
|
23
|
+
end
|
|
24
|
+
private_constant :HALT_ON_REFUSAL
|
|
25
|
+
|
|
26
|
+
attr_reader :breaker, :policy, :retry_policy, :feature
|
|
27
|
+
|
|
28
|
+
def initialize(policy, retry_policy = nil, feature: nil, auto_rollback: true)
|
|
29
|
+
unless retry_policy.nil? || retry_policy.instance_of?(RetryPolicy)
|
|
30
|
+
raise TypeError, "retry_policy must be a StarGates::RetryPolicy or nil, got #{retry_policy.inspect}"
|
|
31
|
+
end
|
|
32
|
+
unless [true, false].include?(auto_rollback)
|
|
33
|
+
raise TypeError, "auto_rollback must be true or false, got #{auto_rollback.inspect}"
|
|
34
|
+
end
|
|
9
35
|
|
|
10
|
-
|
|
11
|
-
@
|
|
12
|
-
@within = parse_time(within)
|
|
13
|
-
@reset_after = parse_time(reset_after)
|
|
36
|
+
@policy = policy
|
|
37
|
+
@retry_policy = retry_policy
|
|
14
38
|
@feature = feature
|
|
15
39
|
@auto_rollback = auto_rollback
|
|
16
|
-
@breaker =
|
|
17
|
-
|
|
40
|
+
@breaker = BreakerMachines::Circuit.new("star_gates_circuit_#{object_id}", policy,
|
|
41
|
+
exceptions: [StandardError], **rollback_hooks)
|
|
42
|
+
freeze
|
|
18
43
|
end
|
|
19
44
|
|
|
20
45
|
def call(&block)
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
breaker.call(&block)
|
|
26
|
-
end
|
|
27
|
-
else
|
|
46
|
+
return breaker.call(&block) unless retry_policy
|
|
47
|
+
|
|
48
|
+
ChronoMachines.retry(retry_policy.strategy, retry_policy.max_attempts,
|
|
49
|
+
delay_from: HALT_ON_REFUSAL, on_retry: method(:instrument_retry)) do
|
|
28
50
|
breaker.call(&block)
|
|
29
51
|
end
|
|
30
52
|
rescue ChronoMachines::MaxRetriesExceededError => e
|
|
31
|
-
# Re-raise the original exception after retries exhausted
|
|
32
53
|
raise e.original_exception
|
|
33
54
|
end
|
|
34
55
|
|
|
@@ -44,128 +65,72 @@ module StarGates
|
|
|
44
65
|
breaker.half_open?
|
|
45
66
|
end
|
|
46
67
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
stats.last_failure_at
|
|
68
|
+
# When the last failure was recorded, Integer nanoseconds on the
|
|
69
|
+
# breaker's clock, or nil.
|
|
70
|
+
def last_failure_at
|
|
71
|
+
breaker.stats.last_failure_at
|
|
51
72
|
end
|
|
52
73
|
|
|
53
74
|
def failure_count
|
|
54
|
-
|
|
55
|
-
stats = breaker.stats
|
|
56
|
-
stats.failure_count || 0
|
|
75
|
+
breaker.stats.failure_count
|
|
57
76
|
end
|
|
58
77
|
|
|
59
78
|
def success_count
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
79
|
+
breaker.stats.success_count
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def to_h
|
|
83
|
+
{ state: breaker.status_name, failure_count: failure_count, success_count: success_count,
|
|
84
|
+
last_failure_at: last_failure_at }
|
|
63
85
|
end
|
|
64
86
|
|
|
65
|
-
|
|
87
|
+
# Closes the circuit and clears its failure window.
|
|
88
|
+
def reset
|
|
66
89
|
breaker.reset
|
|
90
|
+
self
|
|
67
91
|
end
|
|
68
92
|
|
|
69
93
|
private
|
|
70
94
|
|
|
71
|
-
def
|
|
72
|
-
|
|
73
|
-
circuit_name = "star_gates_circuit_#{object_id}"
|
|
95
|
+
def rollback_hooks
|
|
96
|
+
return {} unless @feature && @auto_rollback
|
|
74
97
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
exceptions: [StandardError]
|
|
98
|
+
{
|
|
99
|
+
on_open: -> { handle_circuit_open },
|
|
100
|
+
on_half_open: -> { handle_circuit_half_open },
|
|
101
|
+
on_close: -> { handle_circuit_close }
|
|
80
102
|
}
|
|
103
|
+
end
|
|
81
104
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
config[:on_half_open] = -> { handle_circuit_half_open }
|
|
86
|
-
config[:on_close] = -> { handle_circuit_close }
|
|
87
|
-
end
|
|
105
|
+
def instrument_retry(exception:, attempt:, next_delay:)
|
|
106
|
+
StarGates.instrument('circuit.retry', exception: exception.class.name, attempt: attempt, next_delay: next_delay)
|
|
107
|
+
end
|
|
88
108
|
|
|
89
|
-
|
|
109
|
+
def feature_status
|
|
110
|
+
@feature.status if @feature.respond_to?(:status)
|
|
90
111
|
end
|
|
91
112
|
|
|
92
|
-
#
|
|
113
|
+
# Enabled rolls back to canary; canary rolls back to disabled.
|
|
93
114
|
def handle_circuit_open
|
|
94
115
|
return unless @feature.respond_to?(:status)
|
|
95
116
|
|
|
96
|
-
StarGates.instrument('circuit.auto_rollback',
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
# Auto-rollback feature state when circuit opens
|
|
103
|
-
case @feature.status
|
|
104
|
-
when 'enabled'
|
|
105
|
-
# Circuit is failing → rollback to canary
|
|
106
|
-
@feature.rollout_canary if @feature.respond_to?(:rollout_canary)
|
|
107
|
-
when 'canary'
|
|
108
|
-
# Circuit still failing in canary → disable completely
|
|
109
|
-
@feature.disable if @feature.respond_to?(:disable)
|
|
117
|
+
StarGates.instrument('circuit.auto_rollback', feature_status: feature_status, circuit_state: 'open',
|
|
118
|
+
action: 'rolling_back')
|
|
119
|
+
case feature_status
|
|
120
|
+
when 'enabled' then @feature.rollout_canary if @feature.respond_to?(:rollout_canary)
|
|
121
|
+
when 'canary' then @feature.disable if @feature.respond_to?(:disable)
|
|
110
122
|
end
|
|
111
123
|
end
|
|
112
124
|
|
|
113
125
|
def handle_circuit_half_open
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
StarGates.instrument('circuit.half_open', {
|
|
117
|
-
feature_status: @feature.status,
|
|
118
|
-
circuit_state: 'half_open',
|
|
119
|
-
action: 'testing_recovery'
|
|
120
|
-
})
|
|
126
|
+
StarGates.instrument('circuit.half_open', feature_status: feature_status, circuit_state: 'half_open',
|
|
127
|
+
action: 'testing_recovery')
|
|
121
128
|
end
|
|
122
129
|
|
|
130
|
+
# Recovery does not re-enable the feature: an operator decides that.
|
|
123
131
|
def handle_circuit_close
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
StarGates.instrument('circuit.recovered', {
|
|
127
|
-
feature_status: @feature.status,
|
|
128
|
-
circuit_state: 'closed',
|
|
129
|
-
action: 'circuit_healthy'
|
|
130
|
-
})
|
|
131
|
-
|
|
132
|
-
# Optionally: auto-restore to canary when circuit closes
|
|
133
|
-
# (conservative: don't auto-enable to 100%, just canary)
|
|
134
|
-
# Uncomment if you want auto-restoration:
|
|
135
|
-
# if @feature.status == 'disabled'
|
|
136
|
-
# @feature.rollout_canary if @feature.respond_to?(:rollout_canary)
|
|
137
|
-
# end
|
|
138
|
-
end
|
|
139
|
-
|
|
140
|
-
def parse_time(time)
|
|
141
|
-
# Rails duration helpers can return fractional seconds.
|
|
142
|
-
case time
|
|
143
|
-
when Numeric then time
|
|
144
|
-
when ActiveSupport::Duration then time.to_f
|
|
145
|
-
else time.to_i
|
|
146
|
-
end
|
|
147
|
-
end
|
|
148
|
-
|
|
149
|
-
def build_retry_policy(config)
|
|
150
|
-
return nil unless config
|
|
151
|
-
|
|
152
|
-
# Default retry policy for circuit breakers
|
|
153
|
-
defaults = {
|
|
154
|
-
max_attempts: 3,
|
|
155
|
-
base_delay: 0.1,
|
|
156
|
-
multiplier: 2,
|
|
157
|
-
max_delay: 5,
|
|
158
|
-
retryable_exceptions: [StandardError],
|
|
159
|
-
on_retry: lambda { |exception:, attempt:, next_delay:|
|
|
160
|
-
StarGates.instrument('circuit.retry', {
|
|
161
|
-
exception: exception.class.name,
|
|
162
|
-
attempt: attempt,
|
|
163
|
-
next_delay: next_delay
|
|
164
|
-
})
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
defaults.merge(config)
|
|
132
|
+
StarGates.instrument('circuit.recovered', feature_status: feature_status, circuit_state: 'closed',
|
|
133
|
+
action: 'circuit_healthy')
|
|
169
134
|
end
|
|
170
135
|
end
|
|
171
136
|
end
|