flagsmith 5.2.0 → 5.3.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 +4 -4
- data/.release-please-manifest.json +1 -1
- data/CHANGELOG.md +7 -0
- data/Gemfile.lock +1 -1
- data/lib/flagsmith/engine/features/models.rb +12 -12
- data/lib/flagsmith/engine/mappers/environment.rb +10 -2
- data/lib/flagsmith/sdk/event_processor.rb +229 -40
- data/lib/flagsmith/sdk/models/flags.rb +9 -5
- data/lib/flagsmith/version.rb +1 -1
- data/lib/flagsmith.rb +21 -3
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4f299cb1871e66890f06f9d889c87efb4c86ac5c46f2ae3fb0ec31cb4c0a3fd9
|
|
4
|
+
data.tar.gz: 2ef2710f183b66e2745f3237a8eef850e586535fd1109a3671cc3c2027231c63
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 69ae4cca3593b047e4e534882be4dfd73065c3f84e179d8a5fe1644ed663a8322134d55aafc9d0a7dc3450460691059c9293589e3f8c089a196ff3c57736c8b4
|
|
7
|
+
data.tar.gz: cf9581d4398e4ab476dbf1ca21cf10be896a4173a2d7fe423f2af95a20445de6eea247dfa24e08b9d8b4eb9da752f197b106d5afbf33f2b31c86386f350fdd2b
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{".":"5.
|
|
1
|
+
{".":"5.3.0"}
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [5.3.0](https://github.com/Flagsmith/flagsmith-ruby-client/compare/v5.2.0...v5.3.0) (2026-11-13)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* Retry event delivery and flush on close ([#129](https://github.com/Flagsmith/flagsmith-ruby-client/issues/129)) ([5abcc8e](https://github.com/Flagsmith/flagsmith-ruby-client/commit/5abcc8e5bc6a7b7fed6483e5731bc21cae1e3b25))
|
|
9
|
+
|
|
3
10
|
## [5.2.0](https://github.com/Flagsmith/flagsmith-ruby-client/compare/v5.1.0...v5.2.0) (2026-11-09)
|
|
4
11
|
|
|
5
12
|
|
data/Gemfile.lock
CHANGED
|
@@ -8,7 +8,7 @@ module Flagsmith
|
|
|
8
8
|
class FeatureState
|
|
9
9
|
include Flagsmith::Engine::Utils::HashFunc
|
|
10
10
|
|
|
11
|
-
attr_reader :feature, :enabled, :django_id, :uuid, :feature_segment
|
|
11
|
+
attr_reader :feature, :enabled, :django_id, :uuid, :feature_segment, :experiment
|
|
12
12
|
attr_accessor :multivariate_feature_state_values
|
|
13
13
|
|
|
14
14
|
def initialize(params = {})
|
|
@@ -19,6 +19,7 @@ module Flagsmith
|
|
|
19
19
|
@feature_state_value = params.fetch(:feature_state_value, nil)
|
|
20
20
|
@uuid = params.fetch(:uuid, SecureRandom.uuid)
|
|
21
21
|
@multivariate_feature_state_values = params.fetch(:multivariate_feature_state_values, [])
|
|
22
|
+
@experiment = params.fetch(:experiment, nil)
|
|
22
23
|
end
|
|
23
24
|
|
|
24
25
|
attr_writer :feature_state_value
|
|
@@ -66,14 +67,12 @@ module Flagsmith
|
|
|
66
67
|
class << self
|
|
67
68
|
def build(json)
|
|
68
69
|
multivariate_feature_state_values = build_multivariate_values(json[:multivariate_feature_state_values])
|
|
69
|
-
attributes = json.slice(:uuid, :enabled, :django_id, :feature_state_value)
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
)
|
|
76
|
-
end
|
|
70
|
+
attributes = json.slice(:uuid, :enabled, :django_id, :feature_state_value).merge(
|
|
71
|
+
feature: Flagsmith::Engine::Feature.build(json[:feature]),
|
|
72
|
+
multivariate_feature_state_values: multivariate_feature_state_values,
|
|
73
|
+
experiment: json.dig(:metadata, :experiment)
|
|
74
|
+
)
|
|
75
|
+
attributes[:feature_segment] = Flagsmith::Engine::Features::Segment.new(json[:feature_segment]) unless json[:feature_segment].nil?
|
|
77
76
|
new(**attributes)
|
|
78
77
|
end
|
|
79
78
|
|
|
@@ -113,16 +112,17 @@ module Flagsmith
|
|
|
113
112
|
module Features
|
|
114
113
|
# MultivariateFeatureOptionModel
|
|
115
114
|
class MultivariateOption
|
|
116
|
-
attr_reader :value, :id
|
|
115
|
+
attr_reader :value, :id, :key
|
|
117
116
|
|
|
118
|
-
def initialize(value:, id: nil)
|
|
117
|
+
def initialize(value:, id: nil, key: nil)
|
|
119
118
|
@value = value
|
|
120
119
|
@id = id
|
|
120
|
+
@key = key
|
|
121
121
|
end
|
|
122
122
|
|
|
123
123
|
class << self
|
|
124
124
|
def build(json)
|
|
125
|
-
new(**json.slice(:id, :value))
|
|
125
|
+
new(**json.slice(:id, :value, :key))
|
|
126
126
|
end
|
|
127
127
|
end
|
|
128
128
|
end
|
|
@@ -26,22 +26,30 @@ module Flagsmith
|
|
|
26
26
|
name: feature_state.feature.name,
|
|
27
27
|
enabled: feature_state.enabled,
|
|
28
28
|
value: feature_state.get_value,
|
|
29
|
-
metadata:
|
|
29
|
+
metadata: build_feature_metadata(feature_state)
|
|
30
30
|
}
|
|
31
31
|
add_variants_to_feature(feature_hash, feature_state)
|
|
32
32
|
add_priority_to_feature(feature_hash, feature_state)
|
|
33
33
|
feature_hash
|
|
34
34
|
end
|
|
35
35
|
|
|
36
|
+
# The running experiment a feature state belongs to, if any, is passed through to its flag
|
|
37
|
+
def self.build_feature_metadata(feature_state)
|
|
38
|
+
metadata = { id: feature_state.feature.id }
|
|
39
|
+
metadata[:experiment] = feature_state.experiment if feature_state.experiment
|
|
40
|
+
metadata
|
|
41
|
+
end
|
|
42
|
+
|
|
36
43
|
def self.add_variants_to_feature(feature_hash, feature_state)
|
|
37
44
|
return unless feature_state.multivariate_feature_state_values&.any?
|
|
38
45
|
|
|
39
46
|
feature_hash[:variants] = feature_state.multivariate_feature_state_values.map do |mv|
|
|
40
47
|
{
|
|
41
48
|
value: mv.multivariate_feature_option.value,
|
|
49
|
+
key: mv.multivariate_feature_option.key,
|
|
42
50
|
weight: mv.percentage_allocation,
|
|
43
51
|
priority: mv.id || uuid_to_big_int(mv.mv_fs_value_uuid)
|
|
44
|
-
}
|
|
52
|
+
}.compact
|
|
45
53
|
end
|
|
46
54
|
end
|
|
47
55
|
|
|
@@ -26,26 +26,36 @@ module Flagsmith
|
|
|
26
26
|
#
|
|
27
27
|
# Events are sent every +flush_interval_seconds+, as soon as +max_buffer_items+ are buffered,
|
|
28
28
|
# or on #flush. Sending events never raises into the application: failures are logged.
|
|
29
|
+
#
|
|
30
|
+
# A batch is sent up to +MAX_ATTEMPTS+ times, one batch at a time, backing off with full jitter between
|
|
31
|
+
# attempts. A batch whose last attempt fails transiently is kept for the next flush, unless that attempt got
|
|
32
|
+
# a 500. A batch the events API rejects is dropped, and an events API refusing the environment key stops
|
|
33
|
+
# event tracking. When the buffer is full, the oldest event is dropped. While a failed batch is kept, a full
|
|
34
|
+
# buffer waits for the next flush rather than starting another round of attempts.
|
|
35
|
+
#
|
|
36
|
+
# Once stopped, tracking and flushing do nothing.
|
|
29
37
|
class EventProcessor # rubocop:disable Metrics/ClassLength
|
|
30
|
-
include Flagsmith::SDK::Intervals
|
|
31
|
-
|
|
32
38
|
FLAG_EXPOSURE_EVENT = '$flag_exposure'
|
|
33
39
|
ENDPOINT = 'v1/events'
|
|
34
40
|
FLUSH_INTERVAL_SECONDS = 10
|
|
35
41
|
MAX_BUFFER_ITEMS = 1000
|
|
36
42
|
REQUEST_TIMEOUT_SECONDS = 3
|
|
37
|
-
|
|
43
|
+
MAX_ATTEMPTS = 3
|
|
44
|
+
RETRY_BASE_SECONDS = 1
|
|
45
|
+
CLOSE_TIMEOUT_SECONDS = 5
|
|
46
|
+
# Retried along with every server error
|
|
47
|
+
RETRIED_STATUSES = [408, 429].freeze
|
|
48
|
+
REFUSED_STATUSES = [401, 403].freeze
|
|
38
49
|
|
|
39
|
-
def initialize(config, flush_interval_seconds: FLUSH_INTERVAL_SECONDS, max_buffer_items: MAX_BUFFER_ITEMS
|
|
50
|
+
def initialize(config, flush_interval_seconds: FLUSH_INTERVAL_SECONDS, max_buffer_items: MAX_BUFFER_ITEMS,
|
|
51
|
+
retry_base_seconds: RETRY_BASE_SECONDS)
|
|
40
52
|
@logger = config.logger
|
|
41
53
|
@flush_interval_seconds = flush_interval_seconds
|
|
42
54
|
@max_buffer_items = max_buffer_items
|
|
55
|
+
@retry_base_seconds = retry_base_seconds
|
|
43
56
|
@connection = build_connection(config)
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
@mutex = Mutex.new
|
|
47
|
-
@flush_mutex = Mutex.new
|
|
48
|
-
@flush_pending = false
|
|
57
|
+
initialize_buffer
|
|
58
|
+
initialize_state
|
|
49
59
|
end
|
|
50
60
|
|
|
51
61
|
def track_event(event, identifier: nil, value: nil, traits: nil, metadata: nil)
|
|
@@ -58,51 +68,203 @@ module Flagsmith
|
|
|
58
68
|
identifier: identifier, value: value, traits: traits, metadata: metadata))
|
|
59
69
|
end
|
|
60
70
|
|
|
71
|
+
# Sends the events tracked so far, and returns once each of them has been delivered, dropped, or kept for
|
|
72
|
+
# the next flush, retries included. Events another flush sent while this one waited are not sent again.
|
|
61
73
|
def flush
|
|
62
|
-
|
|
74
|
+
request = @mutex.synchronize { flush_request unless stopping?('flushing events') }
|
|
75
|
+
send_batch(request) if request
|
|
63
76
|
end
|
|
64
77
|
|
|
65
78
|
def start
|
|
66
|
-
return if @interval
|
|
79
|
+
return if @interval || @stopping
|
|
67
80
|
|
|
68
|
-
@interval =
|
|
81
|
+
@interval = Thread.new { send_batch until wait(@flush_interval_seconds) { @stopping } }
|
|
69
82
|
RunningEventProcessors.add(self)
|
|
70
83
|
end
|
|
71
84
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
85
|
+
# Stops flushing every interval, and sends the buffered events, waiting for at most +timeout_seconds+.
|
|
86
|
+
# Events not sent by then are not retried, and no batch is taken to send once it returns.
|
|
87
|
+
def stop(timeout_seconds = CLOSE_TIMEOUT_SECONDS)
|
|
75
88
|
RunningEventProcessors.delete(self)
|
|
76
|
-
|
|
89
|
+
change_state { @stopping = true }
|
|
90
|
+
@interval = nil
|
|
91
|
+
final_flush = Thread.new { send_batch }
|
|
92
|
+
@logger.warn "Gave up sending events after #{timeout_seconds} seconds while closing" unless final_flush.join(timeout_seconds)
|
|
93
|
+
change_state { @closed = true }
|
|
77
94
|
end
|
|
78
95
|
|
|
79
96
|
private
|
|
80
97
|
|
|
98
|
+
def initialize_buffer
|
|
99
|
+
@events = []
|
|
100
|
+
@buffered_exposures = Set.new
|
|
101
|
+
# Events tracked so far, how many of them were in a batch once sent, and the batches sent
|
|
102
|
+
@tracked_count = 0
|
|
103
|
+
@attempted_count = 0
|
|
104
|
+
@settled_count = 0
|
|
105
|
+
@flush_pending = false
|
|
106
|
+
# Whether the buffer holds a batch whose attempts all failed
|
|
107
|
+
@holding_failed_batch = false
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def initialize_state
|
|
111
|
+
@mutex = Mutex.new
|
|
112
|
+
# Held while a batch is in flight, so that one batch is sent at a time
|
|
113
|
+
@flush_mutex = Mutex.new
|
|
114
|
+
@state_changed = ConditionVariable.new
|
|
115
|
+
@refused = false
|
|
116
|
+
@stopping = false
|
|
117
|
+
@closed = false
|
|
118
|
+
end
|
|
119
|
+
|
|
81
120
|
# Only the flush scheduled by a full buffer clears +@flush_pending+, once it holds the buffer, so at most one
|
|
82
121
|
# such flush is waiting at a time.
|
|
83
|
-
def scheduled_flush
|
|
84
|
-
|
|
122
|
+
def scheduled_flush(request)
|
|
123
|
+
send_batch(request) { @flush_pending = false }
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# What a flush requested now asks for: the events tracked so far, unless a batch settled since, including
|
|
127
|
+
# one in flight now, sent them
|
|
128
|
+
def flush_request
|
|
129
|
+
[@tracked_count, @settled_count]
|
|
85
130
|
end
|
|
86
131
|
|
|
87
|
-
|
|
132
|
+
# Sends the buffered events as one batch. Given a +flush_request+, does nothing if a batch settled since the
|
|
133
|
+
# request sent every event tracked up to it.
|
|
134
|
+
def send_batch(...)
|
|
88
135
|
@flush_mutex.synchronize do
|
|
89
|
-
batch =
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
136
|
+
batch, attempted_count = take_batch(...)
|
|
137
|
+
settle(batch, deliver(batch), attempted_count) if batch
|
|
138
|
+
end
|
|
139
|
+
rescue StandardError => e
|
|
140
|
+
@logger.error "Unable to send events to the Flagsmith events API: #{e}"
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def take_batch(request = nil)
|
|
144
|
+
@mutex.synchronize do
|
|
145
|
+
yield if block_given?
|
|
146
|
+
next if @closed || @events.empty? || sent_since?(request)
|
|
147
|
+
|
|
148
|
+
@holding_failed_batch = false
|
|
149
|
+
[clear_buffer, @tracked_count]
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def sent_since?(request)
|
|
154
|
+
return false unless request
|
|
155
|
+
|
|
156
|
+
tracked_count, settled_count = request
|
|
157
|
+
@settled_count > settled_count && @attempted_count >= tracked_count
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# Returns the events the buffer held
|
|
161
|
+
def clear_buffer
|
|
162
|
+
events = @events
|
|
163
|
+
@events = []
|
|
164
|
+
@buffered_exposures.clear
|
|
165
|
+
events
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def settle(batch, outcome, attempted_count)
|
|
169
|
+
@mutex.synchronize do
|
|
170
|
+
@attempted_count = attempted_count
|
|
171
|
+
@settled_count += 1
|
|
172
|
+
hold_failed_batch(batch) if outcome == :kept
|
|
173
|
+
next unless outcome == :refused
|
|
174
|
+
|
|
175
|
+
@refused = true
|
|
176
|
+
clear_buffer
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# Keeps a failed batch ahead of the events tracked since, with one of each exposure
|
|
181
|
+
def hold_failed_batch(batch)
|
|
182
|
+
@buffered_exposures.clear
|
|
183
|
+
@events = (batch + @events).last(@max_buffer_items).reject { |event| duplicate_exposure?(event) }
|
|
184
|
+
@holding_failed_batch = true
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
# Returns :delivered, :dropped, :kept or :refused
|
|
188
|
+
def deliver(batch)
|
|
189
|
+
body = { events: batch }.to_json
|
|
190
|
+
status = nil
|
|
191
|
+
(1..MAX_ATTEMPTS).each do |attempt|
|
|
192
|
+
return keep(batch, 'the client is closed') if attempt > 1 && wait(backoff(attempt)) { @closed }
|
|
193
|
+
|
|
194
|
+
status = post(body, batch, attempt)
|
|
195
|
+
outcome = outcome_of(status, batch)
|
|
196
|
+
return outcome if outcome
|
|
197
|
+
|
|
198
|
+
log_failed_attempt(batch, attempt, "status #{status}") if status
|
|
199
|
+
end
|
|
200
|
+
status == 500 ? drop(batch, 'its last attempt got 500') : keep(batch, 'its last attempt failed')
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
# Full jitter: any wait up to the backoff, which doubles from +@retry_base_seconds+
|
|
204
|
+
def backoff(attempt)
|
|
205
|
+
rand * @retry_base_seconds * (2**(attempt - 2))
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
# Returns the response status, or nil when the request got none
|
|
209
|
+
def post(body, batch, attempt)
|
|
210
|
+
@connection.post(ENDPOINT, body).status
|
|
211
|
+
rescue Faraday::Error => e
|
|
212
|
+
log_failed_attempt(batch, attempt, e)
|
|
213
|
+
nil
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
def log_failed_attempt(batch, attempt, failure)
|
|
217
|
+
@logger.warn "Unable to send #{batch.length} events to the Flagsmith events API, " \
|
|
218
|
+
"attempt #{attempt} of #{MAX_ATTEMPTS}: #{failure}"
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
# Returns the outcome of a batch given its response status, or nil to try again
|
|
222
|
+
def outcome_of(status, batch)
|
|
223
|
+
return nil if status.nil? || status >= 500 || RETRIED_STATUSES.include?(status)
|
|
224
|
+
return :delivered if (200..299).cover?(status)
|
|
225
|
+
|
|
226
|
+
if REFUSED_STATUSES.include?(status)
|
|
227
|
+
@logger.error "The Flagsmith events API refused the environment key with #{status}, stopping event tracking"
|
|
228
|
+
return :refused
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
drop(batch, "the events API rejected it with #{status}")
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
def drop(batch, reason)
|
|
235
|
+
@logger.warn "Dropping #{batch.length} events: #{reason}"
|
|
236
|
+
:dropped
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
def keep(batch, reason)
|
|
240
|
+
@logger.warn "Keeping #{batch.length} events for the next flush: #{reason}"
|
|
241
|
+
:kept
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
def change_state
|
|
245
|
+
@mutex.synchronize do
|
|
246
|
+
yield
|
|
247
|
+
@state_changed.broadcast
|
|
248
|
+
end
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
# Waits for at most +seconds+, until the block is true. Returns the block's value.
|
|
252
|
+
def wait(seconds)
|
|
253
|
+
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + seconds
|
|
254
|
+
@mutex.synchronize do
|
|
255
|
+
until yield
|
|
256
|
+
remaining = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
257
|
+
break if remaining <= 0
|
|
258
|
+
|
|
259
|
+
@state_changed.wait(@mutex, remaining)
|
|
95
260
|
end
|
|
96
|
-
|
|
261
|
+
yield
|
|
97
262
|
end
|
|
98
263
|
end
|
|
99
264
|
|
|
100
265
|
def build_connection(config)
|
|
101
266
|
Faraday.new(url: config.events_api_url, headers: headers(config)) do |f|
|
|
102
267
|
f.options.timeout = REQUEST_TIMEOUT_SECONDS
|
|
103
|
-
f.response :raise_error
|
|
104
|
-
f.request :retry, max: 1, interval: 1, methods: %i[post], retry_statuses: RETRIABLE_STATUSES,
|
|
105
|
-
exceptions: [*Faraday::Retry::Middleware::DEFAULT_EXCEPTIONS, Faraday::ConnectionFailed]
|
|
106
268
|
end
|
|
107
269
|
end
|
|
108
270
|
|
|
@@ -135,29 +297,56 @@ module Flagsmith
|
|
|
135
297
|
end
|
|
136
298
|
|
|
137
299
|
def buffer(event)
|
|
138
|
-
|
|
139
|
-
next
|
|
300
|
+
request = @mutex.synchronize do
|
|
301
|
+
next if stopping?("tracking #{event[:event]} event") || refused?(event) || duplicate_exposure?(event)
|
|
140
302
|
|
|
303
|
+
drop_oldest if @events.length >= @max_buffer_items
|
|
141
304
|
@events << event
|
|
142
|
-
|
|
305
|
+
@tracked_count += 1
|
|
306
|
+
next unless full_buffer_flush_due?
|
|
143
307
|
|
|
144
308
|
@flush_pending = true
|
|
309
|
+
flush_request
|
|
145
310
|
end
|
|
146
|
-
Thread.new { scheduled_flush } if
|
|
311
|
+
Thread.new { scheduled_flush(request) } if request
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
# A full buffer is flushed at once, unless such a flush is already waiting, or it holds a failed batch,
|
|
315
|
+
# which waits for the next flush.
|
|
316
|
+
def full_buffer_flush_due?
|
|
317
|
+
!@flush_pending && !@holding_failed_batch && @events.length >= @max_buffer_items
|
|
318
|
+
end
|
|
319
|
+
|
|
320
|
+
def stopping?(action)
|
|
321
|
+
return false unless @stopping
|
|
322
|
+
|
|
323
|
+
@logger.debug "Not #{action}: the client is closed"
|
|
324
|
+
true
|
|
325
|
+
end
|
|
326
|
+
|
|
327
|
+
def refused?(event)
|
|
328
|
+
return false unless @refused
|
|
329
|
+
|
|
330
|
+
@logger.debug "Not tracking #{event[:event]} event: the Flagsmith events API refused the environment key"
|
|
331
|
+
true
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
def drop_oldest
|
|
335
|
+
dropped = @events.shift
|
|
336
|
+
@buffered_exposures.delete(exposure_key(dropped)) if dropped[:event] == FLAG_EXPOSURE_EVENT
|
|
337
|
+
@logger.warn "Event buffer is full, dropping the oldest #{dropped[:event]} event"
|
|
338
|
+
end
|
|
339
|
+
|
|
340
|
+
def exposure_key(event)
|
|
341
|
+
event.values_at(:feature_name, :identifier, :value)
|
|
147
342
|
end
|
|
148
343
|
|
|
149
344
|
def duplicate_exposure?(event)
|
|
150
345
|
return false unless event[:event] == FLAG_EXPOSURE_EVENT
|
|
151
|
-
return false if @buffered_exposures.add?(event
|
|
346
|
+
return false if @buffered_exposures.add?(exposure_key(event))
|
|
152
347
|
|
|
153
348
|
@logger.debug "Skipping duplicate #{FLAG_EXPOSURE_EVENT} event for feature #{event[:feature_name]}"
|
|
154
349
|
true
|
|
155
350
|
end
|
|
156
|
-
|
|
157
|
-
def send_events(events)
|
|
158
|
-
@connection.post(ENDPOINT, { events: events }.to_json)
|
|
159
|
-
rescue StandardError => e
|
|
160
|
-
@logger.warn "Unable to send #{events.length} events to the Flagsmith events API, dropping them: #{e}"
|
|
161
|
-
end
|
|
162
351
|
end
|
|
163
352
|
end
|
|
@@ -30,7 +30,7 @@ module Flagsmith
|
|
|
30
30
|
end
|
|
31
31
|
end
|
|
32
32
|
|
|
33
|
-
# The running experiment an identity's flag was evaluated in, as returned by the API
|
|
33
|
+
# The running experiment an identity's flag was evaluated in, as returned by the API or the environment document
|
|
34
34
|
Experiment = Struct.new(:id, :name, :in_experiment, keyword_init: true) do
|
|
35
35
|
alias_method :in_experiment?, :in_experiment
|
|
36
36
|
|
|
@@ -192,7 +192,9 @@ module Flagsmith
|
|
|
192
192
|
)
|
|
193
193
|
end
|
|
194
194
|
|
|
195
|
-
|
|
195
|
+
# Only an identity is enrolled in an experiment, as with remote evaluation: environment flags,
|
|
196
|
+
# evaluated without one, are built with +identity: false+ and carry no experiment.
|
|
197
|
+
def from_evaluation_result(evaluation_result, identity: true, **args)
|
|
196
198
|
to_flag_object = lambda { |flag_result, acc|
|
|
197
199
|
id = flag_result.dig(:metadata, :id)
|
|
198
200
|
if id.nil?
|
|
@@ -200,19 +202,21 @@ module Flagsmith
|
|
|
200
202
|
"FlagResult metadata.id is missing for feature \"#{flag_result[:name]}\". This indicates a bug in the SDK, please report it."
|
|
201
203
|
end
|
|
202
204
|
|
|
203
|
-
acc[flag_result[:name]] = Collection.map_evaluated_flag_to_flag_result(flag_result, id)
|
|
205
|
+
acc[flag_result[:name]] = Collection.map_evaluated_flag_to_flag_result(flag_result, id, identity: identity)
|
|
204
206
|
}
|
|
205
207
|
|
|
206
208
|
new(evaluation_result[:flags].each_value.each_with_object({}, &to_flag_object), **args)
|
|
207
209
|
end
|
|
208
210
|
|
|
209
|
-
def map_evaluated_flag_to_flag_result(flag_result, id)
|
|
211
|
+
def map_evaluated_flag_to_flag_result(flag_result, id, identity: true)
|
|
210
212
|
Flagsmith::Flags::Flag.new(
|
|
211
213
|
feature_name: flag_result[:name],
|
|
212
214
|
enabled: flag_result[:enabled],
|
|
213
215
|
value: flag_result[:value],
|
|
214
216
|
feature_id: id,
|
|
215
|
-
reason: flag_result[:reason]
|
|
217
|
+
reason: flag_result[:reason],
|
|
218
|
+
variant: flag_result[:variant],
|
|
219
|
+
experiment: (Experiment.from_api(flag_result[:metadata]) if identity)
|
|
216
220
|
)
|
|
217
221
|
end
|
|
218
222
|
|
data/lib/flagsmith/version.rb
CHANGED
data/lib/flagsmith.rb
CHANGED
|
@@ -50,6 +50,10 @@ module Flagsmith
|
|
|
50
50
|
#
|
|
51
51
|
# flag = flagsmith.get_experiment_flag('foo', 'identifier', plan: 'premium')
|
|
52
52
|
# flagsmith.track_event('purchase', identifier: 'identifier', value: 42)
|
|
53
|
+
#
|
|
54
|
+
# Close the client before the application stops, so that its buffered events are sent:
|
|
55
|
+
#
|
|
56
|
+
# flagsmith.close
|
|
53
57
|
|
|
54
58
|
# Available Configs.
|
|
55
59
|
#
|
|
@@ -221,8 +225,7 @@ module Flagsmith
|
|
|
221
225
|
|
|
222
226
|
# Get a flag for an identity, taking the same traits as get_identity_flags, and record the
|
|
223
227
|
# identity's exposure to the variant it was served when it is in the feature's running experiment.
|
|
224
|
-
# Requires enable_events.
|
|
225
|
-
# evaluation and offline handlers carries no experiment data, so no exposure is recorded from it.
|
|
228
|
+
# Requires enable_events.
|
|
226
229
|
def get_experiment_flag(feature_name, identifier, **traits)
|
|
227
230
|
require_events!
|
|
228
231
|
flag = get_identity_flags(identifier, **traits)[feature_name]
|
|
@@ -260,6 +263,21 @@ module Flagsmith
|
|
|
260
263
|
metadata: metadata)
|
|
261
264
|
end
|
|
262
265
|
|
|
266
|
+
# Send the events tracked so far, and return once each of them has been delivered, dropped, or kept for the
|
|
267
|
+
# next flush, retries included. Requires enable_events.
|
|
268
|
+
def flush_events
|
|
269
|
+
require_events!
|
|
270
|
+
event_processor.flush
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
# Stop the client's background work, and send the buffered events, waiting for at most +timeout_seconds+.
|
|
274
|
+
# Events not sent by then are lost.
|
|
275
|
+
def close(timeout_seconds: Flagsmith::EventProcessor::CLOSE_TIMEOUT_SECONDS)
|
|
276
|
+
@environment_data_polling_manager&.stop
|
|
277
|
+
@realtime_client&.running = false
|
|
278
|
+
@event_processor&.stop(timeout_seconds)
|
|
279
|
+
end
|
|
280
|
+
|
|
263
281
|
def get_identity_segments(identifier, traits = {})
|
|
264
282
|
raise Flagsmith::ClientError, 'Local evaluation or offline handler is required to obtain identity segments.' unless environment
|
|
265
283
|
|
|
@@ -283,7 +301,6 @@ module Flagsmith
|
|
|
283
301
|
def exposure_skip_reason(flag)
|
|
284
302
|
return 'feature not found' unless flag.is_a?(Flagsmith::Flags::Flag)
|
|
285
303
|
return 'feature is disabled' unless flag.enabled?
|
|
286
|
-
return 'flags come from the environment document, which carries no experiment data' if environment
|
|
287
304
|
return 'identity is not in a running experiment' unless flag.experiment&.in_experiment?
|
|
288
305
|
|
|
289
306
|
'flag has no variant' if flag.variant.nil?
|
|
@@ -300,6 +317,7 @@ module Flagsmith
|
|
|
300
317
|
|
|
301
318
|
Flagsmith::Flags::Collection.from_evaluation_result(
|
|
302
319
|
evaluation_result,
|
|
320
|
+
identity: false,
|
|
303
321
|
analytics_processor: analytics_processor,
|
|
304
322
|
default_flag_handler: default_flag_handler,
|
|
305
323
|
offline_handler: offline_handler
|