flagsmith 5.0.1 → 5.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 +4 -4
- data/.release-please-manifest.json +1 -1
- data/CHANGELOG.md +38 -0
- data/Gemfile.lock +6 -2
- data/Makefile +28 -0
- data/codspeed.yml +5 -0
- data/lib/flagsmith/engine/core.rb +74 -42
- data/lib/flagsmith/engine/features/constants.rb +2 -0
- data/lib/flagsmith/engine/lazy_flags.rb +118 -0
- data/lib/flagsmith/engine/mappers/segments.rb +2 -18
- data/lib/flagsmith/engine/mappers.rb +31 -0
- data/lib/flagsmith/engine/segments/constants.rb +3 -0
- data/lib/flagsmith/engine/segments/evaluator.rb +16 -7
- data/lib/flagsmith/engine/segments/models.rb +6 -10
- data/lib/flagsmith/sdk/config.rb +15 -1
- data/lib/flagsmith/sdk/event_processor.rb +163 -0
- data/lib/flagsmith/sdk/models/flags.rb +22 -4
- data/lib/flagsmith/version.rb +1 -1
- data/lib/flagsmith.rb +71 -4
- data/renovate.json +16 -0
- metadata +33 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: bc55f18f6488e55ec0453407d1725f0a4f66e0271c5fbc1566700207c22b1c2f
|
|
4
|
+
data.tar.gz: 2f6a586361b782a2190978ed7755a8c199863bcf7bd622d703eff5a7d6ed1570
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8b5f6325d2826366ef587834ea151f09314cd7f8d3350ca76a186bb6478db48c6bf94fa9362f144bede5530618b88c3d4ea373533a3e31e3a88ff3488f9e45a4
|
|
7
|
+
data.tar.gz: 78a9f8125bb7bc9f372d5498abd3dc8465cddf71dc9808f4d2cd99ed48131b9683ad63262c204a1995ffd866ed833f992fca10300222203fc0e06a9b84a6d1db
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{".":"5.0
|
|
1
|
+
{".":"5.2.0"}
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,43 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [5.2.0](https://github.com/Flagsmith/flagsmith-ruby-client/compare/v5.1.0...v5.2.0) (2026-11-09)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* Support `getExperimentFlag`, `trackEvent` and `trackExposure` methods ([#127](https://github.com/Flagsmith/flagsmith-ruby-client/issues/127)) ([670b413](https://github.com/Flagsmith/flagsmith-ruby-client/commit/670b4136f3f198ee7a4ee3c7914f9d44997b919d))
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* Serve multivariate segment overrides their variant under local evaluation ([#128](https://github.com/Flagsmith/flagsmith-ruby-client/issues/128)) ([dbb7a83](https://github.com/Flagsmith/flagsmith-ruby-client/commit/dbb7a839168f5155d38aa3d0f018f59f82982a93))
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
### Other
|
|
17
|
+
|
|
18
|
+
* Let Renovate keep updating spec bumps the webhook handler commits to ([#123](https://github.com/Flagsmith/flagsmith-ruby-client/issues/123)) ([a6ccd7f](https://github.com/Flagsmith/flagsmith-ruby-client/commit/a6ccd7f236ebf6804545dab265175cfc38d8a547))
|
|
19
|
+
|
|
20
|
+
## [5.1.0](https://github.com/Flagsmith/flagsmith-ruby-client/compare/v5.0.1...v5.1.0) (2026-10-30)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
### Features
|
|
24
|
+
|
|
25
|
+
* Evaluate flags that depend on other flags ([#120](https://github.com/Flagsmith/flagsmith-ruby-client/issues/120)) ([e4efc8f](https://github.com/Flagsmith/flagsmith-ruby-client/commit/e4efc8f36b7edc647bcd696b199775d7e22ddc63))
|
|
26
|
+
* Ruby 4 support ([#116](https://github.com/Flagsmith/flagsmith-ruby-client/issues/116)) ([51544ab](https://github.com/Flagsmith/flagsmith-ruby-client/commit/51544abcfc593efc5d0bb8043744013526a8e488))
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
### Bug Fixes
|
|
30
|
+
|
|
31
|
+
* Align numeric trait conditions with flagsmith-engine ([#118](https://github.com/Flagsmith/flagsmith-ruby-client/issues/118)) ([5b154ca](https://github.com/Flagsmith/flagsmith-ruby-client/commit/5b154caab68f746d62dbfa05280b6a19b9277223))
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
### CI
|
|
35
|
+
|
|
36
|
+
* Benchmark the SDK with CodSpeed ([#117](https://github.com/Flagsmith/flagsmith-ruby-client/issues/117)) ([a95445c](https://github.com/Flagsmith/flagsmith-ruby-client/commit/a95445c3a6580153c49ef19bcb5f58807ddaa624))
|
|
37
|
+
* Label Renovate's SDK spec bump PRs ([#111](https://github.com/Flagsmith/flagsmith-ruby-client/issues/111)) ([ef6c84e](https://github.com/Flagsmith/flagsmith-ruby-client/commit/ef6c84e88e953e2b7d1c2139baa5764a59b3ec25))
|
|
38
|
+
* Pin test runner to ubuntu-24.04 for Ruby 3.0 support ([#115](https://github.com/Flagsmith/flagsmith-ruby-client/issues/115)) ([f10b360](https://github.com/Flagsmith/flagsmith-ruby-client/commit/f10b360da6940c037ffb02c7b8e1d01f1d2385c3))
|
|
39
|
+
* Run the Flagsmith SDK behavioural spec ([#110](https://github.com/Flagsmith/flagsmith-ruby-client/issues/110)) ([74be985](https://github.com/Flagsmith/flagsmith-ruby-client/commit/74be98598292b2c4dad634412c4ad7ad0465b268))
|
|
40
|
+
|
|
3
41
|
## [5.0.1](https://github.com/Flagsmith/flagsmith-ruby-client/compare/v5.0.0...v5.0.1) (2026-08-26)
|
|
4
42
|
|
|
5
43
|
|
data/Gemfile.lock
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
flagsmith (5.0
|
|
4
|
+
flagsmith (5.2.0)
|
|
5
5
|
faraday (>= 2.0.1)
|
|
6
6
|
faraday-retry
|
|
7
7
|
jsonpath (~> 1.1)
|
|
@@ -11,6 +11,7 @@ GEM
|
|
|
11
11
|
remote: https://rubygems.org/
|
|
12
12
|
specs:
|
|
13
13
|
ast (2.4.2)
|
|
14
|
+
benchmark (0.5.0)
|
|
14
15
|
byebug (11.1.3)
|
|
15
16
|
coderay (1.1.3)
|
|
16
17
|
diff-lcs (1.5.0)
|
|
@@ -20,7 +21,7 @@ GEM
|
|
|
20
21
|
logger
|
|
21
22
|
faraday-net_http (3.4.4)
|
|
22
23
|
net-http (~> 0.5)
|
|
23
|
-
faraday-retry (2.
|
|
24
|
+
faraday-retry (2.4.0)
|
|
24
25
|
faraday (~> 2.0)
|
|
25
26
|
gem-release (2.2.2)
|
|
26
27
|
json (2.21.2)
|
|
@@ -32,6 +33,7 @@ GEM
|
|
|
32
33
|
multi_json (1.17.0)
|
|
33
34
|
net-http (0.9.1)
|
|
34
35
|
uri (>= 0.11.1)
|
|
36
|
+
ostruct (0.6.3)
|
|
35
37
|
parallel (1.24.0)
|
|
36
38
|
parser (3.3.0.4)
|
|
37
39
|
ast (~> 2.4.1)
|
|
@@ -82,9 +84,11 @@ PLATFORMS
|
|
|
82
84
|
ruby
|
|
83
85
|
|
|
84
86
|
DEPENDENCIES
|
|
87
|
+
benchmark
|
|
85
88
|
bundler
|
|
86
89
|
flagsmith!
|
|
87
90
|
gem-release
|
|
91
|
+
ostruct
|
|
88
92
|
pry
|
|
89
93
|
pry-byebug
|
|
90
94
|
rake
|
data/Makefile
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
SDK_BENCHMARKS := spec/sdk-benchmarks
|
|
2
|
+
CODSPEED_VERSION := 5.6.0
|
|
3
|
+
CODSPEED_INSTALLER_SHA256 := bf1bf63661e203bcabc0c84fdef0337f8658fc8340c467a6a3bca81040d99f40
|
|
4
|
+
|
|
5
|
+
INSTRUMENT_HOOKS := benchmarks/codspeed/instrument-hooks
|
|
6
|
+
CODSPEED_EXT := tmp/codspeed/codspeed.$(shell ruby -e 'print RbConfig::CONFIG["DLEXT"]')
|
|
7
|
+
|
|
8
|
+
$(SDK_BENCHMARKS)/environments:
|
|
9
|
+
git submodule update --init -- $(SDK_BENCHMARKS)
|
|
10
|
+
|
|
11
|
+
$(INSTRUMENT_HOOKS)/dist/core.c:
|
|
12
|
+
git submodule update --init -- $(INSTRUMENT_HOOKS)
|
|
13
|
+
|
|
14
|
+
$(CODSPEED_EXT): benchmarks/codspeed/codspeed.c benchmarks/codspeed/extconf.rb $(INSTRUMENT_HOOKS)/dist/core.c
|
|
15
|
+
mkdir -p $(@D)
|
|
16
|
+
cd $(@D) && ruby $(CURDIR)/benchmarks/codspeed/extconf.rb && $(MAKE)
|
|
17
|
+
|
|
18
|
+
.PHONY: install-codspeed
|
|
19
|
+
install-codspeed: $(SDK_BENCHMARKS)/environments
|
|
20
|
+
bundle install
|
|
21
|
+
installer=$$(mktemp) && \
|
|
22
|
+
curl -fsSL -o "$$installer" "https://codspeed.io/v$(CODSPEED_VERSION)/install.sh" && \
|
|
23
|
+
echo "$(CODSPEED_INSTALLER_SHA256) $$installer" | sha256sum --check --strict && \
|
|
24
|
+
bash "$$installer" --quiet
|
|
25
|
+
|
|
26
|
+
.PHONY: benchmark
|
|
27
|
+
benchmark: install-codspeed $(CODSPEED_EXT)
|
|
28
|
+
SDK_BENCHMARKS=$(SDK_BENCHMARKS) codspeed run --mode simulation --config codspeed.yml
|
data/codspeed.yml
ADDED
|
@@ -2,11 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
require 'semantic'
|
|
4
4
|
require 'securerandom'
|
|
5
|
+
require 'set'
|
|
5
6
|
|
|
6
7
|
require_relative 'environments/models'
|
|
7
8
|
require_relative 'features/models'
|
|
8
9
|
require_relative 'features/constants'
|
|
9
10
|
require_relative 'identities/models'
|
|
11
|
+
require_relative 'lazy_flags'
|
|
10
12
|
require_relative 'organisations/models'
|
|
11
13
|
require_relative 'projects/models'
|
|
12
14
|
require_relative 'segments/evaluator'
|
|
@@ -16,7 +18,7 @@ require_relative 'mappers'
|
|
|
16
18
|
|
|
17
19
|
module Flagsmith
|
|
18
20
|
# Core evaluation logic for feature flags
|
|
19
|
-
module Engine
|
|
21
|
+
module Engine # rubocop:disable Metrics/ModuleLength
|
|
20
22
|
extend self
|
|
21
23
|
include Flagsmith::Engine::Utils::HashFunc
|
|
22
24
|
include Flagsmith::Engine::Features::TargetingReasons
|
|
@@ -27,9 +29,11 @@ module Flagsmith
|
|
|
27
29
|
# @param evaluation_context [Hash] The evaluation context
|
|
28
30
|
# @return [Hash] Evaluation result with flags and segments
|
|
29
31
|
def get_evaluation_result(evaluation_context)
|
|
30
|
-
|
|
31
|
-
|
|
32
|
+
lazy_flags = LazyFlags.new(get_enriched_context(evaluation_context))
|
|
33
|
+
evaluation_context = lazy_flags.context
|
|
34
|
+
segments, segment_overrides = evaluate_segments(evaluation_context, lazy_flags)
|
|
32
35
|
flags = evaluate_features(evaluation_context, segment_overrides)
|
|
36
|
+
lazy_flags.apply_to(flags) if lazy_flags.used?
|
|
33
37
|
{
|
|
34
38
|
flags: flags,
|
|
35
39
|
segments: segments
|
|
@@ -37,27 +41,30 @@ module Flagsmith
|
|
|
37
41
|
end
|
|
38
42
|
|
|
39
43
|
# Returns { segments: EvaluationResultSegments; segmentOverrides: Record<string, SegmentOverride>; }
|
|
40
|
-
def evaluate_segments(evaluation_context)
|
|
44
|
+
def evaluate_segments(evaluation_context, lazy_flags)
|
|
41
45
|
return [], {} if evaluation_context[:segments].nil?
|
|
42
46
|
|
|
43
|
-
|
|
47
|
+
matching_segments = evaluation_context[:segments].each_value.select do |segment|
|
|
48
|
+
cycle_hits = lazy_flags.cycle_hits
|
|
49
|
+
context_in_segment?(evaluation_context, segment) && lazy_flags.cycle_hits == cycle_hits
|
|
50
|
+
end
|
|
44
51
|
|
|
45
|
-
segments =
|
|
52
|
+
segments = matching_segments.map do |segment|
|
|
46
53
|
{ name: segment[:name], metadata: segment[:metadata] }.compact
|
|
47
54
|
end
|
|
48
55
|
|
|
49
|
-
segment_overrides = process_segment_overrides(
|
|
56
|
+
segment_overrides = process_segment_overrides(matching_segments)
|
|
50
57
|
|
|
51
58
|
[segments, segment_overrides]
|
|
52
59
|
end
|
|
53
60
|
|
|
54
61
|
# Returns Record<string: override.name, SegmentOverride>
|
|
55
|
-
def process_segment_overrides(
|
|
62
|
+
def process_segment_overrides(matching_segments) # rubocop:disable Metrics/MethodLength
|
|
56
63
|
segment_overrides = {}
|
|
57
64
|
|
|
58
|
-
|
|
65
|
+
matching_segments.each do |segment|
|
|
59
66
|
Array(segment[:overrides]).each do |override|
|
|
60
|
-
next unless
|
|
67
|
+
next unless wins_over?(override, segment_overrides[override[:name]]&.dig(:feature))
|
|
61
68
|
|
|
62
69
|
segment_overrides[override[:name]] = {
|
|
63
70
|
feature: override,
|
|
@@ -71,65 +78,94 @@ module Flagsmith
|
|
|
71
78
|
|
|
72
79
|
def evaluate_features(evaluation_context, segment_overrides)
|
|
73
80
|
identity_key = get_identity_key(evaluation_context)
|
|
81
|
+
targeted_feature_names = get_targeted_feature_names(evaluation_context)
|
|
74
82
|
|
|
75
83
|
(evaluation_context[:features] || {}).each_with_object({}) do |(_, feature), flags|
|
|
76
|
-
|
|
77
|
-
|
|
84
|
+
flags[feature[:name].to_sym] =
|
|
85
|
+
get_feature_flag_result(feature, segment_overrides[feature[:name]], identity_key, targeted_feature_names)
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# Returns the flag result of a feature, served by its segment override if any, or else by its environment default
|
|
90
|
+
def get_feature_flag_result(feature, segment_override, identity_key, targeted_feature_names)
|
|
91
|
+
return get_flag_result(segment_override[:feature], identity_key, targeting_match_reason(segment_override)) if segment_override
|
|
92
|
+
|
|
93
|
+
get_flag_result(feature, identity_key, fallback_reason(feature[:name], targeted_feature_names))
|
|
94
|
+
end
|
|
78
95
|
|
|
79
|
-
|
|
80
|
-
|
|
96
|
+
# Returns the names of the features overridden by at least one segment
|
|
97
|
+
def get_targeted_feature_names(evaluation_context)
|
|
98
|
+
names = {}
|
|
99
|
+
evaluation_context[:segments]&.each_value do |segment|
|
|
100
|
+
segment[:overrides]&.each { |override| names[override[:name]] = true }
|
|
81
101
|
end
|
|
102
|
+
names
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# Returns the reason for a flag served by a segment override
|
|
106
|
+
def targeting_match_reason(segment_override)
|
|
107
|
+
"#{TARGETING_REASON_TARGETING_MATCH}; segment=#{segment_override[:segment_name]}"
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# Returns the reason for a flag served by its environment default:
|
|
111
|
+
# DEFAULT if the feature has segment overrides that did not match, STATIC if it has none
|
|
112
|
+
def fallback_reason(feature_name, targeted_feature_names)
|
|
113
|
+
targeted_feature_names.key?(feature_name) ? TARGETING_REASON_DEFAULT : TARGETING_REASON_STATIC
|
|
82
114
|
end
|
|
83
115
|
|
|
84
|
-
# Returns {value: any; reason?: string}
|
|
116
|
+
# Returns {name: string; enabled: boolean; value: any; reason: string; variant?: string; metadata?: any}
|
|
117
|
+
def get_flag_result(feature, identity_key, reason)
|
|
118
|
+
evaluated = evaluate_feature_value(feature, identity_key)
|
|
119
|
+
|
|
120
|
+
flag_result = {
|
|
121
|
+
name: feature[:name],
|
|
122
|
+
enabled: feature[:enabled],
|
|
123
|
+
value: evaluated[:value],
|
|
124
|
+
reason: evaluated[:reason] || reason,
|
|
125
|
+
variant: evaluated[:variant]
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
flag_result[:metadata] = feature[:metadata] if feature[:metadata]
|
|
129
|
+
flag_result
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# Returns {value: any; reason?: string; variant?: string}
|
|
85
133
|
def evaluate_feature_value(feature, identity_key = nil)
|
|
86
134
|
return get_multivariate_feature_value(feature, identity_key) if feature[:variants]&.any? && identity_key
|
|
87
135
|
|
|
88
|
-
{ value: feature[:value], reason: nil }
|
|
136
|
+
{ value: feature[:value], reason: nil, variant: nil }
|
|
89
137
|
end
|
|
90
138
|
|
|
91
|
-
# Returns {value: any; reason?: string}
|
|
139
|
+
# Returns {value: any; reason?: string; variant: string}
|
|
92
140
|
def get_multivariate_feature_value(feature, identity_key)
|
|
93
141
|
percentage_value = hashed_percentage_for_object_ids([feature[:key], identity_key])
|
|
94
|
-
sorted_variants = (feature[:variants] || []).sort_by { |v| v[:priority] ||
|
|
142
|
+
sorted_variants = (feature[:variants] || []).sort_by { |v| v[:priority] || DEFAULT_PRIORITY }
|
|
95
143
|
|
|
96
144
|
variant = find_matching_variant(sorted_variants, percentage_value)
|
|
97
|
-
variant || { value: feature[:value], reason: nil }
|
|
145
|
+
variant || { value: feature[:value], reason: nil, variant: 'control' }
|
|
98
146
|
end
|
|
99
147
|
|
|
100
148
|
def find_matching_variant(sorted_variants, percentage_value)
|
|
101
149
|
start_percentage = 0
|
|
102
150
|
sorted_variants.each do |variant|
|
|
103
151
|
limit = start_percentage + variant[:weight]
|
|
104
|
-
|
|
152
|
+
if start_percentage <= percentage_value && percentage_value < limit
|
|
153
|
+
return { value: variant[:value], reason: "#{TARGETING_REASON_SPLIT}; weight=#{variant[:weight]}", variant: variant[:key] }
|
|
154
|
+
end
|
|
105
155
|
|
|
106
156
|
start_percentage = limit
|
|
107
157
|
end
|
|
108
158
|
nil
|
|
109
159
|
end
|
|
110
160
|
|
|
111
|
-
#
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
161
|
+
# Whether a segment override takes precedence over the one held so far.
|
|
162
|
+
# The lowest priority wins, and the first seen wins a tie.
|
|
163
|
+
def wins_over?(candidate, incumbent)
|
|
164
|
+
incumbent.nil? || (candidate[:priority] || DEFAULT_PRIORITY) < (incumbent[:priority] || DEFAULT_PRIORITY)
|
|
115
165
|
end
|
|
116
166
|
|
|
117
167
|
private
|
|
118
168
|
|
|
119
|
-
def build_flag_result(feature, identity_key, segment_override)
|
|
120
|
-
evaluated = evaluate_feature_value(feature, identity_key)
|
|
121
|
-
|
|
122
|
-
flag_result = {
|
|
123
|
-
name: feature[:name],
|
|
124
|
-
enabled: feature[:enabled],
|
|
125
|
-
value: evaluated[:value],
|
|
126
|
-
reason: evaluated[:reason] || (segment_override ? "#{TARGETING_REASON_TARGETING_MATCH}; segment=#{segment_override[:segment_name]}" : TARGETING_REASON_DEFAULT)
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
flag_result[:metadata] = feature[:metadata] if feature[:metadata]
|
|
130
|
-
flag_result
|
|
131
|
-
end
|
|
132
|
-
|
|
133
169
|
# Extract identity key from evaluation context
|
|
134
170
|
#
|
|
135
171
|
# @param evaluation_context [Hash] The evaluation context
|
|
@@ -139,9 +175,5 @@ module Flagsmith
|
|
|
139
175
|
|
|
140
176
|
evaluation_context[:identity][:key]
|
|
141
177
|
end
|
|
142
|
-
|
|
143
|
-
def stronger_priority?(priority_a, priority_b)
|
|
144
|
-
(priority_a || WEAKEST_PRIORITY) < (priority_b || WEAKEST_PRIORITY)
|
|
145
|
-
end
|
|
146
178
|
end
|
|
147
179
|
end
|
|
@@ -6,8 +6,10 @@ module Flagsmith
|
|
|
6
6
|
# Targeting reason constants for evaluation results
|
|
7
7
|
module TargetingReasons
|
|
8
8
|
TARGETING_REASON_DEFAULT = 'DEFAULT'
|
|
9
|
+
TARGETING_REASON_STATIC = 'STATIC'
|
|
9
10
|
TARGETING_REASON_TARGETING_MATCH = 'TARGETING_MATCH'
|
|
10
11
|
TARGETING_REASON_SPLIT = 'SPLIT'
|
|
12
|
+
TARGETING_REASON_CIRCULAR_DEPENDENCY = 'ERROR; code=CIRCULAR_DEPENDENCY'
|
|
11
13
|
end
|
|
12
14
|
end
|
|
13
15
|
end
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'set'
|
|
4
|
+
|
|
5
|
+
module Flagsmith
|
|
6
|
+
module Engine
|
|
7
|
+
# The `$.flags` of an evaluation context, resolving a flag the first time a segment condition reads it.
|
|
8
|
+
#
|
|
9
|
+
# Flags are only reachable by name: JSONPath reads them through `dig`.
|
|
10
|
+
class LazyFlags
|
|
11
|
+
# The evaluation context, with `$.flags` bound to this instance
|
|
12
|
+
attr_reader :context
|
|
13
|
+
|
|
14
|
+
# Incremented whenever a dependency cycle is cut. A segment whose evaluation increments it
|
|
15
|
+
# read a flag that could not be resolved, so it must not match.
|
|
16
|
+
attr_reader :cycle_hits
|
|
17
|
+
|
|
18
|
+
def initialize(context)
|
|
19
|
+
@context = context.merge(flags: self)
|
|
20
|
+
@cycle_hits = 0
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# @param feature_name [String]
|
|
24
|
+
# @return [Hash, nil] The flag result, or nil for a feature absent from the context or in a cycle
|
|
25
|
+
def dig(feature_name)
|
|
26
|
+
start unless used?
|
|
27
|
+
resolve(feature_name) unless @resolved.key?(feature_name)
|
|
28
|
+
@resolved[feature_name]
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Whether a segment condition read any flag
|
|
32
|
+
def used?
|
|
33
|
+
!@resolved.nil?
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Applies the flags resolved for segment conditions over the single-pass flag results
|
|
37
|
+
#
|
|
38
|
+
# @param flags [Hash{Symbol => Hash}] The flag results, keyed by feature name
|
|
39
|
+
def apply_to(flags)
|
|
40
|
+
@resolved.each_value { |flag| flags[flag[:name].to_sym] = flag }
|
|
41
|
+
@cyclic.each do |feature_name|
|
|
42
|
+
flag = flags[feature_name.to_sym]
|
|
43
|
+
flag[:reason] = Features::TargetingReasons::TARGETING_REASON_CIRCULAR_DEPENDENCY if flag
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
private
|
|
48
|
+
|
|
49
|
+
def start
|
|
50
|
+
@resolved = {}
|
|
51
|
+
@cyclic = Set.new
|
|
52
|
+
@resolving = []
|
|
53
|
+
@segment_matches = {}
|
|
54
|
+
@segment_keys_by_feature_name = {}
|
|
55
|
+
(@context[:segments] || {}).each do |segment_key, segment|
|
|
56
|
+
Array(segment[:overrides]).each do |override|
|
|
57
|
+
(@segment_keys_by_feature_name[override[:name]] ||= []) << segment_key
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def resolve(feature_name) # rubocop:disable Metrics/MethodLength
|
|
63
|
+
if @resolving.include?(feature_name)
|
|
64
|
+
@cyclic.merge(@resolving[@resolving.index(feature_name)..])
|
|
65
|
+
@cycle_hits += 1
|
|
66
|
+
return
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
feature = find_feature(feature_name)
|
|
70
|
+
return unless feature
|
|
71
|
+
|
|
72
|
+
@resolving.push(feature_name)
|
|
73
|
+
begin
|
|
74
|
+
segment_override = find_segment_override(feature_name)
|
|
75
|
+
ensure
|
|
76
|
+
@resolving.pop
|
|
77
|
+
end
|
|
78
|
+
return if @cyclic.include?(feature_name)
|
|
79
|
+
|
|
80
|
+
@resolved[feature_name] = Engine.get_feature_flag_result(
|
|
81
|
+
feature, segment_override, @context.dig(:identity, :key), @segment_keys_by_feature_name
|
|
82
|
+
)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def find_feature(feature_name)
|
|
86
|
+
features = @context[:features]
|
|
87
|
+
return nil unless features
|
|
88
|
+
|
|
89
|
+
features[feature_name] || features[feature_name.to_sym]
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def find_segment_override(feature_name)
|
|
93
|
+
segment_override = nil
|
|
94
|
+
@segment_keys_by_feature_name.fetch(feature_name, []).each do |segment_key|
|
|
95
|
+
next unless segment_match?(segment_key)
|
|
96
|
+
|
|
97
|
+
segment = @context[:segments][segment_key]
|
|
98
|
+
segment[:overrides].each do |override|
|
|
99
|
+
next unless override[:name] == feature_name && Engine.wins_over?(override, segment_override&.dig(:feature))
|
|
100
|
+
|
|
101
|
+
segment_override = { feature: override, segment_name: segment[:name] }
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
segment_override
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def segment_match?(segment_key)
|
|
108
|
+
return @segment_matches[segment_key] if @segment_matches.key?(segment_key)
|
|
109
|
+
|
|
110
|
+
cycle_hits = @cycle_hits
|
|
111
|
+
matches = Segments::Evaluator.context_in_segment?(@context, @context[:segments][segment_key])
|
|
112
|
+
return false if @cycle_hits != cycle_hits # Rests on a flag that could not be resolved
|
|
113
|
+
|
|
114
|
+
@segment_matches[segment_key] = matches
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
@@ -26,24 +26,8 @@ module Flagsmith
|
|
|
26
26
|
}
|
|
27
27
|
end
|
|
28
28
|
|
|
29
|
-
def self.build_overrides(feature_states)
|
|
30
|
-
feature_states.map
|
|
31
|
-
override_hash = {
|
|
32
|
-
key: feature_state.django_id&.to_s || feature_state.uuid,
|
|
33
|
-
name: feature_state.feature.name,
|
|
34
|
-
enabled: feature_state.enabled,
|
|
35
|
-
value: feature_state.get_value,
|
|
36
|
-
metadata: { id: feature_state.feature.id }
|
|
37
|
-
}
|
|
38
|
-
add_priority_to_override(override_hash, feature_state)
|
|
39
|
-
override_hash
|
|
40
|
-
end
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
def self.add_priority_to_override(override_hash, feature_state)
|
|
44
|
-
return unless feature_state.feature_segment&.priority
|
|
45
|
-
|
|
46
|
-
override_hash[:priority] = feature_state.feature_segment.priority
|
|
29
|
+
def self.build_overrides(feature_states)
|
|
30
|
+
feature_states.map { |feature_state| Environment.build_feature_hash(feature_state) }
|
|
47
31
|
end
|
|
48
32
|
|
|
49
33
|
def self.map_rule(rule)
|
|
@@ -11,6 +11,9 @@ module Flagsmith
|
|
|
11
11
|
STRONGEST_PRIORITY = Float::INFINITY
|
|
12
12
|
WEAKEST_PRIORITY = -Float::INFINITY
|
|
13
13
|
|
|
14
|
+
# Condition properties resolvable without an identity
|
|
15
|
+
IDENTITY_FREE_PROPERTY_PREFIXES = ['$.environment.', '$.environment[', '$.flags.', '$.flags['].freeze
|
|
16
|
+
|
|
14
17
|
# @param environment [Flagsmith::Engine::Environment] The environment model
|
|
15
18
|
# @param identity [Flagsmith::Engine::Identity, nil] Optional identity model
|
|
16
19
|
# @param override_traits [Array<Flagsmith::Engine::Identities::Trait>, nil] Optional override traits
|
|
@@ -37,6 +40,34 @@ module Flagsmith
|
|
|
37
40
|
context
|
|
38
41
|
end
|
|
39
42
|
|
|
43
|
+
# Maps environment model to an evaluation context for flags served without an identity
|
|
44
|
+
#
|
|
45
|
+
# @param environment [Flagsmith::Engine::Environment] The environment model
|
|
46
|
+
# @return [Hash] Context with :environment, :features, and the :segments not needing an identity
|
|
47
|
+
def self.get_environment_flags_evaluation_context(environment)
|
|
48
|
+
{
|
|
49
|
+
environment: Environment.build_environment_context(environment),
|
|
50
|
+
features: Environment.build_features_context(environment.feature_states),
|
|
51
|
+
segments: select_identity_free_segments(Segments.build_segments_context(environment.project.segments))
|
|
52
|
+
}
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Selects the segments that can be evaluated without an identity, i.e. reading neither identity nor traits
|
|
56
|
+
#
|
|
57
|
+
# @param segments [Hash] Segments context, keyed by segment key
|
|
58
|
+
# @return [Hash] Segments context without the segments needing an identity
|
|
59
|
+
def self.select_identity_free_segments(segments)
|
|
60
|
+
segments.select { |_, segment| identity_free_rules?(segment[:rules]) }
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def self.identity_free_rules?(rules)
|
|
64
|
+
Array(rules).all? do |rule|
|
|
65
|
+
Array(rule[:conditions]).all? { |condition| condition[:property].to_s.start_with?(*IDENTITY_FREE_PROPERTY_PREFIXES) } &&
|
|
66
|
+
identity_free_rules?(rule[:rules])
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
private_class_method :identity_free_rules?
|
|
70
|
+
|
|
40
71
|
# Maps identity model to identity context
|
|
41
72
|
#
|
|
42
73
|
# @param identity [Flagsmith::Engine::Identity] The identity model
|
|
@@ -11,6 +11,9 @@ module Flagsmith
|
|
|
11
11
|
|
|
12
12
|
RULE_TYPES = [ALL_RULE, ANY_RULE, NONE_RULE].freeze
|
|
13
13
|
|
|
14
|
+
# Segment override and variant priority: the lowest wins
|
|
15
|
+
DEFAULT_PRIORITY = Float::INFINITY
|
|
16
|
+
|
|
14
17
|
# Segment Condition Operators
|
|
15
18
|
EQUAL = 'EQUAL'
|
|
16
19
|
GREATER_THAN = 'GREATER_THAN'
|
|
@@ -14,6 +14,9 @@ module Flagsmith
|
|
|
14
14
|
include Flagsmith::Engine::Segments::Constants
|
|
15
15
|
include Flagsmith::Engine::Utils::HashFunc
|
|
16
16
|
|
|
17
|
+
# Compiled JSONPath queries, keyed by condition property
|
|
18
|
+
JSON_PATHS = Hash.new { |json_paths, json_path| json_paths[json_path] = JsonPath.new(json_path, use_symbols: true) }
|
|
19
|
+
|
|
17
20
|
module_function
|
|
18
21
|
|
|
19
22
|
def get_enriched_context(context)
|
|
@@ -36,12 +39,19 @@ module Flagsmith
|
|
|
36
39
|
def get_segments_from_context(context)
|
|
37
40
|
return [] unless context[:identity] && context[:segments]
|
|
38
41
|
|
|
39
|
-
context[:segments].values.select
|
|
40
|
-
|
|
42
|
+
context[:segments].values.select { |segment| context_in_segment?(context, segment) }
|
|
43
|
+
end
|
|
41
44
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
+
# Whether the context belongs to a segment, i.e. matches all of its rules
|
|
46
|
+
#
|
|
47
|
+
# @param context [Hash] The evaluation context
|
|
48
|
+
# @param segment [Hash] The segment context
|
|
49
|
+
# @return [Boolean] False for a segment without rules
|
|
50
|
+
def context_in_segment?(context, segment)
|
|
51
|
+
rules = segment[:rules]
|
|
52
|
+
return false if rules.nil? || rules.empty?
|
|
53
|
+
|
|
54
|
+
rules.all? { |rule| traits_match_segment_rule_from_context(rule, segment[:key], context) }
|
|
45
55
|
end
|
|
46
56
|
|
|
47
57
|
def traits_match_segment_condition(identity_traits, condition, segment_id, identity_id)
|
|
@@ -227,8 +237,7 @@ module Flagsmith
|
|
|
227
237
|
def get_context_value(json_path, context)
|
|
228
238
|
return nil unless context && json_path&.start_with?('$.')
|
|
229
239
|
|
|
230
|
-
|
|
231
|
-
results.first
|
|
240
|
+
JSON_PATHS[json_path].first(context)
|
|
232
241
|
rescue StandardError
|
|
233
242
|
nil
|
|
234
243
|
end
|
|
@@ -42,9 +42,9 @@ module Flagsmith
|
|
|
42
42
|
LESS_THAN => ->(other_value, self_value) { (other_value || false) && other_value < self_value },
|
|
43
43
|
LESS_THAN_INCLUSIVE => ->(other_value, self_value) { (other_value || false) && other_value <= self_value },
|
|
44
44
|
NOT_EQUAL => ->(other_value, self_value) { other_value != self_value },
|
|
45
|
-
CONTAINS => ->(other_value, self_value) { (
|
|
45
|
+
CONTAINS => ->(other_value, self_value) { other_value.is_a?(String) && other_value.include?(self_value) },
|
|
46
46
|
|
|
47
|
-
NOT_CONTAINS => ->(other_value, self_value) { (
|
|
47
|
+
NOT_CONTAINS => ->(other_value, self_value) { other_value.is_a?(String) && !other_value.include?(self_value) },
|
|
48
48
|
REGEX => ->(other_value, self_value) { (other_value || false) && other_value.to_s.match?(self_value) }
|
|
49
49
|
}.freeze
|
|
50
50
|
|
|
@@ -77,6 +77,8 @@ module Flagsmith
|
|
|
77
77
|
type_as_trait_value = format_to_type_of(trait_value)
|
|
78
78
|
formatted_value = type_as_trait_value ? type_as_trait_value.call(@value) : @value
|
|
79
79
|
MATCHING_FUNCTIONS[operator]&.call(trait_value, formatted_value)
|
|
80
|
+
rescue ArgumentError # Rule value not coercible to the trait value's type, e.g. 2 > '1.5'
|
|
81
|
+
false
|
|
80
82
|
end
|
|
81
83
|
|
|
82
84
|
TYPE_CONVERTERS = {
|
|
@@ -84,14 +86,8 @@ module Flagsmith
|
|
|
84
86
|
'Semantic::Version' => ->(v) { Semantic::Version.new(v.to_s.gsub(/:semver$/, '')) },
|
|
85
87
|
'TrueClass' => ->(v) { ['True', 'true', 'TRUE', true, 1, '1'].include?(v) },
|
|
86
88
|
'FalseClass' => ->(v) { !['False', 'false', 'FALSE', false].include?(v) },
|
|
87
|
-
'Integer' =>
|
|
88
|
-
|
|
89
|
-
i.to_s == v.to_s ? i : v
|
|
90
|
-
},
|
|
91
|
-
'Float' => lambda { |v|
|
|
92
|
-
f = v.to_f
|
|
93
|
-
f.to_s == v.to_s ? f : v
|
|
94
|
-
}
|
|
89
|
+
'Integer' => ->(v) { Integer(v.to_s, 10) },
|
|
90
|
+
'Float' => ->(v) { Float(v.to_s) }
|
|
95
91
|
}.freeze
|
|
96
92
|
|
|
97
93
|
def format_to_type_of(input)
|
data/lib/flagsmith/sdk/config.rb
CHANGED
|
@@ -5,12 +5,13 @@ module Flagsmith
|
|
|
5
5
|
class Config
|
|
6
6
|
DEFAULT_API_URL = 'https://edge.api.flagsmith.com/api/v1/'
|
|
7
7
|
DEFAULT_REALTIME_API_URL = 'https://realtime.flagsmith.com/'
|
|
8
|
+
DEFAULT_EVENTS_API_URL = 'https://events.api.flagsmith.com/'
|
|
8
9
|
|
|
9
10
|
OPTIONS = %i[
|
|
10
11
|
environment_key api_url custom_headers request_timeout_seconds enable_local_evaluation
|
|
11
12
|
environment_refresh_interval_seconds retries enable_analytics default_flag_handler
|
|
12
13
|
offline_mode offline_handler polling_manager_failure_limit
|
|
13
|
-
realtime_api_url enable_realtime_updates logger
|
|
14
|
+
realtime_api_url enable_realtime_updates logger enable_events events_api_url
|
|
14
15
|
].freeze
|
|
15
16
|
|
|
16
17
|
# Available Configs
|
|
@@ -47,6 +48,9 @@ module Flagsmith
|
|
|
47
48
|
# non-standard realtime endpoint.
|
|
48
49
|
# +enable_realtime_updates+ - A boolean to enable realtime updates.
|
|
49
50
|
# +logger+ - Pass your logger, default is Logger.new($stdout)
|
|
51
|
+
# +enable_events+ - if enabled, sends tracking and exposure events to the
|
|
52
|
+
# Flagsmith events API to power experiments
|
|
53
|
+
# +events_api_url+ - Override the URL of the Flagsmith events API
|
|
50
54
|
#
|
|
51
55
|
attr_reader(*OPTIONS)
|
|
52
56
|
|
|
@@ -72,6 +76,10 @@ module Flagsmith
|
|
|
72
76
|
@enable_realtime_updates
|
|
73
77
|
end
|
|
74
78
|
|
|
79
|
+
def enable_events?
|
|
80
|
+
@enable_events
|
|
81
|
+
end
|
|
82
|
+
|
|
75
83
|
def environment_flags_url
|
|
76
84
|
'flags/'
|
|
77
85
|
end
|
|
@@ -106,9 +114,15 @@ module Flagsmith
|
|
|
106
114
|
@realtime_api_url << '/' unless @realtime_api_url.end_with? '/'
|
|
107
115
|
@enable_realtime_updates = opts.fetch(:enable_realtime_updates, false)
|
|
108
116
|
@logger = options.fetch(:logger, Logger.new($stdout).tap { |l| l.level = :debug })
|
|
117
|
+
@enable_events = opts.fetch(:enable_events, false)
|
|
118
|
+
@events_api_url = with_trailing_slash(opts.fetch(:events_api_url, Flagsmith::Config::DEFAULT_EVENTS_API_URL))
|
|
109
119
|
end
|
|
110
120
|
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
|
|
111
121
|
|
|
122
|
+
def with_trailing_slash(url)
|
|
123
|
+
url.end_with?('/') ? url : "#{url}/"
|
|
124
|
+
end
|
|
125
|
+
|
|
112
126
|
class << self
|
|
113
127
|
def environment_key
|
|
114
128
|
ENV.fetch('FLAGSMITH_ENVIRONMENT_KEY', nil)
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'set'
|
|
4
|
+
|
|
5
|
+
module Flagsmith
|
|
6
|
+
# Started event processors, stopped together at exit. A stopped processor leaves, so it is not kept in memory.
|
|
7
|
+
module RunningEventProcessors
|
|
8
|
+
@processors = Set.new
|
|
9
|
+
@mutex = Mutex.new
|
|
10
|
+
at_exit { to_a.each(&:stop) }
|
|
11
|
+
|
|
12
|
+
def self.add(processor)
|
|
13
|
+
@mutex.synchronize { @processors.add(processor) }
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def self.delete(processor)
|
|
17
|
+
@mutex.synchronize { @processors.delete(processor) }
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def self.to_a
|
|
21
|
+
@mutex.synchronize { @processors.to_a }
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Buffers tracking and exposure events, and sends them to the Flagsmith events API in batches.
|
|
26
|
+
#
|
|
27
|
+
# Events are sent every +flush_interval_seconds+, as soon as +max_buffer_items+ are buffered,
|
|
28
|
+
# or on #flush. Sending events never raises into the application: failures are logged.
|
|
29
|
+
class EventProcessor # rubocop:disable Metrics/ClassLength
|
|
30
|
+
include Flagsmith::SDK::Intervals
|
|
31
|
+
|
|
32
|
+
FLAG_EXPOSURE_EVENT = '$flag_exposure'
|
|
33
|
+
ENDPOINT = 'v1/events'
|
|
34
|
+
FLUSH_INTERVAL_SECONDS = 10
|
|
35
|
+
MAX_BUFFER_ITEMS = 1000
|
|
36
|
+
REQUEST_TIMEOUT_SECONDS = 3
|
|
37
|
+
RETRIABLE_STATUSES = [500, 502, 503, 504].freeze
|
|
38
|
+
|
|
39
|
+
def initialize(config, flush_interval_seconds: FLUSH_INTERVAL_SECONDS, max_buffer_items: MAX_BUFFER_ITEMS)
|
|
40
|
+
@logger = config.logger
|
|
41
|
+
@flush_interval_seconds = flush_interval_seconds
|
|
42
|
+
@max_buffer_items = max_buffer_items
|
|
43
|
+
@connection = build_connection(config)
|
|
44
|
+
@events = []
|
|
45
|
+
@buffered_exposures = Set.new
|
|
46
|
+
@mutex = Mutex.new
|
|
47
|
+
@flush_mutex = Mutex.new
|
|
48
|
+
@flush_pending = false
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def track_event(event, identifier: nil, value: nil, traits: nil, metadata: nil)
|
|
52
|
+
buffer(payload(event, nil, identifier: identifier, value: value, traits: traits, metadata: metadata))
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Equal exposures within one flush interval add no information, so only the first is sent.
|
|
56
|
+
def track_exposure_event(feature_name, identifier:, value: nil, traits: nil, metadata: nil)
|
|
57
|
+
buffer(payload(FLAG_EXPOSURE_EVENT, feature_name,
|
|
58
|
+
identifier: identifier, value: value, traits: traits, metadata: metadata))
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def flush
|
|
62
|
+
drain
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def start
|
|
66
|
+
return if @interval
|
|
67
|
+
|
|
68
|
+
@interval = set_interval(@flush_interval_seconds) { flush }
|
|
69
|
+
RunningEventProcessors.add(self)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def stop
|
|
73
|
+
@flush_mutex.synchronize { clear_interval(@interval) } if @interval # never interrupts a batch being sent
|
|
74
|
+
@interval = nil
|
|
75
|
+
RunningEventProcessors.delete(self)
|
|
76
|
+
flush
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
private
|
|
80
|
+
|
|
81
|
+
# Only the flush scheduled by a full buffer clears +@flush_pending+, once it holds the buffer, so at most one
|
|
82
|
+
# such flush is waiting at a time.
|
|
83
|
+
def scheduled_flush
|
|
84
|
+
drain { @flush_pending = false }
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def drain
|
|
88
|
+
@flush_mutex.synchronize do
|
|
89
|
+
batch = @mutex.synchronize do
|
|
90
|
+
buffered = @events
|
|
91
|
+
@events = []
|
|
92
|
+
@buffered_exposures.clear
|
|
93
|
+
yield if block_given?
|
|
94
|
+
buffered
|
|
95
|
+
end
|
|
96
|
+
send_events(batch) unless batch.empty?
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def build_connection(config)
|
|
101
|
+
Faraday.new(url: config.events_api_url, headers: headers(config)) do |f|
|
|
102
|
+
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
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def headers(config)
|
|
110
|
+
config.custom_headers.merge(
|
|
111
|
+
'Content-Type' => 'application/json; charset=utf-8',
|
|
112
|
+
'X-Environment-Key' => config.environment_key,
|
|
113
|
+
'User-Agent' => Flagsmith::SDK::Utils.user_agent,
|
|
114
|
+
'Flagsmith-SDK-User-Agent' => Flagsmith::SDK::Utils.user_agent
|
|
115
|
+
)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def payload(event, feature_name, details)
|
|
119
|
+
{
|
|
120
|
+
event: event,
|
|
121
|
+
feature_name: feature_name,
|
|
122
|
+
identifier: details[:identifier],
|
|
123
|
+
value: details[:value]&.to_s,
|
|
124
|
+
traits: trait_values(details[:traits]),
|
|
125
|
+
metadata: (details[:metadata] || {}).transform_keys(&:to_s).merge('sdk_version' => Flagsmith::VERSION),
|
|
126
|
+
timestamp: (Time.now.to_f * 1000).to_i
|
|
127
|
+
}
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# Traits given as { value:, transient: } configurations are sent as their value.
|
|
131
|
+
def trait_values(traits)
|
|
132
|
+
return nil if traits.nil? || traits.empty?
|
|
133
|
+
|
|
134
|
+
traits.to_h { |key, value| [key.to_s, value.is_a?(Hash) ? value[:value] : value] }
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def buffer(event)
|
|
138
|
+
schedule_flush = @mutex.synchronize do
|
|
139
|
+
next false if duplicate_exposure?(event)
|
|
140
|
+
|
|
141
|
+
@events << event
|
|
142
|
+
next false if @flush_pending || @events.length < @max_buffer_items
|
|
143
|
+
|
|
144
|
+
@flush_pending = true
|
|
145
|
+
end
|
|
146
|
+
Thread.new { scheduled_flush } if schedule_flush
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def duplicate_exposure?(event)
|
|
150
|
+
return false unless event[:event] == FLAG_EXPOSURE_EVENT
|
|
151
|
+
return false if @buffered_exposures.add?(event.values_at(:feature_name, :identifier, :value))
|
|
152
|
+
|
|
153
|
+
@logger.debug "Skipping duplicate #{FLAG_EXPOSURE_EVENT} event for feature #{event[:feature_name]}"
|
|
154
|
+
true
|
|
155
|
+
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
|
+
end
|
|
163
|
+
end
|
|
@@ -30,15 +30,29 @@ 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
|
|
34
|
+
Experiment = Struct.new(:id, :name, :in_experiment, keyword_init: true) do
|
|
35
|
+
alias_method :in_experiment?, :in_experiment
|
|
36
|
+
|
|
37
|
+
def self.from_api(metadata)
|
|
38
|
+
experiment = metadata&.dig(:experiment)
|
|
39
|
+
return nil unless experiment
|
|
40
|
+
|
|
41
|
+
new(id: experiment[:id], name: experiment[:name], in_experiment: experiment[:in_experiment])
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
33
45
|
# 'live' Flag class as returned by API or local evaluation
|
|
34
46
|
class Flag < BaseFlag
|
|
35
|
-
attr_reader :feature_name, :feature_id, :reason
|
|
47
|
+
attr_reader :feature_name, :feature_id, :reason, :variant, :experiment
|
|
36
48
|
|
|
37
|
-
def initialize(feature_name:, enabled:, value:, feature_id:, reason: nil)
|
|
49
|
+
def initialize(feature_name:, enabled:, value:, feature_id:, reason: nil, variant: nil, experiment: nil) # rubocop:disable Metrics/ParameterLists
|
|
38
50
|
super(enabled: enabled, value: value, default: false)
|
|
39
51
|
@feature_name = feature_name
|
|
40
52
|
@feature_id = feature_id
|
|
41
53
|
@reason = reason
|
|
54
|
+
@variant = variant
|
|
55
|
+
@experiment = experiment
|
|
42
56
|
end
|
|
43
57
|
|
|
44
58
|
def <=>(other)
|
|
@@ -56,7 +70,9 @@ module Flagsmith
|
|
|
56
70
|
value: value,
|
|
57
71
|
enabled: enabled,
|
|
58
72
|
default: default,
|
|
59
|
-
reason: reason
|
|
73
|
+
reason: reason,
|
|
74
|
+
variant: variant,
|
|
75
|
+
experiment: experiment&.to_h
|
|
60
76
|
}
|
|
61
77
|
end
|
|
62
78
|
|
|
@@ -75,7 +91,9 @@ module Flagsmith
|
|
|
75
91
|
enabled: json_flag_data[:enabled],
|
|
76
92
|
value: json_flag_data.fetch(:feature_state_value) { json_flag_data[:value] },
|
|
77
93
|
feature_name: json_flag_data.dig(:feature, :name),
|
|
78
|
-
feature_id: json_flag_data.dig(:feature, :id)
|
|
94
|
+
feature_id: json_flag_data.dig(:feature, :id),
|
|
95
|
+
variant: json_flag_data[:variant],
|
|
96
|
+
experiment: Experiment.from_api(json_flag_data[:metadata])
|
|
79
97
|
)
|
|
80
98
|
end
|
|
81
99
|
end
|
data/lib/flagsmith/version.rb
CHANGED
data/lib/flagsmith.rb
CHANGED
|
@@ -14,6 +14,7 @@ require 'flagsmith/sdk/api_client'
|
|
|
14
14
|
require 'flagsmith/sdk/config'
|
|
15
15
|
require 'flagsmith/sdk/errors'
|
|
16
16
|
require 'flagsmith/sdk/intervals'
|
|
17
|
+
require 'flagsmith/sdk/event_processor'
|
|
17
18
|
require 'flagsmith/sdk/utils'
|
|
18
19
|
require 'flagsmith/sdk/pooling_manager'
|
|
19
20
|
require 'flagsmith/sdk/models/flags'
|
|
@@ -44,13 +45,18 @@ module Flagsmith
|
|
|
44
45
|
# feature_value_for_identity = identity_flags.get_feature_value('foo')
|
|
45
46
|
#
|
|
46
47
|
# identity_segments = flagsmith.get_identity_segments('identifier', {'foo': 'bar'})
|
|
48
|
+
#
|
|
49
|
+
# With enable_events: true, experiment flags record exposures, and events can be tracked:
|
|
50
|
+
#
|
|
51
|
+
# flag = flagsmith.get_experiment_flag('foo', 'identifier', plan: 'premium')
|
|
52
|
+
# flagsmith.track_event('purchase', identifier: 'identifier', value: 42)
|
|
47
53
|
|
|
48
54
|
# Available Configs.
|
|
49
55
|
#
|
|
50
56
|
# :environment_key, :api_url, :custom_headers, :request_timeout_seconds, :enable_local_evaluation,
|
|
51
57
|
# :environment_refresh_interval_seconds, :retries, :enable_analytics, :default_flag_handler,
|
|
52
58
|
# :offline_mode, :offline_handler, :polling_manager_failure_limit
|
|
53
|
-
# :realtime_api_url, :enable_realtime_updates, :logger
|
|
59
|
+
# :realtime_api_url, :enable_realtime_updates, :logger, :enable_events, :events_api_url
|
|
54
60
|
#
|
|
55
61
|
# You can see full description in the Flagsmith::Config
|
|
56
62
|
|
|
@@ -68,6 +74,7 @@ module Flagsmith
|
|
|
68
74
|
|
|
69
75
|
api_client
|
|
70
76
|
analytics_processor
|
|
77
|
+
event_processor
|
|
71
78
|
environment_data_polling_manager
|
|
72
79
|
load_offline_handler
|
|
73
80
|
end
|
|
@@ -77,6 +84,7 @@ module Flagsmith
|
|
|
77
84
|
raise Flagsmith::ClientError,
|
|
78
85
|
'The offline_mode config param requires a matching offline_handler.'
|
|
79
86
|
end
|
|
87
|
+
raise Flagsmith::ClientError, 'The enable_events config param cannot be used with offline_mode.' if @config.offline_mode? && @config.enable_events?
|
|
80
88
|
return unless @config.offline_handler && @config.default_flag_handler
|
|
81
89
|
|
|
82
90
|
raise Flagsmith::ClientError,
|
|
@@ -109,6 +117,12 @@ module Flagsmith
|
|
|
109
117
|
)
|
|
110
118
|
end
|
|
111
119
|
|
|
120
|
+
def event_processor
|
|
121
|
+
return nil unless @config.enable_events?
|
|
122
|
+
|
|
123
|
+
@event_processor ||= Flagsmith::EventProcessor.new(@config).tap(&:start)
|
|
124
|
+
end
|
|
125
|
+
|
|
112
126
|
def load_offline_handler
|
|
113
127
|
@environment = offline_handler.environment if offline_handler
|
|
114
128
|
end
|
|
@@ -205,6 +219,47 @@ module Flagsmith
|
|
|
205
219
|
flag.value
|
|
206
220
|
end
|
|
207
221
|
|
|
222
|
+
# Get a flag for an identity, taking the same traits as get_identity_flags, and record the
|
|
223
|
+
# identity's exposure to the variant it was served when it is in the feature's running experiment.
|
|
224
|
+
# Requires enable_events. Exposure needs remote evaluation: the environment document used by local
|
|
225
|
+
# evaluation and offline handlers carries no experiment data, so no exposure is recorded from it.
|
|
226
|
+
def get_experiment_flag(feature_name, identifier, **traits)
|
|
227
|
+
require_events!
|
|
228
|
+
flag = get_identity_flags(identifier, **traits)[feature_name]
|
|
229
|
+
skip_reason = exposure_skip_reason(flag)
|
|
230
|
+
if skip_reason
|
|
231
|
+
@config.logger.debug "Not sending #{Flagsmith::EventProcessor::FLAG_EXPOSURE_EVENT} for feature #{feature_name}: #{skip_reason}."
|
|
232
|
+
else
|
|
233
|
+
track_exposure_event(feature_name, identifier: identifier, value: flag.variant, traits: traits,
|
|
234
|
+
metadata: { experiment_id: flag.experiment.id })
|
|
235
|
+
end
|
|
236
|
+
flag
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
# Send an application event, e.g. a conversion, to Flagsmith. Requires enable_events.
|
|
240
|
+
def track_event(event, identifier: nil, value: nil, traits: nil, metadata: nil)
|
|
241
|
+
require_events!
|
|
242
|
+
if event.start_with?('$')
|
|
243
|
+
raise Flagsmith::ClientError,
|
|
244
|
+
"Event name \"#{event}\" uses the \"$\" prefix, which is reserved for events sent by the SDK."
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
event_processor.track_event(event, identifier: identifier, value: value, traits: traits, metadata: metadata)
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
# Record that an identity was shown a variant of a feature. Requires enable_events.
|
|
251
|
+
def track_exposure_event(feature_name, identifier:, value: nil, traits: nil, metadata: nil)
|
|
252
|
+
require_events!
|
|
253
|
+
if identifier.to_s.empty?
|
|
254
|
+
@config.logger.warn "Not sending #{Flagsmith::EventProcessor::FLAG_EXPOSURE_EVENT} for feature #{feature_name}: " \
|
|
255
|
+
'an exposure requires an identifier to reconcile with conversion events.'
|
|
256
|
+
return
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
event_processor.track_exposure_event(feature_name, identifier: identifier, value: value, traits: traits,
|
|
260
|
+
metadata: metadata)
|
|
261
|
+
end
|
|
262
|
+
|
|
208
263
|
def get_identity_segments(identifier, traits = {})
|
|
209
264
|
raise Flagsmith::ClientError, 'Local evaluation or offline handler is required to obtain identity segments.' unless environment
|
|
210
265
|
|
|
@@ -221,14 +276,26 @@ module Flagsmith
|
|
|
221
276
|
|
|
222
277
|
private
|
|
223
278
|
|
|
224
|
-
def
|
|
225
|
-
|
|
279
|
+
def require_events!
|
|
280
|
+
raise Flagsmith::ClientError, 'The enable_events config param is required to send events.' unless event_processor
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
def exposure_skip_reason(flag)
|
|
284
|
+
return 'feature not found' unless flag.is_a?(Flagsmith::Flags::Flag)
|
|
285
|
+
return 'feature is disabled' unless flag.enabled?
|
|
286
|
+
return 'flags come from the environment document, which carries no experiment data' if environment
|
|
287
|
+
return 'identity is not in a running experiment' unless flag.experiment&.in_experiment?
|
|
288
|
+
|
|
289
|
+
'flag has no variant' if flag.variant.nil?
|
|
290
|
+
end
|
|
226
291
|
|
|
227
|
-
|
|
292
|
+
def environment_flags_from_document # rubocop:disable Metrics/MethodLength
|
|
293
|
+
unless environment
|
|
228
294
|
raise Flagsmith::ClientError,
|
|
229
295
|
'Unable to get flags. No environment present.'
|
|
230
296
|
end
|
|
231
297
|
|
|
298
|
+
context = Flagsmith::Engine::Mappers.get_environment_flags_evaluation_context(environment)
|
|
232
299
|
evaluation_result = Flagsmith::Engine.get_evaluation_result(context)
|
|
233
300
|
|
|
234
301
|
Flagsmith::Flags::Collection.from_evaluation_result(
|
data/renovate.json
CHANGED
|
@@ -1,9 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
|
|
3
|
+
"gitIgnoredAuthors": ["216534828+flagsmith-engineering[bot]@users.noreply.github.com"],
|
|
3
4
|
"extends": ["config:recommended", ":semanticCommitScopeDisabled"],
|
|
4
5
|
"ignorePresets": [":semanticPrefixFixDepsChoreOthers"],
|
|
5
6
|
"semanticCommitType": "deps",
|
|
6
7
|
"git-submodules": { "enabled": true },
|
|
8
|
+
"customManagers": [
|
|
9
|
+
{
|
|
10
|
+
"customType": "regex",
|
|
11
|
+
"managerFilePatterns": ["/^\\.github/workflows/sdk-spec\\.yml$/"],
|
|
12
|
+
"matchStrings": ["spec-ref: (?<currentValue>v[\\d.]+)"],
|
|
13
|
+
"depNameTemplate": "Flagsmith/sdk-spec",
|
|
14
|
+
"datasourceTemplate": "github-tags"
|
|
15
|
+
}
|
|
16
|
+
],
|
|
7
17
|
"packageRules": [
|
|
8
18
|
{
|
|
9
19
|
"matchUpdateTypes": [
|
|
@@ -23,6 +33,12 @@
|
|
|
23
33
|
"matchManagers": ["git-submodules"],
|
|
24
34
|
"versioning": "semver-coerced",
|
|
25
35
|
"enabled": true
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
"matchPackageNames": ["Flagsmith/sdk-spec"],
|
|
39
|
+
"groupName": "SDK spec",
|
|
40
|
+
"enabled": true,
|
|
41
|
+
"addLabels": ["sdk-spec"]
|
|
26
42
|
}
|
|
27
43
|
]
|
|
28
44
|
}
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: flagsmith
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 5.0
|
|
4
|
+
version: 5.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Tom Stuart
|
|
@@ -12,6 +12,20 @@ bindir: exe
|
|
|
12
12
|
cert_chain: []
|
|
13
13
|
date: 2026-11-23 00:00:00.000000000 Z
|
|
14
14
|
dependencies:
|
|
15
|
+
- !ruby/object:Gem::Dependency
|
|
16
|
+
name: benchmark
|
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
|
18
|
+
requirements:
|
|
19
|
+
- - ">="
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '0'
|
|
22
|
+
type: :development
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
requirements:
|
|
26
|
+
- - ">="
|
|
27
|
+
- !ruby/object:Gem::Version
|
|
28
|
+
version: '0'
|
|
15
29
|
- !ruby/object:Gem::Dependency
|
|
16
30
|
name: bundler
|
|
17
31
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -40,6 +54,20 @@ dependencies:
|
|
|
40
54
|
- - ">="
|
|
41
55
|
- !ruby/object:Gem::Version
|
|
42
56
|
version: '0'
|
|
57
|
+
- !ruby/object:Gem::Dependency
|
|
58
|
+
name: ostruct
|
|
59
|
+
requirement: !ruby/object:Gem::Requirement
|
|
60
|
+
requirements:
|
|
61
|
+
- - ">="
|
|
62
|
+
- !ruby/object:Gem::Version
|
|
63
|
+
version: '0'
|
|
64
|
+
type: :development
|
|
65
|
+
prerelease: false
|
|
66
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
67
|
+
requirements:
|
|
68
|
+
- - ">="
|
|
69
|
+
- !ruby/object:Gem::Version
|
|
70
|
+
version: '0'
|
|
43
71
|
- !ruby/object:Gem::Dependency
|
|
44
72
|
name: pry
|
|
45
73
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -186,8 +214,10 @@ files:
|
|
|
186
214
|
- Gemfile
|
|
187
215
|
- Gemfile.lock
|
|
188
216
|
- LICENCE
|
|
217
|
+
- Makefile
|
|
189
218
|
- README.md
|
|
190
219
|
- Rakefile
|
|
220
|
+
- codspeed.yml
|
|
191
221
|
- lib/flagsmith.rb
|
|
192
222
|
- lib/flagsmith/engine/core.rb
|
|
193
223
|
- lib/flagsmith/engine/environments/models.rb
|
|
@@ -195,6 +225,7 @@ files:
|
|
|
195
225
|
- lib/flagsmith/engine/features/constants.rb
|
|
196
226
|
- lib/flagsmith/engine/features/models.rb
|
|
197
227
|
- lib/flagsmith/engine/identities/models.rb
|
|
228
|
+
- lib/flagsmith/engine/lazy_flags.rb
|
|
198
229
|
- lib/flagsmith/engine/mappers.rb
|
|
199
230
|
- lib/flagsmith/engine/mappers/environment.rb
|
|
200
231
|
- lib/flagsmith/engine/mappers/identity.rb
|
|
@@ -210,6 +241,7 @@ files:
|
|
|
210
241
|
- lib/flagsmith/sdk/api_client.rb
|
|
211
242
|
- lib/flagsmith/sdk/config.rb
|
|
212
243
|
- lib/flagsmith/sdk/errors.rb
|
|
244
|
+
- lib/flagsmith/sdk/event_processor.rb
|
|
213
245
|
- lib/flagsmith/sdk/intervals.rb
|
|
214
246
|
- lib/flagsmith/sdk/models/flags.rb
|
|
215
247
|
- lib/flagsmith/sdk/models/segments.rb
|