standard_flags 0.1.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 +7 -0
- data/CHANGELOG.md +84 -0
- data/LICENSE +21 -0
- data/README.md +548 -0
- data/Rakefile +6 -0
- data/lib/generators/standard_flags/install/install_generator.rb +40 -0
- data/lib/generators/standard_flags/install/templates/flags.rb.tt +58 -0
- data/lib/generators/standard_flags/install/templates/initializer.rb.tt +43 -0
- data/lib/standard_flags/bucket.rb +61 -0
- data/lib/standard_flags/config.rb +77 -0
- data/lib/standard_flags/context.rb +73 -0
- data/lib/standard_flags/controller.rb +107 -0
- data/lib/standard_flags/definition.rb +285 -0
- data/lib/standard_flags/diagnostics.rb +101 -0
- data/lib/standard_flags/engine.rb +28 -0
- data/lib/standard_flags/env_pin.rb +50 -0
- data/lib/standard_flags/errors.rb +35 -0
- data/lib/standard_flags/evaluation.rb +41 -0
- data/lib/standard_flags/evaluator.rb +58 -0
- data/lib/standard_flags/health_check.rb +50 -0
- data/lib/standard_flags/job.rb +75 -0
- data/lib/standard_flags/memo.rb +57 -0
- data/lib/standard_flags/mode.rb +78 -0
- data/lib/standard_flags/notifiers/logger.rb +49 -0
- data/lib/standard_flags/notifiers/metrics.rb +29 -0
- data/lib/standard_flags/notifiers/sentry.rb +27 -0
- data/lib/standard_flags/parser.rb +36 -0
- data/lib/standard_flags/payload.rb +29 -0
- data/lib/standard_flags/reader.rb +69 -0
- data/lib/standard_flags/reason.rb +34 -0
- data/lib/standard_flags/registry.rb +168 -0
- data/lib/standard_flags/reporting.rb +56 -0
- data/lib/standard_flags/resolvers.rb +103 -0
- data/lib/standard_flags/rspec.rb +58 -0
- data/lib/standard_flags/sensor.rb +77 -0
- data/lib/standard_flags/snapshot.rb +34 -0
- data/lib/standard_flags/subscribers.rb +45 -0
- data/lib/standard_flags/testing.rb +85 -0
- data/lib/standard_flags/tooling.rb +224 -0
- data/lib/standard_flags/version.rb +3 -0
- data/lib/standard_flags.rb +140 -0
- data/lib/tasks/standard_flags.rake +37 -0
- metadata +156 -0
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
require "date"
|
|
2
|
+
|
|
3
|
+
module StandardFlags
|
|
4
|
+
# One declared flag. Built by Registry::Builder and immutable afterwards.
|
|
5
|
+
#
|
|
6
|
+
# Construction validates everything that can be checked from the
|
|
7
|
+
# declaration alone and raises DefinitionError; cross-flag checks (unit
|
|
8
|
+
# references, tombstones, wire-name clashes) live in Registry::Builder.
|
|
9
|
+
class Definition
|
|
10
|
+
KINDS = %i[flag kill_switch mode experiment compat].freeze
|
|
11
|
+
BOOLEAN_KINDS = %i[flag kill_switch compat].freeze
|
|
12
|
+
|
|
13
|
+
PRESETS = {
|
|
14
|
+
sensor: %i[off sensor on],
|
|
15
|
+
cutover: %i[off allowlist all],
|
|
16
|
+
shadow: %i[off shadow enforce]
|
|
17
|
+
}.freeze
|
|
18
|
+
|
|
19
|
+
COMMON_ATTRIBUTES = %i[owner description default env inverted forbidden_in fail expose unit].freeze
|
|
20
|
+
ATTRIBUTES = {
|
|
21
|
+
flag: COMMON_ATTRIBUTES + %i[remove_by final permanent],
|
|
22
|
+
kill_switch: COMMON_ATTRIBUTES,
|
|
23
|
+
mode: COMMON_ATTRIBUTES + %i[preset values],
|
|
24
|
+
experiment: COMMON_ATTRIBUTES + %i[variants],
|
|
25
|
+
compat: COMMON_ATTRIBUTES + %i[remove_by]
|
|
26
|
+
}.freeze
|
|
27
|
+
|
|
28
|
+
KEY_FORMAT = /\A[a-z][a-z0-9_]*\z/
|
|
29
|
+
ENV_VAR_FORMAT = /\A[A-Z][A-Z0-9_]*\z/
|
|
30
|
+
WIRE_FORMAT = /\A[A-Za-z][A-Za-z0-9_]*\z/
|
|
31
|
+
|
|
32
|
+
attr_reader :key, :kind, :owner, :description, :env, :inverted, :forbidden_in,
|
|
33
|
+
:expose, :unit, :remove_by, :final, :values, :variants, :permanent
|
|
34
|
+
|
|
35
|
+
def initialize(kind, key, attributes)
|
|
36
|
+
@kind = kind.to_sym
|
|
37
|
+
raise DefinitionError, "unknown flag kind #{kind.inspect}" unless KINDS.include?(@kind)
|
|
38
|
+
|
|
39
|
+
@key = normalize_key(key)
|
|
40
|
+
check_attributes!(attributes)
|
|
41
|
+
assign(attributes)
|
|
42
|
+
validate!
|
|
43
|
+
freeze
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def boolean?
|
|
47
|
+
BOOLEAN_KINDS.include?(kind)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def mode?
|
|
51
|
+
kind == :mode
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def experiment?
|
|
55
|
+
kind == :experiment
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def kill_switch?
|
|
59
|
+
kind == :kill_switch
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# A `flag` declared `permanent: true`: a standing switch (an environment
|
|
63
|
+
# permission, say) with no remove_by: or final:, never stale.
|
|
64
|
+
def permanent?
|
|
65
|
+
permanent
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Every value this flag can take, in declared order.
|
|
69
|
+
def allowed_values
|
|
70
|
+
if boolean? then [ true, false ]
|
|
71
|
+
elsif mode? then values
|
|
72
|
+
else variants
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# The value a guard forces: false, the first mode value, or the first
|
|
77
|
+
# (control) variant.
|
|
78
|
+
def off_value
|
|
79
|
+
boolean? ? false : allowed_values.first
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# The value when nothing else applies and no `default:` is declared.
|
|
83
|
+
def kind_default
|
|
84
|
+
case kind
|
|
85
|
+
when :kill_switch, :compat then true
|
|
86
|
+
when :flag then false
|
|
87
|
+
else allowed_values.first
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def default_for(env_name)
|
|
92
|
+
return kind_default if @default.nil?
|
|
93
|
+
return @default unless @default.is_a?(Hash)
|
|
94
|
+
|
|
95
|
+
@default.fetch(env_name.to_s) { kind_default }
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# The declared default as written (scalar or per-environment Hash with
|
|
99
|
+
# String keys), for the manifest.
|
|
100
|
+
def declared_default
|
|
101
|
+
@default
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# The value returned when evaluation raises. Kill switches fail open.
|
|
105
|
+
def fail_value
|
|
106
|
+
@fail.nil? ? kind_default : @fail
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def variant_for(value)
|
|
110
|
+
return nil if value.nil?
|
|
111
|
+
return value.to_s unless boolean?
|
|
112
|
+
|
|
113
|
+
value ? "on" : "off"
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# The canonical pin, STANDARD_FLAGS_<KEY>. It is never inverted.
|
|
117
|
+
def canonical_env_var
|
|
118
|
+
"STANDARD_FLAGS_#{key.to_s.upcase}"
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def forbidden_in?(env_name)
|
|
122
|
+
forbidden_in.include?(env_name.to_s)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def wire_name(audience)
|
|
126
|
+
expose[audience.to_sym]
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def audiences
|
|
130
|
+
expose.keys
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# Permanent flags have no remove_by:, so they are never stale.
|
|
134
|
+
def stale?(today = Date.today)
|
|
135
|
+
!remove_by.nil? && remove_by < today
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def inspect
|
|
139
|
+
"#<StandardFlags::Definition #{kind} :#{key}>"
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
private
|
|
143
|
+
|
|
144
|
+
def normalize_key(key)
|
|
145
|
+
key = key.to_s
|
|
146
|
+
raise DefinitionError, "flag key #{key.inspect} must match #{KEY_FORMAT.inspect}" unless key.match?(KEY_FORMAT)
|
|
147
|
+
|
|
148
|
+
key.to_sym
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def check_attributes!(attributes)
|
|
152
|
+
unknown = attributes.keys.map(&:to_sym) - ATTRIBUTES.fetch(kind)
|
|
153
|
+
return if unknown.empty?
|
|
154
|
+
|
|
155
|
+
raise DefinitionError, "#{kind} :#{key} has unknown attribute(s): #{unknown.map(&:inspect).join(', ')}"
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def assign(attributes)
|
|
159
|
+
attributes = attributes.transform_keys(&:to_sym)
|
|
160
|
+
@owner = attributes[:owner]&.to_s
|
|
161
|
+
@description = attributes[:description]&.to_s
|
|
162
|
+
@env = Array(attributes[:env]).map(&:to_s).freeze
|
|
163
|
+
@inverted = attributes.fetch(:inverted, false)
|
|
164
|
+
@forbidden_in = Array(attributes[:forbidden_in]).map(&:to_s).freeze
|
|
165
|
+
@expose = normalize_expose(attributes[:expose])
|
|
166
|
+
@unit = attributes[:unit]&.to_sym
|
|
167
|
+
@remove_by = parse_date(attributes[:remove_by], :remove_by)
|
|
168
|
+
@values = resolve_values(attributes)
|
|
169
|
+
@variants = attributes[:variants] && Array(attributes[:variants]).map(&:to_s).freeze
|
|
170
|
+
fail!("variants: must list at least two unique variants") if experiment? && invalid_variants?
|
|
171
|
+
@default = normalize_default(attributes[:default])
|
|
172
|
+
@fail = attributes.key?(:fail) ? coerce(attributes[:fail], :fail) : nil
|
|
173
|
+
@final = attributes.key?(:final) ? coerce(attributes[:final], :final) : nil
|
|
174
|
+
@permanent = attributes.fetch(:permanent, false)
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
def validate!
|
|
178
|
+
fail!("is missing owner:") if owner.nil? || owner.strip.empty?
|
|
179
|
+
validate_flag_lifecycle!
|
|
180
|
+
validate_env!
|
|
181
|
+
validate_forbidden_in!
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def validate_flag_lifecycle!
|
|
185
|
+
return unless kind == :flag
|
|
186
|
+
|
|
187
|
+
fail!("permanent: must be true or false") unless [ true, false ].include?(permanent)
|
|
188
|
+
return validate_permanent! if permanent
|
|
189
|
+
|
|
190
|
+
fail!("is missing remove_by: (release flags are temporary; declare permanent: true for a standing switch)") if remove_by.nil?
|
|
191
|
+
fail!("is missing final: (the value it settles on when removed)") if @final.nil?
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
def validate_permanent!
|
|
195
|
+
fail!("is permanent: and cannot have remove_by:") unless remove_by.nil?
|
|
196
|
+
fail!("is permanent: and cannot have final:") unless @final.nil?
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
def validate_env!
|
|
200
|
+
env.each do |name|
|
|
201
|
+
fail!("env: #{name.inspect} is not an environment variable name") unless name.match?(ENV_VAR_FORMAT)
|
|
202
|
+
fail!("env: #{name.inspect} is the canonical name; list only legacy names") if name == canonical_env_var
|
|
203
|
+
end
|
|
204
|
+
fail!("inverted: must be true or false") unless [ true, false ].include?(inverted)
|
|
205
|
+
fail!("inverted: needs a legacy env: name") if inverted && env.empty?
|
|
206
|
+
fail!("inverted: only applies to on/off kinds") if inverted && !boolean?
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
def validate_forbidden_in!
|
|
210
|
+
forbidden_in.each do |env_name|
|
|
211
|
+
next if default_for(env_name) == off_value
|
|
212
|
+
|
|
213
|
+
fail!("defaults to #{default_for(env_name).inspect} in #{env_name}, where it is forbidden_in:")
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
def invalid_variants?
|
|
218
|
+
variants.nil? || variants.size < 2 || variants.uniq.size != variants.size
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
def resolve_values(attributes)
|
|
222
|
+
return nil unless mode?
|
|
223
|
+
|
|
224
|
+
preset = attributes[:preset]
|
|
225
|
+
explicit = attributes[:values]
|
|
226
|
+
fail!("needs exactly one of preset: or values:") if preset.nil? == explicit.nil?
|
|
227
|
+
|
|
228
|
+
if preset
|
|
229
|
+
PRESETS.fetch(preset.to_sym) { fail!("preset: must be one of #{PRESETS.keys.inspect}") }
|
|
230
|
+
else
|
|
231
|
+
list = Array(explicit).map(&:to_sym)
|
|
232
|
+
fail!("values: must list at least two unique values") if list.size < 2 || list.uniq.size != list.size
|
|
233
|
+
list.freeze
|
|
234
|
+
end
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
def normalize_default(default)
|
|
238
|
+
return nil if default.nil?
|
|
239
|
+
return coerce(default, :default) unless default.is_a?(Hash)
|
|
240
|
+
|
|
241
|
+
default.to_h { |env_name, value| [ env_name.to_s, coerce(value, :default) ] }.freeze
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
def normalize_expose(expose)
|
|
245
|
+
pairs =
|
|
246
|
+
case expose
|
|
247
|
+
when nil then {}
|
|
248
|
+
when Hash then expose
|
|
249
|
+
else Array(expose).to_h { |audience| [ audience, key ] }
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
pairs.to_h do |audience, wire|
|
|
253
|
+
wire = wire.to_s
|
|
254
|
+
fail!("expose: wire name #{wire.inspect} must match #{WIRE_FORMAT.inspect}") unless wire.match?(WIRE_FORMAT)
|
|
255
|
+
[ audience.to_sym, wire ]
|
|
256
|
+
end.freeze
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
# Casts a declared value to this kind's type, raising for anything outside
|
|
260
|
+
# the declared set.
|
|
261
|
+
def coerce(value, attribute)
|
|
262
|
+
cast =
|
|
263
|
+
if boolean? then value
|
|
264
|
+
elsif mode? then value.respond_to?(:to_sym) ? value.to_sym : value
|
|
265
|
+
else value.to_s
|
|
266
|
+
end
|
|
267
|
+
return cast if allowed_values.include?(cast)
|
|
268
|
+
|
|
269
|
+
fail!("#{attribute}: #{value.inspect} is not one of #{allowed_values.inspect}")
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
def parse_date(value, attribute)
|
|
273
|
+
return nil if value.nil?
|
|
274
|
+
return value if value.is_a?(Date)
|
|
275
|
+
|
|
276
|
+
Date.iso8601(value.to_s)
|
|
277
|
+
rescue Date::Error
|
|
278
|
+
fail!("#{attribute}: #{value.inspect} is not a YYYY-MM-DD date")
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
def fail!(message)
|
|
282
|
+
raise DefinitionError, "#{kind} :#{key} #{message}"
|
|
283
|
+
end
|
|
284
|
+
end
|
|
285
|
+
end
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
require "date"
|
|
2
|
+
require "time"
|
|
3
|
+
|
|
4
|
+
module StandardFlags
|
|
5
|
+
# What the running process thinks, for a diagnostics page or the health
|
|
6
|
+
# check: every flag's value and where it came from, plus the pins that need
|
|
7
|
+
# attention. Variable NAMES only; raw environment values never appear.
|
|
8
|
+
module Diagnostics
|
|
9
|
+
CANONICAL_PREFIX = "STANDARD_FLAGS_".freeze
|
|
10
|
+
|
|
11
|
+
module_function
|
|
12
|
+
|
|
13
|
+
def report(registry: StandardFlags.registry, env: StandardFlags.config.env_source, today: Date.today)
|
|
14
|
+
environment = safe_env(registry)
|
|
15
|
+
rows = registry.definitions.map { |definition| row(registry, definition, env, environment, today) }
|
|
16
|
+
|
|
17
|
+
report = {
|
|
18
|
+
environment: environment,
|
|
19
|
+
generated_at: Time.now.utc.iso8601,
|
|
20
|
+
flags: rows.map { |r| r.except(:scan, :forbidden_pin) },
|
|
21
|
+
forbidden_pins: forbidden_pins(rows, environment),
|
|
22
|
+
rejected_pins: rows.flat_map { |r| r[:scan].rejected.map { |var| { flag: r[:key], env_var: var } } },
|
|
23
|
+
legacy_env_vars: legacy_env_vars(rows),
|
|
24
|
+
stale: rows.select { |r| r[:stale] }.map { |r| { flag: r[:key], remove_by: r[:remove_by] } },
|
|
25
|
+
unknown_env_vars: unknown_env_vars(registry, env)
|
|
26
|
+
}
|
|
27
|
+
report.merge(status: status(report))
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# One line per warning, names only.
|
|
31
|
+
def warnings(report)
|
|
32
|
+
report[:rejected_pins].map { |p| "#{p[:env_var]} for :#{p[:flag]} is not a valid value (ignored)" } +
|
|
33
|
+
report[:legacy_env_vars].map { |p| "#{p[:env_var]} for :#{p[:flag]} is deprecated; rename to #{p[:canonical]}" } +
|
|
34
|
+
report[:stale].map { |s| ":#{s[:flag]} is past remove_by #{s[:remove_by]}" } +
|
|
35
|
+
report[:unknown_env_vars].map { |var| "#{var} does not match a declared flag" }
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def row(registry, definition, env, environment, today)
|
|
39
|
+
evaluation = Evaluator.evaluate(registry, definition.key, Context.new(env: environment))
|
|
40
|
+
scan = EnvPin.scan(definition, env)
|
|
41
|
+
{
|
|
42
|
+
key: definition.key,
|
|
43
|
+
kind: definition.kind,
|
|
44
|
+
owner: definition.owner,
|
|
45
|
+
value: evaluation.value,
|
|
46
|
+
reason: evaluation.reason,
|
|
47
|
+
source_env_var: evaluation.source_env_var,
|
|
48
|
+
pinned: !scan.pin.nil?,
|
|
49
|
+
remove_by: definition.remove_by&.iso8601,
|
|
50
|
+
permanent: definition.permanent?,
|
|
51
|
+
stale: definition.stale?(today),
|
|
52
|
+
audiences: definition.expose.transform_keys(&:to_s),
|
|
53
|
+
scan: scan,
|
|
54
|
+
forbidden_pin: forbidden_pin(definition, scan, environment)
|
|
55
|
+
}
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# The variable pinning a `forbidden_in` flag on here, if any.
|
|
59
|
+
def forbidden_pin(definition, scan, environment)
|
|
60
|
+
return unless environment && definition.forbidden_in?(environment)
|
|
61
|
+
return unless scan.pin && scan.pin.value != definition.off_value
|
|
62
|
+
|
|
63
|
+
scan.pin.var
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def forbidden_pins(rows, environment)
|
|
67
|
+
rows.filter_map do |r|
|
|
68
|
+
{ flag: r[:key], env_var: r[:forbidden_pin], environment: environment } if r[:forbidden_pin]
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def legacy_env_vars(rows)
|
|
73
|
+
rows.flat_map do |r|
|
|
74
|
+
r[:scan].legacy.map do |var|
|
|
75
|
+
{ flag: r[:key], env_var: var, canonical: "#{CANONICAL_PREFIX}#{r[:key].to_s.upcase}",
|
|
76
|
+
shadowed: r[:scan].shadowed_legacy.include?(var) }
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def unknown_env_vars(registry, env)
|
|
82
|
+
return [] unless env.respond_to?(:keys)
|
|
83
|
+
|
|
84
|
+
declared = registry.keys.map { |key| "#{CANONICAL_PREFIX}#{key.to_s.upcase}" }
|
|
85
|
+
env.keys.select { |name| name.to_s.start_with?(CANONICAL_PREFIX) }.map(&:to_s).reject { |n| declared.include?(n) }.sort
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def status(report)
|
|
89
|
+
return :fail if report[:forbidden_pins].any?
|
|
90
|
+
return :warn if warnings(report).any?
|
|
91
|
+
|
|
92
|
+
:ok
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def safe_env(registry)
|
|
96
|
+
registry.current_env
|
|
97
|
+
rescue StandardError
|
|
98
|
+
nil
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
module StandardFlags
|
|
2
|
+
# Library-only engine: no routes, no models. It wires three things into the
|
|
3
|
+
# host:
|
|
4
|
+
#
|
|
5
|
+
# * an executor hook that opens a fresh memo per unit of work (requests,
|
|
6
|
+
# jobs, mailers, console reloads), instead of Rack middleware;
|
|
7
|
+
# * config/flags.rb, loaded on boot and on every code reload;
|
|
8
|
+
# * the Logger / Sentry / Metrics subscribers, once initializers have run.
|
|
9
|
+
#
|
|
10
|
+
# Rake tasks need no `rake_tasks` block: Rails::Engine already loads
|
|
11
|
+
# lib/tasks/**/*.rake, and loading the file again would give every task a
|
|
12
|
+
# second action.
|
|
13
|
+
class Engine < ::Rails::Engine
|
|
14
|
+
isolate_namespace StandardFlags
|
|
15
|
+
|
|
16
|
+
initializer "standard_flags.executor" do |app|
|
|
17
|
+
app.executor.register_hook(StandardFlags::Memo::Hook)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
initializer "standard_flags.subscribers", after: :load_config_initializers do
|
|
21
|
+
StandardFlags.subscribers.setup!
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
config.to_prepare do
|
|
25
|
+
StandardFlags.load_registry!
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
module StandardFlags
|
|
2
|
+
# Reads a flag's environment variables without side effects.
|
|
3
|
+
#
|
|
4
|
+
# The canonical STANDARD_FLAGS_<KEY> is read first, then each legacy `env:`
|
|
5
|
+
# name in declared order. The first variable that parses wins, so a valid
|
|
6
|
+
# canonical pin beats any legacy one. `inverted: true` flips legacy values
|
|
7
|
+
# only (a legacy *_DISABLED=true means the flag is off).
|
|
8
|
+
#
|
|
9
|
+
# Callers decide what to report; Diagnostics and the health check use the
|
|
10
|
+
# same scan, so they can't disagree with evaluation.
|
|
11
|
+
module EnvPin
|
|
12
|
+
Pin = Data.define(:var, :value, :legacy)
|
|
13
|
+
|
|
14
|
+
# * pin — the winning Pin, or nil.
|
|
15
|
+
# * rejected — names of set variables that failed to parse.
|
|
16
|
+
# * legacy — names of legacy variables that are set (whether or not
|
|
17
|
+
# they won).
|
|
18
|
+
Scan = Data.define(:pin, :rejected, :legacy) do
|
|
19
|
+
# Legacy variables that are set but did not decide the value.
|
|
20
|
+
def shadowed_legacy
|
|
21
|
+
legacy - [ pin&.var ]
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
module_function
|
|
26
|
+
|
|
27
|
+
def scan(definition, env)
|
|
28
|
+
pin = nil
|
|
29
|
+
rejected = []
|
|
30
|
+
legacy = []
|
|
31
|
+
|
|
32
|
+
([ definition.canonical_env_var ] + definition.env).each do |var|
|
|
33
|
+
raw = env[var]
|
|
34
|
+
next if Parser.blank?(raw)
|
|
35
|
+
|
|
36
|
+
is_legacy = var != definition.canonical_env_var
|
|
37
|
+
legacy << var if is_legacy
|
|
38
|
+
value = Parser.parse(definition, raw)
|
|
39
|
+
if value.equal?(Parser::INVALID)
|
|
40
|
+
rejected << var
|
|
41
|
+
elsif pin.nil?
|
|
42
|
+
value = !value if is_legacy && definition.inverted
|
|
43
|
+
pin = Pin.new(var: var, value: value, legacy: is_legacy)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
Scan.new(pin: pin, rejected: rejected.freeze, legacy: legacy.freeze)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
module StandardFlags
|
|
2
|
+
class Error < StandardError; end
|
|
3
|
+
|
|
4
|
+
# Raised while building the registry: a bad declaration fails boot rather
|
|
5
|
+
# than misbehaving at read time.
|
|
6
|
+
class DefinitionError < Error; end
|
|
7
|
+
|
|
8
|
+
# Raised for an undeclared key when `config.strict` is on (the default
|
|
9
|
+
# outside production). In production the read returns a fail-safe result
|
|
10
|
+
# with reason ERROR and error code FLAG_NOT_FOUND instead.
|
|
11
|
+
class UnknownFlag < Error; end
|
|
12
|
+
|
|
13
|
+
# Raised in strict mode when a reader doesn't match the flag's kind, for
|
|
14
|
+
# example `on?` on a mode.
|
|
15
|
+
class TypeMismatch < Error; end
|
|
16
|
+
|
|
17
|
+
# Raised by `StandardFlags::Job.requires_flag ..., retry: true` when the
|
|
18
|
+
# flag is off, so `retry_on StandardFlags::Disabled` can reschedule the job.
|
|
19
|
+
class Disabled < Error
|
|
20
|
+
attr_reader :key
|
|
21
|
+
|
|
22
|
+
def initialize(key, message = nil)
|
|
23
|
+
@key = key.to_sym
|
|
24
|
+
super(message || "flag :#{@key} is disabled")
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Reported (never raised) through Rails.error when an ENV pin can't be
|
|
29
|
+
# parsed. The message names the variable, never its value.
|
|
30
|
+
class PinRejected < Error; end
|
|
31
|
+
|
|
32
|
+
# Reported (never raised) through Rails.error when a `forbidden_in` flag is
|
|
33
|
+
# pinned on in an environment that forbids it.
|
|
34
|
+
class ForbiddenPin < Error; end
|
|
35
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
module StandardFlags
|
|
2
|
+
# The result of evaluating one flag, in the OpenFeature shape.
|
|
3
|
+
#
|
|
4
|
+
# * `value` — true/false for flag, kill_switch and compat; a Symbol
|
|
5
|
+
# for a mode; a String for an experiment.
|
|
6
|
+
# * `variant` — the value as a string ("on"/"off" for booleans).
|
|
7
|
+
# * `reason` — one of StandardFlags::Reason.
|
|
8
|
+
# * `error_code` — one of StandardFlags::ErrorCode, only when reason is ERROR.
|
|
9
|
+
# * `rule_id` — the matching runtime rule; always nil until runtime
|
|
10
|
+
# rules exist.
|
|
11
|
+
# * `source_env_var` — the NAME of the environment variable that decided the
|
|
12
|
+
# value, never its contents.
|
|
13
|
+
Evaluation = Data.define(:key, :value, :variant, :reason, :error_code, :rule_id, :source_env_var) do
|
|
14
|
+
def self.build(definition, value, reason:, error_code: nil, source_env_var: nil)
|
|
15
|
+
new(
|
|
16
|
+
key: definition.key,
|
|
17
|
+
value: value,
|
|
18
|
+
variant: definition.variant_for(value),
|
|
19
|
+
reason: reason,
|
|
20
|
+
error_code: error_code,
|
|
21
|
+
rule_id: nil,
|
|
22
|
+
source_env_var: source_env_var
|
|
23
|
+
)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.not_found(key)
|
|
27
|
+
new(
|
|
28
|
+
key: key, value: nil, variant: nil, reason: Reason::ERROR,
|
|
29
|
+
error_code: ErrorCode::FLAG_NOT_FOUND, rule_id: nil, source_env_var: nil
|
|
30
|
+
)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def error?
|
|
34
|
+
reason == Reason::ERROR
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def pinned?
|
|
38
|
+
!source_env_var.nil?
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
module StandardFlags
|
|
2
|
+
# Turns (registry, key, context) into an Evaluation.
|
|
3
|
+
#
|
|
4
|
+
# Errors are isolated per flag: anything raised while resolving one flag
|
|
5
|
+
# returns that flag's `fail:` value with reason ERROR, reports once through
|
|
6
|
+
# Rails.error, and leaves every other flag alone.
|
|
7
|
+
#
|
|
8
|
+
# @api private
|
|
9
|
+
module Evaluator
|
|
10
|
+
module_function
|
|
11
|
+
|
|
12
|
+
def evaluate(registry, key, context)
|
|
13
|
+
key = key.to_sym
|
|
14
|
+
definition = registry[key]
|
|
15
|
+
return unknown(registry, key) unless definition
|
|
16
|
+
|
|
17
|
+
evaluation = Memo.fetch([ key, context.cache_key ]) { resolve(registry, definition, context) }
|
|
18
|
+
sample(evaluation)
|
|
19
|
+
evaluation
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def resolve(registry, definition, context)
|
|
23
|
+
request = Resolvers::Request.new(
|
|
24
|
+
definition: definition,
|
|
25
|
+
context: context,
|
|
26
|
+
env_name: (context.env || registry.current_env).to_s,
|
|
27
|
+
env: StandardFlags.config.env_source
|
|
28
|
+
)
|
|
29
|
+
Resolvers::CHAIN.each do |resolver|
|
|
30
|
+
evaluation = resolver.call(request)
|
|
31
|
+
return evaluation if evaluation
|
|
32
|
+
end
|
|
33
|
+
rescue StandardError => e
|
|
34
|
+
Reporting.once(:error, definition.key, e.class.name) { Reporting.report(e, flag: definition.key.to_s) }
|
|
35
|
+
Evaluation.build(definition, definition.fail_value, reason: Reason::ERROR, error_code: ErrorCode::GENERAL)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def unknown(registry, key)
|
|
39
|
+
message =
|
|
40
|
+
if registry.tombstoned?(key)
|
|
41
|
+
"flag :#{key} was removed on #{registry.tombstones[key].removed.iso8601}"
|
|
42
|
+
else
|
|
43
|
+
"unknown flag :#{key}; declare it in config/flags.rb"
|
|
44
|
+
end
|
|
45
|
+
raise UnknownFlag, message if StandardFlags.config.strict?
|
|
46
|
+
|
|
47
|
+
Reporting.once(:unknown, key) { Reporting.report(UnknownFlag.new(message), flag: key.to_s) }
|
|
48
|
+
Evaluation.not_found(key)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def sample(evaluation)
|
|
52
|
+
rate = StandardFlags.config.evaluate_sample_rate.to_f
|
|
53
|
+
return unless rate.positive? && (rate >= 1 || rand < rate)
|
|
54
|
+
|
|
55
|
+
Reporting.instrument(:evaluate, evaluation.to_h)
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
require "standard_health/check"
|
|
2
|
+
|
|
3
|
+
module StandardFlags
|
|
4
|
+
# An opt-in standard_health check. Loaded on first reference, so apps
|
|
5
|
+
# without standard_health never touch it.
|
|
6
|
+
#
|
|
7
|
+
# StandardHealth.configure do |c|
|
|
8
|
+
# c.register_check :flags, StandardFlags::HealthCheck
|
|
9
|
+
# end
|
|
10
|
+
#
|
|
11
|
+
# * Fails when a `forbidden_in` flag is pinned on in this environment.
|
|
12
|
+
# * Warns on unparseable pins, legacy env names in use (deprecated), flags
|
|
13
|
+
# past remove_by, and STANDARD_FLAGS_* variables that match no flag.
|
|
14
|
+
#
|
|
15
|
+
# standard_health has no warning status, so warnings ride on the row as
|
|
16
|
+
# `warnings:` and the check stays :ok. Pass `warn_as: :fail` to let them
|
|
17
|
+
# degrade the tier instead. Non-critical by default: a bad pin is a reason
|
|
18
|
+
# to look, not to pull an instance out of rotation.
|
|
19
|
+
class HealthCheck < ::StandardHealth::Check
|
|
20
|
+
VIOLATION_LABEL = "StandardFlags::ForbiddenPin".freeze
|
|
21
|
+
WARNING_LABEL = "StandardFlags::FlagWarning".freeze
|
|
22
|
+
|
|
23
|
+
attr_reader :warn_as
|
|
24
|
+
|
|
25
|
+
def initialize(name: :standard_flags, critical: false, warn_as: :ok)
|
|
26
|
+
super(name: name, critical: critical)
|
|
27
|
+
raise ArgumentError, "warn_as: must be :ok or :fail" unless %i[ok fail].include?(warn_as)
|
|
28
|
+
|
|
29
|
+
@warn_as = warn_as
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def run
|
|
33
|
+
report = nil
|
|
34
|
+
timing = with_timing { report = Diagnostics.report }
|
|
35
|
+
return timing unless timing[:status] == :ok
|
|
36
|
+
|
|
37
|
+
forbidden = report[:forbidden_pins]
|
|
38
|
+
unless forbidden.empty?
|
|
39
|
+
names = forbidden.map { |p| "#{p[:flag]} (#{p[:env_var]})" }.join(", ")
|
|
40
|
+
return timing.merge(status: :fail, error: "forbidden flags pinned on: #{names}", error_class: VIOLATION_LABEL)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
warnings = Diagnostics.warnings(report)
|
|
44
|
+
return timing if warnings.empty?
|
|
45
|
+
return timing.merge(warnings: warnings) if warn_as == :ok
|
|
46
|
+
|
|
47
|
+
timing.merge(status: :fail, error: warnings.join("; "), error_class: WARNING_LABEL, warnings: warnings)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|