benchdog-flipper 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 +91 -0
- data/lib/benchdog/flipper/api.rb +209 -0
- data/lib/benchdog/flipper/mapping.rb +50 -0
- data/lib/benchdog/flipper/store.rb +114 -0
- data/lib/benchdog/flipper.rb +25 -0
- data/lib/benchdog-flipper.rb +3 -0
- data/sig/benchdog-flipper.rbs +91 -0
- data/sig/flipper.rbs +41 -0
- metadata +84 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 0f32288e2a60a7fe8faf1abcd0c9a1c5e208bb21b832a57df5b6b4e907c6e73d
|
|
4
|
+
data.tar.gz: e3be71f3e14707c4e66708dc31812c0f6d2e167c6f67d052dcd9fb7ba69958de
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: add0f46f5334e74b47d8064ec003441d5da29ec0668d0923d595dfe27244d4df0a9a59817e717ba11f8310f95f2135fed4a6150829ac2bda3814a76ec6cc2285
|
|
7
|
+
data.tar.gz: 06a3e268fbbeae8a562d1f5619f176bc8e31c6a07bd36a4ce21f213662e888bbc87bc7af2479b762eaa91d8626e35ebb05a8a3c6c2ecb52c0535a6e429c275c0
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Joe Gaudet
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# benchdog-flipper
|
|
2
|
+
|
|
3
|
+
[Benchdog](https://joegaudet.com/benchdog/) on [Flipper](https://www.flippercloud.io/) data. The
|
|
4
|
+
gem has:
|
|
5
|
+
|
|
6
|
+
- `Benchdog::Flipper::Store`: a Benchdog store that reads and writes through a Flipper adapter.
|
|
7
|
+
- `Benchdog::Flipper.import`: a one-time copy of Flipper features into a Benchdog store.
|
|
8
|
+
- `Benchdog::Flipper::Api`: the Flipper HTTP API on a Benchdog store, for Flipper clients.
|
|
9
|
+
|
|
10
|
+
Benchdog uses Flipper's bucket function, so a percentage of actors gives the same actors in
|
|
11
|
+
Flipper and in Benchdog. The Flipper actor id (`flipper_id`) is the Benchdog actor id.
|
|
12
|
+
|
|
13
|
+
## The Flipper store
|
|
14
|
+
|
|
15
|
+
```ruby
|
|
16
|
+
store = Benchdog::Flipper::Store.new(Flipper.adapter, environment: "production")
|
|
17
|
+
|
|
18
|
+
Benchdog::Rails.configure do |config|
|
|
19
|
+
config.store = store
|
|
20
|
+
config.environments = [{ name: "production" }]
|
|
21
|
+
config.environment = "production"
|
|
22
|
+
# ...
|
|
23
|
+
end
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Flipper keeps only the gates. The store has these capabilities: `boolean`, `actors`, `groups`,
|
|
27
|
+
`percentageOfActors`, `percentageOfTime`, `expressions` and `write`. A write that needs more
|
|
28
|
+
(variants, rules, prerequisites, schedules, segments, the kill switch, tags, an owner, or a second
|
|
29
|
+
environment) fails with `Benchdog::CapabilityMissing`. The admin API gives it as `422` with the
|
|
30
|
+
code `CAPABILITY_MISSING`.
|
|
31
|
+
|
|
32
|
+
Flipper has no audit log, so the store keeps the Benchdog audit entries in a side store: the
|
|
33
|
+
`audit:` option. The default, `Benchdog::Flipper::MemoryAuditLog`, is in memory. A side store has
|
|
34
|
+
`audit_log(environment)` and `append(environment, entry)`.
|
|
35
|
+
|
|
36
|
+
## The importer
|
|
37
|
+
|
|
38
|
+
```ruby
|
|
39
|
+
Benchdog::Flipper.import(Flipper.instance, store: Benchdog::Rails::ActiveRecordStore.new,
|
|
40
|
+
environment: "production")
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Each gate goes to the gate with the same function: boolean to `boolean`, actors to `actors`,
|
|
44
|
+
groups to `groups`, expression to `expression`, percentage of actors to `percentageOfActors`, and
|
|
45
|
+
percentage of time to `percentageOfTime`. Each flag gets an audit entry with the source `external`.
|
|
46
|
+
|
|
47
|
+
## The Flipper HTTP API
|
|
48
|
+
|
|
49
|
+
A Flipper client (`Flipper::Adapters::Http`) can use a Benchdog store as its server. Mount one app
|
|
50
|
+
for each environment. The app has no authentication. A Flipper client is a server and has no
|
|
51
|
+
session, so put the app behind a token check of your own:
|
|
52
|
+
|
|
53
|
+
```ruby
|
|
54
|
+
# config/routes.rb
|
|
55
|
+
flipper_token = lambda { |request|
|
|
56
|
+
ActiveSupport::SecurityUtils.secure_compare(
|
|
57
|
+
request.headers["Benchdog-Token"].to_s, ENV.fetch("FLIPPER_API_TOKEN")
|
|
58
|
+
)
|
|
59
|
+
}
|
|
60
|
+
constraints(flipper_token) do
|
|
61
|
+
mount Benchdog::Flipper::Api.new(store: Benchdog::Rails::ActiveRecordStore.new,
|
|
62
|
+
environment: "production"), at: "/flipper/api"
|
|
63
|
+
end
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
```ruby
|
|
67
|
+
# In the Flipper client app
|
|
68
|
+
Flipper.configure do |config|
|
|
69
|
+
config.adapter do
|
|
70
|
+
Flipper::Adapters::Http.new(url: "https://app.example.com/flipper/api",
|
|
71
|
+
headers: { "Benchdog-Token" => ENV.fetch("FLIPPER_API_TOKEN") })
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
The app serves the features list, one feature, add, remove, clear, and enable and disable for each
|
|
77
|
+
gate. Each write makes a Benchdog audit entry with the actor `flipper-api`.
|
|
78
|
+
|
|
79
|
+
A Benchdog flag that Flipper cannot show (variants, rules, prerequisites, a schedule, segments or
|
|
80
|
+
the kill switch) is not in the features list. A read or a write of that flag gives `422` with the
|
|
81
|
+
code `100`, so a Flipper client never gets wrong data. Tags and the owner do not change an
|
|
82
|
+
evaluation, so a flag with them shows, and a write keeps them.
|
|
83
|
+
|
|
84
|
+
## Checks
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
bundle install
|
|
88
|
+
bundle exec rubocop
|
|
89
|
+
bundle exec steep check
|
|
90
|
+
bundle exec rspec
|
|
91
|
+
```
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "uri"
|
|
5
|
+
|
|
6
|
+
module Benchdog
|
|
7
|
+
module Flipper
|
|
8
|
+
# The Flipper HTTP API (the routes that Flipper::Adapters::Http calls) on a Benchdog store, as
|
|
9
|
+
# a plain Rack app. Mount one app for each environment, behind your own authentication:
|
|
10
|
+
#
|
|
11
|
+
# mount Benchdog::Flipper::Api.new(store: store, environment: "production"), at: "/flipper"
|
|
12
|
+
#
|
|
13
|
+
# A flag that Flipper cannot show (variants, rules, prerequisites, a schedule, segments, the
|
|
14
|
+
# kill switch) is not in the feature list, and a read or a write of it gives 422. Tags and the
|
|
15
|
+
# owner do not change an evaluation, so a flag with them shows, and a write keeps them.
|
|
16
|
+
# Each write makes a Benchdog audit entry with the actor "flipper-api".
|
|
17
|
+
class Api
|
|
18
|
+
ACTOR = { "id" => "flipper-api", "name" => "Flipper API" }.freeze
|
|
19
|
+
MORE_INFO = "https://flippercloud.io/docs/api#error-code-reference"
|
|
20
|
+
ERRORS = {
|
|
21
|
+
feature_not_found: [404, 1, "Feature not found."],
|
|
22
|
+
percentage_invalid: [422, 3, "Percentage must be a positive number less than or equal to 100."],
|
|
23
|
+
flipper_id_invalid: [422, 4, "Required parameter flipper_id is missing."],
|
|
24
|
+
name_invalid: [422, 5, "Required parameter name is missing."],
|
|
25
|
+
expression_invalid: [422, 7, "The provided expression was not valid."],
|
|
26
|
+
method_not_allowed: [405, 10, "Request method not allowed."],
|
|
27
|
+
not_flipper: [422, 100, "Flipper cannot show this Benchdog flag."]
|
|
28
|
+
}.freeze
|
|
29
|
+
# The gates in the order of Flipper's Feature#gates: [key, name, Benchdog gate].
|
|
30
|
+
GATES = [%w[boolean boolean boolean], %w[expression expression expression], %w[actors actor actors],
|
|
31
|
+
%w[percentage_of_actors percentage_of_actors percentageOfActors],
|
|
32
|
+
%w[percentage_of_time percentage_of_time percentageOfTime], %w[groups group groups]].freeze
|
|
33
|
+
|
|
34
|
+
# An error answer in the format of the flipper-api gem.
|
|
35
|
+
class Halt < StandardError
|
|
36
|
+
attr_reader :error
|
|
37
|
+
|
|
38
|
+
def initialize(error)
|
|
39
|
+
super(error.to_s)
|
|
40
|
+
@error = error
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def initialize(store:, environment:)
|
|
45
|
+
@store = store
|
|
46
|
+
@environment = environment
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def call(env)
|
|
50
|
+
request = AdminApi::Request.new(env)
|
|
51
|
+
path = request.path.delete_suffix("/")
|
|
52
|
+
return features(request) if path == "/features"
|
|
53
|
+
|
|
54
|
+
key, gate = path.match(%r{\A/features/([^/]+)(?:/([a-z_]+))?\z})&.captures
|
|
55
|
+
raise Halt, :method_not_allowed unless key
|
|
56
|
+
|
|
57
|
+
feature(request, URI.decode_uri_component(key), gate)
|
|
58
|
+
rescue Halt => e
|
|
59
|
+
status, code, message = ERRORS.fetch(e.error)
|
|
60
|
+
json({ "code" => code, "message" => message, "more_info" => MORE_INFO }, status)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
private
|
|
64
|
+
|
|
65
|
+
def json(body, status = 200) = [status, { "content-type" => "application/json" }, [JSON.generate(body)]]
|
|
66
|
+
|
|
67
|
+
def flags = @store.load(@environment)["flags"]
|
|
68
|
+
|
|
69
|
+
def shown?(flag) = Mapping.missing(flag.except("tags", "owner")).nil?
|
|
70
|
+
|
|
71
|
+
def features(request)
|
|
72
|
+
case request.method
|
|
73
|
+
when "GET"
|
|
74
|
+
keys = request.query["keys"]&.split(",")
|
|
75
|
+
shown = flags.values.select { |f| shown?(f) && (keys.nil? || keys.include?(f["key"])) }
|
|
76
|
+
json({ "features" => shown.map { |f| feature_json(f, request.query["exclude_gate_names"] == "true") } })
|
|
77
|
+
when "POST"
|
|
78
|
+
name = body(request)["name"].to_s
|
|
79
|
+
raise Halt, :name_invalid if name.empty?
|
|
80
|
+
|
|
81
|
+
json(feature_json(flags[name] || change(name, "POST /features") { |flag| flag }, false))
|
|
82
|
+
else raise Halt, :method_not_allowed
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def feature(request, key, gate)
|
|
87
|
+
flag = flags[key]
|
|
88
|
+
raise Halt, :not_flipper if flag && !shown?(flag)
|
|
89
|
+
return read_or_remove(request, key, flag) if gate.nil?
|
|
90
|
+
return clear(request, key, flag) if gate == "clear"
|
|
91
|
+
|
|
92
|
+
next_gates = gates(request, gate, flag.to_h["gates"].to_h)
|
|
93
|
+
json(feature_json(change(key, "#{request.method} /features/#{key}/#{gate}") do |current|
|
|
94
|
+
current.merge("gates" => next_gates).reject { |k, v| k == "gates" && v.empty? }
|
|
95
|
+
end, false))
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def read_or_remove(request, key, flag)
|
|
99
|
+
case request.method
|
|
100
|
+
when "GET"
|
|
101
|
+
raise Halt, :feature_not_found unless flag
|
|
102
|
+
|
|
103
|
+
json(feature_json(flag, request.query["exclude_gate_names"] == "true"))
|
|
104
|
+
when "DELETE"
|
|
105
|
+
write({ "kind" => "deleteFlag", "key" => key }, "DELETE /features/#{key}") if flag
|
|
106
|
+
[204, {}, []]
|
|
107
|
+
else raise Halt, :method_not_allowed
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def clear(request, key, flag)
|
|
112
|
+
raise Halt, :method_not_allowed unless request.method == "DELETE"
|
|
113
|
+
|
|
114
|
+
change(key, "DELETE /features/#{key}/clear") { |current| current.except("gates") } if flag
|
|
115
|
+
[204, {}, []]
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# The gates after a Flipper enable (POST) or disable (DELETE) of one gate.
|
|
119
|
+
def gates(request, gate, current)
|
|
120
|
+
enable = request.method == "POST"
|
|
121
|
+
raise Halt, :method_not_allowed unless enable || request.method == "DELETE"
|
|
122
|
+
# Flipper's disable of the boolean gate clears each gate.
|
|
123
|
+
return enable ? current.merge("boolean" => true) : current.slice if gate == "boolean"
|
|
124
|
+
|
|
125
|
+
Mapping.open_gates(current.merge(gate_update(gate, body(request), enable, current)))
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def gate_update(gate, params, enable, current)
|
|
129
|
+
case gate
|
|
130
|
+
when "actors" then { "actors" => member(current["actors"], params["flipper_id"], enable, :flipper_id_invalid) }
|
|
131
|
+
when "groups" then { "groups" => member(current["groups"], params["name"], enable, :name_invalid) }
|
|
132
|
+
when "percentage_of_actors" then { "percentageOfActors" => percentage(params, enable) }
|
|
133
|
+
when "percentage_of_time" then { "percentageOfTime" => percentage(params, enable) }
|
|
134
|
+
when "expression" then { "expression" => enable ? expression(params) : nil }
|
|
135
|
+
else raise Halt, :method_not_allowed
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def member(list, value, enable, error)
|
|
140
|
+
raise Halt, error if value.to_s.empty?
|
|
141
|
+
|
|
142
|
+
enable ? (list.to_a | [value.to_s]) : list.to_a - [value.to_s]
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def percentage(params, enable)
|
|
146
|
+
return 0 unless enable
|
|
147
|
+
|
|
148
|
+
value = Float(params["percentage"].to_s, exception: false)
|
|
149
|
+
raise Halt, :percentage_invalid unless value&.between?(0, 100)
|
|
150
|
+
|
|
151
|
+
value == value.floor ? value.to_i : value
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def expression(params)
|
|
155
|
+
unless Validation.expression?(params) && Mapping.missing({ "gates" => { "expression" => params } }).nil?
|
|
156
|
+
raise Halt,
|
|
157
|
+
:expression_invalid
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
params
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
# Writes the flag that the block makes from the current flag (or a new one), with an audit entry.
|
|
164
|
+
def change(key, route)
|
|
165
|
+
flag = yield(flags[key] || { "key" => key })
|
|
166
|
+
write({ "kind" => "putFlag", "flag" => flag }, route)
|
|
167
|
+
flag
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def write(change, route)
|
|
171
|
+
@store.write(@environment, change, { "actor" => ACTOR, "source" => "api", "reason" => "Flipper API: #{route}" })
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def body(request)
|
|
175
|
+
parsed = JSON.parse(request.env["rack.input"]&.read.to_s.then { |text| text.empty? ? "{}" : text })
|
|
176
|
+
parsed.is_a?(Hash) ? parsed : {}
|
|
177
|
+
rescue JSON::ParserError
|
|
178
|
+
{}
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
def feature_json(flag, exclude_names)
|
|
182
|
+
gates = flag["gates"].to_h
|
|
183
|
+
values = GATES.map { |key, name, gate| [key, name, gate_value(gate, gates[gate])] }
|
|
184
|
+
{
|
|
185
|
+
"key" => flag["key"], "state" => state(gates),
|
|
186
|
+
"gates" => values.map do |key, name, value|
|
|
187
|
+
{ "key" => key, "value" => value }.merge(exclude_names ? {} : { "name" => name })
|
|
188
|
+
end
|
|
189
|
+
}
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
# The value as a Flipper adapter keeps it: "true", a number as text, a list, or nil.
|
|
193
|
+
def gate_value(gate, value)
|
|
194
|
+
case gate
|
|
195
|
+
when "boolean" then value ? "true" : nil
|
|
196
|
+
when "actors", "groups" then value.to_a
|
|
197
|
+
when "expression" then value
|
|
198
|
+
else value&.to_s
|
|
199
|
+
end
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
def state(gates)
|
|
203
|
+
return "on" if gates["boolean"] || gates["percentageOfTime"] == 100
|
|
204
|
+
|
|
205
|
+
Mapping.open_gates(gates).empty? ? "off" : "conditional"
|
|
206
|
+
end
|
|
207
|
+
end
|
|
208
|
+
end
|
|
209
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Benchdog
|
|
4
|
+
module Flipper
|
|
5
|
+
# The map between a Flipper feature and a Benchdog flag. Flipper keeps only the gates.
|
|
6
|
+
module Mapping
|
|
7
|
+
# The flag fields that Flipper cannot keep, with the capability that each one needs. The
|
|
8
|
+
# other fields that are not "key" or "gates" ("enabled", "tags", "owner") need no capability
|
|
9
|
+
# name: Flipper has no place for them.
|
|
10
|
+
FIELDS = { "type" => "variants", "variations" => "variants", "offVariation" => "variants",
|
|
11
|
+
"onVariation" => "variants", "fallthrough" => "variants", "rules" => "variants",
|
|
12
|
+
"prerequisites" => "prerequisites", "schedule" => "schedules" }.freeze
|
|
13
|
+
|
|
14
|
+
module_function
|
|
15
|
+
|
|
16
|
+
# The flag of a Flipper feature, from its Flipper::GateValues.
|
|
17
|
+
def flag(key, values)
|
|
18
|
+
gates = open_gates({ "boolean" => values.boolean, "actors" => values.actors.to_a.sort,
|
|
19
|
+
"groups" => values.groups.to_a.sort, "expression" => values.expression,
|
|
20
|
+
"percentageOfActors" => values.percentage_of_actors,
|
|
21
|
+
"percentageOfTime" => values.percentage_of_time })
|
|
22
|
+
gates.empty? ? { "key" => key } : { "key" => key, "gates" => gates }
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# The gates without the closed ones. A closed gate is the same as no gate (SPEC section 3).
|
|
26
|
+
def open_gates(gates) = gates.reject { |_, value| closed?(value) }
|
|
27
|
+
|
|
28
|
+
def closed?(value)
|
|
29
|
+
value.nil? || value == false || (value.is_a?(Numeric) && value.zero?) || (value.is_a?(Array) && value.empty?)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# The capability that a flag needs and Flipper does not have, and a message, or nil.
|
|
33
|
+
def missing(flag)
|
|
34
|
+
field = (flag.keys - %w[key gates]).first
|
|
35
|
+
return [FIELDS[field], "Flipper has no \"#{field}\" on a feature"] if field
|
|
36
|
+
return ["segments", "Flipper has no segments"] if segment?(flag.dig("gates", "expression"))
|
|
37
|
+
|
|
38
|
+
nil
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def segment?(expression)
|
|
42
|
+
case expression
|
|
43
|
+
when Hash then expression.key?("Segment") || expression.values.any? { |args| segment?(args) }
|
|
44
|
+
when Array then expression.any? { |arg| segment?(arg) }
|
|
45
|
+
else false
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
|
|
5
|
+
module Benchdog
|
|
6
|
+
module Flipper
|
|
7
|
+
# A Benchdog store that reads and writes through a Flipper adapter, so Benchdog and the
|
|
8
|
+
# dashboard work on existing Flipper data. One store is one environment. Flipper has no audit
|
|
9
|
+
# log, so the Benchdog audit entries go to `audit`: an object with `audit_log(environment)` and
|
|
10
|
+
# `append(environment, entry)`.
|
|
11
|
+
#
|
|
12
|
+
# store = Benchdog::Flipper::Store.new(Flipper.adapter, environment: "production")
|
|
13
|
+
class Store
|
|
14
|
+
CAPABILITIES = %w[boolean actors groups percentageOfActors percentageOfTime expressions write].freeze
|
|
15
|
+
|
|
16
|
+
def initialize(adapter, environment:, audit: MemoryAuditLog.new)
|
|
17
|
+
@flipper = ::Flipper.new(adapter)
|
|
18
|
+
@environment = environment
|
|
19
|
+
@audit = audit
|
|
20
|
+
@lock = Mutex.new
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def capabilities = CAPABILITIES
|
|
24
|
+
|
|
25
|
+
def load(environment)
|
|
26
|
+
return Benchdog::Store.empty_set(environment) unless environment == @environment
|
|
27
|
+
|
|
28
|
+
values = @flipper.adapter.get_all
|
|
29
|
+
flags = values.keys.sort.to_h { |key| [key, Mapping.flag(key, ::Flipper::GateValues.new(values[key]))] }
|
|
30
|
+
Benchdog::Store.empty_set(environment).merge("flags" => flags)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# The data and the last audit hash: a change made outside Benchdog also changes it.
|
|
34
|
+
def version(environment)
|
|
35
|
+
last = audit_log(environment).last
|
|
36
|
+
Digest::SHA256.hexdigest(Audit.canonical_json(load(environment)) + (last ? last["hash"] : ""))
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def write(environment, change, audit)
|
|
40
|
+
@lock.synchronize do
|
|
41
|
+
check(environment, change)
|
|
42
|
+
entry = Benchdog::Store.next_audit_entry(environment, audit_log(environment).last, load(environment),
|
|
43
|
+
change, audit)
|
|
44
|
+
change["kind"] == "putFlag" ? put(change["flag"]) : @flipper.remove(change["key"])
|
|
45
|
+
@audit.append(environment, entry)
|
|
46
|
+
entry
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def audit_log(environment) = environment == @environment ? @audit.audit_log(environment) : []
|
|
51
|
+
|
|
52
|
+
private
|
|
53
|
+
|
|
54
|
+
def check(environment, change)
|
|
55
|
+
unless environment == @environment
|
|
56
|
+
raise CapabilityMissing.new("environments", "The Flipper store has one environment: #{@environment}")
|
|
57
|
+
end
|
|
58
|
+
raise CapabilityMissing.new("segments", "Flipper has no segments") if change["kind"].end_with?("Segment")
|
|
59
|
+
|
|
60
|
+
capability, message = Mapping.missing(change["flag"].to_h)
|
|
61
|
+
raise CapabilityMissing.new(capability, message.to_s) if message
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Changes only the gates that differ, so a reader never sees the feature off for a moment.
|
|
65
|
+
def put(flag)
|
|
66
|
+
feature = @flipper[flag["key"]]
|
|
67
|
+
feature.add
|
|
68
|
+
now = gates_of(feature)
|
|
69
|
+
want = Mapping.open_gates(flag["gates"].to_h)
|
|
70
|
+
# Flipper turns the boolean gate off with a clear of each gate.
|
|
71
|
+
if now["boolean"] && !want["boolean"]
|
|
72
|
+
feature.clear
|
|
73
|
+
now = gates_of(feature)
|
|
74
|
+
end
|
|
75
|
+
feature.enable if want["boolean"] && !now["boolean"]
|
|
76
|
+
put_lists(feature, now, want)
|
|
77
|
+
put_values(feature, now, want)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def gates_of(feature) = Mapping.flag(feature.key, feature.gate_values)["gates"].to_h
|
|
81
|
+
|
|
82
|
+
def put_lists(feature, now, want)
|
|
83
|
+
(want["actors"].to_a - now["actors"].to_a).each { |id| feature.enable_actor(::Flipper::Actor.new(id)) }
|
|
84
|
+
(now["actors"].to_a - want["actors"].to_a).each { |id| feature.disable_actor(::Flipper::Actor.new(id)) }
|
|
85
|
+
(want["groups"].to_a - now["groups"].to_a).each { |name| feature.enable_group(name) }
|
|
86
|
+
(now["groups"].to_a - want["groups"].to_a).each { |name| feature.disable_group(name) }
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def put_values(feature, now, want)
|
|
90
|
+
if want["expression"] != now["expression"]
|
|
91
|
+
want["expression"] ? feature.enable_expression(want["expression"]) : feature.disable_expression
|
|
92
|
+
end
|
|
93
|
+
percentage = want.fetch("percentageOfActors", 0)
|
|
94
|
+
feature.enable_percentage_of_actors(percentage) if percentage != now.fetch("percentageOfActors", 0)
|
|
95
|
+
percentage = want.fetch("percentageOfTime", 0)
|
|
96
|
+
feature.enable_percentage_of_time(percentage) if percentage != now.fetch("percentageOfTime", 0)
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# Audit entries in memory, for a store that has no audit log.
|
|
101
|
+
class MemoryAuditLog
|
|
102
|
+
def initialize
|
|
103
|
+
@entries = {}
|
|
104
|
+
@lock = Mutex.new
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def audit_log(environment) = @lock.synchronize { Benchdog::Store.copy(@entries[environment].to_a) }
|
|
108
|
+
|
|
109
|
+
def append(environment, entry)
|
|
110
|
+
@lock.synchronize { @entries[environment] = @entries[environment].to_a + [Benchdog::Store.copy(entry)] }
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "benchdog"
|
|
4
|
+
require "flipper"
|
|
5
|
+
require_relative "flipper/mapping"
|
|
6
|
+
require_relative "flipper/store"
|
|
7
|
+
require_relative "flipper/api"
|
|
8
|
+
|
|
9
|
+
module Benchdog
|
|
10
|
+
# Benchdog on Flipper data. Inside this module, `::Flipper` is the Flipper gem.
|
|
11
|
+
module Flipper
|
|
12
|
+
IMPORT_ACTOR = { "id" => "benchdog:import", "name" => "Flipper import" }.freeze
|
|
13
|
+
|
|
14
|
+
# Copies each feature of a Flipper instance into a Benchdog store, one time. Each flag gets an
|
|
15
|
+
# audit entry with the source "external". Returns the flag keys.
|
|
16
|
+
def self.import(flipper, store:, environment:, actor: IMPORT_ACTOR)
|
|
17
|
+
values = flipper.adapter.get_all
|
|
18
|
+
values.keys.sort.each do |key|
|
|
19
|
+
flag = Mapping.flag(key, ::Flipper::GateValues.new(values[key]))
|
|
20
|
+
store.write(environment, { "kind" => "putFlag", "flag" => flag },
|
|
21
|
+
{ "actor" => actor, "source" => "external", "reason" => "Import from Flipper" })
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
module Benchdog
|
|
2
|
+
module Flipper
|
|
3
|
+
IMPORT_ACTOR: json_hash
|
|
4
|
+
|
|
5
|
+
def self.import: (::Flipper::DSL flipper, store: _Store, environment: String, ?actor: json_hash) -> Array[String]
|
|
6
|
+
|
|
7
|
+
interface _AuditLog
|
|
8
|
+
def audit_log: (String environment) -> Array[json_hash]
|
|
9
|
+
def append: (String environment, json_hash entry) -> untyped
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
module Mapping
|
|
13
|
+
FIELDS: Hash[String, String]
|
|
14
|
+
|
|
15
|
+
def self?.flag: (String key, ::Flipper::GateValues values) -> json_hash
|
|
16
|
+
def self?.open_gates: (json_hash gates) -> json_hash
|
|
17
|
+
def self?.closed?: (json value) -> bool
|
|
18
|
+
def self?.missing: (json_hash flag) -> [String?, String]?
|
|
19
|
+
def self?.segment?: (json expression) -> bool
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
class Store
|
|
23
|
+
include _Store
|
|
24
|
+
|
|
25
|
+
CAPABILITIES: Array[String]
|
|
26
|
+
|
|
27
|
+
@flipper: ::Flipper::DSL
|
|
28
|
+
@environment: String
|
|
29
|
+
@audit: _AuditLog
|
|
30
|
+
@lock: Thread::Mutex
|
|
31
|
+
|
|
32
|
+
def initialize: (untyped adapter, environment: String, ?audit: _AuditLog) -> void
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
def check: (String environment, json_hash change) -> void
|
|
37
|
+
def put: (json_hash flag) -> void
|
|
38
|
+
def gates_of: (::Flipper::Feature feature) -> json_hash
|
|
39
|
+
def put_lists: (::Flipper::Feature feature, json_hash now, json_hash want) -> void
|
|
40
|
+
def put_values: (::Flipper::Feature feature, json_hash now, json_hash want) -> void
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
class Api
|
|
44
|
+
ACTOR: json_hash
|
|
45
|
+
MORE_INFO: String
|
|
46
|
+
ERRORS: Hash[Symbol, [Integer, Integer, String]]
|
|
47
|
+
GATES: Array[[String, String, String]]
|
|
48
|
+
|
|
49
|
+
class Halt < StandardError
|
|
50
|
+
attr_reader error: Symbol
|
|
51
|
+
def initialize: (Symbol error) -> void
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
@store: _Store
|
|
55
|
+
@environment: String
|
|
56
|
+
|
|
57
|
+
def initialize: (store: _Store, environment: String) -> void
|
|
58
|
+
def call: (rack_env env) -> rack_response
|
|
59
|
+
|
|
60
|
+
private
|
|
61
|
+
|
|
62
|
+
def json: (untyped body, ?Integer status) -> rack_response
|
|
63
|
+
def flags: () -> Hash[String, json_hash]
|
|
64
|
+
def shown?: (json_hash flag) -> bool
|
|
65
|
+
def features: (AdminApi::Request request) -> rack_response
|
|
66
|
+
def feature: (AdminApi::Request request, String key, String? gate) -> rack_response
|
|
67
|
+
def read_or_remove: (AdminApi::Request request, String key, json_hash? flag) -> rack_response
|
|
68
|
+
def clear: (AdminApi::Request request, String key, json_hash? flag) -> rack_response
|
|
69
|
+
def gates: (AdminApi::Request request, String gate, json_hash current) -> json_hash
|
|
70
|
+
def gate_update: (String gate, json_hash params, bool enable, json_hash current) -> json_hash
|
|
71
|
+
def member: (Array[String]? list, untyped value, bool enable, Symbol error) -> Array[String]
|
|
72
|
+
def percentage: (json_hash params, bool enable) -> Numeric
|
|
73
|
+
def expression: (json_hash params) -> json_hash
|
|
74
|
+
def change: (String key, String route) { (json_hash flag) -> json_hash } -> json_hash
|
|
75
|
+
def write: (json_hash change, String route) -> json_hash
|
|
76
|
+
def body: (AdminApi::Request request) -> json_hash
|
|
77
|
+
def feature_json: (json_hash flag, bool exclude_names) -> json_hash
|
|
78
|
+
def gate_value: (String gate, json value) -> json
|
|
79
|
+
def state: (json_hash gates) -> String
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
class MemoryAuditLog
|
|
83
|
+
include _AuditLog
|
|
84
|
+
|
|
85
|
+
@entries: Hash[String, Array[json_hash]]
|
|
86
|
+
@lock: Thread::Mutex
|
|
87
|
+
|
|
88
|
+
def initialize: () -> void
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
data/sig/flipper.rbs
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# The parts of the flipper gem that Benchdog uses. Flipper has no RBS of its own.
|
|
2
|
+
module Flipper
|
|
3
|
+
def self.new: (untyped adapter) -> DSL
|
|
4
|
+
|
|
5
|
+
class DSL
|
|
6
|
+
def adapter: () -> untyped
|
|
7
|
+
def []: (String | Symbol name) -> Feature
|
|
8
|
+
def remove: (String | Symbol name) -> bool
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
class Feature
|
|
12
|
+
def add: () -> bool
|
|
13
|
+
def clear: () -> bool
|
|
14
|
+
def enable: (*untyped) -> bool
|
|
15
|
+
def disable: (*untyped) -> bool
|
|
16
|
+
def key: () -> String
|
|
17
|
+
def enable_actor: (untyped actor) -> bool
|
|
18
|
+
def disable_actor: (untyped actor) -> bool
|
|
19
|
+
def disable_group: (String | Symbol group) -> bool
|
|
20
|
+
def disable_expression: () -> bool
|
|
21
|
+
def enable_group: (String | Symbol group) -> bool
|
|
22
|
+
def enable_expression: (untyped expression) -> bool
|
|
23
|
+
def enable_percentage_of_actors: (Numeric percentage) -> bool
|
|
24
|
+
def enable_percentage_of_time: (Numeric percentage) -> bool
|
|
25
|
+
def gate_values: () -> GateValues
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
class GateValues
|
|
29
|
+
attr_reader boolean: bool
|
|
30
|
+
attr_reader actors: Set[String]
|
|
31
|
+
attr_reader groups: Set[String]
|
|
32
|
+
attr_reader expression: Hash[String, untyped]?
|
|
33
|
+
attr_reader percentage_of_actors: Numeric
|
|
34
|
+
attr_reader percentage_of_time: Numeric
|
|
35
|
+
def initialize: (Hash[Symbol, untyped] adapter_values) -> void
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
class Actor
|
|
39
|
+
def initialize: (String flipper_id) -> void
|
|
40
|
+
end
|
|
41
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: benchdog-flipper
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Joe Gaudet
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-12-03 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: benchdog
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - '='
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: 0.1.0
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - '='
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: 0.1.0
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: flipper
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '1.3'
|
|
34
|
+
type: :runtime
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '1.3'
|
|
41
|
+
description:
|
|
42
|
+
email:
|
|
43
|
+
- joe@joegaudet.com
|
|
44
|
+
executables: []
|
|
45
|
+
extensions: []
|
|
46
|
+
extra_rdoc_files: []
|
|
47
|
+
files:
|
|
48
|
+
- LICENSE
|
|
49
|
+
- README.md
|
|
50
|
+
- lib/benchdog-flipper.rb
|
|
51
|
+
- lib/benchdog/flipper.rb
|
|
52
|
+
- lib/benchdog/flipper/api.rb
|
|
53
|
+
- lib/benchdog/flipper/mapping.rb
|
|
54
|
+
- lib/benchdog/flipper/store.rb
|
|
55
|
+
- sig/benchdog-flipper.rbs
|
|
56
|
+
- sig/flipper.rbs
|
|
57
|
+
homepage: https://joegaudet.com/benchdog/
|
|
58
|
+
licenses:
|
|
59
|
+
- MIT
|
|
60
|
+
metadata:
|
|
61
|
+
source_code_uri: https://github.com/joegaudet/benchdog/tree/main/ruby/benchdog-flipper
|
|
62
|
+
bug_tracker_uri: https://github.com/joegaudet/benchdog/issues
|
|
63
|
+
rubygems_mfa_required: 'true'
|
|
64
|
+
post_install_message:
|
|
65
|
+
rdoc_options: []
|
|
66
|
+
require_paths:
|
|
67
|
+
- lib
|
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
69
|
+
requirements:
|
|
70
|
+
- - ">="
|
|
71
|
+
- !ruby/object:Gem::Version
|
|
72
|
+
version: '3.3'
|
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
74
|
+
requirements:
|
|
75
|
+
- - ">="
|
|
76
|
+
- !ruby/object:Gem::Version
|
|
77
|
+
version: '0'
|
|
78
|
+
requirements: []
|
|
79
|
+
rubygems_version: 3.5.11
|
|
80
|
+
signing_key:
|
|
81
|
+
specification_version: 4
|
|
82
|
+
summary: 'Benchdog on Flipper data: a Flipper backend, an importer, and the Flipper
|
|
83
|
+
HTTP API.'
|
|
84
|
+
test_files: []
|