flagnetic 0.1.0 → 0.1.2

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: 7777e134a978460329e49acf4e982e291f02a01cc1eac12b312ff142a149dad2
4
- data.tar.gz: c40536af379f16b170e3eedb0a899dab8726e7966df71c384bab6210bad213f0
3
+ metadata.gz: 66ef6f1803ef21cf2f595614c9069ef974f960baa799b915a3b5970663b452c6
4
+ data.tar.gz: 11a3bdeec65a65a0358de993947a99faa7fd919df52c0f416324877390e2d5f9
5
5
  SHA512:
6
- metadata.gz: 07ec2f99b51334afb56925b35a4c83445073dddeb7ed288ab2e1d99f322e870af18b4539f1690ef90adc82cab0badfab97e63c823a968529603b639b6698cf1a
7
- data.tar.gz: 172615dba045bc0d9392a61af845dfb8d2baa571226bc8b6f949289a1fd4695c246405d801bff54440bb247b0496961f32e3ae68bf515ffc75acf2e9f8045201
6
+ metadata.gz: 3498e26e7391884ee01637ce99ecbb570b66555bafb8bb08d13916801a825af8798f8683678990ce8e6e0ed570f30ab1b81f92788c90fc929954e75051dca135
7
+ data.tar.gz: 3835e3a2ada5e2588ab4b9b11bcfd7fd552cae18cdf036b8b06ebe572e5dee3ebcd0e4a15e2a4f7f383c17aec185d69e1a53e29fe1f3014b780345cfbf792dff
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 | |
@@ -131,10 +132,12 @@ Log events have stable names (`Flagnetic::EVENTS`); alert on the name, never on
131
132
  | `exposures_disabled` | warn | |
132
133
  | `ingest_failed` | warn | `kind`, `events`, `retry_in`, `status`, `error` |
133
134
  | `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
+ | `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.
@@ -256,7 +259,7 @@ Every other flag evaluates normally. The fix is to upgrade the SDK.
256
259
 
257
260
  ## Regular expressions
258
261
 
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.
262
+ 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`.
260
263
 
261
264
  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
265
 
@@ -45,11 +45,21 @@ module Flagnetic
45
45
  a = before.to_h { |i| [key.call(i), i] }
46
46
  b = after.to_h { |i| [key.call(i), i] }
47
47
  out = {}
48
- a.each { |k, v| out[k] = true unless b.key?(k) && b[k] == v }
48
+ a.each { |k, v| out[k] = true unless b.key?(k) && same?(b[k], v) }
49
49
  b.each_key { |k| out[k] = true unless a.key?(k) }
50
50
  out
51
51
  end
52
52
 
53
+ # same? compares two decoded items by value. A variation value nested
54
+ # deeper than the stack allows makes the comparison raise
55
+ # SystemStackError; such an item is reported as changed, which only
56
+ # costs a listener call for that one flag and never fails the ruleset.
57
+ def same?(left, right)
58
+ left == right
59
+ rescue SystemStackError
60
+ false
61
+ end
62
+
53
63
  # reaches maps every flag of either ruleset to what its evaluation
54
64
  # reads: segments (transitively through segment conditions), attribute
55
65
  # and unit type names, and prerequisite flags.
@@ -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