flagnetic 0.1.1 → 0.1.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1550286a0e18788c593c2adc2268f4fa8ecb823b7dd64654bec4ff01a4fc1155
4
- data.tar.gz: f5a48dc03a8524cb2aee5a3739e87392dc8ebb41a182eaf96a9383b3b4761125
3
+ metadata.gz: 366e3febc729c08e8fc6c15f02ff5d68cc9c8820c436576955affc70f295515e
4
+ data.tar.gz: 449827190dfedce8c6c2976a2fd601ccb7866878c7ac88d1c65089fab8b41ed2
5
5
  SHA512:
6
- metadata.gz: 322119cf31f48bc9eaf88b4770830e8efc7dfdc09ab7ab77e6087f18f6504533725abb7cde9188bcfa25db6ec4af11f12c416c6b3a878cb364a8f4bbf3bee07a
7
- data.tar.gz: 27358c4b7f92b3fe6e6cbf80835e1742cceb6def862dbf9749799172ac68e3ba28cd48ad5f319b1c869956735f8d58f1ec5f6d038177497e139cf1a7bdabb656
6
+ metadata.gz: 177ac8930d64bd42d82d9df68cc191eb3f7ad719ddafcd325d5bf63774a694208d35faa247f35b871da96c9544183ba5a1e3ba62c4b0ca223111439f5fa2e197
7
+ data.tar.gz: 1cab276e7375fe1139e45f82c12ae3c3311dd676a2be7fafff5b969d3972c06af2116cd094c77da60575c57c621d103dbbb4e86c2dd037fbe309d1121e302a03
data/README.md CHANGED
@@ -37,7 +37,7 @@ A change saved in Flagnetic, a kill switch included, reaches a client holding th
37
37
 
38
38
  ## Client
39
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.
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. A stream is stable once it has delivered a heartbeat: one the server closes before that (a draining replica, a proxy that ends the stream early) is a failure like any other and the reconnect backs off, while the close of a stable stream waits 0 to 2 seconds.
41
41
 
42
42
  ```ruby
43
43
  require "flagnetic"
@@ -62,7 +62,7 @@ A client key (`fn_cli_...`) is refused at construction with an `ArgumentError`.
62
62
  | `stream:` | `false` turns the change stream off; the ruleset is then polled every 30 s (default `true`). |
63
63
  | `poll_interval:` | Seconds between polls while the stream is up (default 300, at least 30). |
64
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. |
65
+ | `store:` | Path of a file keeping the last good ruleset across restarts. It is read at construction and rewritten on every accepted ruleset. The file is headed by a digest of the SDK key: one written under another key (another environment's, say) or under none is discarded with `store_failed`. |
66
66
  | `logger:` | A callable receiving `(level, event, fields)`, or a standard `Logger`. Default: warnings and errors to `$stderr`. |
67
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
68
  | `app_version:` | The application's version, sent with every evaluation summary and as an exposure's `app_version` when the context names none. |
@@ -102,7 +102,7 @@ client.flush(timeout: 5)
102
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
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
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.
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. A string that is not valid UTF-8 (a unit ID taken from a cookie, say) is sent with each invalid byte replaced by U+FFFD; an event that still cannot be encoded is dropped alone, logged as `queue_dropped` with cause `not_serialisable` and the class of the error as `fault`, and costs no other event.
106
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
107
 
108
108
  An offline client sends nothing: it hands every summary, exposure and alias to its `recorder`.
@@ -114,11 +114,12 @@ Log events have stable names (`Flagnetic::EVENTS`); alert on the name, never on
114
114
  | `ruleset_loaded` | info | `source` (`fetch`, `store`, `offline`), `ruleset_version`, `flags` |
115
115
  | `fetch_failed` | warn (error offline) | `cause`, `stage` (`source` offline) |
116
116
  | `sse_connected` | info | |
117
- | `sse_dropped` | info after a server close, warn otherwise | `cause` |
117
+ | `sse_dropped` | info after a server close of a stable stream, warn otherwise | `cause` |
118
118
  | `stage_changed` | info to `fresh`, warn otherwise | `from`, `to` |
119
119
  | `key_revoked` | error, once per source until it recovers | `status`, `source` (`fetch`, `stream`) |
120
120
  | `schema_unsupported` | error, once | `schema_version`, `supported` |
121
121
  | `rule_unsupported` | error, once per flag and ruleset version | `flag`, `constructs`, `ruleset_version` |
122
+ | `condition_invalid` | error, once per flag and ruleset version | `flag`, `conditions`, `ruleset_version` |
122
123
  | `override_set` | warn | `flag` |
123
124
  | `override_refused` | warn | `flag`, `reason` (`not_allowed`, `protected`), `environment` |
124
125
  | `local_overrides_allowed` | warn | |
@@ -130,18 +131,20 @@ Log events have stable names (`Flagnetic::EVENTS`); alert on the name, never on
130
131
  | `exposure_skipped` | debug | `flag`, `cause` (`no_experiment`, `no_detail`, `repeat`) |
131
132
  | `exposures_disabled` | warn | |
132
133
  | `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 |
134
+ | `client_not_configured` | warn | `rails_env`: the Rails integration found no SDK key and base URL outside production and serves built-in defaults |
135
+ | `queue_dropped` | warn | `kind` (`exposure_or_alias`, `evaluation_summary`), `cause` (`queue_full`, `not_serialisable`, `client_error`, `refused_by_the_platform`, `window_older_than_one_hour`) and what was dropped (`fault` for `not_serialisable`) |
135
136
 
136
137
  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
 
139
+ A condition this SDK cannot evaluate although the server accepted it, such as a pattern its regular expression engine refuses, makes the flag serve its default with `inconsistent_ruleset` in this SDK only. The client logs `condition_invalid` once per flag and ruleset version when the ruleset loads, with the reason of each condition in `conditions`.
140
+
138
141
  ## Flagnetic.client
139
142
 
140
143
  `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
144
 
142
145
  ## Rails
143
146
 
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`.
147
+ 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 and without `offline_file`, building the client in production raises `Flagnetic::NotConfigured`, naming what is missing and where to set it, so a deployment that lost its key fails at boot (in console, runner and rake, on first use) instead of serving built-in defaults to every user; in any other environment the client serves built-in defaults and logs `client_not_configured`.
145
148
 
146
149
  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
150
 
@@ -189,6 +192,8 @@ client.recorder.aliases
189
192
  client.recorder.reset
190
193
  ```
191
194
 
195
+ Each of the recorder's evaluation summaries, exposures and aliases keeps the latest 10,000 entries and drops the oldest beyond that, so an offline client left running in a long process stays bounded.
196
+
192
197
  `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
198
 
194
199
  ```ruby
@@ -256,7 +261,7 @@ Every other flag evaluates normally. The fix is to upgrade the SDK.
256
261
 
257
262
  ## Regular expressions
258
263
 
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.
264
+ 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 matched by an engine of its own that runs in time linear in the input, so a context value can never make a match slow or cut it off. `^` 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 string longer than 64 KiB (in bytes) is not matched at all: the condition is false for it, without an error, because the cost of a match grows with the input.
260
265
 
261
266
  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
267
 
@@ -26,14 +26,15 @@ module Flagnetic
26
26
  include FallbackChain
27
27
 
28
28
  # Timing holds the delays and intervals of the background thread, in
29
- # seconds. Only poll_interval and request_timeout are public options;
30
- # the timing option exists for tests and is not part of the API.
29
+ # seconds, and the random source of the stream's waits (nil: Random.new).
30
+ # Only poll_interval and request_timeout are public options; the timing
31
+ # option exists for tests and is not part of the API.
31
32
  Timing = Struct.new(:request_timeout, :fetch_base, :fetch_cap, :unauthorized_cap, :stream_base, :stream_cap,
32
- :stream_stable, :close_jitter, :silence, :poll_connected, :poll_down, :event_retries,
33
- :exposure_flush, :summary_flush, keyword_init: true)
33
+ :worker_stable, :close_jitter, :silence, :poll_connected, :poll_down, :event_retries,
34
+ :exposure_flush, :summary_flush, :random, keyword_init: true)
34
35
  DEFAULT_TIMING = Timing.new(
35
36
  request_timeout: 10, fetch_base: 1, fetch_cap: 300, unauthorized_cap: 900, stream_base: 1, stream_cap: 30,
36
- stream_stable: 60, close_jitter: 2, silence: 45, poll_connected: 300, poll_down: 30, event_retries: [1, 2, 4],
37
+ worker_stable: 60, close_jitter: 2, silence: 45, poll_connected: 300, poll_down: 30, event_retries: [1, 2, 4],
37
38
  exposure_flush: 2, summary_flush: 60
38
39
  ).freeze
39
40
  MIN_POLL_INTERVAL = 30
@@ -51,6 +52,8 @@ module Flagnetic
51
52
  # call is ever made; the stage is fresh; reload re-reads the source.
52
53
  # - store: a file path holding the last good ruleset, loaded as
53
54
  # last_good at construction and rewritten on every accepted ruleset.
55
+ # The file belongs to the SDK key that wrote it: one written under
56
+ # another key, or under none, is discarded with store_failed.
54
57
  # - logger: a callable receiving (level, event, fields), or a Logger.
55
58
  # Default: warn and above to $stderr.
56
59
  # - stream: false disables the change stream; polling then runs every
@@ -299,6 +302,7 @@ module Flagnetic
299
302
  @overrides = {}.freeze
300
303
  @once = {}
301
304
  @unsupported_logged = {}
305
+ @invalid_logged = {}
302
306
  @pid = Process.pid
303
307
  @closed = false
304
308
  end
@@ -335,7 +339,7 @@ module Flagnetic
335
339
  @transport = Transport.new(sdk_key: sdk_key, base_url: base_url, platform: platform,
336
340
  timeout: @timing.request_timeout)
337
341
  build_reporters(@transport, nil)
338
- @store = store && FileStore.new(store)
342
+ @store = store && FileStore.new(store, sdk_key)
339
343
  load_store if @store
340
344
  @worker = Worker.new(self, @transport, @timing, stream: stream)
341
345
  end
@@ -23,7 +23,7 @@ module Flagnetic
23
23
  # constructs it holds.
24
24
  CompiledSegment = Struct.new(:conditions, :unsupported)
25
25
 
26
- attr_reader :flags, :segments, :unsupported
26
+ attr_reader :flags, :segments, :unsupported, :invalid
27
27
 
28
28
  def initialize(ruleset)
29
29
  # A unit type named as an attribute is a string attribute holding
@@ -34,15 +34,25 @@ module Flagnetic
34
34
  @segments = ruleset.segments.to_h { |s| [s.key, compiler.segment(s)] }.freeze
35
35
  @flags = {}
36
36
  @unsupported = {}
37
- ruleset.flags.each do |f|
38
- @flags[f.key] = compiler.flag(f)
39
- found = compiler.unsupported_in(f, @flags[f.key], @segments)
40
- @unsupported[f.key] = found.freeze unless found.empty?
41
- end
37
+ @invalid = {}
38
+ ruleset.flags.each { |f| index_flag(compiler, f) }
42
39
  @flags.freeze
43
40
  @unsupported.freeze
41
+ @invalid.freeze
44
42
  freeze
45
43
  end
44
+
45
+ private
46
+
47
+ # index_flag compiles a flag and records what it holds that this
48
+ # evaluator does not know or cannot evaluate.
49
+ def index_flag(compiler, flag)
50
+ @flags[flag.key] = compiler.flag(flag)
51
+ found = compiler.unsupported_in(flag, @flags[flag.key], @segments)
52
+ @unsupported[flag.key] = found.freeze unless found.empty?
53
+ broken = compiler.invalid_in(flag, @flags[flag.key], @segments)
54
+ @invalid[flag.key] = broken.freeze unless broken.empty?
55
+ end
46
56
  end
47
57
 
48
58
  # The condition kinds and serve kinds this evaluator knows.
@@ -90,8 +100,29 @@ module Flagnetic
90
100
  found
91
101
  end
92
102
 
103
+ # invalid_in lists why the conditions of a compiled flag, and of the
104
+ # segments its rules name, cannot be evaluated by this evaluator, in
105
+ # document order, each once, apart from those that hold a construct it
106
+ # does not know (unsupported_in reports those). A server accepts only
107
+ # patterns and values its own evaluator reads, so one this evaluator
108
+ # refuses is a difference between the two.
109
+ def invalid_in(flag, compiled, segments)
110
+ found = []
111
+ flag.rules.each_index do |i|
112
+ compiled.rules[i].each do |c|
113
+ add_invalid(found, c)
114
+ segments[c.segment]&.conditions&.each { |sc| add_invalid(found, sc) } unless c.segment.to_s.empty?
115
+ end
116
+ end
117
+ found
118
+ end
119
+
93
120
  private
94
121
 
122
+ def add_invalid(found, compiled)
123
+ found << compiled.error if compiled.error && !compiled.unsupported && !found.include?(compiled.error)
124
+ end
125
+
95
126
  # valid_experiment? reports whether an experiment serve holds what
96
127
  # assigning a unit to a variation relies on: an experiment key, a unit
97
128
  # type and both salts; a traffic allocation within 0 to 10000; and at