benchdog 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/LICENSE +21 -0
- data/README.md +67 -0
- data/lib/benchdog/admin_api/audit_routes.rb +94 -0
- data/lib/benchdog/admin_api/flag_routes.rb +139 -0
- data/lib/benchdog/admin_api/meta_routes.rb +159 -0
- data/lib/benchdog/admin_api/request.rb +41 -0
- data/lib/benchdog/admin_api.rb +208 -0
- data/lib/benchdog/approvals.rb +60 -0
- data/lib/benchdog/audit.rb +76 -0
- data/lib/benchdog/bucket.rb +11 -0
- data/lib/benchdog/client.rb +36 -0
- data/lib/benchdog/evaluator.rb +194 -0
- data/lib/benchdog/expression.rb +122 -0
- data/lib/benchdog/flag.schema.json +419 -0
- data/lib/benchdog/payload.rb +18 -0
- data/lib/benchdog/schedule.rb +57 -0
- data/lib/benchdog/spec/store_examples.rb +121 -0
- data/lib/benchdog/store.rb +137 -0
- data/lib/benchdog/usage.rb +39 -0
- data/lib/benchdog/validation.rb +27 -0
- data/lib/benchdog/values.rb +49 -0
- data/lib/benchdog/version.rb +5 -0
- data/lib/benchdog.rb +23 -0
- data/sig/admin_api.rbs +202 -0
- data/sig/benchdog.rbs +127 -0
- data/sig/json_schemer.rbs +10 -0
- metadata +86 -0
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "securerandom"
|
|
4
|
+
require "benchdog"
|
|
5
|
+
|
|
6
|
+
# The store contract (packages/store-tests in TypeScript), as RSpec shared examples:
|
|
7
|
+
#
|
|
8
|
+
# RSpec.describe MyStore do
|
|
9
|
+
# it_behaves_like "a Benchdog store", MyStore::CAPABILITIES do
|
|
10
|
+
# let(:store) { MyStore.new }
|
|
11
|
+
# end
|
|
12
|
+
# end
|
|
13
|
+
#
|
|
14
|
+
# `store` MUST have no data. `env` gives a new environment name for each call; a store for one
|
|
15
|
+
# environment can define it again. The examples that need a capability exist only when
|
|
16
|
+
# `capabilities` has it.
|
|
17
|
+
RSpec.shared_examples "a Benchdog store" do |capabilities = Benchdog::CAPABILITIES|
|
|
18
|
+
def env = "env-#{SecureRandom.hex(4)}"
|
|
19
|
+
|
|
20
|
+
def who = { "actor" => { "id" => "user:42", "name" => "Zoë" }, "source" => "api", "reason" => "test" }
|
|
21
|
+
|
|
22
|
+
def put(flag) = { "kind" => "putFlag", "flag" => flag }
|
|
23
|
+
|
|
24
|
+
it "writes a change and an audit entry in one step" do
|
|
25
|
+
e = env
|
|
26
|
+
store.write(e, put({ "key" => "a" }), who)
|
|
27
|
+
store.write(e, put({ "key" => "a", "gates" => { "boolean" => true } }), who)
|
|
28
|
+
|
|
29
|
+
expect(store.load(e)["flags"]).to eq("a" => { "key" => "a", "gates" => { "boolean" => true } })
|
|
30
|
+
log = store.audit_log(e)
|
|
31
|
+
expect(log.map { |x| [x["seq"], x["action"], x["flag"]] }).to eq([[1, "flag.create", "a"], [2, "flag.update", "a"]])
|
|
32
|
+
expect(log[0]["actor"]).to eq(who["actor"])
|
|
33
|
+
expect(log[1]["before"]).to eq("key" => "a")
|
|
34
|
+
expect(Benchdog::Audit.verify_chain(log)).to eq("valid" => true)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it "gives an empty flag set for an unknown environment" do
|
|
38
|
+
set = store.load(env)
|
|
39
|
+
expect(set["flags"]).to eq({})
|
|
40
|
+
expect(set["segments"].to_h).to eq({})
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it "changes the version on each write" do
|
|
44
|
+
e = env
|
|
45
|
+
v0 = store.version(e)
|
|
46
|
+
store.write(e, put({ "key" => "a" }), who)
|
|
47
|
+
v1 = store.version(e)
|
|
48
|
+
store.write(e, { "kind" => "deleteFlag", "key" => "a" }, who)
|
|
49
|
+
expect([v0, v1, store.version(e)].uniq.size).to eq(3)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
it "refuses to delete a flag that does not exist, and writes no entry" do
|
|
53
|
+
e = env
|
|
54
|
+
expect { store.write(e, { "kind" => "deleteFlag", "key" => "nope" }, who) }.to raise_error(Benchdog::NotFoundError)
|
|
55
|
+
expect(store.audit_log(e)).to eq([])
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
it "gives copies, so a caller cannot change the stored data" do
|
|
59
|
+
e = env
|
|
60
|
+
store.write(e, put({ "key" => "a" }), who)
|
|
61
|
+
store.load(e)["flags"]["a"]["enabled"] = false
|
|
62
|
+
expect(store.load(e)["flags"]["a"]).not_to have_key("enabled")
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
it "gives a gapless, valid chain under concurrent writes" do
|
|
66
|
+
e = env
|
|
67
|
+
Array.new(8) { |i| Thread.new { store.write(e, put({ "key" => "f#{i}" }), who) } }.each(&:join)
|
|
68
|
+
log = store.audit_log(e)
|
|
69
|
+
expect(log.map { |x| x["seq"] }).to eq((1..8).to_a)
|
|
70
|
+
expect(Benchdog::Audit.verify_chain(log)).to eq("valid" => true)
|
|
71
|
+
expect(store.load(e)["flags"].keys.sort).to eq((0..7).map { |i| "f#{i}" })
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
if capabilities.include?("environments")
|
|
75
|
+
it "keeps each environment apart" do
|
|
76
|
+
a = env
|
|
77
|
+
b = env
|
|
78
|
+
store.write(a, put({ "key" => "x" }), who)
|
|
79
|
+
expect(store.load(b)["flags"]).to eq({})
|
|
80
|
+
expect(store.audit_log(b)).to eq([])
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
if capabilities.include?("segments")
|
|
85
|
+
it "stores segments" do
|
|
86
|
+
e = env
|
|
87
|
+
expression = { "Equal" => [{ "Property" => "plan" }, "pro"] }
|
|
88
|
+
store.write(e, { "kind" => "putSegment", "name" => "pro", "expression" => expression }, who)
|
|
89
|
+
expect(store.load(e)["segments"]).to eq("pro" => expression)
|
|
90
|
+
store.write(e, { "kind" => "deleteSegment", "name" => "pro" }, who)
|
|
91
|
+
expect(store.load(e)["segments"].to_h).to eq({})
|
|
92
|
+
expect(store.audit_log(e).map { |x| [x["action"], x["segment"]] })
|
|
93
|
+
.to eq([%w[segment.create pro], %w[segment.delete pro]])
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
if capabilities.include?("variants")
|
|
98
|
+
it "keeps every field of a large flag, with Unicode" do
|
|
99
|
+
e = env
|
|
100
|
+
flag = {
|
|
101
|
+
"key" => "theme",
|
|
102
|
+
"type" => "object",
|
|
103
|
+
"variations" => [
|
|
104
|
+
{ "key" => "a",
|
|
105
|
+
"value" => { "label" => "Zoë 🚩", "columns" => 2, "ratio" => 0.333, "list" => [1, "two", nil] } },
|
|
106
|
+
{ "key" => "b", "value" => { "long" => "x" * 20_000 } }
|
|
107
|
+
],
|
|
108
|
+
"offVariation" => "a",
|
|
109
|
+
"onVariation" => "b",
|
|
110
|
+
"gates" => { "actors" => Array.new(500) { |i| "user:#{i}" }, "percentageOfActors" => 33.333 },
|
|
111
|
+
"rules" => [{ "id" => "r", "expression" => { "Segment" => "pro" },
|
|
112
|
+
"serve" => { "rollout" => [{ "variation" => "a", "weight" => 100 }] } }],
|
|
113
|
+
"schedule" => [{ "at" => "2026-01-01T00:00:00Z", "set" => { "enabled" => false } }],
|
|
114
|
+
"tags" => ["client"]
|
|
115
|
+
}
|
|
116
|
+
store.write(e, put(flag), who)
|
|
117
|
+
expect(store.load(e)["flags"]["theme"]).to eq(flag)
|
|
118
|
+
expect(store.audit_log(e)[0]["after"]).to eq(flag)
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "time"
|
|
5
|
+
|
|
6
|
+
module Benchdog
|
|
7
|
+
# What a store can do. The dashboard hides the controls for a missing capability. The Benchdog
|
|
8
|
+
# stores have each one. A foreign backend can also have "nativeAudit".
|
|
9
|
+
CAPABILITIES = %w[boolean actors groups percentageOfActors percentageOfTime expressions variants
|
|
10
|
+
jsonValues segments environments schedules approvals prerequisites write].freeze
|
|
11
|
+
|
|
12
|
+
# Raised when a change names a flag or a segment that does not exist.
|
|
13
|
+
class NotFoundError < StandardError; end
|
|
14
|
+
|
|
15
|
+
# Raised when a write needs a capability that the store does not have.
|
|
16
|
+
class CapabilityMissing < StandardError
|
|
17
|
+
attr_reader :capability
|
|
18
|
+
|
|
19
|
+
def initialize(capability, message)
|
|
20
|
+
super(message)
|
|
21
|
+
@capability = capability
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# The parts that each store shares. A change is a JSON hash: {"kind" => "putFlag", "flag" => ...},
|
|
26
|
+
# "deleteFlag" with "key", "putSegment" with "name" and "expression", "deleteSegment" with "name".
|
|
27
|
+
# An audit input has "actor", "source", and can have "reason" and "action".
|
|
28
|
+
module Store
|
|
29
|
+
module_function
|
|
30
|
+
|
|
31
|
+
EMPTY_SET = JSON.parse(%({"flags": {}, "segments": {}})).freeze
|
|
32
|
+
|
|
33
|
+
def empty_set(environment) = copy(EMPTY_SET).merge("environment" => environment)
|
|
34
|
+
|
|
35
|
+
# A deep copy with JSON semantics, so a caller cannot change the stored data.
|
|
36
|
+
def copy(value) = JSON.parse(JSON.generate(value))
|
|
37
|
+
|
|
38
|
+
def iso_time(time) = time.utc.iso8601(3).sub(/\.000Z\z/, "Z")
|
|
39
|
+
|
|
40
|
+
# The audit fields of a change, before the entry is sealed.
|
|
41
|
+
def describe_change(set, change)
|
|
42
|
+
case change.fetch("kind")
|
|
43
|
+
when "putFlag" then describe_put_flag(set, change["flag"])
|
|
44
|
+
when "deleteFlag" then describe_delete_flag(set, change["key"])
|
|
45
|
+
when "putSegment" then describe_put_segment(set, change["name"], change["expression"])
|
|
46
|
+
when "deleteSegment" then describe_delete_segment(set, change["name"])
|
|
47
|
+
else raise ArgumentError, "Unknown change kind #{change['kind'].inspect}"
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def describe_put_flag(set, flag)
|
|
52
|
+
before = set["flags"][flag["key"]]
|
|
53
|
+
{ "action" => before ? "flag.update" : "flag.create", "flag" => flag["key"], "before" => before, "after" => flag }
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def describe_delete_flag(set, key)
|
|
57
|
+
before = set["flags"][key] or raise NotFoundError, "No flag \"#{key}\""
|
|
58
|
+
{ "action" => "flag.delete", "flag" => key, "before" => before, "after" => nil }
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def describe_put_segment(set, name, expression)
|
|
62
|
+
segments = set["segments"].to_h
|
|
63
|
+
action = segments.key?(name) ? "segment.update" : "segment.create"
|
|
64
|
+
before = segments.key?(name) ? { "expression" => segments[name] } : nil
|
|
65
|
+
{ "action" => action, "segment" => name, "before" => before, "after" => { "expression" => expression } }
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def describe_delete_segment(set, name)
|
|
69
|
+
segments = set["segments"].to_h
|
|
70
|
+
raise NotFoundError, "No segment \"#{name}\"" unless segments.key?(name)
|
|
71
|
+
|
|
72
|
+
{ "action" => "segment.delete", "segment" => name, "before" => { "expression" => segments[name] },
|
|
73
|
+
"after" => nil }
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Applies a change to a copy of a flag set.
|
|
77
|
+
def apply_change(set, change)
|
|
78
|
+
result = copy(set)
|
|
79
|
+
result["segments"] = result["segments"].to_h
|
|
80
|
+
case change["kind"]
|
|
81
|
+
when "putFlag" then result["flags"][change["flag"]["key"]] = copy(change["flag"])
|
|
82
|
+
when "deleteFlag" then result["flags"].delete(change["key"])
|
|
83
|
+
when "putSegment" then result["segments"][change["name"]] = copy(change["expression"])
|
|
84
|
+
when "deleteSegment" then result["segments"].delete(change["name"])
|
|
85
|
+
end
|
|
86
|
+
result
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# Builds and seals the next audit entry of a chain. `last` is the last entry, or nil.
|
|
90
|
+
def next_audit_entry(environment, last, set, change, input, now = Time.now)
|
|
91
|
+
described = describe_change(set, change)
|
|
92
|
+
entry = { "seq" => (last ? last["seq"] : 0) + 1, "at" => iso_time(now), "environment" => environment }
|
|
93
|
+
entry.merge!(described.slice("flag", "segment"))
|
|
94
|
+
entry["action"] = input["action"] || described["action"]
|
|
95
|
+
entry.merge!(copy(input.slice("actor", "source", "reason")))
|
|
96
|
+
entry.merge!(copy(described.slice("before", "after")))
|
|
97
|
+
entry["prevHash"] = last ? last["hash"] : Audit::GENESIS_HASH
|
|
98
|
+
entry.merge("hash" => Audit.hash_entry(entry))
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# A store in memory, for tests and single-process apps.
|
|
103
|
+
class MemoryStore
|
|
104
|
+
def initialize
|
|
105
|
+
@sets = {}
|
|
106
|
+
@logs = {}
|
|
107
|
+
@versions = Hash.new(0)
|
|
108
|
+
@lock = Mutex.new
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def capabilities = CAPABILITIES
|
|
112
|
+
|
|
113
|
+
# The flag set of an environment. An unknown environment is an empty flag set.
|
|
114
|
+
def load(environment)
|
|
115
|
+
@lock.synchronize { Store.copy(@sets[environment] || Store.empty_set(environment)) }
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# A tag that changes on each write to the environment.
|
|
119
|
+
def version(environment) = @lock.synchronize { @versions[environment].to_s }
|
|
120
|
+
|
|
121
|
+
# Applies the change and appends its audit entry in one step. Returns the entry.
|
|
122
|
+
def write(environment, change, audit)
|
|
123
|
+
@lock.synchronize do
|
|
124
|
+
set = @sets[environment] || Store.empty_set(environment)
|
|
125
|
+
log = @logs[environment] || []
|
|
126
|
+
entry = Store.next_audit_entry(environment, log.last, set, change, audit)
|
|
127
|
+
@sets[environment] = Store.apply_change(set, change)
|
|
128
|
+
@logs[environment] = log + [entry]
|
|
129
|
+
@versions[environment] += 1
|
|
130
|
+
Store.copy(entry)
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# The audit entries of an environment, oldest first.
|
|
135
|
+
def audit_log(environment) = @lock.synchronize { Store.copy(@logs[environment].to_a) }
|
|
136
|
+
end
|
|
137
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "time"
|
|
4
|
+
|
|
5
|
+
# The stale-flag report.
|
|
6
|
+
module Benchdog
|
|
7
|
+
# The stale state of each flag. The Ruby gem keeps no usage counts yet, so a flag is stale only
|
|
8
|
+
# when it gives every actor the same value and did not change for `days`. `last_changed` maps
|
|
9
|
+
# each flag to the time of its last audit entry.
|
|
10
|
+
def self.stale_flags(flag_set, last_changed, days: 30, now: Time.now)
|
|
11
|
+
limit = now - (days * 86_400)
|
|
12
|
+
flag_set["flags"].to_h do |key, flag|
|
|
13
|
+
changed = last_changed[key] ? Time.iso8601(last_changed[key]) : now
|
|
14
|
+
usage = { "lastEvaluatedAt" => nil, "lastVariation" => nil, "count" => 0 }
|
|
15
|
+
if uniform?(flag) && changed < limit
|
|
16
|
+
[key, usage.merge("stale" => true, "staleReason" => "The same for everyone for #{days} days")]
|
|
17
|
+
elsif changed < limit
|
|
18
|
+
[key, usage.merge("stale" => true, "staleReason" => "Not evaluated for #{days} days")]
|
|
19
|
+
else
|
|
20
|
+
[key, usage.merge("stale" => false)]
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def self.uniform?(flag)
|
|
26
|
+
return true if flag["enabled"] == false
|
|
27
|
+
|
|
28
|
+
gates = flag["gates"].to_h
|
|
29
|
+
return true if gates["boolean"] == true || gates.fetch("percentageOfActors", 0) >= 100
|
|
30
|
+
|
|
31
|
+
!targeted?(flag, gates)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def self.targeted?(flag, gates)
|
|
35
|
+
gates["actors"].to_a.any? || gates["groups"].to_a.any? || !gates["expression"].nil? ||
|
|
36
|
+
gates.fetch("percentageOfActors", 0).positive? || gates.fetch("percentageOfTime", 0).positive? ||
|
|
37
|
+
flag["rules"].to_a.any? || flag["fallthrough"].to_h.key?("rollout") || flag["schedule"].to_a.any?
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json_schemer"
|
|
4
|
+
require "pathname"
|
|
5
|
+
|
|
6
|
+
module Benchdog
|
|
7
|
+
# Checks flags and expressions against the spec's JSON Schema.
|
|
8
|
+
module Validation
|
|
9
|
+
# A copy of spec/schema/flag.schema.json. A spec checks that the two are the same.
|
|
10
|
+
SCHEMA_PATH = File.join(File.dirname(__FILE__), "flag.schema.json")
|
|
11
|
+
FLAG = JSONSchemer.schema(Pathname.new(SCHEMA_PATH))
|
|
12
|
+
EXPRESSION = FLAG.ref("#/$defs/expression")
|
|
13
|
+
|
|
14
|
+
module_function
|
|
15
|
+
|
|
16
|
+
# The first error, as the TypeScript admin API words it, or nil for a valid flag.
|
|
17
|
+
def flag_error(flag)
|
|
18
|
+
error = FLAG.validate(flag).first
|
|
19
|
+
return nil unless error
|
|
20
|
+
|
|
21
|
+
pointer = error["data_pointer"].to_s
|
|
22
|
+
"The flag is not valid: #{pointer.empty? ? '/' : pointer} #{error['error']}".strip
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def expression?(expression) = EXPRESSION.valid?(expression)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "time"
|
|
5
|
+
|
|
6
|
+
module Benchdog
|
|
7
|
+
# The value conversions of the expression functions (SPEC sections 6.1 and 6.5).
|
|
8
|
+
module Values
|
|
9
|
+
# A point in time, as seconds since the Unix epoch. It is never equal to a number.
|
|
10
|
+
class TimeValue
|
|
11
|
+
attr_reader :seconds
|
|
12
|
+
|
|
13
|
+
def initialize(seconds)
|
|
14
|
+
@seconds = seconds
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
DECIMAL = /\A-?\d+(\.\d+)?\z/
|
|
19
|
+
|
|
20
|
+
module_function
|
|
21
|
+
|
|
22
|
+
def to_number(value)
|
|
23
|
+
return value if value.is_a?(Numeric)
|
|
24
|
+
return nil unless value.is_a?(String) && DECIMAL.match?(value)
|
|
25
|
+
|
|
26
|
+
value.include?(".") ? Float(value) : Integer(value, 10)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def to_time(value)
|
|
30
|
+
return TimeValue.new(value) if value.is_a?(Numeric)
|
|
31
|
+
return nil unless value.is_a?(String)
|
|
32
|
+
|
|
33
|
+
TimeValue.new(Time.iso8601(value).to_r)
|
|
34
|
+
rescue ArgumentError
|
|
35
|
+
nil
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# String(x) of SPEC section 6.5.
|
|
39
|
+
def string_of(value)
|
|
40
|
+
case value
|
|
41
|
+
when nil then ""
|
|
42
|
+
when Float then value.finite? && value == value.floor ? value.to_i.to_s : value.to_s
|
|
43
|
+
when TimeValue then Time.at(value.seconds).utc.iso8601
|
|
44
|
+
when Hash, Array then JSON.generate(value)
|
|
45
|
+
else value.to_s
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
data/lib/benchdog.rb
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "benchdog/version"
|
|
4
|
+
require_relative "benchdog/bucket"
|
|
5
|
+
require_relative "benchdog/values"
|
|
6
|
+
require_relative "benchdog/expression"
|
|
7
|
+
require_relative "benchdog/schedule"
|
|
8
|
+
require_relative "benchdog/evaluator"
|
|
9
|
+
require_relative "benchdog/audit"
|
|
10
|
+
require_relative "benchdog/store"
|
|
11
|
+
require_relative "benchdog/client"
|
|
12
|
+
require_relative "benchdog/validation"
|
|
13
|
+
require_relative "benchdog/payload"
|
|
14
|
+
require_relative "benchdog/usage"
|
|
15
|
+
require_relative "benchdog/approvals"
|
|
16
|
+
require_relative "benchdog/admin_api"
|
|
17
|
+
|
|
18
|
+
# Feature flags for Ruby. See https://joegaudet.com/benchdog/.
|
|
19
|
+
module Benchdog
|
|
20
|
+
# The version of spec/VERSION that this gem follows. The TypeScript and Python cores have the
|
|
21
|
+
# same constant, and each test suite checks it against that file.
|
|
22
|
+
SPEC_VERSION = "1"
|
|
23
|
+
end
|
data/sig/admin_api.rbs
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
module Benchdog
|
|
2
|
+
type rack_env = Hash[String, untyped]
|
|
3
|
+
type rack_response = [Integer, Hash[String, String], untyped]
|
|
4
|
+
|
|
5
|
+
CAPABILITIES: Array[String]
|
|
6
|
+
CLIENT_TAG: String
|
|
7
|
+
SCHEDULER_ACTOR: json_hash
|
|
8
|
+
|
|
9
|
+
class NotFoundError < StandardError
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
class CapabilityMissing < StandardError
|
|
13
|
+
attr_reader capability: String?
|
|
14
|
+
def initialize: (String? capability, String message) -> void
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
module Store
|
|
18
|
+
EMPTY_SET: json_hash
|
|
19
|
+
|
|
20
|
+
def self?.empty_set: (String environment) -> json_hash
|
|
21
|
+
def self?.copy: (untyped value) -> untyped
|
|
22
|
+
def self?.iso_time: (Time time) -> String
|
|
23
|
+
def self?.describe_change: (json_hash set, json_hash change) -> json_hash
|
|
24
|
+
def self?.describe_put_flag: (json_hash set, json_hash flag) -> json_hash
|
|
25
|
+
def self?.describe_delete_flag: (json_hash set, String key) -> json_hash
|
|
26
|
+
def self?.describe_put_segment: (json_hash set, String name, json expression) -> json_hash
|
|
27
|
+
def self?.describe_delete_segment: (json_hash set, String name) -> json_hash
|
|
28
|
+
def self?.apply_change: (json_hash set, json_hash change) -> json_hash
|
|
29
|
+
def self?.next_audit_entry: (String environment, json_hash? last, json_hash set, json_hash change, json_hash input, ?Time now) -> json_hash
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
interface _Store
|
|
33
|
+
def capabilities: () -> Array[String]
|
|
34
|
+
def load: (String environment) -> json_hash
|
|
35
|
+
def version: (String environment) -> String
|
|
36
|
+
def write: (String environment, json_hash change, json_hash audit) -> json_hash
|
|
37
|
+
def audit_log: (String environment) -> Array[json_hash]
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
class MemoryStore
|
|
41
|
+
include _Store
|
|
42
|
+
@sets: Hash[String, json_hash]
|
|
43
|
+
@logs: Hash[String, Array[json_hash]]
|
|
44
|
+
@versions: Hash[String, Integer]
|
|
45
|
+
@lock: Thread::Mutex
|
|
46
|
+
def initialize: () -> void
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
class Client
|
|
50
|
+
@store: _Store
|
|
51
|
+
@environment: String
|
|
52
|
+
@groups: Hash[String, ^(json_hash) -> untyped]
|
|
53
|
+
attr_reader snapshot: json_hash
|
|
54
|
+
def initialize: (store: _Store, environment: String, ?groups: Hash[String, ^(json_hash) -> untyped]) -> void
|
|
55
|
+
def refresh: () -> nil
|
|
56
|
+
def details: (String key, ?json_hash? actor, ?json default, ?json_hash context) -> Result
|
|
57
|
+
def enabled?: (String key, ?json_hash? actor, ?json_hash context) -> bool
|
|
58
|
+
def value: (String key, json_hash? actor, json default, ?json_hash context) -> json
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
module Validation
|
|
62
|
+
SCHEMA_PATH: String
|
|
63
|
+
FLAG: JSONSchemer::Schema
|
|
64
|
+
EXPRESSION: JSONSchemer::Schema
|
|
65
|
+
def self?.flag_error: (json flag) -> String?
|
|
66
|
+
def self?.expression?: (json expression) -> bool
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def self.client_payload: (json_hash flag_set, json_hash context, ?groups: Hash[String, untyped], ?now: Time) -> json_hash
|
|
70
|
+
def self.stale_flags: (json_hash flag_set, Hash[String, String] last_changed, ?days: Integer, ?now: Time) -> json_hash
|
|
71
|
+
def self.uniform?: (json_hash flag) -> bool
|
|
72
|
+
def self.targeted?: (json_hash flag, json_hash gates) -> bool
|
|
73
|
+
def self.apply_due_schedules: (_Store store, String environment, ?Time now) -> Array[String]
|
|
74
|
+
def self.scheduled: (json_hash flag, Rational now) -> json_hash
|
|
75
|
+
|
|
76
|
+
class MemoryApprovalStore
|
|
77
|
+
@items: Hash[[String, String], json_hash]
|
|
78
|
+
@lock: Thread::Mutex
|
|
79
|
+
def initialize: () -> void
|
|
80
|
+
def create: (json_hash approval) -> json_hash
|
|
81
|
+
def get: (String environment, String id) -> json_hash?
|
|
82
|
+
def list: (String environment, ?String? state) -> Array[json_hash]
|
|
83
|
+
def decide: (String environment, String id, json_hash decision) -> json_hash?
|
|
84
|
+
def set_state: (String environment, String id, String state) -> void
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
class MemoryChangeFeed
|
|
88
|
+
@listeners: Hash[String, Array[^() -> void]]
|
|
89
|
+
@lock: Thread::Mutex
|
|
90
|
+
def initialize: () -> void
|
|
91
|
+
def subscribe: (String environment) { () -> void } -> ^() -> void
|
|
92
|
+
def publish: (String environment) -> void
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
class AdminApi
|
|
96
|
+
class Problem < StandardError
|
|
97
|
+
attr_reader status: Integer
|
|
98
|
+
attr_reader code: String
|
|
99
|
+
def initialize: (Integer status, String code, String message) -> void
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
class Request
|
|
103
|
+
@query: Hash[String, String]?
|
|
104
|
+
@body: json_hash?
|
|
105
|
+
attr_reader env: rack_env
|
|
106
|
+
def initialize: (rack_env env) -> void
|
|
107
|
+
def method: () -> String
|
|
108
|
+
def path: () -> String
|
|
109
|
+
def header: (String name) -> String?
|
|
110
|
+
def query: () -> Hash[String, String]
|
|
111
|
+
def body: () -> json_hash
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
class EventStream
|
|
115
|
+
@queue: Thread::Queue
|
|
116
|
+
@unsubscribe: ^() -> void
|
|
117
|
+
@keep_alive: Float
|
|
118
|
+
def initialize: (MemoryChangeFeed feed, String env, Float keep_alive) -> void
|
|
119
|
+
def each: () { (String chunk) -> void } -> void
|
|
120
|
+
def close: () -> void
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
RANK: Hash[String, Integer]
|
|
124
|
+
SOURCES: Array[String]
|
|
125
|
+
ROUTES: Array[[String, Regexp, Symbol]]
|
|
126
|
+
CHECK_REASONS: Hash[String, String]
|
|
127
|
+
|
|
128
|
+
@options: Hash[Symbol, untyped]
|
|
129
|
+
@store: _Store
|
|
130
|
+
@environments: Array[Hash[Symbol, untyped]]
|
|
131
|
+
@known: Hash[String, Hash[Symbol, untyped]]
|
|
132
|
+
@feed: MemoryChangeFeed
|
|
133
|
+
@approvals: MemoryApprovalStore
|
|
134
|
+
|
|
135
|
+
def initialize: (**untyped options) -> void
|
|
136
|
+
def call: (rack_env env) -> rack_response
|
|
137
|
+
|
|
138
|
+
private
|
|
139
|
+
|
|
140
|
+
def json: (untyped body, ?Integer status, ?Hash[String, String] headers) -> rack_response
|
|
141
|
+
def no_content: () -> rack_response
|
|
142
|
+
def problem: (Problem error) -> rack_response
|
|
143
|
+
def invalid: (String message) -> Problem
|
|
144
|
+
def not_found: (String message) -> Problem
|
|
145
|
+
def read_reason: (untyped value) -> String
|
|
146
|
+
def environment: (String name) -> String
|
|
147
|
+
def signed_in: (Request request) -> json_hash
|
|
148
|
+
def role: (json_hash user, String env) -> String?
|
|
149
|
+
def need: (json_hash user, String env, String role) -> void
|
|
150
|
+
def any_role: (json_hash user) -> void
|
|
151
|
+
def authorize: (Request request, String name, String role) -> [json_hash, String]
|
|
152
|
+
def etag: (String env) -> String
|
|
153
|
+
def check_if_match: (Request request, String env) -> void
|
|
154
|
+
def audit_input: (Request request, json_hash user, String reason, ?String? action) -> json_hash
|
|
155
|
+
def write: (String env, json_hash change, json_hash input) -> void
|
|
156
|
+
def change: (Request request, json_hash user, String env, json_hash next_change, String reason, ?String? action) { () -> rack_response } -> rack_response
|
|
157
|
+
def flag_in: (String env, String key) -> json_hash
|
|
158
|
+
def query_environment: (Request request) -> String
|
|
159
|
+
|
|
160
|
+
# meta_routes.rb
|
|
161
|
+
def groups: () -> Hash[String, ^(json_hash) -> untyped]
|
|
162
|
+
def capabilities: (Request request) -> rack_response
|
|
163
|
+
def me: (Request request) -> rack_response
|
|
164
|
+
def references: (Request request) -> rack_response
|
|
165
|
+
def environments: (Request request) -> rack_response
|
|
166
|
+
def client: (Request request) -> rack_response
|
|
167
|
+
def audience: (Request request) -> rack_response
|
|
168
|
+
def flags_by_group: (String env) -> Hash[String, Array[String]]
|
|
169
|
+
def audience_group: (String key, String? description, Hash[String, Array[String]] flags) -> json_hash
|
|
170
|
+
def actors: (Request request) -> rack_response
|
|
171
|
+
def actor_query: (Hash[String, String] params) -> Hash[Symbol, untyped]
|
|
172
|
+
def check: (Request request, String key) -> rack_response
|
|
173
|
+
def check_reason: (Result result, json_hash flag, json_hash actor) -> String
|
|
174
|
+
def stream: (Request request) -> rack_response
|
|
175
|
+
|
|
176
|
+
# flag_routes.rb
|
|
177
|
+
def flag_set: (Request request, String name) -> rack_response
|
|
178
|
+
def get_flag: (Request request, String name, String key) -> rack_response
|
|
179
|
+
def put_flag: (Request request, String name, String key) -> rack_response
|
|
180
|
+
def delete_flag: (Request request, String name, String key) -> rack_response
|
|
181
|
+
def kill: (Request request, String name, String key) -> rack_response
|
|
182
|
+
def promote: (Request request, String name, String key) -> rack_response
|
|
183
|
+
def put_segment: (Request request, String name, String segment) -> rack_response
|
|
184
|
+
def delete_segment: (Request request, String name, String segment) -> rack_response
|
|
185
|
+
def check_variations: (json_hash flag) -> void
|
|
186
|
+
def check_prerequisites: (json_hash flag, json_hash set) -> void
|
|
187
|
+
def variation_keys: (json_hash flag) -> Array[String]
|
|
188
|
+
def serve_keys: (json_hash? serve) -> Array[String]
|
|
189
|
+
def uses_segment?: (json expression, String name) -> bool
|
|
190
|
+
def segment_users: (json_hash set, String name) -> Array[String]
|
|
191
|
+
|
|
192
|
+
# audit_routes.rb
|
|
193
|
+
def audit: (Request request, String name) -> rack_response
|
|
194
|
+
def number: (String? text, Float default) -> Float
|
|
195
|
+
def audit_export: (Request request, String name) -> rack_response
|
|
196
|
+
def audit_verify: (Request request, String name) -> rack_response
|
|
197
|
+
def approvals: (Request request, String name) -> rack_response
|
|
198
|
+
def decide: (Request request, String name, String id, String decision) -> rack_response
|
|
199
|
+
def usage: (Request request, String name) -> rack_response
|
|
200
|
+
def cron: (Request request) -> rack_response
|
|
201
|
+
end
|
|
202
|
+
end
|