flagnetic 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 +202 -0
- data/README.md +276 -0
- data/lib/flagnetic/backoff.rb +29 -0
- data/lib/flagnetic/changes.rb +88 -0
- data/lib/flagnetic/client.rb +609 -0
- data/lib/flagnetic/controller_helpers.rb +97 -0
- data/lib/flagnetic/detail.rb +73 -0
- data/lib/flagnetic/eval/evaluation.rb +382 -0
- data/lib/flagnetic/eval/instant.rb +71 -0
- data/lib/flagnetic/eval/murmur3.rb +70 -0
- data/lib/flagnetic/eval/operators.rb +225 -0
- data/lib/flagnetic/eval/regex.rb +647 -0
- data/lib/flagnetic/eval/ruleset.rb +267 -0
- data/lib/flagnetic/eval/semver.rb +80 -0
- data/lib/flagnetic/eval/types.rb +148 -0
- data/lib/flagnetic/eval/unicode_tables.rb +180 -0
- data/lib/flagnetic/eval.rb +38 -0
- data/lib/flagnetic/event_batch.rb +119 -0
- data/lib/flagnetic/exposures.rb +342 -0
- data/lib/flagnetic/fallback_chain.rb +230 -0
- data/lib/flagnetic/generations.rb +50 -0
- data/lib/flagnetic/global.rb +59 -0
- data/lib/flagnetic/local_overrides.rb +65 -0
- data/lib/flagnetic/logging.rb +62 -0
- data/lib/flagnetic/railtie.rb +146 -0
- data/lib/flagnetic/recorder.rb +135 -0
- data/lib/flagnetic/reporter.rb +94 -0
- data/lib/flagnetic/sse_parser.rb +94 -0
- data/lib/flagnetic/store.rb +34 -0
- data/lib/flagnetic/stream.rb +65 -0
- data/lib/flagnetic/summaries.rb +257 -0
- data/lib/flagnetic/testing.rb +160 -0
- data/lib/flagnetic/transport.rb +106 -0
- data/lib/flagnetic/uuid.rb +25 -0
- data/lib/flagnetic/version.rb +16 -0
- data/lib/flagnetic/worker.rb +270 -0
- data/lib/flagnetic.rb +34 -0
- metadata +79 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 7777e134a978460329e49acf4e982e291f02a01cc1eac12b312ff142a149dad2
|
|
4
|
+
data.tar.gz: c40536af379f16b170e3eedb0a899dab8726e7966df71c384bab6210bad213f0
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 07ec2f99b51334afb56925b35a4c83445073dddeb7ed288ab2e1d99f322e870af18b4539f1690ef90adc82cab0badfab97e63c823a968529603b639b6698cf1a
|
|
7
|
+
data.tar.gz: 172615dba045bc0d9392a61af845dfb8d2baa571226bc8b6f949289a1fd4695c246405d801bff54440bb247b0496961f32e3ae68bf515ffc75acf2e9f8045201
|
data/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
data/README.md
ADDED
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
# Flagnetic SDK for Ruby
|
|
2
|
+
|
|
3
|
+
The Ruby SDK for [Flagnetic](../../README.md), server mode: it evaluates flags inside a Ruby process from a ruleset held in memory. It depends on nothing outside the standard library.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bundle add flagnetic
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
| Runtime | Supported |
|
|
12
|
+
|---|---|
|
|
13
|
+
| Ruby | 3.3 and later (CI: 3.3, 3.4, 4.0) |
|
|
14
|
+
| Rails, for the integration | 8.0 and later (CI: 8.0, 8.1) |
|
|
15
|
+
| Flagnetic server | 0.1.0 and later: the gem reads ruleset `schema_version` 1 over `/sdk/v1` ([compatibility](../../docs/sdks/compatibility.md)) |
|
|
16
|
+
|
|
17
|
+
The oldest Ruby and Rails still supported upstream set the floor; one that reaches its end of life is dropped in a minor release.
|
|
18
|
+
|
|
19
|
+
## Quick start
|
|
20
|
+
|
|
21
|
+
```ruby
|
|
22
|
+
require "flagnetic"
|
|
23
|
+
|
|
24
|
+
client = Flagnetic::Client.new(ENV.fetch("FLAGNETIC_SDK_KEY"), base_url: "https://flags.example.com")
|
|
25
|
+
client.ready(timeout: 5)
|
|
26
|
+
|
|
27
|
+
if client.bool("new_checkout", default: false, context: { units: { user: "u-1001" } })
|
|
28
|
+
# the new checkout
|
|
29
|
+
end
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Under Rails no initializer is needed: set `FLAGNETIC_SDK_KEY` and `FLAGNETIC_BASE_URL` and use `Flagnetic.client` (see [Rails](#rails)). In tests, `Flagnetic::Testing` serves the values you set without a server (see [Testing](#testing)).
|
|
33
|
+
|
|
34
|
+
## Propagation
|
|
35
|
+
|
|
36
|
+
A change saved in Flagnetic, a kill switch included, reaches a client holding the change stream (the default) within **2 seconds at the 99th percentile**, whichever replica the change and the client each go through. With `stream: false`, or behind a proxy that blocks `text/event-stream`, it arrives at the next poll, within 30 seconds. Every pull request that changes this gem checks the 2 seconds against two real replicas.
|
|
37
|
+
|
|
38
|
+
## Client
|
|
39
|
+
|
|
40
|
+
`Flagnetic::Client` is the server-mode client. It downloads the ruleset with the server key, holds it in memory, follows every change through the change stream (polling while the stream is down) and evaluates locally, so a getter never waits on the network and never raises.
|
|
41
|
+
|
|
42
|
+
```ruby
|
|
43
|
+
require "flagnetic"
|
|
44
|
+
|
|
45
|
+
client = Flagnetic::Client.new(ENV.fetch("FLAGNETIC_SDK_KEY"), base_url: "https://flags.example.com")
|
|
46
|
+
client.ready(timeout: 5) # => :fresh, :last_good or :built_in
|
|
47
|
+
|
|
48
|
+
context = { units: { user: "u-1001" }, attributes: { plan: "pro" } }
|
|
49
|
+
client.bool("new_checkout", default: false, context: context)
|
|
50
|
+
client.int("max_items", default: 10, context: context) # a number flag, whole numbers only
|
|
51
|
+
detail = client.detail("new_checkout", default: false, context: context)
|
|
52
|
+
detail.value # => true
|
|
53
|
+
detail.reason # => :rule
|
|
54
|
+
|
|
55
|
+
client.close
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
A client key (`fn_cli_...`) is refused at construction with an `ArgumentError`. `base_url` is the only required option besides the key.
|
|
59
|
+
|
|
60
|
+
| Option | Meaning |
|
|
61
|
+
|---|---|
|
|
62
|
+
| `stream:` | `false` turns the change stream off; the ruleset is then polled every 30 s (default `true`). |
|
|
63
|
+
| `poll_interval:` | Seconds between polls while the stream is up (default 300, at least 30). |
|
|
64
|
+
| `request_timeout:` | Seconds per request (default 10). |
|
|
65
|
+
| `store:` | Path of a file keeping the last good ruleset across restarts. It is read at construction and rewritten on every accepted ruleset. |
|
|
66
|
+
| `logger:` | A callable receiving `(level, event, fields)`, or a standard `Logger`. Default: warnings and errors to `$stderr`. |
|
|
67
|
+
| `platform:` | The platform in the `User-Agent` (`flagnetic-ruby/<version> (<platform>)`) and the events envelope, and an exposure's `platform` when the context names none (default `"server"`). |
|
|
68
|
+
| `app_version:` | The application's version, sent with every evaluation summary and as an exposure's `app_version` when the context names none. |
|
|
69
|
+
| `flush_interval:` | Seconds between sends of the exposure and alias queue (default 2); 500 queued events are sent at once. |
|
|
70
|
+
| `summary_interval:` | Seconds between evaluation summaries (default 60). |
|
|
71
|
+
| `send_summaries:` | `false` stops counting and sending evaluation summaries. |
|
|
72
|
+
| `send_exposures:` | `false` makes `exposure` and `alias` send nothing (logged as `exposures_disabled`). |
|
|
73
|
+
| `offline:`, `offline_file:` | A ruleset document (JSON text, a `Hash` or a callable returning one) or the path of one. No request is ever made, the stage is `:fresh`, and `reload` re-reads the source. |
|
|
74
|
+
| `allow_overrides:` | Enables local overrides. |
|
|
75
|
+
|
|
76
|
+
`stage` is `:fresh` while the latest attempt succeeded (a `304` included), `:last_good` while the held ruleset is older than the latest attempt (it came from an earlier success or from the store) and `:built_in` before any ruleset was received. `status` adds `stale_since`. `on_change` and `on_stage` register blocks and return a callable that removes them.
|
|
77
|
+
|
|
78
|
+
Every request, the change stream included, goes through `Net::HTTP`, so the proxy configured for the process (`http_proxy`, `https_proxy`, `no_proxy`) applies.
|
|
79
|
+
|
|
80
|
+
After `fork` the child starts its own background thread on its first call; `after_fork` starts it at once, so a worker that has not served a request yet already follows the stream.
|
|
81
|
+
|
|
82
|
+
Local overrides replace a flag's value inside this process and are refused in a protected environment:
|
|
83
|
+
|
|
84
|
+
```ruby
|
|
85
|
+
client = Flagnetic::Client.new(key, base_url: url, allow_overrides: true)
|
|
86
|
+
client.local_overrides.set("new_checkout", true) # raises LocalOverridesNotAllowed without allow_overrides
|
|
87
|
+
client.local_overrides.remove("new_checkout")
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Events
|
|
91
|
+
|
|
92
|
+
The client reports what it served so that experiments can be analysed:
|
|
93
|
+
|
|
94
|
+
```ruby
|
|
95
|
+
context = { units: { user: "u-1001" }, attributes: { platform: "linux", app_version: "1.18.0" } }
|
|
96
|
+
client.string("pricing", default: "standard", context: context)
|
|
97
|
+
client.exposure("pricing", context: context) # the unit saw the behaviour the flag decided
|
|
98
|
+
client.alias("anonymous", "d-8f1c", "user", "u-1001") # after sign-up
|
|
99
|
+
client.flush(timeout: 5)
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
- **Evaluation summaries.** Every evaluation is counted per flag, variation and reason (`fallback` and `local_override` included, never a unit ID or a value) and sent every `summary_interval` as one or more `evaluation_summary` events of at most 1,000 rows, with the version of the ruleset held at the interval's end. A report that fails merges into the next one; one older than an hour is dropped.
|
|
103
|
+
- **Exposures.** Evaluating records nothing on its own. `exposure` sends the experiment block of the latest evaluation of that flag for that context, or nothing when it carried none (`exposure_skipped` at debug says why). A flag with automatic exposure sends one as it is evaluated. A repeat for the same experiment, unit and variation is sent once per `flush_interval`.
|
|
104
|
+
- **Aliases.** `alias` links the unit ID used before sign-up to the one after it; it changes no context.
|
|
105
|
+
- **Delivery.** Events go gzipped to `POST /sdk/v1/events` in batches of at most 500, each with a UUIDv7 event ID kept across retries, so the server drops a resend as a duplicate. An evaluation summary that is still unanswered when its flush ends is merged into the next window and sent under new IDs. A 429, a 5xx or a network error is retried with backoff (evaluation summaries wait for `Retry-After`, capped at five minutes); a 401 or 403 logs `key_revoked` and keeps the events; an event the server rejects is logged as `queue_dropped` and never sent again. The queue holds 10,000 events and drops the oldest beyond that.
|
|
106
|
+
- `flush(timeout:)` sends what is queued and the current window and waits at most `timeout` seconds; `close(timeout:)` flushes first and then stops the background thread, both within the one `timeout`. A forked child starts with an empty queue and window.
|
|
107
|
+
|
|
108
|
+
An offline client sends nothing: it hands every summary, exposure and alias to its `recorder`.
|
|
109
|
+
|
|
110
|
+
Log events have stable names (`Flagnetic::EVENTS`); alert on the name, never on the text:
|
|
111
|
+
|
|
112
|
+
| Event | Level | Fields |
|
|
113
|
+
|---|---|---|
|
|
114
|
+
| `ruleset_loaded` | info | `source` (`fetch`, `store`, `offline`), `ruleset_version`, `flags` |
|
|
115
|
+
| `fetch_failed` | warn (error offline) | `cause`, `stage` (`source` offline) |
|
|
116
|
+
| `sse_connected` | info | |
|
|
117
|
+
| `sse_dropped` | info after a server close, warn otherwise | `cause` |
|
|
118
|
+
| `stage_changed` | info to `fresh`, warn otherwise | `from`, `to` |
|
|
119
|
+
| `key_revoked` | error, once per source until it recovers | `status`, `source` (`fetch`, `stream`) |
|
|
120
|
+
| `schema_unsupported` | error, once | `schema_version`, `supported` |
|
|
121
|
+
| `rule_unsupported` | error, once per flag and ruleset version | `flag`, `constructs`, `ruleset_version` |
|
|
122
|
+
| `override_set` | warn | `flag` |
|
|
123
|
+
| `override_refused` | warn | `flag`, `reason` (`not_allowed`, `protected`), `environment` |
|
|
124
|
+
| `local_overrides_allowed` | warn | |
|
|
125
|
+
| `type_mismatch` | warn, once per flag | `flag`, `flag_type`, `requested_type` |
|
|
126
|
+
| `evaluation_failed` | error | `flag`, `cause` |
|
|
127
|
+
| `listener_failed` | error | `cause` |
|
|
128
|
+
| `store_failed` | error | `path`, `cause` |
|
|
129
|
+
| `worker_failed` | error | `cause` |
|
|
130
|
+
| `exposure_skipped` | debug | `flag`, `cause` (`no_experiment`, `no_detail`, `repeat`) |
|
|
131
|
+
| `exposures_disabled` | warn | |
|
|
132
|
+
| `ingest_failed` | warn | `kind`, `events`, `retry_in`, `status`, `error` |
|
|
133
|
+
| `client_not_configured` | warn | `rails_env`: the Rails integration found no SDK key and base URL and serves built-in defaults |
|
|
134
|
+
| `queue_dropped` | warn | `kind` (`exposure_or_alias`, `evaluation_summary`), `cause` (`queue_full`, `client_error`, `refused_by_the_platform`, `window_older_than_one_hour`) and what was dropped |
|
|
135
|
+
|
|
136
|
+
A ruleset from a newer server may hold constructs this SDK does not know. The client logs `rule_unsupported` once per flag and ruleset version, and the flag serves its default with the error code `inconsistent_ruleset`.
|
|
137
|
+
|
|
138
|
+
## Flagnetic.client
|
|
139
|
+
|
|
140
|
+
`Flagnetic.client` is the process-wide client: assign one with `Flagnetic.client = ...`, or set `Flagnetic.client_builder` to a callable that builds it on first use, once. It raises `Flagnetic::NotConfigured` until either is set.
|
|
141
|
+
|
|
142
|
+
## Rails
|
|
143
|
+
|
|
144
|
+
Under Rails the gem loads a railtie (nothing of it loads without Rails) that sets `Flagnetic.client` up without an initializer. The SDK key and base URL come from the first of `config.flagnetic.sdk_key` and `config.flagnetic.base_url`, the `FLAGNETIC_SDK_KEY` and `FLAGNETIC_BASE_URL` environment variables, and credentials (`flagnetic.sdk_key`, `flagnetic.base_url`). Without both, the client serves built-in defaults and logs `client_not_configured`.
|
|
145
|
+
|
|
146
|
+
A server or job process builds the client at boot and waits up to `ready_timeout` seconds for the ruleset; `rails console`, `rails runner` and rake build it on first use, so a task that never asks for a flag makes no request. Every forked child (Puma cluster mode, Unicorn) starts its own background thread at once, and every process closes its client at exit, which flushes what it queued. Local overrides are allowed in every environment but production, where no setting enables them.
|
|
147
|
+
|
|
148
|
+
| `config.flagnetic.` | |
|
|
149
|
+
|---|---|
|
|
150
|
+
| `sdk_key`, `base_url` | See above. |
|
|
151
|
+
| `offline_file` | A ruleset document to serve instead; no request is made. |
|
|
152
|
+
| `start` | `nil` (default): at boot in a server or job process, on first use in console, runner and rake. `true`: at boot everywhere. `false`: on first use. |
|
|
153
|
+
| `ready_timeout`, `close_timeout` | Seconds the boot waits for the ruleset and the exit flush may take (default 5 each). |
|
|
154
|
+
| `allow_overrides` | `false` disables local overrides outside production too. |
|
|
155
|
+
| `logger` | The client's `logger:` (default `Rails.logger`). |
|
|
156
|
+
| `client_options` | Any other `Flagnetic::Client.new` option, as a `Hash`. |
|
|
157
|
+
| `platform`, `app_version` | Attributes of every request context, and the client's `platform:` and `app_version:`. |
|
|
158
|
+
| `user_unit_type`, `user_id` | The signed-in user's unit type, and a callable receiving the controller that returns the ID (default `current_user&.id`, else `session[:user_id]`). |
|
|
159
|
+
| `anonymous_unit_type`, `anonymous_cookie` | The unit type of a random ID kept in a cookie (default name `flagnetic_anonymous_id`). |
|
|
160
|
+
| `attributes` | A callable receiving the controller and returning more attributes. |
|
|
161
|
+
|
|
162
|
+
Controllers and views get `flagnetic_context`, the current request's context, and `flagnetic_anonymous_id`, the visitor's anonymous ID (set in an HTTP-only cookie on the first request). No unit type is set by default. Signing in keeps the anonymous ID beside the user's, so an experiment spanning sign-up keeps its variation; call `Flagnetic.client.alias` at sign-up to link the two.
|
|
163
|
+
|
|
164
|
+
```ruby
|
|
165
|
+
config.flagnetic.user_unit_type = "user"
|
|
166
|
+
config.flagnetic.anonymous_unit_type = "anonymous"
|
|
167
|
+
|
|
168
|
+
# in a controller or view
|
|
169
|
+
Flagnetic.client.bool("new_checkout", default: false, context: flagnetic_context)
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## Testing
|
|
173
|
+
|
|
174
|
+
`Flagnetic::Testing` builds offline clients for tests: they make no request and keep what they would have sent in a recorder.
|
|
175
|
+
|
|
176
|
+
```ruby
|
|
177
|
+
require "flagnetic/testing"
|
|
178
|
+
|
|
179
|
+
client = Flagnetic::Testing.offline(values: { "new_checkout" => true, "banner" => "spring" })
|
|
180
|
+
client = Flagnetic::Testing.offline(File.read("ruleset.json")) # or a Hash, or file: "ruleset.json"
|
|
181
|
+
|
|
182
|
+
client.set("banner", "autumn") # served to everyone as fallthrough; a new key is added
|
|
183
|
+
client.reload # back to the source
|
|
184
|
+
|
|
185
|
+
client.recorder.summary # running counts per flag, variation and reason
|
|
186
|
+
client.recorder.evaluation_summaries # what flush would have sent
|
|
187
|
+
client.recorder.exposures # what exposure and automatic exposure recorded
|
|
188
|
+
client.recorder.aliases
|
|
189
|
+
client.recorder.reset
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
`with_flags` and `with_ruleset` make such a client `Flagnetic.client` for a block and restore the previous one afterwards, even when the block raises. They are process-wide, so tests running in parallel threads must not use them. Include the helpers in RSpec or Minitest:
|
|
193
|
+
|
|
194
|
+
```ruby
|
|
195
|
+
RSpec.configure { |config| config.include Flagnetic::Testing::Helpers }
|
|
196
|
+
|
|
197
|
+
class ActiveSupport::TestCase
|
|
198
|
+
include Flagnetic::Testing::Helpers
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
with_flags(new_checkout: true) do |client|
|
|
202
|
+
get "/checkout"
|
|
203
|
+
assert client.recorder.summary.any? { |row| row.flag == "new_checkout" }
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
with_ruleset(file: "test/fixtures/flags.json") do |client|
|
|
207
|
+
post "/checkout"
|
|
208
|
+
assert_equal ["checkout_v2"], client.recorder.exposures.map(&:experiment_key)
|
|
209
|
+
end
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
## OpenFeature
|
|
213
|
+
|
|
214
|
+
[`flagnetic-openfeature`](../ruby-openfeature/README.md) is an OpenFeature provider over this gem, in a gem of its own so that `flagnetic` stays free of the dependency. It needs Ruby 3.4 or later and `openfeature-sdk` 0.6.1 or a later 0.x release; exposures and aliases go through `track`.
|
|
215
|
+
|
|
216
|
+
```bash
|
|
217
|
+
bundle add flagnetic-openfeature
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
```ruby
|
|
221
|
+
require "flagnetic/openfeature"
|
|
222
|
+
|
|
223
|
+
provider = Flagnetic::OpenFeature::Provider.new(ENV.fetch("FLAGNETIC_SDK_KEY"), base_url: "https://flags.example.com")
|
|
224
|
+
OpenFeature::SDK.configure { |config| config.set_provider_and_wait(provider) }
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
## Evaluator
|
|
228
|
+
|
|
229
|
+
`Flagnetic::Eval` evaluates any ruleset document exactly as the reference evaluator in [`eval/`](../../eval) does, which every fixture of the [conformance suite](../../conformance) proves with no case skipped.
|
|
230
|
+
|
|
231
|
+
```ruby
|
|
232
|
+
ruleset = Flagnetic::Eval::Ruleset.decode(File.read("ruleset.json"))
|
|
233
|
+
|
|
234
|
+
context = { units: { user: "u-1001" }, attributes: { plan: "pro", app_version: "1.42.0" } }
|
|
235
|
+
result = ruleset.evaluate("new_checkout", context)
|
|
236
|
+
result.value # => true
|
|
237
|
+
result.variation # => "on"
|
|
238
|
+
result.reason # => :rule
|
|
239
|
+
result.rule_id # => "r1"
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
`Ruleset.decode` raises `Flagnetic::Eval::DecodeError` only for a document the evaluator cannot read: malformed JSON, a field of the wrong JSON type, an unsupported `schema_version` or a scope other than `server`. `evaluate` never raises: a problem is a result with reason `error` and an `error_code` (`flag_not_found`, `prerequisite_cycle`, `inconsistent_ruleset`).
|
|
243
|
+
|
|
244
|
+
Evaluation records nothing. An exposure is an explicit call, never a side effect of evaluating.
|
|
245
|
+
|
|
246
|
+
## Unsupported rules
|
|
247
|
+
|
|
248
|
+
A server newer than the SDK may send an operator, a condition kind or a serve kind this evaluator does not know. Decoding reports them per flag, and evaluating a flag that reaches one answers `error` with `inconsistent_ruleset`, so the flag serves the fallback:
|
|
249
|
+
|
|
250
|
+
```ruby
|
|
251
|
+
ruleset.unsupported_rules
|
|
252
|
+
# => { "new_checkout" => [#<struct Flagnetic::Eval::Construct kind=:operator, name="sounds_like">] }
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
Every other flag evaluates normally. The fix is to upgrade the SDK.
|
|
256
|
+
|
|
257
|
+
## Regular expressions
|
|
258
|
+
|
|
259
|
+
A `matches` condition is RE2 syntax with RE2 semantics, as Go's `regexp` reads it, and not the dialect of Ruby's own engine. A pattern is parsed as RE2 and translated, so `^` and `$` are not line anchors, `\s` is `[\t\n\f\r ]`, lookarounds and backreferences are refused and `(?i)` folds case by Go's rules, `\p{...}` included. A pattern RE2 refuses makes the condition `inconsistent_ruleset`. A match that takes longer than 0.1 s makes the condition false.
|
|
260
|
+
|
|
261
|
+
The case folding orbits and property names come from Go's `unicode` package, and `lib/flagnetic/eval/unicode_tables.rb` is generated from it:
|
|
262
|
+
|
|
263
|
+
```bash
|
|
264
|
+
go run ./sdks/ruby/script/_unicode_tables > sdks/ruby/lib/flagnetic/eval/unicode_tables.rb
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
## Development
|
|
268
|
+
|
|
269
|
+
```bash
|
|
270
|
+
bundle install
|
|
271
|
+
bundle exec rake test # unit tests, every conformance fixture, the protocol scenarios, the RSpec helpers and the p99 test
|
|
272
|
+
bundle exec rubocop
|
|
273
|
+
(cd rails && bundle install && bundle exec rake test) # the Rails integration; RAILS_VERSION=8.0 for the older major
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
`bundle exec rake e2e` runs the smoke against two real replicas brought up by `flagnetic-e2e up`; [docs/sdks/ruby.md](../../docs/sdks/ruby.md#how-it-is-tested) shows how.
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Flagnetic
|
|
4
|
+
# Backoff computes retry delays: exponential from base, capped, with full
|
|
5
|
+
# jitter, so a platform restart does not bring every SDK back at once.
|
|
6
|
+
# The n-th delay is uniform between zero and min(cap, base * 2**n).
|
|
7
|
+
class Backoff
|
|
8
|
+
def initialize(base:, cap:, random: Random.new)
|
|
9
|
+
@base = base
|
|
10
|
+
@cap = cap
|
|
11
|
+
@random = random
|
|
12
|
+
@attempts = 0
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# next_delay returns the next delay in seconds. cap overrides the
|
|
16
|
+
# configured cap, for the unauthorized cap of a revoked key.
|
|
17
|
+
def next_delay(cap: @cap)
|
|
18
|
+
ceiling = @attempts > 62 ? cap : [cap, @base * (2**@attempts)].min
|
|
19
|
+
@attempts += 1
|
|
20
|
+
@random.rand * ceiling
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# reset starts over from the base, after a success.
|
|
24
|
+
def reset
|
|
25
|
+
@attempts = 0
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
private_constant :Backoff
|
|
29
|
+
end
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Flagnetic
|
|
4
|
+
# Changes finds the flags whose evaluation may differ between two
|
|
5
|
+
# rulesets: flags added, removed or reconfigured, and flags that reach,
|
|
6
|
+
# through their conditions, the conditions of the segments those name,
|
|
7
|
+
# their overrides, rollouts, experiments and prerequisites, a segment, a
|
|
8
|
+
# declared attribute or unit type, or a prerequisite flag that changed,
|
|
9
|
+
# transitively. A flag's own definition names its segments and
|
|
10
|
+
# prerequisites but not their contents, so comparing definitions alone
|
|
11
|
+
# misses a segment edit that flips a value.
|
|
12
|
+
module Changes
|
|
13
|
+
module_function
|
|
14
|
+
|
|
15
|
+
# flags returns the sorted keys; before nil means every flag of after.
|
|
16
|
+
def flags(before, after)
|
|
17
|
+
return after.flags.map(&:key).sort if before.nil?
|
|
18
|
+
|
|
19
|
+
segments = changed(before.segments, after.segments, &:key)
|
|
20
|
+
names = changed(before.attributes, after.attributes, &:key)
|
|
21
|
+
.merge(changed(before.unit_types, after.unit_types, &:itself))
|
|
22
|
+
affected = changed(before.flags, after.flags, &:key)
|
|
23
|
+
spread(affected, reaches(before, after), segments, names)
|
|
24
|
+
affected.keys.sort
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# spread adds to affected every flag reaching a changed segment or name,
|
|
28
|
+
# or an affected prerequisite, until nothing more is.
|
|
29
|
+
def spread(affected, reaches, segments, names)
|
|
30
|
+
loop do
|
|
31
|
+
grew = reaches.select do |key, r|
|
|
32
|
+
!affected[key] && (r[:segments].any? { |s| segments[s] } || r[:names].any? { |n| names[n] } ||
|
|
33
|
+
r[:flags].any? { |f| affected[f] })
|
|
34
|
+
end
|
|
35
|
+
return if grew.empty?
|
|
36
|
+
|
|
37
|
+
grew.each_key { |k| affected[k] = true }
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# changed returns the keys of items added, removed or changed, as a
|
|
42
|
+
# Hash used as a set. Decoded items are frozen structs compared by
|
|
43
|
+
# value.
|
|
44
|
+
def changed(before, after, &key)
|
|
45
|
+
a = before.to_h { |i| [key.call(i), i] }
|
|
46
|
+
b = after.to_h { |i| [key.call(i), i] }
|
|
47
|
+
out = {}
|
|
48
|
+
a.each { |k, v| out[k] = true unless b.key?(k) && b[k] == v }
|
|
49
|
+
b.each_key { |k| out[k] = true unless a.key?(k) }
|
|
50
|
+
out
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# reaches maps every flag of either ruleset to what its evaluation
|
|
54
|
+
# reads: segments (transitively through segment conditions), attribute
|
|
55
|
+
# and unit type names, and prerequisite flags.
|
|
56
|
+
def reaches(*rulesets)
|
|
57
|
+
out = Hash.new { |h, k| h[k] = { segments: {}, names: {}, flags: {} } }
|
|
58
|
+
rulesets.each do |rs|
|
|
59
|
+
by_segment = rs.segments.to_h { |s| [s.key, s] }
|
|
60
|
+
rs.flags.each do |f|
|
|
61
|
+
r = out[f.key]
|
|
62
|
+
f.prerequisites.each { |p| r[:flags][p.flag] = true }
|
|
63
|
+
f.overrides.each { |o| r[:names][o.unit_type] = true }
|
|
64
|
+
f.rules.each do |rule|
|
|
65
|
+
walk(rule.conditions, r, by_segment)
|
|
66
|
+
r[:names][rule.serve.unit_type] = true
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
out.transform_values { |r| r.transform_values(&:keys) }
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def walk(conditions, reach, by_segment)
|
|
74
|
+
conditions.each do |c|
|
|
75
|
+
case c.kind
|
|
76
|
+
when "in_segment", "not_in_segment"
|
|
77
|
+
next if reach[:segments][c.segment]
|
|
78
|
+
|
|
79
|
+
reach[:segments][c.segment] = true
|
|
80
|
+
walk(by_segment[c.segment]&.conditions || [], reach, by_segment)
|
|
81
|
+
when "prerequisite" then reach[:flags][c.flag] = true
|
|
82
|
+
else reach[:names][c.attribute] = true # an attribute, or a unit type read as one
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
private_constant :Changes
|
|
88
|
+
end
|