standard_flags 0.1.0 → 0.2.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 +229 -0
- data/README.md +837 -33
- data/lib/generators/standard_flags/admin/admin_generator.rb +115 -0
- data/lib/generators/standard_flags/admin/templates/controller.rb.tt +103 -0
- data/lib/generators/standard_flags/admin/templates/index.tsx.tt +64 -0
- data/lib/generators/standard_flags/admin/templates/show.tsx.tt +275 -0
- data/lib/generators/standard_flags/install/install_generator.rb +16 -1
- data/lib/generators/standard_flags/install/templates/flags.rb.tt +3 -2
- data/lib/generators/standard_flags/install/templates/initializer.rb.tt +10 -0
- data/lib/generators/standard_flags/migrations/migrations_generator.rb +57 -0
- data/lib/generators/standard_flags/migrations/templates/create_standard_flags_tables.rb.tt +78 -0
- data/lib/standard_flags/actor.rb +50 -0
- data/lib/standard_flags/audit_trail.rb +56 -0
- data/lib/standard_flags/config.rb +78 -0
- data/lib/standard_flags/context.rb +5 -1
- data/lib/standard_flags/definition.rb +85 -13
- data/lib/standard_flags/diagnostics.rb +35 -3
- data/lib/standard_flags/engine.rb +10 -2
- data/lib/standard_flags/env_pin.rb +15 -4
- data/lib/standard_flags/errors.rb +4 -0
- data/lib/standard_flags/evaluation.rb +5 -4
- data/lib/standard_flags/evaluator.rb +14 -1
- data/lib/standard_flags/events.rb +49 -0
- data/lib/standard_flags/health_check.rb +12 -2
- data/lib/standard_flags/health_contribution.rb +65 -0
- data/lib/standard_flags/mcp/cohorts.rb +65 -0
- data/lib/standard_flags/mcp/flags.rb +143 -0
- data/lib/standard_flags/mcp/members.rb +154 -0
- data/lib/standard_flags/mcp/writes.rb +98 -0
- data/lib/standard_flags/mcp.rb +238 -0
- data/lib/standard_flags/memo.rb +15 -1
- data/lib/standard_flags/notifiers/logger.rb +14 -0
- data/lib/standard_flags/open_feature/provider.rb +232 -0
- data/lib/standard_flags/open_feature.rb +26 -0
- data/lib/standard_flags/operations/audit.rb +218 -0
- data/lib/standard_flags/operations/base.rb +290 -0
- data/lib/standard_flags/operations/cohorts.rb +80 -0
- data/lib/standard_flags/operations/failure.rb +16 -0
- data/lib/standard_flags/operations/members.rb +164 -0
- data/lib/standard_flags/operations/restore.rb +330 -0
- data/lib/standard_flags/operations/restore_target.rb +197 -0
- data/lib/standard_flags/operations/rules.rb +233 -0
- data/lib/standard_flags/operations/validation.rb +154 -0
- data/lib/standard_flags/operations.rb +43 -0
- data/lib/standard_flags/parser.rb +16 -0
- data/lib/standard_flags/purge_job.rb +14 -0
- data/lib/standard_flags/registry.rb +38 -5
- data/lib/standard_flags/reporting.rb +51 -7
- data/lib/standard_flags/resolvers.rb +14 -7
- data/lib/standard_flags/result.rb +43 -0
- data/lib/standard_flags/rspec.rb +67 -13
- data/lib/standard_flags/runtime/active_record_store.rb +428 -0
- data/lib/standard_flags/runtime/cache.rb +98 -0
- data/lib/standard_flags/runtime/evaluation_tracker.rb +93 -0
- data/lib/standard_flags/runtime/memory_store.rb +408 -0
- data/lib/standard_flags/runtime/models.rb +43 -0
- data/lib/standard_flags/runtime/resolver.rb +256 -0
- data/lib/standard_flags/runtime/rule_snapshot.rb +60 -0
- data/lib/standard_flags/runtime/store.rb +161 -0
- data/lib/standard_flags/runtime/values.rb +181 -0
- data/lib/standard_flags/runtime.rb +266 -0
- data/lib/standard_flags/sensor.rb +12 -5
- data/lib/standard_flags/subscribers.rb +6 -0
- data/lib/standard_flags/testing.rb +100 -1
- data/lib/standard_flags/tooling/changes.rb +196 -0
- data/lib/standard_flags/tooling.rb +232 -19
- data/lib/standard_flags/version.rb +1 -1
- data/lib/standard_flags.rb +33 -5
- data/lib/tasks/standard_flags.rake +45 -1
- metadata +41 -1
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
module StandardFlags
|
|
2
|
+
module Operations
|
|
3
|
+
# Draft or paused → running, after checking everything the rule would
|
|
4
|
+
# serve (Base checks every running rule after the change).
|
|
5
|
+
class Start < Base
|
|
6
|
+
arguments
|
|
7
|
+
|
|
8
|
+
private
|
|
9
|
+
|
|
10
|
+
def event = Events::FLAG_EVENTS[:started]
|
|
11
|
+
|
|
12
|
+
def extra_cohort_keys
|
|
13
|
+
validate.attachments(definition, @rule)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def perform
|
|
17
|
+
unless @rule.draft? || @rule.paused?
|
|
18
|
+
raise Failure.new(:invalid, ":#{@key} is #{@rule.status}; only a draft or paused rule can start")
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
save!(status: "running")
|
|
22
|
+
nil
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Running → paused. A paused rule keeps its settings, so Start resumes
|
|
27
|
+
# it, and serves the code default meanwhile.
|
|
28
|
+
class Pause < Base
|
|
29
|
+
arguments
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def event = Events::FLAG_EVENTS[:paused]
|
|
34
|
+
|
|
35
|
+
def perform
|
|
36
|
+
raise Failure.new(:invalid, ":#{@key} is #{@rule.status}; only a running rule can pause") unless @rule.running?
|
|
37
|
+
|
|
38
|
+
save!(status: "paused")
|
|
39
|
+
nil
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# → stopped, serving `serve:` to everyone (the winner, or the flag's
|
|
44
|
+
# `final:`) until the code drops the flag.
|
|
45
|
+
class Stop < Base
|
|
46
|
+
arguments required: %i[serve]
|
|
47
|
+
|
|
48
|
+
private
|
|
49
|
+
|
|
50
|
+
def event = Events::FLAG_EVENTS[:stopped]
|
|
51
|
+
|
|
52
|
+
def perform
|
|
53
|
+
save!(status: "stopped", stop_value: validate.value!(definition, arg(:serve), "serve"))
|
|
54
|
+
nil
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Sets the percentage of units (in basis points, 0–10,000) that get the
|
|
59
|
+
# rollout value, and `value:`, the rollout value itself (on/off flags
|
|
60
|
+
# roll out to on). Reaching 10,000 records full_since.
|
|
61
|
+
class Ramp < Base
|
|
62
|
+
scope "flags:ramp"
|
|
63
|
+
arguments required: %i[bps], optional: %i[value]
|
|
64
|
+
|
|
65
|
+
private
|
|
66
|
+
|
|
67
|
+
def event = Events::FLAG_EVENTS[:ramped]
|
|
68
|
+
|
|
69
|
+
def perform
|
|
70
|
+
validate.unit!(definition, registry, "a percentage")
|
|
71
|
+
bps = validate.bps!(arg(:bps))
|
|
72
|
+
rollout = args.key?(:value) ? validate.value!(definition, arg(:value), "value") : @rule.rollout_value
|
|
73
|
+
if !definition.boolean? && rollout.nil? && @rule.variant_weights.empty?
|
|
74
|
+
raise Failure.new(:invalid, ":#{@key} is a #{definition.kind}; pass value: (what the percentage serves)")
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
full_since = bps == Bucket::BUCKETS ? (@rule.full_since || Time.now.utc) : nil
|
|
78
|
+
save!(percentage_bps: bps, rollout_value: rollout, full_since: full_since)
|
|
79
|
+
nil
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Sets variant weights over the flag's variants or mode values, summing
|
|
84
|
+
# to 10,000. An empty Hash clears them.
|
|
85
|
+
class SetVariants < Base
|
|
86
|
+
scope "flags:ramp"
|
|
87
|
+
arguments required: %i[weights]
|
|
88
|
+
|
|
89
|
+
private
|
|
90
|
+
|
|
91
|
+
def event = Events::FLAG_EVENTS[:variants_set]
|
|
92
|
+
|
|
93
|
+
def perform
|
|
94
|
+
validate.unit!(definition, registry, "variant weights")
|
|
95
|
+
save!(variant_weights: validate.weights!(definition, arg(:weights)))
|
|
96
|
+
nil
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# Sets the fallthrough value, served to every unit no other part of a
|
|
101
|
+
# running rule matched. This is how a mode moves off → sensor → on at
|
|
102
|
+
# runtime, and how a kill switch is thrown from the database.
|
|
103
|
+
# `value: nil` clears it, so the code default serves.
|
|
104
|
+
class SetValue < Base
|
|
105
|
+
arguments required: %i[value]
|
|
106
|
+
|
|
107
|
+
private
|
|
108
|
+
|
|
109
|
+
def event = Events::FLAG_EVENTS[:value_set]
|
|
110
|
+
|
|
111
|
+
def perform
|
|
112
|
+
value = arg(:value).nil? ? nil : validate.value!(definition, arg(:value), "value")
|
|
113
|
+
save!(fallthrough_value: value)
|
|
114
|
+
nil
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# Replaces the rule's conditions: `platforms:` (client platforms that
|
|
119
|
+
# may get the percentage and variants), `versions:` (a version range per
|
|
120
|
+
# platform) and `segments:` (`{ name => value }`, asked of
|
|
121
|
+
# `config.segment_resolver`, in order). Omitted ones are cleared.
|
|
122
|
+
class SetConditions < Base
|
|
123
|
+
arguments optional: %i[platforms versions segments]
|
|
124
|
+
|
|
125
|
+
private
|
|
126
|
+
|
|
127
|
+
def event = Events::FLAG_EVENTS[:conditions_set]
|
|
128
|
+
|
|
129
|
+
def perform
|
|
130
|
+
conditions = { "platforms" => platforms, "versions" => versions, "segments" => segments }.reject { |_, v| v.empty? }
|
|
131
|
+
validate.conditions_size!(conditions, config)
|
|
132
|
+
save!(conditions: conditions)
|
|
133
|
+
nil
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def platforms
|
|
137
|
+
list = Array(arg(:platforms))
|
|
138
|
+
unless list.all? { |p| (p.is_a?(String) || p.is_a?(Symbol)) && !p.to_s.strip.empty? }
|
|
139
|
+
raise Failure.new(:invalid, "platforms must be a list of names")
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
list.map { |platform| platform.to_s.downcase }.uniq
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def versions
|
|
146
|
+
ranges = arg(:versions) || {}
|
|
147
|
+
raise Failure.new(:invalid, "versions must be a Hash of platform => range") unless ranges.is_a?(Hash)
|
|
148
|
+
|
|
149
|
+
ranges.to_h { |platform, range| [ platform.to_s.downcase, validate.version_range!(range) ] }
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def segments
|
|
153
|
+
given = arg(:segments) || {}
|
|
154
|
+
pairs = given.is_a?(Hash) ? given.to_a : Array(given).map { |s| s.to_h.transform_keys(&:to_s).values_at("name", "value") }
|
|
155
|
+
validate.unit!(definition, registry, "segments") unless pairs.empty?
|
|
156
|
+
pairs.map do |name, value|
|
|
157
|
+
raise Failure.new(:invalid, "segment names must not be blank") if name.to_s.strip.empty?
|
|
158
|
+
|
|
159
|
+
{ "name" => name.to_s, "value" => validate.value!(definition, value, "segment #{name}'s value") }
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
# Shared by AttachCohort and DetachCohort: the policy must allow both
|
|
165
|
+
# the flag and the cohort.
|
|
166
|
+
module CohortAttachment
|
|
167
|
+
private
|
|
168
|
+
|
|
169
|
+
def cohort_key
|
|
170
|
+
arg(:cohort).to_s.delete_prefix("cohort:")
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def authorization_keys
|
|
174
|
+
[ *super, "cohort:#{cohort_key}" ]
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# Attaches a cohort to a flag's rule, serving `value:` to its members.
|
|
179
|
+
# Attaching it again changes the value and keeps its place in the order.
|
|
180
|
+
class AttachCohort < Base
|
|
181
|
+
include CohortAttachment
|
|
182
|
+
|
|
183
|
+
arguments required: %i[cohort value]
|
|
184
|
+
|
|
185
|
+
private
|
|
186
|
+
|
|
187
|
+
def event = Events::COHORT_EVENTS[:attached]
|
|
188
|
+
|
|
189
|
+
def extra_cohort_keys
|
|
190
|
+
[ cohort_key ]
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def perform
|
|
194
|
+
key = cohort_key
|
|
195
|
+
validate.unit!(definition, registry, "cohorts")
|
|
196
|
+
validate.cohort!(definition, key, @cohorts[key])
|
|
197
|
+
value = validate.value!(definition, arg(:value), "value")
|
|
198
|
+
attachments = @rule.cohorts.map(&:dup)
|
|
199
|
+
if (existing = attachments.find { |a| a["cohort"] == key })
|
|
200
|
+
existing["value"] = value
|
|
201
|
+
else
|
|
202
|
+
attachments << { "cohort" => key, "value" => value }
|
|
203
|
+
end
|
|
204
|
+
if attachments.size > config.max_cohorts_per_rule
|
|
205
|
+
raise Failure.new(:limit_exceeded, ":#{@key} would have #{attachments.size} cohorts; the limit is #{config.max_cohorts_per_rule}")
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
save!(cohorts: attachments)
|
|
209
|
+
{ cohort: key }
|
|
210
|
+
end
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
# Detaches a cohort from a flag's rule.
|
|
214
|
+
class DetachCohort < Base
|
|
215
|
+
include CohortAttachment
|
|
216
|
+
|
|
217
|
+
arguments required: %i[cohort]
|
|
218
|
+
|
|
219
|
+
private
|
|
220
|
+
|
|
221
|
+
def event = Events::COHORT_EVENTS[:detached]
|
|
222
|
+
|
|
223
|
+
def perform
|
|
224
|
+
key = cohort_key
|
|
225
|
+
attachments = @rule.cohorts.reject { |a| a["cohort"] == key }
|
|
226
|
+
raise Failure.new(:invalid, "cohort #{key.inspect} is not attached to :#{@key}") if attachments.size == @rule.cohorts.size
|
|
227
|
+
|
|
228
|
+
save!(cohorts: attachments)
|
|
229
|
+
{ cohort: key }
|
|
230
|
+
end
|
|
231
|
+
end
|
|
232
|
+
end
|
|
233
|
+
end
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
require "time"
|
|
3
|
+
|
|
4
|
+
module StandardFlags
|
|
5
|
+
module Operations
|
|
6
|
+
# Validation on write (R13). Everything an operation stores is checked
|
|
7
|
+
# here against the flag's definition and the configured limits; the read
|
|
8
|
+
# path never checks a limit. Each check raises Failure (:invalid or
|
|
9
|
+
# :limit_exceeded) with a message that never quotes a unit key.
|
|
10
|
+
#
|
|
11
|
+
# @api private
|
|
12
|
+
module Validation
|
|
13
|
+
module_function
|
|
14
|
+
|
|
15
|
+
# The stored string for a value the flag can take. Takes Ruby values
|
|
16
|
+
# (true, :sensor, "savings") or stored strings ("on").
|
|
17
|
+
def value!(definition, value, field)
|
|
18
|
+
wire = Runtime.wire(value)
|
|
19
|
+
return wire unless definition.value_from_wire(wire).equal?(Parser::INVALID)
|
|
20
|
+
|
|
21
|
+
invalid!("#{field} #{value.inspect} is not one of #{allowed(definition)} for :#{definition.key}")
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def bps!(bps)
|
|
25
|
+
return bps if bps.is_a?(Integer) && (0..Bucket::BUCKETS).cover?(bps)
|
|
26
|
+
|
|
27
|
+
invalid!("bps must be an Integer from 0 to #{Bucket::BUCKETS}, got #{bps.inspect}")
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# `{ variant_or_mode_value => weight }`, weights summing to 10,000.
|
|
31
|
+
# An empty Hash clears the weights.
|
|
32
|
+
def weights!(definition, weights)
|
|
33
|
+
invalid!(":#{definition.key} is an on/off flag; ramp it with Ramp, not variant weights") if definition.boolean?
|
|
34
|
+
invalid!("weights must be a Hash of value => Integer") unless weights.is_a?(Hash)
|
|
35
|
+
|
|
36
|
+
stored = weights.to_h { |value, weight| [ value!(definition, value, "variant"), weight ] }
|
|
37
|
+
invalid!("weights name the same value twice") if stored.size != weights.size
|
|
38
|
+
return stored if stored.empty?
|
|
39
|
+
|
|
40
|
+
invalid!("weights must be Integers of 0 or more") unless stored.values.all? { |w| w.is_a?(Integer) && w >= 0 }
|
|
41
|
+
sum = stored.values.sum
|
|
42
|
+
invalid!("weights must sum to #{Bucket::BUCKETS}, not #{sum}") unless sum == Bucket::BUCKETS
|
|
43
|
+
|
|
44
|
+
order = definition.allowed_values.map { |value| Runtime.wire(value) }
|
|
45
|
+
stored.sort_by { |wire, _| order.index(wire) }.to_h
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# The flag's declared unit; percentages, variants, segments and
|
|
49
|
+
# members all need one.
|
|
50
|
+
def unit!(definition, registry, what)
|
|
51
|
+
unit = definition.unit && registry.units[definition.unit]
|
|
52
|
+
invalid!(":#{definition.key} has no unit: declared, so it cannot have #{what}") unless unit
|
|
53
|
+
|
|
54
|
+
unit
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def version_range!(range)
|
|
58
|
+
Gem::Requirement.new(*range.to_s.split(",").map(&:strip))
|
|
59
|
+
range.to_s
|
|
60
|
+
rescue Gem::Requirement::BadRequirementError, ArgumentError
|
|
61
|
+
invalid!("version range #{range.inspect} does not parse")
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def conditions_size!(conditions, config)
|
|
65
|
+
bytes = JSON.generate(conditions).bytesize
|
|
66
|
+
return if bytes <= config.max_conditions_bytes
|
|
67
|
+
|
|
68
|
+
limit!("conditions are #{bytes} bytes of JSON; the limit is #{config.max_conditions_bytes}")
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def keys_count!(count, config)
|
|
72
|
+
return if count <= config.max_keys_per_operation
|
|
73
|
+
|
|
74
|
+
limit!("#{count} keys in one operation; the limit is #{config.max_keys_per_operation}")
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def members_count!(count, owner, config)
|
|
78
|
+
return if count <= config.max_members
|
|
79
|
+
|
|
80
|
+
limit!("#{owner} would have #{count} live members; the limit is #{config.max_members}")
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def expires_at!(value, now)
|
|
84
|
+
return nil if value.nil?
|
|
85
|
+
|
|
86
|
+
time = value.is_a?(String) ? Time.iso8601(value) : value
|
|
87
|
+
invalid!("expires_at must be a Time") unless time.respond_to?(:to_time)
|
|
88
|
+
time = time.to_time.utc
|
|
89
|
+
invalid!("expires_at must be in the future") unless time > now
|
|
90
|
+
time
|
|
91
|
+
rescue ArgumentError
|
|
92
|
+
invalid!("expires_at #{value.inspect} is not an ISO 8601 time")
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Everything a rule would serve once running: Start checks it, and so
|
|
96
|
+
# does every operation that leaves a rule running. `cohorts` maps each
|
|
97
|
+
# attached key to its Cohort (missing when it doesn't exist).
|
|
98
|
+
def rule!(definition, rule, registry, cohorts)
|
|
99
|
+
stored_shape(definition) do
|
|
100
|
+
%w[rollout_value fallthrough_value stop_value].each do |field|
|
|
101
|
+
stored = rule.public_send(field)
|
|
102
|
+
value!(definition, stored, field) unless stored.nil?
|
|
103
|
+
end
|
|
104
|
+
weights!(definition, rule.weights) if rule.weights.any?
|
|
105
|
+
bps!(rule.percentage_bps) unless rule.percentage_bps.nil?
|
|
106
|
+
unit!(definition, registry, "targeting") if rule.needs_unit?
|
|
107
|
+
if !rule.percentage_bps.nil? && !definition.boolean? && rule.rollout_value.nil? && rule.weights.empty?
|
|
108
|
+
invalid!(":#{definition.key} has a percentage but no rollout value or variant weights to serve")
|
|
109
|
+
end
|
|
110
|
+
rule.platforms
|
|
111
|
+
rule.versions.each_value { |range| version_range!(range) }
|
|
112
|
+
rule.segments.each { |segment| value!(definition, segment["value"], "segment #{segment['name']}'s value") }
|
|
113
|
+
rule.cohort_attachments.each do |key, value|
|
|
114
|
+
cohort!(definition, key, cohorts[key])
|
|
115
|
+
value!(definition, value, "cohort #{key}'s value")
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# The keys of the cohorts a rule attaches, in order.
|
|
121
|
+
def attachments(definition, rule)
|
|
122
|
+
stored_shape(definition) { rule.cohort_attachments.map(&:first) }
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# A stored rule whose shape the read path rejects (a row edited by
|
|
126
|
+
# hand, say) is :invalid, naming the field, not an unexpected error.
|
|
127
|
+
def stored_shape(definition)
|
|
128
|
+
yield
|
|
129
|
+
rescue Runtime::Resolver::Invalid => e
|
|
130
|
+
invalid!(":#{definition.key}'s stored rule #{e.message}")
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def cohort!(definition, key, cohort)
|
|
134
|
+
invalid!("cohort #{key.inspect} does not exist") unless cohort
|
|
135
|
+
invalid!("cohort #{key.inspect} is archived") if cohort.archived?
|
|
136
|
+
return if cohort.unit == definition.unit
|
|
137
|
+
|
|
138
|
+
invalid!("cohort #{key.inspect} holds :#{cohort.unit} units; :#{definition.key} targets #{definition.unit.inspect}")
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def allowed(definition)
|
|
142
|
+
definition.allowed_values.map { |value| Runtime.wire(value) }.join("/")
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def invalid!(message)
|
|
146
|
+
raise Failure.new(:invalid, message)
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def limit!(message)
|
|
150
|
+
raise Failure.new(:limit_exceeded, message)
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
require "standard_flags/operations/failure"
|
|
2
|
+
require "standard_flags/operations/validation"
|
|
3
|
+
require "standard_flags/operations/audit"
|
|
4
|
+
require "standard_flags/operations/base"
|
|
5
|
+
require "standard_flags/operations/rules"
|
|
6
|
+
require "standard_flags/operations/members"
|
|
7
|
+
require "standard_flags/operations/cohorts"
|
|
8
|
+
require "standard_flags/operations/restore_target"
|
|
9
|
+
require "standard_flags/operations/restore"
|
|
10
|
+
|
|
11
|
+
module StandardFlags
|
|
12
|
+
# The write path for runtime rules. Every change to a rule, cohort or
|
|
13
|
+
# membership goes through one of these, and nothing else writes the
|
|
14
|
+
# tables:
|
|
15
|
+
#
|
|
16
|
+
# ops = StandardFlags::Operations
|
|
17
|
+
# ops::Ramp.call(:new_checkout, bps: 500, actor: current_user, reason: "1% canary")
|
|
18
|
+
# ops::SetValue.call(:strict_gates, value: :sensor, actor: current_user, reason: "observe first")
|
|
19
|
+
# ops::AddMembers.call(:new_checkout, keys: [ org ], value: true, actor: current_user, reason: "pilot")
|
|
20
|
+
# ops::AddMembers.call("cohort:staff", keys: staff, actor: current_user, reason: "new hires")
|
|
21
|
+
#
|
|
22
|
+
# Each is called as `.call(key_or_cohort, actor:, reason:, expected_revision: nil, **args)` and
|
|
23
|
+
# returns a StandardFlags::Result. See Base for the steps every one takes.
|
|
24
|
+
module Operations
|
|
25
|
+
SCOPES = %w[flags:read flags:write flags:ramp].freeze
|
|
26
|
+
|
|
27
|
+
# Names a cohort where an operation takes a flag key or a cohort, for
|
|
28
|
+
# example `AddMembers.call(Operations.cohort(:staff), ...)`. The string
|
|
29
|
+
# "cohort:staff" works too.
|
|
30
|
+
CohortRef = Data.define(:key) do
|
|
31
|
+
def to_s
|
|
32
|
+
"cohort:#{key}"
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def self.cohort(key)
|
|
37
|
+
CohortRef.new(key: key.to_s)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
ALL = [ Start, Pause, Stop, Ramp, SetVariants, SetValue, SetConditions, AddMembers, RemoveMembers,
|
|
41
|
+
CreateCohort, ArchiveCohort, AttachCohort, DetachCohort ].freeze
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -6,6 +6,12 @@ module StandardFlags
|
|
|
6
6
|
#
|
|
7
7
|
# Matching is trimmed and case-insensitive. Anything else is INVALID: the
|
|
8
8
|
# caller ignores it and reports it. A blank value counts as unset.
|
|
9
|
+
#
|
|
10
|
+
# A flag declared `legacy_parser: :exact` reads its legacy `env:` names
|
|
11
|
+
# (never the canonical pin) the way a hand-written reader did, with no
|
|
12
|
+
# trimming or case folding: on/off kinds are on for a `legacy_true:` value
|
|
13
|
+
# and off for anything else (EnvPin pins a set-but-blank value off too);
|
|
14
|
+
# modes and experiments match a value exactly.
|
|
9
15
|
module Parser
|
|
10
16
|
INVALID = Object.new.tap { |o| def o.inspect = "StandardFlags::Parser::INVALID" }.freeze
|
|
11
17
|
|
|
@@ -21,6 +27,16 @@ module StandardFlags
|
|
|
21
27
|
definition.allowed_values.find { |value| value.to_s.downcase == text } || INVALID
|
|
22
28
|
end
|
|
23
29
|
|
|
30
|
+
# Parses a legacy `env:` value with the flag's `legacy_parser:`.
|
|
31
|
+
def parse_legacy(definition, raw)
|
|
32
|
+
return parse(definition, raw) unless definition.legacy_parser == :exact
|
|
33
|
+
|
|
34
|
+
text = raw.to_s
|
|
35
|
+
return definition.legacy_true.include?(text) if definition.boolean?
|
|
36
|
+
|
|
37
|
+
definition.allowed_values.find { |value| value.to_s == text } || INVALID
|
|
38
|
+
end
|
|
39
|
+
|
|
24
40
|
def boolean(raw)
|
|
25
41
|
text = raw.to_s.strip.downcase
|
|
26
42
|
return true if TRUE_WORDS.include?(text)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
module StandardFlags
|
|
2
|
+
# Deletes memberships removed or expired more than
|
|
3
|
+
# `config.membership_retention` (default 90 days) ago, the same as
|
|
4
|
+
# `bin/rails flags:purge`. Schedule it with the app's job scheduler, for
|
|
5
|
+
# example daily. Autoloaded on first reference, which loads ActiveJob; an
|
|
6
|
+
# app without activejob never defines it.
|
|
7
|
+
class PurgeJob < ::ActiveJob::Base
|
|
8
|
+
def perform
|
|
9
|
+
return unless Runtime.enabled?
|
|
10
|
+
|
|
11
|
+
Runtime.purge!
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -4,7 +4,27 @@ module StandardFlags
|
|
|
4
4
|
# The flags an app declares in config/flags.rb. Immutable once built;
|
|
5
5
|
# `StandardFlags.define` builds a new one and swaps it in.
|
|
6
6
|
class Registry
|
|
7
|
-
|
|
7
|
+
# `key` is the declared `key:` callable, or nil for the default.
|
|
8
|
+
Unit = Data.define(:name, :resolver, :key) do
|
|
9
|
+
# The unit key for one actor (a record or a raw key): the `key:`
|
|
10
|
+
# callable's answer, else `id` for objects that have one, else the value
|
|
11
|
+
# itself, as a String. Blank means missing.
|
|
12
|
+
def key_for(actor)
|
|
13
|
+
return nil if actor.nil?
|
|
14
|
+
|
|
15
|
+
raw = if key then key.call(actor)
|
|
16
|
+
elsif actor.respond_to?(:id) then actor.id
|
|
17
|
+
else actor
|
|
18
|
+
end
|
|
19
|
+
text = raw&.to_s
|
|
20
|
+
text unless text.nil? || text.strip.empty?
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# The unit key for a context, through `from:` / `resolver:`.
|
|
24
|
+
def key_from(context)
|
|
25
|
+
key_for(resolver.call(context))
|
|
26
|
+
end
|
|
27
|
+
end
|
|
8
28
|
Tombstone = Data.define(:key, :removed)
|
|
9
29
|
|
|
10
30
|
attr_reader :tombstones, :units
|
|
@@ -25,6 +45,17 @@ module StandardFlags
|
|
|
25
45
|
@flags[key.to_sym]
|
|
26
46
|
end
|
|
27
47
|
|
|
48
|
+
# A copy in which `unit` resolves every context to `unit_key`, used as
|
|
49
|
+
# given: what one unit gets, asked without its record (the MCP GetFlag
|
|
50
|
+
# tool, the admin page).
|
|
51
|
+
def with_unit_key(unit, unit_key)
|
|
52
|
+
unit = unit.to_sym
|
|
53
|
+
raise ArgumentError, "unknown unit :#{unit}" unless @units.key?(unit)
|
|
54
|
+
|
|
55
|
+
probe = Unit.new(name: unit, resolver: ->(_context) { unit_key }, key: nil)
|
|
56
|
+
Registry.new(flags: @flags, tombstones: @tombstones, units: @units.merge(unit => probe), env_resolver: @env_resolver)
|
|
57
|
+
end
|
|
58
|
+
|
|
28
59
|
def key?(key)
|
|
29
60
|
@flags.key?(key.to_sym)
|
|
30
61
|
end
|
|
@@ -91,14 +122,16 @@ module StandardFlags
|
|
|
91
122
|
end
|
|
92
123
|
|
|
93
124
|
# `from:` is a lambda over the Context; `resolver:` is any object with
|
|
94
|
-
# `call(context)`.
|
|
95
|
-
#
|
|
96
|
-
|
|
125
|
+
# `call(context)`. Runtime rules target a unit by its key: `key:` turns
|
|
126
|
+
# an actor into one (normalising a phone number, say); the default is
|
|
127
|
+
# `record.id.to_s`, or the value itself for anything without an `id`.
|
|
128
|
+
def unit(name, from: nil, resolver: nil, key: nil)
|
|
97
129
|
callable = from || resolver
|
|
98
130
|
raise DefinitionError, "unit :#{name} needs from: or resolver:" unless callable.respond_to?(:call)
|
|
99
131
|
raise DefinitionError, "unit :#{name} is declared twice" if @units.key?(name.to_sym)
|
|
132
|
+
raise DefinitionError, "unit :#{name} key: must respond to call(actor)" unless key.nil? || key.respond_to?(:call)
|
|
100
133
|
|
|
101
|
-
@units[name.to_sym] = Unit.new(name: name.to_sym, resolver: callable)
|
|
134
|
+
@units[name.to_sym] = Unit.new(name: name.to_sym, resolver: callable, key: key)
|
|
102
135
|
end
|
|
103
136
|
|
|
104
137
|
Definition::KINDS.each do |kind|
|
|
@@ -8,27 +8,39 @@ module StandardFlags
|
|
|
8
8
|
# @api private
|
|
9
9
|
module Reporting
|
|
10
10
|
SOURCE = "standard_flags".freeze
|
|
11
|
+
# Most tokens `once` remembers. Unknown keys can come from outside the
|
|
12
|
+
# code, so past this it stops recording (and reporting) new tokens and
|
|
13
|
+
# emits one `report_registry_full` notification, instead of growing.
|
|
14
|
+
LIMIT = 1_000
|
|
11
15
|
|
|
12
16
|
@mutex = Mutex.new
|
|
13
17
|
@seen = Set.new
|
|
18
|
+
@full = false
|
|
14
19
|
|
|
15
20
|
class << self
|
|
16
21
|
# Runs the block the first time `token` is seen in this process, so a
|
|
17
22
|
# bad pin read on every request reports once rather than per request.
|
|
18
23
|
def once(*token)
|
|
19
|
-
first = @mutex.synchronize {
|
|
24
|
+
first, newly_full = @mutex.synchronize { record(token) }
|
|
25
|
+
instrument(:report_registry_full, limit: LIMIT) if newly_full
|
|
20
26
|
yield if first
|
|
21
|
-
|
|
27
|
+
first
|
|
22
28
|
end
|
|
23
29
|
|
|
24
30
|
def reset!
|
|
25
|
-
@mutex.synchronize
|
|
31
|
+
@mutex.synchronize do
|
|
32
|
+
@seen.clear
|
|
33
|
+
@full = false
|
|
34
|
+
end
|
|
26
35
|
end
|
|
27
36
|
|
|
28
|
-
# Reports a handled warning through
|
|
29
|
-
# their error
|
|
37
|
+
# Reports a handled warning through `config.error_reporter` when one is
|
|
38
|
+
# set, else through Rails.error, which hosts wire to their error
|
|
39
|
+
# tracker. A custom reporter that raises falls back to Rails.error.
|
|
30
40
|
def report(exception, **context)
|
|
31
41
|
exception.set_backtrace(caller) if exception.backtrace.nil?
|
|
42
|
+
return if custom_report(exception, context)
|
|
43
|
+
|
|
32
44
|
reporter&.report(exception, handled: true, severity: :warning, context: context, source: SOURCE)
|
|
33
45
|
rescue StandardError => e
|
|
34
46
|
warn "[standard_flags] error report failed: #{e.class}: #{e.message}"
|
|
@@ -37,13 +49,45 @@ module StandardFlags
|
|
|
37
49
|
# Emits `<event>.standard_flags`. A failing subscriber must not break
|
|
38
50
|
# the read that triggered it.
|
|
39
51
|
def instrument(event, payload)
|
|
40
|
-
|
|
52
|
+
publish("#{event}.standard_flags", payload)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Emits an event by its full name (the write path's
|
|
56
|
+
# `standard_flags.flag.*` and `standard_flags.cohort.*`).
|
|
57
|
+
def publish(name, payload)
|
|
58
|
+
ActiveSupport::Notifications.instrument(name, payload)
|
|
41
59
|
rescue StandardError => e
|
|
42
|
-
warn "[standard_flags] #{
|
|
60
|
+
warn "[standard_flags] #{name.delete_suffix('.standard_flags')} notification failed: #{e.class}: #{e.message}"
|
|
43
61
|
end
|
|
44
62
|
|
|
45
63
|
private
|
|
46
64
|
|
|
65
|
+
# [first time seen, registry just filled]. Called under the mutex.
|
|
66
|
+
def record(token)
|
|
67
|
+
return [ false, false ] if @seen.include?(token)
|
|
68
|
+
|
|
69
|
+
if @seen.size < LIMIT
|
|
70
|
+
@seen.add(token)
|
|
71
|
+
return [ true, false ]
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
newly_full = !@full
|
|
75
|
+
@full = true
|
|
76
|
+
[ false, newly_full ]
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# true when a custom reporter took the report.
|
|
80
|
+
def custom_report(exception, context)
|
|
81
|
+
custom = StandardFlags.config.error_reporter
|
|
82
|
+
return false unless custom
|
|
83
|
+
|
|
84
|
+
custom.call(exception, context)
|
|
85
|
+
true
|
|
86
|
+
rescue StandardError => e
|
|
87
|
+
warn "[standard_flags] config.error_reporter failed (#{e.class}); reporting through Rails.error"
|
|
88
|
+
false
|
|
89
|
+
end
|
|
90
|
+
|
|
47
91
|
def reporter
|
|
48
92
|
if defined?(::Rails) && ::Rails.respond_to?(:error)
|
|
49
93
|
::Rails.error
|
|
@@ -5,15 +5,16 @@ module StandardFlags
|
|
|
5
5
|
# 1. TestOverride — `with_flag` and friends; inert outside tests.
|
|
6
6
|
# 2. Guard — `forbidden_in:` forces the off value.
|
|
7
7
|
# 3. EnvPin — STANDARD_FLAGS_<KEY>, then legacy `env:` names.
|
|
8
|
-
# 4.
|
|
8
|
+
# 4. RuntimeRule — the flag's stored rule, when runtime rules are on.
|
|
9
|
+
# 5. CodeDefault — the registry default for the current environment.
|
|
9
10
|
#
|
|
10
11
|
# Test overrides beat guards, so a spec proving a `forbidden_in:` guard
|
|
11
12
|
# must use a real ENV pin, not `with_flag`.
|
|
12
13
|
#
|
|
13
|
-
# Runtime rules
|
|
14
|
-
#
|
|
14
|
+
# Runtime rules sit below the pin, so an ENV pin keeps beating the
|
|
15
|
+
# database and works with it down. See Runtime::Resolver.
|
|
15
16
|
module Resolvers
|
|
16
|
-
Request = Data.define(:definition, :context, :env_name, :env)
|
|
17
|
+
Request = Data.define(:registry, :definition, :context, :env_name, :env)
|
|
17
18
|
|
|
18
19
|
class TestOverride
|
|
19
20
|
def call(request)
|
|
@@ -26,13 +27,19 @@ module StandardFlags
|
|
|
26
27
|
end
|
|
27
28
|
end
|
|
28
29
|
|
|
30
|
+
# Forbidden when the context's environment or the process's own
|
|
31
|
+
# (`registry.current_env`) is, so a caller-supplied env can't relax it.
|
|
29
32
|
class Guard
|
|
30
33
|
def call(request)
|
|
31
34
|
definition = request.definition
|
|
32
|
-
return
|
|
35
|
+
return if definition.forbidden_in.empty?
|
|
36
|
+
|
|
37
|
+
env_name = request.env_name
|
|
38
|
+
env_name = request.registry.current_env unless definition.forbidden_in?(env_name)
|
|
39
|
+
return unless definition.forbidden_in?(env_name)
|
|
33
40
|
|
|
34
41
|
pin = ::StandardFlags::EnvPin.scan(definition, request.env).pin
|
|
35
|
-
report(definition,
|
|
42
|
+
report(definition, env_name, pin) if pin && pin.value != definition.off_value
|
|
36
43
|
Evaluation.build(definition, definition.off_value, reason: Reason::DISABLED)
|
|
37
44
|
end
|
|
38
45
|
|
|
@@ -96,7 +103,7 @@ module StandardFlags
|
|
|
96
103
|
TestOverride.new,
|
|
97
104
|
Guard.new,
|
|
98
105
|
EnvPin.new,
|
|
99
|
-
|
|
106
|
+
Runtime::Resolver.new,
|
|
100
107
|
CodeDefault.new
|
|
101
108
|
].freeze
|
|
102
109
|
end
|