satee-openfeature-provider 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 +1 -0
- data/LICENSE +21 -0
- data/README.md +210 -0
- data/lib/satee/openfeature/provider/clock.rb +38 -0
- data/lib/satee/openfeature/provider/configuration_error.rb +17 -0
- data/lib/satee/openfeature/provider/defect.rb +18 -0
- data/lib/satee/openfeature/provider/error.rb +10 -0
- data/lib/satee/openfeature/provider/flag.rb +11 -0
- data/lib/satee/openfeature/provider/flag_file.rb +76 -0
- data/lib/satee/openfeature/provider/fork_restart.rb +41 -0
- data/lib/satee/openfeature/provider/invalid_flag_file.rb +10 -0
- data/lib/satee/openfeature/provider/log.rb +90 -0
- data/lib/satee/openfeature/provider/poller.rb +325 -0
- data/lib/satee/openfeature/provider/sdk_key.rb +59 -0
- data/lib/satee/openfeature/provider/stderr_logger.rb +31 -0
- data/lib/satee/openfeature/provider/transport.rb +117 -0
- data/lib/satee/openfeature/provider/version.rb +9 -0
- data/lib/satee/openfeature/provider.rb +137 -0
- data/sig/satee/openfeature/provider/clock.rbs +38 -0
- data/sig/satee/openfeature/provider/configuration_error.rbs +11 -0
- data/sig/satee/openfeature/provider/defect.rbs +14 -0
- data/sig/satee/openfeature/provider/error.rbs +8 -0
- data/sig/satee/openfeature/provider/flag.rbs +16 -0
- data/sig/satee/openfeature/provider/flag_file.rbs +26 -0
- data/sig/satee/openfeature/provider/fork_restart.rbs +28 -0
- data/sig/satee/openfeature/provider/invalid_flag_file.rbs +8 -0
- data/sig/satee/openfeature/provider/log.rbs +54 -0
- data/sig/satee/openfeature/provider/poller.rbs +101 -0
- data/sig/satee/openfeature/provider/sdk_key.rbs +26 -0
- data/sig/satee/openfeature/provider/stderr_logger.rbs +19 -0
- data/sig/satee/openfeature/provider/transport.rbs +57 -0
- data/sig/satee/openfeature/provider.rbs +57 -0
- metadata +116 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: d8ec1095d41d8fce1cfba0a3df744376103f08809a2a23fc814af4f63d6f06a1
|
|
4
|
+
data.tar.gz: c2204718875eaa8f6003f411bc9dc4d4c9b0c0a873229d56997d38d70aadf19e
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 93f96b20262d091666b3a25fab7d089baaede6a93a91f07ef43df57cb5ec8e44cf422cd389ad8ae3139454225594db20d988f7fd8764d9a05d235deb1dfdd5e5
|
|
7
|
+
data.tar.gz: b83f22942d679990e9fdd02c78ac56f12502d813d989cbe9039f155e9db88d9c1ef9277dd914160427ba2b2792ca8702a6381bb23164952e2ff4cffd2c107e5e
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Release notes are on [GitHub Releases](https://github.com/satee-dev/openfeature-provider-ruby/releases).
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Satee
|
|
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,210 @@
|
|
|
1
|
+
# satee-openfeature-provider
|
|
2
|
+
|
|
3
|
+
An [OpenFeature](https://openfeature.dev) provider for reading [Satee](https://satee.dev) feature flags in Ruby.
|
|
4
|
+
|
|
5
|
+
The provider downloads your flags from Satee, keeps them in memory, and polls for changes in the background. Reading a
|
|
6
|
+
flag never waits on the network.
|
|
7
|
+
|
|
8
|
+
The SDK key is for server code only. Never ship it in browser code: anyone who has it can read every flag.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
bundle add satee-openfeature-provider
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
This also installs [`openfeature-sdk`](https://github.com/open-feature/ruby-sdk), the API your code reads flags through.
|
|
17
|
+
|
|
18
|
+
## Quickstart
|
|
19
|
+
|
|
20
|
+
Set `SATEE_SDK_KEY` to the server key from the Setup page in Satee, then:
|
|
21
|
+
|
|
22
|
+
```ruby
|
|
23
|
+
require "satee/openfeature/provider"
|
|
24
|
+
|
|
25
|
+
OpenFeature::SDK.configure do |config|
|
|
26
|
+
config.set_provider_and_wait(Satee::OpenFeature::Provider.new)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
flags = OpenFeature::SDK.build_client
|
|
30
|
+
puts flags.fetch_boolean_value(flag_key: "new-checkout", default_value: false)
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Save it as `app.rb` and run it with `bundle exec ruby app.rb`. `set_provider_and_wait` returns once the flags have loaded or Satee refuses
|
|
34
|
+
the key, and after at most 5 seconds otherwise. It never raises because Satee is unreachable, and polling goes on in
|
|
35
|
+
the background.
|
|
36
|
+
|
|
37
|
+
## Options
|
|
38
|
+
|
|
39
|
+
```ruby
|
|
40
|
+
require "logger"
|
|
41
|
+
|
|
42
|
+
Satee::OpenFeature::Provider.new(
|
|
43
|
+
sdk_key: ENV["SATEE_SDK_KEY"], # the default
|
|
44
|
+
poll_interval: 5, # seconds between polls (the default); values under 1 become 1
|
|
45
|
+
logger: Logger.new($stdout) # any object with debug, info, warn and error methods
|
|
46
|
+
)
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
The key is checked when the provider is created. A missing key, a browser key (`satee_client_`), or anything other
|
|
50
|
+
than `satee_server_` followed by 24 base58 characters raises `Satee::OpenFeature::Provider::ConfigurationError`. Its
|
|
51
|
+
`category` is `:missing`, `:client_key` or `:invalid`, and its message never contains the key.
|
|
52
|
+
|
|
53
|
+
Without a `logger`, lines at info and above go to standard error and debug lines are dropped. No line ever contains
|
|
54
|
+
the key, the path ID or the flag file URL; a line names the key only by its last four characters.
|
|
55
|
+
|
|
56
|
+
## How flags are read
|
|
57
|
+
|
|
58
|
+
Every Satee flag is boolean. Reading a flag is a lookup in memory. Once flags have loaded:
|
|
59
|
+
|
|
60
|
+
- A flag returns the value of its default variant, `on` (`true`) or `off` (`false`), with the reason `STATIC`.
|
|
61
|
+
- A flag that is not held returns the default you passed, with the error code `FLAG_NOT_FOUND`.
|
|
62
|
+
- A disabled flag returns the default you passed, with the reason `DISABLED` and no error code.
|
|
63
|
+
- Reading an enabled flag with a getter other than `fetch_boolean_value` or `fetch_boolean_details` returns the default
|
|
64
|
+
you passed, with the error code `TYPE_MISMATCH`.
|
|
65
|
+
|
|
66
|
+
Before any flags have loaded, every flag returns the default you passed, with the error code `PROVIDER_NOT_READY`.
|
|
67
|
+
|
|
68
|
+
## Polling and changes
|
|
69
|
+
|
|
70
|
+
The provider polls every `poll_interval` seconds with the flag file's ETag, so an unchanged file costs a `304`. A new
|
|
71
|
+
file replaces the held flags all at once. A file with a lower revision than the one held is refused: the held flags
|
|
72
|
+
stay, and it counts as a failed poll (see [When Satee is down](#when-satee-is-down)).
|
|
73
|
+
|
|
74
|
+
When a new file changes flags, the provider emits `PROVIDER_CONFIGURATION_CHANGED` with the changed keys:
|
|
75
|
+
|
|
76
|
+
```ruby
|
|
77
|
+
OpenFeature::SDK.add_handler(OpenFeature::SDK::ProviderEvent::PROVIDER_CONFIGURATION_CHANGED, ->(details) {
|
|
78
|
+
puts "Satee flags changed: #{details[:flags_changed].join(", ")}"
|
|
79
|
+
})
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
A key counts as changed when it was added or removed, was enabled or disabled, or is enabled and its default variant
|
|
83
|
+
changed.
|
|
84
|
+
|
|
85
|
+
## When Satee is down
|
|
86
|
+
|
|
87
|
+
The provider never takes your app down. It keeps the last flags it loaded and keeps trying.
|
|
88
|
+
|
|
89
|
+
| What happens | Flags served | Status | Logged |
|
|
90
|
+
| ----------------------------------------------- | -------------------------------- | ------- | ------------------------------ |
|
|
91
|
+
| Flags do not load within 5 seconds of startup | your defaults | `ERROR` | error, then info on recovery |
|
|
92
|
+
| A poll fails after flags loaded | the flags last loaded | `STALE` | warn, then info on recovery |
|
|
93
|
+
| Satee answers `404` or `403` for the key | the flags last loaded, if any | `ERROR` | error naming the key's last four characters, then info on recovery |
|
|
94
|
+
| A poll after flags loaded brings a file the provider cannot use | the flags last loaded | `STALE` | warn |
|
|
95
|
+
| An exception that is not a `StandardError` ends polling | the flags last loaded, if any | `ERROR` | error |
|
|
96
|
+
| A forked child cannot start polling | the flags its parent held, if any | `ERROR` | error |
|
|
97
|
+
|
|
98
|
+
- During startup a failed request is retried every second.
|
|
99
|
+
- After a `404` or `403`, polling slows to once a minute, or to `poll_interval` if that is longer, until the key works
|
|
100
|
+
again. A key that starts working needs no restart.
|
|
101
|
+
- A `429` or `503` with `Retry-After` (seconds or an HTTP date) delays the next poll by that much, up to the same
|
|
102
|
+
limit.
|
|
103
|
+
- Each request is abandoned after 5 seconds.
|
|
104
|
+
- The status changes emit `PROVIDER_READY`, `PROVIDER_STALE` and `PROVIDER_ERROR`. `PROVIDER_ERROR` carries the error
|
|
105
|
+
code `PROVIDER_NOT_READY` while no flags are held, and `GENERAL` while held flags are served.
|
|
106
|
+
|
|
107
|
+
Every other failed poll is logged at debug only, and so is a `Retry-After` that is neither seconds nor an HTTP date.
|
|
108
|
+
|
|
109
|
+
An error that no server or network causes, such as a `NoMethodError`, is a defect in the provider. It is logged at
|
|
110
|
+
error with its class and message, and polling continues. A defect while requesting or taking the flags counts as a
|
|
111
|
+
failed poll. One anywhere else in polling reports `ERROR` until a poll succeeds. Please report either.
|
|
112
|
+
|
|
113
|
+
An exception that is not a `StandardError`, such as `LoadError` or `SystemStackError`, raised while the provider
|
|
114
|
+
handles a response or calls your code ends polling for good. The provider reports `ERROR` and logs at error that its
|
|
115
|
+
flags will no longer refresh, naming the exception's class. The exception still ends the thread, so Ruby reports it
|
|
116
|
+
as usual, but shutting the provider down or replacing it with another does not raise it a second time. One raised
|
|
117
|
+
while requesting ends only that request, which counts as a failed poll.
|
|
118
|
+
|
|
119
|
+
A `StandardError` from your own code does not stop polling either:
|
|
120
|
+
|
|
121
|
+
- A `logger` that raises loses no line: the line goes to standard error instead, followed by the class of the
|
|
122
|
+
logger's error.
|
|
123
|
+
- The OpenFeature SDK catches an error raised by a handler you add with `add_handler` and logs it to its own logger,
|
|
124
|
+
so set one, for example `OpenFeature::SDK.logger = Rails.logger`. Without it, the SDK drops the error. An error
|
|
125
|
+
that still reaches the provider while it emits an event is logged at warn with its class and message.
|
|
126
|
+
|
|
127
|
+
## Rails
|
|
128
|
+
|
|
129
|
+
```ruby
|
|
130
|
+
# config/initializers/satee.rb
|
|
131
|
+
if ENV["SATEE_SDK_KEY"].present?
|
|
132
|
+
OpenFeature::SDK.configure do |config|
|
|
133
|
+
config.set_provider_and_wait(Satee::OpenFeature::Provider.new(logger: Rails.logger))
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Without a provider, OpenFeature returns your defaults, so environments with no key (tests, asset builds) boot and run
|
|
139
|
+
without Satee. When Satee cannot be reached, boot waits up to 5 seconds, then continues on your defaults.
|
|
140
|
+
|
|
141
|
+
## Forking servers and job runners
|
|
142
|
+
|
|
143
|
+
Nothing to set up. After a fork, the child process keeps polling on its own: it starts from the flags its parent held
|
|
144
|
+
and carries on, without waiting for a new startup. This covers Puma in cluster mode, Solid Queue's workers, and
|
|
145
|
+
`fork` or `Process.daemon` in your own scripts. Sidekiq runs in one process, where every thread shares the provider.
|
|
146
|
+
|
|
147
|
+
With Puma's `preload_app!`, the initializer runs once in the master and the master keeps polling too.
|
|
148
|
+
|
|
149
|
+
The provider looks up `flags.satee.dev` on a background thread, and Ruby makes `fork` wait for any lookup still
|
|
150
|
+
running. If the system DNS resolver hangs, a fork, such as Puma booting its workers, can wait until the resolver gives
|
|
151
|
+
up, even after the provider has abandoned the request at 5 seconds. A responsive resolver avoids this.
|
|
152
|
+
|
|
153
|
+
## Shutdown
|
|
154
|
+
|
|
155
|
+
```ruby
|
|
156
|
+
OpenFeature::SDK.shutdown
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
This stops polling and returns once the polling thread has ended. It also removes every handler added with
|
|
160
|
+
`add_handler`. Call it where your process shuts down, such as `at_exit`. A process that exits without it ends
|
|
161
|
+
normally: the polling thread never keeps a process alive.
|
|
162
|
+
|
|
163
|
+
Setting a provider again while it is still set does nothing: OpenFeature starts a provider only once until it is shut
|
|
164
|
+
down or replaced. To start it over, call `OpenFeature::SDK.shutdown` and set it again, or create a new provider. Don't
|
|
165
|
+
call the provider's own `shutdown` while it is set: polling stops, but OpenFeature keeps serving its last flags and
|
|
166
|
+
reports nothing.
|
|
167
|
+
|
|
168
|
+
## Ruby support
|
|
169
|
+
|
|
170
|
+
Ruby 3.4 or later, the versions `openfeature-sdk` supports.
|
|
171
|
+
|
|
172
|
+
## Development
|
|
173
|
+
|
|
174
|
+
CI runs these commands on Ruby 3.4 and 4.0. Run them the same way before opening a pull request:
|
|
175
|
+
|
|
176
|
+
```sh
|
|
177
|
+
git submodule update --init
|
|
178
|
+
bundle install
|
|
179
|
+
bundle exec standardrb
|
|
180
|
+
bundle exec rspec
|
|
181
|
+
bundle exec rbs collection install --frozen
|
|
182
|
+
bundle exec steep check
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
`bundle exec rake` runs Standard, RSpec and Steep in one go, once the RBS collection is installed.
|
|
186
|
+
|
|
187
|
+
`spec/conformance` is the [Satee provider conformance suite](https://github.com/satee-dev/openfeature-provider-conformance),
|
|
188
|
+
pinned to one commit. Every check in it runs as part of RSpec.
|
|
189
|
+
|
|
190
|
+
### Live check
|
|
191
|
+
|
|
192
|
+
Before a release, the owner checks the provider against `flags.satee.dev` with a real server key and a flag that
|
|
193
|
+
exists and is enabled in that project. From a checkout:
|
|
194
|
+
|
|
195
|
+
```sh
|
|
196
|
+
SATEE_SDK_KEY=<server key> bundle exec ruby -Ilib -e '
|
|
197
|
+
require "satee/openfeature/provider"
|
|
198
|
+
OpenFeature::SDK.configure { |config| config.set_provider_and_wait(Satee::OpenFeature::Provider.new) }
|
|
199
|
+
details = OpenFeature::SDK.build_client.fetch_boolean_details(flag_key: ARGV.fetch(0), default_value: false)
|
|
200
|
+
puts [details.value, details.variant, details.reason, details.error_code].inspect
|
|
201
|
+
OpenFeature::SDK.shutdown
|
|
202
|
+
' <flag key>
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
It passes when it prints the flag's value and variant with the reason `"STATIC"` and no error code, for example
|
|
206
|
+
`[true, "on", "STATIC", nil]`. Change the flag in Satee and run it again: the new value shows.
|
|
207
|
+
|
|
208
|
+
## License
|
|
209
|
+
|
|
210
|
+
[MIT](LICENSE)
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Satee
|
|
4
|
+
module OpenFeature
|
|
5
|
+
class Provider
|
|
6
|
+
# Monotonic time for timers, wall time for HTTP dates, and mailboxes whose reads time out on monotonic time.
|
|
7
|
+
class Clock
|
|
8
|
+
def now
|
|
9
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def wall_now
|
|
13
|
+
Time.now
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def mailbox
|
|
17
|
+
Mailbox.new(self)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
class Mailbox
|
|
21
|
+
def initialize(clock)
|
|
22
|
+
@clock = clock
|
|
23
|
+
@queue = Thread::Queue.new
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def push(message)
|
|
27
|
+
@queue.push(message)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Returns the next message, or nil once the deadline, if any, passes without one.
|
|
31
|
+
def pop(deadline)
|
|
32
|
+
@queue.pop(timeout: deadline && [deadline - @clock.now, 0].max)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Satee
|
|
4
|
+
module OpenFeature
|
|
5
|
+
class Provider
|
|
6
|
+
class ConfigurationError < Error
|
|
7
|
+
# Lets a caller tell the mistakes apart without matching messages.
|
|
8
|
+
attr_reader :category
|
|
9
|
+
|
|
10
|
+
def initialize(message, category:)
|
|
11
|
+
super(message)
|
|
12
|
+
@category = category
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Satee
|
|
4
|
+
module OpenFeature
|
|
5
|
+
class Provider
|
|
6
|
+
# An error that no server or network causes, kept as its class and a message with the given texts hidden.
|
|
7
|
+
# Building one never raises, so it can be built in any rescue.
|
|
8
|
+
Defect = Data.define(:error_class, :message) do
|
|
9
|
+
def self.from(error, hiding: {})
|
|
10
|
+
message = hiding.reduce(error.message.to_s) { |text, (secret, label)| text.gsub(secret, label) }
|
|
11
|
+
new(error_class: error.class, message:)
|
|
12
|
+
rescue
|
|
13
|
+
new(error_class: error.class, message: "(its message could not be shown)")
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module Satee
|
|
6
|
+
module OpenFeature
|
|
7
|
+
class Provider
|
|
8
|
+
# A flag file in flagd format, parsed whole: either every flag in it is usable or the file is refused.
|
|
9
|
+
class FlagFile
|
|
10
|
+
MAX_REVISION = 9_007_199_254_740_991
|
|
11
|
+
VARIANTS = {"on" => true, "off" => false}.freeze
|
|
12
|
+
STATES = {"ENABLED" => true, "DISABLED" => false}.freeze
|
|
13
|
+
private_constant :MAX_REVISION, :VARIANTS, :STATES
|
|
14
|
+
|
|
15
|
+
attr_reader :revision, :flags
|
|
16
|
+
|
|
17
|
+
def self.parse(json)
|
|
18
|
+
document = JSON.parse(json, allow_duplicate_key: false)
|
|
19
|
+
raise InvalidFlagFile, "the file is not a JSON object" unless document.is_a?(Hash)
|
|
20
|
+
|
|
21
|
+
metadata = document["metadata"]
|
|
22
|
+
raise InvalidFlagFile, "metadata is not an object" unless metadata.is_a?(Hash)
|
|
23
|
+
|
|
24
|
+
revision = metadata["revision"]
|
|
25
|
+
unless revision.is_a?(Integer) && revision.between?(0, MAX_REVISION)
|
|
26
|
+
raise InvalidFlagFile, "metadata.revision is not an integer from 0 to 2^53 - 1"
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
flags = document["flags"]
|
|
30
|
+
raise InvalidFlagFile, "flags is not an object" unless flags.is_a?(Hash)
|
|
31
|
+
|
|
32
|
+
new(revision:, flags: flags.to_h { |key, definition| [key, parse_flag(key, definition)] })
|
|
33
|
+
rescue JSON::ParserError
|
|
34
|
+
raise InvalidFlagFile, "the file is not valid JSON or repeats a member name"
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def self.parse_flag(key, definition)
|
|
38
|
+
flag = "flag #{key.slice(0, 64).to_s.dump}"
|
|
39
|
+
raise InvalidFlagFile, "#{flag} is not an object" unless definition.is_a?(Hash)
|
|
40
|
+
|
|
41
|
+
enabled = STATES.fetch(definition["state"]) do
|
|
42
|
+
raise InvalidFlagFile, "#{flag} has a state other than \"ENABLED\" or \"DISABLED\""
|
|
43
|
+
end
|
|
44
|
+
raise InvalidFlagFile, "#{flag} has targeting rules" if definition.key?("targeting")
|
|
45
|
+
unless definition["variants"] == VARIANTS
|
|
46
|
+
raise InvalidFlagFile, "#{flag} does not have exactly the variants on: true and off: false"
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
variant = definition["defaultVariant"]
|
|
50
|
+
unless VARIANTS.key?(variant)
|
|
51
|
+
raise InvalidFlagFile, "#{flag} has a defaultVariant other than \"on\" or \"off\""
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
Flag.new(enabled:, variant:)
|
|
55
|
+
end
|
|
56
|
+
private_class_method :parse_flag, :new
|
|
57
|
+
|
|
58
|
+
def initialize(revision:, flags:)
|
|
59
|
+
@revision = revision
|
|
60
|
+
@flags = flags.freeze
|
|
61
|
+
freeze
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Keys added or removed, whose state changed, or that are enabled in both files with another variant. A
|
|
65
|
+
# disabled flag serves the caller's default, so its variant changing changes nothing.
|
|
66
|
+
def changed_keys(previous)
|
|
67
|
+
(flags.keys | previous.flags.keys).select do |key|
|
|
68
|
+
now = flags[key]
|
|
69
|
+
before = previous.flags[key]
|
|
70
|
+
now.nil? || before.nil? || now.enabled != before.enabled || (now.enabled && now.variant != before.variant)
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Satee
|
|
4
|
+
module OpenFeature
|
|
5
|
+
class Provider
|
|
6
|
+
# Only the forking thread survives a fork, so a child would hold its parent's flags with nothing keeping them
|
|
7
|
+
# current. Ruby calls Process._fork for every fork it makes except Process.daemon's, so hooking both lets each
|
|
8
|
+
# started poller carry on in the child before the child runs anything else.
|
|
9
|
+
module ForkRestart
|
|
10
|
+
@pollers = ObjectSpace::WeakMap.new
|
|
11
|
+
@lock = Mutex.new
|
|
12
|
+
|
|
13
|
+
def self.track(poller)
|
|
14
|
+
@lock.synchronize { @pollers[poller] = true }
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.untrack(poller)
|
|
18
|
+
@lock.synchronize { @pollers.delete(poller) }
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def self.restart_all
|
|
22
|
+
@lock.synchronize { @pollers.keys }.each(&:restart_in_child)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
module Hook
|
|
26
|
+
def _fork
|
|
27
|
+
pid = super
|
|
28
|
+
ForkRestart.restart_all if pid.zero?
|
|
29
|
+
pid
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def daemon(...)
|
|
33
|
+
super.tap { ForkRestart.restart_all }
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
Process.singleton_class.prepend(Hook)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Satee
|
|
4
|
+
module OpenFeature
|
|
5
|
+
class Provider
|
|
6
|
+
# Every line the provider logs at info or above. A line names the key only by its last four characters, never
|
|
7
|
+
# by the key, the path ID or the URL. A StandardError from the logger never reaches the polling thread, and the
|
|
8
|
+
# line goes to standard error instead.
|
|
9
|
+
class Log
|
|
10
|
+
def initialize(logger, key_hint:)
|
|
11
|
+
@logger = logger
|
|
12
|
+
@key_hint = key_hint
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def not_ready
|
|
16
|
+
write(:error, "Satee flags did not load within the startup wait. Serving defaults and retrying.")
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def key_not_found
|
|
20
|
+
write(:error, "Satee found no flags for the SDK key ending in #{@key_hint} (HTTP 404). Check SATEE_SDK_KEY.")
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def request_refused
|
|
24
|
+
write(:error, "Satee refused the flag request for the SDK key ending in #{@key_hint} (HTTP 403).")
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def stale
|
|
28
|
+
write(:warn, "Satee flags could not be refreshed. Serving the flags last loaded.")
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def recovered
|
|
32
|
+
write(:info, "Satee flags are current again.")
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def file_rejected(reason)
|
|
36
|
+
write(:warn, "Satee sent a flag file this provider cannot use: #{reason}. Keeping the current flags.")
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def poll_failed(reason)
|
|
40
|
+
write(:debug, "Satee flag poll failed: #{reason}.")
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def handler_failed(error)
|
|
44
|
+
write(:warn, "A Satee provider event handler raised #{describe(error)}")
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def defect(during, defect)
|
|
48
|
+
write(:error, "The Satee provider failed while #{during}, which is a defect in it: #{defect.error_class}: #{defect.message}. Please report it.")
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def retry_after_ignored(value)
|
|
52
|
+
write(:debug, "Satee ignored a Retry-After that is neither seconds nor an HTTP date: #{value[0, 64].inspect}.")
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def polling_stopped(error_class)
|
|
56
|
+
write(:error, "Satee polling stopped on #{error_class}. Flags will not refresh in this process.")
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def restart_failed(error)
|
|
60
|
+
write(:error, "Satee flags will not refresh in this forked process: polling could not restart (#{describe(error)}).")
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
private
|
|
64
|
+
|
|
65
|
+
def describe(error)
|
|
66
|
+
"#{error.class}: #{error.message}"
|
|
67
|
+
rescue
|
|
68
|
+
"#{error.class} (its message raised)"
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def write(level, message)
|
|
72
|
+
case level
|
|
73
|
+
in :debug then @logger.debug(message)
|
|
74
|
+
in :info then @logger.info(message)
|
|
75
|
+
in :warn then @logger.warn(message)
|
|
76
|
+
in :error then @logger.error(message)
|
|
77
|
+
end
|
|
78
|
+
rescue => e
|
|
79
|
+
fall_back(level, "#{message} (the logger raised #{e.class})")
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def fall_back(level, message)
|
|
83
|
+
$stderr.write("satee-openfeature #{level.upcase}: #{message}\n")
|
|
84
|
+
rescue IOError, SystemCallError
|
|
85
|
+
nil
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|