flagnetic-openfeature 0.1.1 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ef577665b1a799075e4bbd51390754a941e64ed0472f68895a88d194666f3614
4
- data.tar.gz: 196e8a634b296d2c78ed0c3130bf3fa1831545d4bedc76d7799c0bb9d10f98bd
3
+ metadata.gz: 13007baf29ef16f09900528864838f9d1aa4d261ce9163b34d1ad947ec8f5343
4
+ data.tar.gz: 0211c01ceb4943de1db6256ea01cf7dcca64c811430184fd90cf088a7dc7c6f1
5
5
  SHA512:
6
- metadata.gz: 4c2c24869173e1fc7de2ccabec325134ae959e9194bdbe3dc43d0f70100fc6beb5c635a20fa418c32708bfe30bbed45825d1430394300a5b9e56e7d342f78c52
7
- data.tar.gz: b4e9474c6a879400a69922a72073cdc727cf577ff21b0ec4ead5db711593cc70fa4c7523ece3e867f2b3b19e6e5aba49ca38fc7189ce41f4a88bcd3b8990067b
6
+ metadata.gz: 520aaa9b8b3a0969a08c0f92a9811615247822d0701ffcceb15c06d7474ce8a4690c0723ff283f0b5fc119fed5f3c18fcef8923bc93f62ad199ae988ef5579e7
7
+ data.tar.gz: 8bd2fead17a10d6bf8c1048854ee20fafe25943a6cad6cceef02e0c19b868340832224d060f1c55f254fe22a3707827a1a5fb1c7acc853cf1d412f35c9b7b963
data/README.md CHANGED
@@ -35,6 +35,10 @@ client.fetch_boolean_value(flag_key: "new_checkout", default_value: false, evalu
35
35
  - `provider.client` returns the wrapped `Flagnetic::Client`: local overrides, the recorder of an offline client (`offline:` for tests), flush and status.
36
36
  - A `logger:` keeps receiving every log event of the client.
37
37
 
38
+ ## What 1.0 promises
39
+
40
+ From 1.0.0 the public API changes only additively until 2.0: `Flagnetic::OpenFeature::Provider` (`new` with `unit_type:`, `ready_timeout:`, `logger:` and every `Flagnetic::Client.new` option; `client`, `metadata`, `init`, `shutdown`, the `fetch_*_value` resolvers and `track`), `Provider::NAME`, `Flagnetic::OpenFeature::REASON_METADATA_KEY` and `Flagnetic::OpenFeature.reason_of`, the events and reason mapping documented here, and the flag metadata keys. The defaults (`unit_type` `user`, `ready_timeout` 10 seconds) are documented, not constants. Everything else is internal. The provider makes no request of its own: the wire promise is the `flagnetic` gem's.
41
+
38
42
  ## Events
39
43
 
40
44
  | When | Event |
@@ -72,7 +76,7 @@ client.track("alias", tracking_event_details: OpenFeature::SDK::TrackingEventDet
72
76
  ))
73
77
  ```
74
78
 
75
- `exposure` records the exposure of the most recent evaluation of that flag for that context, as the client's `exposure` does, and `alias` links two unit IDs, as `alias` does. Any other event name, or one missing an attribute, does nothing. An evaluation alone never records an exposure, unless the flag asks for automatic exposure.
79
+ `exposure` records the exposure of the provider's most recent evaluation of that flag for the same units, with the context the evaluation had when a `before` hook added attributes to it, as the client's `exposure` does, and `alias` links two unit IDs, as `alias` does. Any other event name, or one missing an attribute, does nothing. An evaluation alone never records an exposure, unless the flag asks for automatic exposure.
76
80
 
77
81
  ## Development
78
82
 
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Flagnetic
4
+ module OpenFeature
5
+ # LastContexts remembers the context the provider last evaluated an
6
+ # experiment flag with, per flag and units. OpenFeature runs no hooks for
7
+ # track, so the context track receives lacks what a before hook added to
8
+ # the evaluation, and the client would find no served detail under it:
9
+ # the remembered context is the one the detail is filed under.
10
+ class LastContexts
11
+ # CAPACITY bounds the evaluations remembered; the oldest goes out first.
12
+ CAPACITY = 10_000
13
+
14
+ def initialize(capacity: CAPACITY)
15
+ @capacity = capacity
16
+ @mutex = Mutex.new
17
+ @by_key = {}
18
+ end
19
+
20
+ def remember(flag, context)
21
+ key = key_of(flag, context)
22
+ @mutex.synchronize do
23
+ @by_key.delete(key)
24
+ @by_key[key] = context
25
+ @by_key.shift while @by_key.size > @capacity
26
+ end
27
+ end
28
+
29
+ # forget drops what was remembered, so a flag that no longer carries an
30
+ # experiment block falls back to the tracked context.
31
+ def forget(flag, context)
32
+ key = key_of(flag, context)
33
+ @mutex.synchronize { @by_key.delete(key) }
34
+ end
35
+
36
+ # recall returns the context the flag was last evaluated with for the
37
+ # units of fallback, or fallback itself.
38
+ def recall(flag, fallback)
39
+ key = key_of(flag, fallback)
40
+ @mutex.synchronize { @by_key.fetch(key, fallback) }
41
+ end
42
+
43
+ def size = @mutex.synchronize { @by_key.size }
44
+
45
+ private
46
+
47
+ def key_of(flag, context) = [flag, context[:units].sort]
48
+ end
49
+ private_constant :LastContexts
50
+ end
51
+ end
@@ -32,6 +32,7 @@ module Flagnetic
32
32
  NAME = "flagnetic"
33
33
  DEFAULT_UNIT_TYPE = "user"
34
34
  DEFAULT_READY_TIMEOUT = 10
35
+ private_constant :DEFAULT_UNIT_TYPE, :DEFAULT_READY_TIMEOUT
35
36
 
36
37
  # The tracking event names every Flagnetic provider accepts, and the
37
38
  # attributes alias reads.
@@ -72,6 +73,7 @@ module Flagnetic
72
73
  @initialized = false
73
74
  @closed = false
74
75
  @revoked = false
76
+ @last = LastContexts.new
75
77
  @client = Flagnetic::Client.new(sdk_key, base_url: base_url, logger: method(:observe), **client_options)
76
78
  @unsubscribe = [@client.on_change { |keys| changed(keys) }, @client.on_stage { |stage| staged(stage) }]
77
79
  end
@@ -118,7 +120,7 @@ module Flagnetic
118
120
  # client's int getter does: a number that is not whole serves
119
121
  # default_value with TYPE_MISMATCH rather than a truncated value.
120
122
  def fetch_integer_value(flag_key:, default_value:, evaluation_context: nil)
121
- Mapping.resolution(integral(detail(flag_key, default_value, evaluation_context), default_value))
123
+ Mapping.resolution(integer_detail(flag_key, default_value, evaluation_context))
122
124
  end
123
125
 
124
126
  # fetch_float_value serves a number flag as a Float.
@@ -133,8 +135,10 @@ module Flagnetic
133
135
  Mapping.resolution(detail(flag_key, default_value, evaluation_context))
134
136
  end
135
137
 
136
- # track reports the exposure of the flag named by the flag attribute
137
- # for evaluation_context, as Flagnetic::Client#exposure does, for the
138
+ # track reports the exposure of the provider's most recent evaluation
139
+ # of the flag named by the flag attribute for the same units, with the
140
+ # context that evaluation had when a before hook added attributes to
141
+ # it (track runs no hook), as Flagnetic::Client#exposure does, for the
138
142
  # event "exposure"; for "alias" it records the alias named by the
139
143
  # fromUnitType, fromUnitId, toUnitType and toUnitId attributes. An
140
144
  # event naming neither, or missing what its mapping needs, does
@@ -144,7 +148,7 @@ module Flagnetic
144
148
  case tracking_event_name
145
149
  when TRACK_EXPOSURE
146
150
  flag = fields["flag"]
147
- @client.exposure(flag, context: context(evaluation_context)) if present?(flag)
151
+ @client.exposure(flag, context: @last.recall(flag, context(evaluation_context))) if present?(flag)
148
152
  when TRACK_ALIAS
149
153
  units = fields.values_at(*ALIAS_ATTRIBUTES)
150
154
  @client.alias(*units) if units.all? { |unit| present?(unit) }
@@ -155,12 +159,32 @@ module Flagnetic
155
159
  private
156
160
 
157
161
  def detail(flag_key, default_value, evaluation_context)
158
- @client.detail(flag_key, default: default_value, context: context(evaluation_context))
162
+ ctx = context(evaluation_context)
163
+ remember(flag_key, ctx, @client.detail(flag_key, default: default_value, context: ctx))
159
164
  end
160
165
 
161
- def context(evaluation_context) = Mapping.context(evaluation_context, @unit_type)
166
+ # remember files the context of an evaluation that carried an
167
+ # experiment block, for track's exposure, and returns served.
168
+ def remember(flag_key, ctx, served)
169
+ if served.experiment
170
+ @last.remember(flag_key, ctx)
171
+ else
172
+ @last.forget(flag_key, ctx)
173
+ end
174
+ served
175
+ end
162
176
 
163
- def present?(value) = value.is_a?(String) && !value.empty?
177
+ # integer_detail narrows before the client counts and exposes the
178
+ # evaluation, where the client can. A flagnetic release without
179
+ # int_detail narrows after, as the provider always did.
180
+ def integer_detail(flag_key, default_value, evaluation_context)
181
+ ctx = context(evaluation_context)
182
+ if @client.respond_to?(:int_detail)
183
+ return remember(flag_key, ctx, @client.int_detail(flag_key, default: default_value, context: ctx))
184
+ end
185
+
186
+ integral(remember(flag_key, ctx, @client.detail(flag_key, default: default_value, context: ctx)), default_value)
187
+ end
164
188
 
165
189
  # integral narrows a served number to an Integer; one that is not
166
190
  # whole is a type mismatch, served as the fallback the client's own
@@ -178,6 +202,10 @@ module Flagnetic
178
202
  stage: served.stage)
179
203
  end
180
204
 
205
+ def context(evaluation_context) = Mapping.context(evaluation_context, @unit_type)
206
+
207
+ def present?(value) = value.is_a?(String) && !value.empty?
208
+
181
209
  # announce sends the state init found: ERROR on a revoked key, else
182
210
  # READY, followed by STALE when only a last good ruleset is held.
183
211
  def announce
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Flagnetic
4
4
  module OpenFeature
5
- VERSION = "0.1.1"
5
+ VERSION = "0.2.0"
6
6
  end
7
7
  end
@@ -5,6 +5,7 @@ require "open_feature/sdk"
5
5
 
6
6
  require_relative "openfeature/version"
7
7
  require_relative "openfeature/mapping"
8
+ require_relative "openfeature/last_contexts"
8
9
  require_relative "openfeature/provider"
9
10
 
10
11
  module Flagnetic
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flagnetic-openfeature
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anatolii Zverev
@@ -15,14 +15,14 @@ dependencies:
15
15
  requirements:
16
16
  - - "~>"
17
17
  - !ruby/object:Gem::Version
18
- version: 0.1.0
18
+ version: 0.2.0
19
19
  type: :runtime
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
23
  - - "~>"
24
24
  - !ruby/object:Gem::Version
25
- version: 0.1.0
25
+ version: 0.2.0
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: openfeature-sdk
28
28
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +52,7 @@ files:
52
52
  - LICENSE
53
53
  - README.md
54
54
  - lib/flagnetic/openfeature.rb
55
+ - lib/flagnetic/openfeature/last_contexts.rb
55
56
  - lib/flagnetic/openfeature/mapping.rb
56
57
  - lib/flagnetic/openfeature/provider.rb
57
58
  - lib/flagnetic/openfeature/version.rb