gitlab-experiment 2.0.0 → 2.1.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/README.md +53 -11
- data/lib/gitlab/experiment/base_interface.rb +5 -2
- data/lib/gitlab/experiment/cache/cookie_store.rb +31 -0
- data/lib/gitlab/experiment/cache/read_only_store.rb +30 -0
- data/lib/gitlab/experiment/cache/redis_hash_store.rb +2 -1
- data/lib/gitlab/experiment/cache.rb +61 -10
- data/lib/gitlab/experiment/configuration.rb +11 -0
- data/lib/gitlab/experiment/context.rb +4 -0
- data/lib/gitlab/experiment/cookies.rb +64 -6
- data/lib/gitlab/experiment/force_assignment.rb +1 -0
- data/lib/gitlab/experiment/middleware.rb +9 -3
- data/lib/gitlab/experiment/version.rb +1 -1
- data/lib/gitlab/experiment.rb +1 -0
- metadata +12 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d074b0e254265dba380cfd83e9b0164b3ffba946dc73f472faa6fe9ee8eac361
|
|
4
|
+
data.tar.gz: 3dd12eeb0041f2bb015c34f11706175e992b6cc57b010c3fc721e3b00d48a7f3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: fff2fbad2b7b1b929e02ea2244abfbf2cd4a91af65710b376e7f60d74605846a68f62732e78f0848a35cc6c07acaf6ef8accbdf0df79787dd51dcd9b9d2d31f4
|
|
7
|
+
data.tar.gz: c42aa0abc97b4447657ceeb8908b011c0ff552847672f7712fdc0a5ad01c1e7323a29a9b3627ba30fb08b1e93a0c32ac9fe26ea2ff307fff897d9c6c96abab8f
|
data/README.md
CHANGED
|
@@ -464,9 +464,9 @@ Share experiment assignments across multiple applications:
|
|
|
464
464
|
```ruby
|
|
465
465
|
# Shared Redis cache
|
|
466
466
|
Gitlab::Experiment.configure do |config|
|
|
467
|
+
redis = Redis.new(url: ENV['REDIS_URL'])
|
|
467
468
|
config.cache = Gitlab::Experiment::Cache::RedisHashStore.new(
|
|
468
|
-
|
|
469
|
-
expires_in: 30.days
|
|
469
|
+
pool: ->(&block) { block.call(redis) }
|
|
470
470
|
)
|
|
471
471
|
end
|
|
472
472
|
|
|
@@ -1014,9 +1014,12 @@ This is the primary use case for QA testing signup flows and landing pages. The
|
|
|
1014
1014
|
nil and the experiment uses a cookie-based context key.
|
|
1015
1015
|
|
|
1016
1016
|
1. Anonymous user visits `https://your-app.com/signup?glex_force=signup_cta:candidate`
|
|
1017
|
-
2. The forced variant `:candidate` is written to
|
|
1018
|
-
|
|
1019
|
-
|
|
1017
|
+
2. The forced variant `:candidate` is written to the signed variant cookie (see
|
|
1018
|
+
"Anonymous assignments" in [Advanced: Caching Configuration](#advanced-caching-configuration)), not to the cache
|
|
1019
|
+
store
|
|
1020
|
+
3. The user signs in -- the standard cookie migration carries the forced variant to their real identity in the cache
|
|
1021
|
+
store
|
|
1022
|
+
4. All future requests use `:candidate` from the cache store, permanently
|
|
1020
1023
|
|
|
1021
1024
|
This means a QA tester can force a variant before signup and have it follow the user through the entire
|
|
1022
1025
|
anonymous-to-authenticated journey.
|
|
@@ -1184,8 +1187,9 @@ You'll need to provide the `request` as an option to the experiment if it's outs
|
|
|
1184
1187
|
experiment(:example, actor: current_user, request: request)
|
|
1185
1188
|
```
|
|
1186
1189
|
|
|
1187
|
-
Note:
|
|
1188
|
-
|
|
1190
|
+
Note: Avoid passing the cookie value through yourself (e.g. `actor: request.cookie_jar.signed['example_id']`).
|
|
1191
|
+
That makes the anonymous id a provided actor, so its assignment is written to the cache store again, one entry per
|
|
1192
|
+
visitor. Pass the `request` instead, so the assignment stays in the variant cookie.
|
|
1189
1193
|
The cookie name is the full experiment name (including any configured prefix) with `_id` appended -- e.g.
|
|
1190
1194
|
`pill_color_id` for the `PillColorExperiment`.
|
|
1191
1195
|
|
|
@@ -1276,9 +1280,9 @@ Gitlab::Experiment.configure do |config|
|
|
|
1276
1280
|
end
|
|
1277
1281
|
|
|
1278
1282
|
# How experiments are cached (recommended: Redis)
|
|
1283
|
+
redis = Redis.new(url: ENV['REDIS_URL'])
|
|
1279
1284
|
config.cache = Gitlab::Experiment::Cache::RedisHashStore.new(
|
|
1280
|
-
|
|
1281
|
-
expires_in: 30.days
|
|
1285
|
+
pool: ->(&block) { block.call(redis) }
|
|
1282
1286
|
)
|
|
1283
1287
|
|
|
1284
1288
|
# Optional: Prefix all experiment names
|
|
@@ -1309,9 +1313,9 @@ end
|
|
|
1309
1313
|
|
|
1310
1314
|
# Option 2: Use Redis directly (recommended for scale)
|
|
1311
1315
|
Gitlab::Experiment.configure do |config|
|
|
1316
|
+
redis = Redis.new(url: ENV['REDIS_URL'])
|
|
1312
1317
|
config.cache = Gitlab::Experiment::Cache::RedisHashStore.new(
|
|
1313
|
-
|
|
1314
|
-
expires_in: 30.days
|
|
1318
|
+
pool: ->(&block) { block.call(redis) }
|
|
1315
1319
|
)
|
|
1316
1320
|
end
|
|
1317
1321
|
|
|
@@ -1338,6 +1342,38 @@ cache will be roughly `1 / candidate_percentage` the size it would have been bef
|
|
|
1338
1342
|
periodic cleanup using your store's deletion API -- for the `RedisHashStore`, that's
|
|
1339
1343
|
`experiment.cache.store.clear(key: experiment.name)`.
|
|
1340
1344
|
|
|
1345
|
+
**Anonymous assignments:**
|
|
1346
|
+
|
|
1347
|
+
When the actor comes from the experiment cookie (`actor: nil` in a request, see
|
|
1348
|
+
[Cookies and the actor keyword](#cookies-and-the-actor-keyword)), the assignment is kept in a second signed cookie
|
|
1349
|
+
instead of the configured cache store. Anonymous assignments are never read from or written to the configured store,
|
|
1350
|
+
so anonymous traffic can't grow it, however many requests arrive. (Experiment-wide attributes, such as the round robin
|
|
1351
|
+
counter, still live in the store.)
|
|
1352
|
+
|
|
1353
|
+
- The cookie is named after the actor cookie with a `_variant` suffix (`<name>_id_variant` by default), and uses the
|
|
1354
|
+
same `secure`, `domain`, and `httponly` settings.
|
|
1355
|
+
- It expires 30 days after the experiment last ran for the visitor: each run that finds the cookie writes it again,
|
|
1356
|
+
so the expiry restarts. Variant cookies for experiments a visitor no longer sees still expire, so they don't pile up
|
|
1357
|
+
in their requests. The next run after it expires resolves the variant again and writes a new cookie: deterministic
|
|
1358
|
+
rollouts (such as the default percent rollout) give the same variant unless the rollout changed in the meantime (for
|
|
1359
|
+
example, a higher percentage), while random, round robin, and forced variants may differ. A visitor who signs in
|
|
1360
|
+
after the cookie expires, without running the experiment again first, is resolved again under their signed-in key.
|
|
1361
|
+
Change the lifetime with `config.variant_cookie_expires_in = 90.days`, or set it to `nil` to make the cookie
|
|
1362
|
+
permanent like the actor cookie.
|
|
1363
|
+
- It holds one entry: the context key and the variant. If the context changes (for example `actor: nil, project: a`,
|
|
1364
|
+
then `project: b`), the entry is replaced and the rollout resolves the variant again.
|
|
1365
|
+
- When the actor signs in, the variant moves from the cookie to the signed-in key in the configured store, and both
|
|
1366
|
+
cookies are deleted.
|
|
1367
|
+
- Contexts with `sticky_to:` stay in the configured store, because every visitor shares that key.
|
|
1368
|
+
- If you configure `cookie_name` to return the same name for every experiment, those experiments also share one
|
|
1369
|
+
variant cookie and replace each other's entry, so anonymous assignments re-roll as visitors move between them. Keep
|
|
1370
|
+
the default per-experiment name.
|
|
1371
|
+
- Clearing an experiment's cache doesn't re-roll anonymous visitors, because their cookie still holds the assignment.
|
|
1372
|
+
- When upgrading to this version, assignments that anonymous visitors already have in the configured store are no
|
|
1373
|
+
longer read. Those visitors are resolved again: deterministic rollouts (such as the default percent rollout) give
|
|
1374
|
+
them the same variant, while random, round robin, and forced variants may differ. Code using `only_assigned: true`
|
|
1375
|
+
treats them as unassigned until an experiment run assigns them again.
|
|
1376
|
+
|
|
1341
1377
|
### Advanced: Custom Rollout Strategies
|
|
1342
1378
|
|
|
1343
1379
|
Build custom integrations with your existing infrastructure:
|
|
@@ -1431,6 +1467,12 @@ experiment_redirect_url(ex, url: 'https//gitlab.com/docs') # => "https://gitlab.
|
|
|
1431
1467
|
"#{Gitlab::Experiment::Configuration.mount_at}/#{ex.to_param}?https//docs.gitlab.com/"
|
|
1432
1468
|
```
|
|
1433
1469
|
|
|
1470
|
+
The middleware never writes to the cache. It reads the variant for the key in the URL (from the variant cookie for
|
|
1471
|
+
anonymous visitors, otherwise from the cache store). On a miss it resolves the variant for the `visited` event without
|
|
1472
|
+
storing it, so a crafted URL can't add cache entries. Generate redirect links from an experiment that has run, so its
|
|
1473
|
+
variant is already stored. Forced assignment (`glex_force`) never applies to redirects, and `visited` events honor the
|
|
1474
|
+
`DNT` request header like other tracking.
|
|
1475
|
+
|
|
1434
1476
|
## Testing (rspec support)
|
|
1435
1477
|
|
|
1436
1478
|
This gem comes with some rspec helpers and custom matchers.
|
|
@@ -38,10 +38,13 @@ module Gitlab
|
|
|
38
38
|
end
|
|
39
39
|
end
|
|
40
40
|
|
|
41
|
-
def from_param(id)
|
|
41
|
+
def from_param(id, request: nil)
|
|
42
42
|
%r{/?(?<name>.*):(?<key>.*)$} =~ id
|
|
43
43
|
name = CGI.unescape(name) if name
|
|
44
|
-
constantize(name).new(name).tap
|
|
44
|
+
constantize(name).new(name).tap do |e|
|
|
45
|
+
e.context.key(key)
|
|
46
|
+
e.context.reinitialize(request) if request
|
|
47
|
+
end
|
|
45
48
|
end
|
|
46
49
|
end
|
|
47
50
|
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gitlab
|
|
4
|
+
class Experiment
|
|
5
|
+
module Cache
|
|
6
|
+
class CookieStore
|
|
7
|
+
def initialize(context)
|
|
8
|
+
@context = context
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def read(key)
|
|
12
|
+
@context.read_cookie_assignment(key)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def write(key, value)
|
|
16
|
+
return false if value.blank?
|
|
17
|
+
|
|
18
|
+
@context.write_cookie_assignment(key, value)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def delete(key)
|
|
22
|
+
@context.delete_cookie_assignment if read(key)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def fetch(key)
|
|
26
|
+
(read(key) || (yield if block_given?)).tap { |value| write(key, value) }
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gitlab
|
|
4
|
+
class Experiment
|
|
5
|
+
module Cache
|
|
6
|
+
class ReadOnlyStore
|
|
7
|
+
def initialize(store, context)
|
|
8
|
+
@store = store
|
|
9
|
+
@context = context
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def read(key)
|
|
13
|
+
@context.read_cookie_assignment(key) || @store.read(key)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def write(_key, _value); end
|
|
17
|
+
|
|
18
|
+
def delete(_key); end
|
|
19
|
+
|
|
20
|
+
def increment(key, amount = 1)
|
|
21
|
+
@store.read(key).to_i + amount
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def fetch(key)
|
|
25
|
+
read(key) || (yield if block_given?)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -10,8 +10,9 @@
|
|
|
10
10
|
#
|
|
11
11
|
# Example configuration usage:
|
|
12
12
|
#
|
|
13
|
+
# redis = Redis.new(url: ENV['REDIS_URL'])
|
|
13
14
|
# config.cache = Gitlab::Experiment::Cache::RedisHashStore.new(
|
|
14
|
-
# pool: ->(&block) { block.call(
|
|
15
|
+
# pool: ->(&block) { block.call(redis) }
|
|
15
16
|
# )
|
|
16
17
|
#
|
|
17
18
|
module Gitlab
|
|
@@ -3,39 +3,70 @@
|
|
|
3
3
|
module Gitlab
|
|
4
4
|
class Experiment
|
|
5
5
|
module Cache
|
|
6
|
+
autoload :CookieStore, 'gitlab/experiment/cache/cookie_store.rb'
|
|
7
|
+
autoload :ReadOnlyStore, 'gitlab/experiment/cache/read_only_store.rb'
|
|
6
8
|
autoload :RedisHashStore, 'gitlab/experiment/cache/redis_hash_store.rb'
|
|
7
9
|
|
|
8
10
|
class Interface
|
|
9
|
-
attr_reader :store
|
|
11
|
+
attr_reader :store
|
|
10
12
|
|
|
11
13
|
def initialize(experiment, store)
|
|
12
14
|
@experiment = experiment
|
|
13
15
|
@store = store
|
|
14
|
-
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def key
|
|
19
|
+
@experiment.cache_key
|
|
15
20
|
end
|
|
16
21
|
|
|
17
22
|
def read
|
|
18
|
-
|
|
23
|
+
assignment_store.read(key)
|
|
19
24
|
end
|
|
20
25
|
|
|
21
26
|
def write(value = nil)
|
|
22
|
-
|
|
27
|
+
assignment_store.write(key, value || @experiment.assigned.name)
|
|
23
28
|
end
|
|
24
29
|
|
|
25
30
|
def delete
|
|
26
|
-
|
|
31
|
+
assignment_store.delete(key)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def fetch(&)
|
|
35
|
+
assignment_store.fetch(key, &)
|
|
27
36
|
end
|
|
28
37
|
|
|
29
38
|
def attr_get(name)
|
|
30
|
-
store.read(
|
|
39
|
+
store.read(attr_key(name))
|
|
31
40
|
end
|
|
32
41
|
|
|
33
42
|
def attr_set(name, value)
|
|
34
|
-
|
|
43
|
+
attr_store.write(attr_key(name), value)
|
|
35
44
|
end
|
|
36
45
|
|
|
37
46
|
def attr_inc(name, amount = 1)
|
|
38
|
-
|
|
47
|
+
attr_store.increment(attr_key(name), amount)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def assignment_store
|
|
51
|
+
return unless store
|
|
52
|
+
return read_only_store if @experiment.read_only?
|
|
53
|
+
return CookieStore.new(@experiment.context) if @experiment.context.assignment_in_cookie?
|
|
54
|
+
|
|
55
|
+
store
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
private
|
|
59
|
+
|
|
60
|
+
def attr_store
|
|
61
|
+
@experiment.read_only? ? read_only_store : store
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def read_only_store
|
|
65
|
+
ReadOnlyStore.new(store, @experiment.context)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def attr_key(name)
|
|
69
|
+
@experiment.cache_key(name, suffix: :attrs)
|
|
39
70
|
end
|
|
40
71
|
end
|
|
41
72
|
|
|
@@ -46,7 +77,7 @@ module Gitlab
|
|
|
46
77
|
def cache_variant(specified = nil, &)
|
|
47
78
|
return (specified.presence || yield) unless cache.store
|
|
48
79
|
|
|
49
|
-
result = migrated_cache_fetch(cache.
|
|
80
|
+
result = migrated_cache_fetch(cache.assignment_store) || find_variant(&)
|
|
50
81
|
return result unless specified.present?
|
|
51
82
|
|
|
52
83
|
cache.write(specified) if result.to_s != specified.to_s
|
|
@@ -54,7 +85,16 @@ module Gitlab
|
|
|
54
85
|
end
|
|
55
86
|
|
|
56
87
|
def find_variant(&)
|
|
57
|
-
cache.
|
|
88
|
+
cache.fetch(&)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def read_only!
|
|
92
|
+
@_read_only = true
|
|
93
|
+
self
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def read_only?
|
|
97
|
+
@_read_only == true
|
|
58
98
|
end
|
|
59
99
|
|
|
60
100
|
def cache_key(key = nil, suffix: nil)
|
|
@@ -65,6 +105,12 @@ module Gitlab
|
|
|
65
105
|
|
|
66
106
|
def migrated_cache_fetch(store)
|
|
67
107
|
migrations = context.signature[:migration_keys]&.map { |key| cache_key(key) } || []
|
|
108
|
+
cookie_value = migrated_cookie_value(migrations)
|
|
109
|
+
if cookie_value
|
|
110
|
+
store.write(cache_key, cookie_value)
|
|
111
|
+
return cookie_value
|
|
112
|
+
end
|
|
113
|
+
|
|
68
114
|
migrations.find do |old_key|
|
|
69
115
|
value = store.read(old_key)
|
|
70
116
|
|
|
@@ -75,6 +121,11 @@ module Gitlab
|
|
|
75
121
|
break value
|
|
76
122
|
end
|
|
77
123
|
end
|
|
124
|
+
|
|
125
|
+
def migrated_cookie_value(migrations)
|
|
126
|
+
assignment = context.migrated_cookie_assignment
|
|
127
|
+
assignment['v'] if assignment && migrations.include?(cache_key(assignment['k']))
|
|
128
|
+
end
|
|
78
129
|
end
|
|
79
130
|
end
|
|
80
131
|
end
|
|
@@ -62,6 +62,16 @@ module Gitlab
|
|
|
62
62
|
# config.secure_cookie = Rails.env.production?
|
|
63
63
|
@secure_cookie = true
|
|
64
64
|
|
|
65
|
+
# How long the anonymous variant cookie (`<cookie_name>_variant`) lasts.
|
|
66
|
+
#
|
|
67
|
+
# Each run that finds the cookie writes it again, so it expires this long
|
|
68
|
+
# after the experiment last ran for the visitor. When it expires, the next
|
|
69
|
+
# run resolves the variant again and writes a new cookie. Deterministic
|
|
70
|
+
# rollouts (such as the default percent rollout) give the same variant
|
|
71
|
+
# unless the rollout changed; random, round robin, and forced variants may
|
|
72
|
+
# differ. Set to nil to make the cookie permanent.
|
|
73
|
+
@variant_cookie_expires_in = 30.days
|
|
74
|
+
|
|
65
75
|
# The default rollout strategy.
|
|
66
76
|
#
|
|
67
77
|
# The recommended default rollout strategy when not using caching would
|
|
@@ -196,6 +206,7 @@ module Gitlab
|
|
|
196
206
|
:cookie_domain,
|
|
197
207
|
:cookie_name,
|
|
198
208
|
:secure_cookie,
|
|
209
|
+
:variant_cookie_expires_in,
|
|
199
210
|
:allow_forced_assignment,
|
|
200
211
|
:context_key_secret,
|
|
201
212
|
:context_key_bit_length,
|
|
@@ -3,9 +3,33 @@
|
|
|
3
3
|
module Gitlab
|
|
4
4
|
class Experiment
|
|
5
5
|
module Cookies
|
|
6
|
+
attr_reader :migrated_cookie_assignment
|
|
7
|
+
|
|
8
|
+
def anonymous_actor?
|
|
9
|
+
@anonymous_actor == true
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def read_cookie_assignment(cache_key)
|
|
13
|
+
assignment = cookie_assignment(cookie_jar&.signed&.[](variant_cookie_name))
|
|
14
|
+
assignment['v'] if assignment && assignment['k'] == cookie_key(cache_key)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def write_cookie_assignment(cache_key, variant)
|
|
18
|
+
write_cookie(
|
|
19
|
+
variant_cookie_name,
|
|
20
|
+
{ 'k' => cookie_key(cache_key), 'v' => variant.to_s },
|
|
21
|
+
expires: Configuration.variant_cookie_expires_in
|
|
22
|
+
)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def delete_cookie_assignment
|
|
26
|
+
delete_cookie(variant_cookie_name)
|
|
27
|
+
end
|
|
28
|
+
|
|
6
29
|
private
|
|
7
30
|
|
|
8
31
|
def migrate_cookie(hash, cookie_name)
|
|
32
|
+
@cookie_name = cookie_name
|
|
9
33
|
return hash if cookie_jar.nil?
|
|
10
34
|
|
|
11
35
|
resolver = [hash, :actor, cookie_name, cookie_jar.signed[cookie_name]]
|
|
@@ -19,10 +43,13 @@ module Gitlab
|
|
|
19
43
|
def resolve_cookie(hash, key, cookie_name, cookie)
|
|
20
44
|
return if cookie.to_s.empty? && hash[key].nil?
|
|
21
45
|
return hash if cookie.to_s.empty?
|
|
22
|
-
return hash.merge(key => cookie) if hash[key].nil?
|
|
46
|
+
return anonymous(hash.merge(key => cookie)) if hash[key].nil?
|
|
23
47
|
|
|
24
48
|
add_unmerged_migration(key => cookie)
|
|
25
|
-
|
|
49
|
+
@anonymous_actor = false
|
|
50
|
+
@migrated_cookie_assignment = cookie_assignment(cookie_jar.signed[variant_cookie_name])
|
|
51
|
+
delete_cookie(cookie_name)
|
|
52
|
+
delete_cookie_assignment
|
|
26
53
|
|
|
27
54
|
hash
|
|
28
55
|
end
|
|
@@ -31,11 +58,42 @@ module Gitlab
|
|
|
31
58
|
return hash unless hash.key?(key)
|
|
32
59
|
|
|
33
60
|
cookie ||= SecureRandom.uuid
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
61
|
+
write_cookie(cookie_name, cookie)
|
|
62
|
+
|
|
63
|
+
anonymous(hash.merge(key => cookie))
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def anonymous(hash)
|
|
67
|
+
@anonymous_actor = true
|
|
68
|
+
hash
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def cookie_assignment(value)
|
|
72
|
+
value if value.is_a?(Hash) && value.key?('k') && value.key?('v')
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def write_cookie(name, value, expires: nil)
|
|
76
|
+
if expires
|
|
77
|
+
cookie_jar.signed[name] = cookie_options.merge(value:, expires:)
|
|
78
|
+
else
|
|
79
|
+
cookie_jar.permanent.signed[name] = cookie_options.merge(value:)
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def delete_cookie(name)
|
|
84
|
+
cookie_jar.delete(name, domain:)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def cookie_options
|
|
88
|
+
{ secure: Configuration.secure_cookie, domain:, httponly: true }
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def variant_cookie_name
|
|
92
|
+
"#{@cookie_name}_variant"
|
|
93
|
+
end
|
|
37
94
|
|
|
38
|
-
|
|
95
|
+
def cookie_key(cache_key)
|
|
96
|
+
cache_key.delete_prefix("#{@experiment.name}:")
|
|
39
97
|
end
|
|
40
98
|
|
|
41
99
|
def domain
|
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
module Gitlab
|
|
4
4
|
class Experiment
|
|
5
5
|
class Middleware
|
|
6
|
-
def self.redirect(id, url)
|
|
6
|
+
def self.redirect(id, url, request: nil)
|
|
7
7
|
raise Error, 'no url to redirect to' if url.blank?
|
|
8
8
|
|
|
9
|
-
experiment = Gitlab::Experiment.from_param(id)
|
|
9
|
+
experiment = Gitlab::Experiment.from_param(id, request:).read_only!
|
|
10
10
|
[303, { 'Location' => experiment.process_redirect_url(url) || raise(Error, 'not redirecting') }, []]
|
|
11
11
|
end
|
|
12
12
|
|
|
@@ -18,10 +18,16 @@ module Gitlab
|
|
|
18
18
|
def call(env)
|
|
19
19
|
return @app.call(env) if env['REQUEST_METHOD'] != 'GET' || (match = @matcher.match(env['PATH_INFO'])).nil?
|
|
20
20
|
|
|
21
|
-
Middleware.redirect(match[:id], env['QUERY_STRING'])
|
|
21
|
+
Middleware.redirect(match[:id], env['QUERY_STRING'], request: build_request(env))
|
|
22
22
|
rescue Error
|
|
23
23
|
@app.call(env)
|
|
24
24
|
end
|
|
25
|
+
|
|
26
|
+
private
|
|
27
|
+
|
|
28
|
+
def build_request(env)
|
|
29
|
+
ActionDispatch::Request.new(env) if defined?(ActionDispatch::Request)
|
|
30
|
+
end
|
|
25
31
|
end
|
|
26
32
|
end
|
|
27
33
|
end
|
data/lib/gitlab/experiment.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: gitlab-experiment
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.
|
|
4
|
+
version: 2.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- GitLab
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-11-25 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activesupport
|
|
@@ -44,14 +44,20 @@ dependencies:
|
|
|
44
44
|
requirements:
|
|
45
45
|
- - "~>"
|
|
46
46
|
- !ruby/object:Gem::Version
|
|
47
|
-
version: 4.
|
|
47
|
+
version: '4.14'
|
|
48
|
+
- - ">="
|
|
49
|
+
- !ruby/object:Gem::Version
|
|
50
|
+
version: 4.14.1
|
|
48
51
|
type: :development
|
|
49
52
|
prerelease: false
|
|
50
53
|
version_requirements: !ruby/object:Gem::Requirement
|
|
51
54
|
requirements:
|
|
52
55
|
- - "~>"
|
|
53
56
|
- !ruby/object:Gem::Version
|
|
54
|
-
version: 4.
|
|
57
|
+
version: '4.14'
|
|
58
|
+
- - ">="
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: 4.14.1
|
|
55
61
|
- !ruby/object:Gem::Dependency
|
|
56
62
|
name: gitlab-styles
|
|
57
63
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -215,6 +221,8 @@ files:
|
|
|
215
221
|
- lib/gitlab/experiment.rb
|
|
216
222
|
- lib/gitlab/experiment/base_interface.rb
|
|
217
223
|
- lib/gitlab/experiment/cache.rb
|
|
224
|
+
- lib/gitlab/experiment/cache/cookie_store.rb
|
|
225
|
+
- lib/gitlab/experiment/cache/read_only_store.rb
|
|
218
226
|
- lib/gitlab/experiment/cache/redis_hash_store.rb
|
|
219
227
|
- lib/gitlab/experiment/callbacks.rb
|
|
220
228
|
- lib/gitlab/experiment/configuration.rb
|