gitlab-experiment 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fae9519317810c50decdff810249899d5c1855881544a20a31a2219cf9bda715
4
+ data.tar.gz: 19987d9c24486e99424aa6cfcade34d3123dbe1fc4a6afb8dc6c4c6ad4f5a99b
5
+ SHA512:
6
+ metadata.gz: afe30984c7b7f96bb4ad1122c5a4f5ff48a45f8c7cfca5b1d2d04368ba3eef25c84776e3768588cde6cd717ee918b25e8158746ea9ea3e9bb20fe9ef39d20377
7
+ data.tar.gz: 2492d43cbc76cfa20c1b785033fdc635ca75874d1052270c950a86acff17d04011c406bf1879bc5f21b06fb604c5ed259c149aecbea33efe2adae59ab12f6164
@@ -0,0 +1,25 @@
1
+ Copyright (c) 2020-2022 GitLab B.V.
2
+
3
+ With regard to the GitLab Software:
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
22
+
23
+ For all third party components incorporated into the GitLab Software, those
24
+ components are licensed under the original license provided by the owner of the
25
+ applicable component.
@@ -0,0 +1,182 @@
1
+ # Experiment Guide
2
+
3
+ Experiments can be conducted by any GitLab team, but are most often conducted by the teams from the [Growth Sub-department](https://about.gitlab.com/handbook/engineering/development/growth/).
4
+
5
+ Experiments are run as A/B/n tests and are evaluated by the data the experiment generates. The team reviews the data, determines the variant that performed most effectively, and promotes that variant as the new default code path (or reverts back to the control). In all instances, an experiment will be cleaned up after it has generated enough data to determine performance and be considered resolved.
6
+
7
+ ## Process and tracking issue
8
+
9
+ Each experiment should have a related [Experiment tracking issue](https://gitlab.com/gitlab-org/gitlab/-/issues/new?issuable_template=Experiment%20tracking) created, which is intended to track the experiment from deployment and rollout through to resolution and cleanup.
10
+
11
+ At the time an experiment is rolled out, the due date of the tracking issue should be specified. The timeline depends on the experiment and can be up to several weeks in the future.
12
+
13
+ With experiment resolution, the outcome of the experiment should be posted to the tracking issue with the reasoning for the decision. Any and all non-relevant experiment code should be removed and review concerns should be addressed during cleanup. After cleanup, the tracking issue can be closed.
14
+
15
+ ## Implementing an experiment
16
+
17
+ For the sake of our example, let's say we want to run an experiment for how to cancel a subscription. In our control (current world) we show a toggle that reads "Auto-renew", and in our experiment candidate we want to show a "Cancel subscription" button with a confirmation. Ultimately the behavior is the same, but the interface will be considerably different.
18
+
19
+ ### Defining the feature flag
20
+
21
+ Let's name our experiment `subscription_cancellation`. It's important to understand how this name is prefixed as `growth_experiment_subscription_cancellation` in our Unleash feature detection and in our Snowplow tracking calls. We prefix our experiments so we can consistently identify them as experiments and clean them up over time.
22
+
23
+ This means that you'll need to go to the [Feature Flags interface](https://gitlab.com/gitlab-org/gitlab/-/feature_flags) interface (for the project you're working on) and add a `growth_experiment_subscription_cancellation` feature flag and define which environment(s) it should be rolled out on for initial deployment, for instance, `gitlab-staging` or `customers-staging`.
24
+
25
+ You can include yourself and others in a list and assign that list using the `User List` rollout strategy, or you can add your user id to the experiment manually. You can use this to include or exclude yourself from an experiment for verification before rolling it out to others.
26
+
27
+ ### Implementation
28
+
29
+ When you implement an experiment in code you'll need to provide the name that you've given it in the feature flags interface, and a context -- which usually will include something like a user / user id, but may also include several other aspects.
30
+
31
+ ```ruby
32
+ class SubscriptionsController < ApplicationController
33
+ include Gitlab::GrowthExperiment::Interface
34
+
35
+ def show
36
+ experiment(:subscription_cancellation, user_id: user.id) do |e|
37
+ e.use { render_toggle_button } # control
38
+ e.try { render_cancel_button } # candidate
39
+ end
40
+ end
41
+ end
42
+ ```
43
+
44
+ You can also provide different variants for the experience if you've defined variants in the Feature Flag interface (not yet available).
45
+
46
+ ```ruby
47
+ experiment(:subscription_cancellation, user_id: user.id) do |e|
48
+ e.use { render_toggle_button } # control
49
+ e.try(:variant_one) { render_cancel_button(confirmation: true) }
50
+ e.try(:variant_two) { render_cancel_button(confirmation: false) }
51
+ end
52
+ ```
53
+
54
+ Later, and elsewhere in code you can use the same `experiment` call to track events on the experiment. The important detail here is to use the same context between your calls to `experiment`. If the context is the same, we're able to consistently track the event in a way that associates it to which variant is being presented -- which may be based on the user we're presenting it to.
55
+
56
+ ```ruby
57
+ exp = experiment(:subscription_cancellation, user_id: user.id)
58
+ exp.track('clicked_button')
59
+ ```
60
+
61
+ <details>
62
+ <summary>You can use the more low level class or instance interfaces...</summary>
63
+
64
+ ### Class level interface using `.run`
65
+
66
+ ```ruby
67
+ exp = Gitlab::GrowthExperiment.run(:subscription_cancellation, user_id: user.id) do |e|
68
+ # context can be passed to `experiment`, `.run`, `new`, or after the fact like here.
69
+ # context must be added before `#run` or `#track` calls.
70
+ e.context(project_id: project.id)
71
+
72
+ e.use { toggle_button_interface } # control
73
+ e.try { cancel_button_interface } # candidate
74
+ end
75
+
76
+ # track an event on the experiment we've defined.
77
+ exp.track(:clicked_button)
78
+ ```
79
+
80
+ While `Gitlab::GrowthExperiment.run` is what we document, you can also use `Gitlab::GrowthExperiment.experiment`.
81
+
82
+ ### Instance level interface
83
+
84
+ ```ruby
85
+ exp = Gitlab::GrowthExperiment.new(:subscription_cancellation, user_id: user.id)
86
+ # context can be passed to `.new`, or after the fact like here.
87
+ # context must be added before `#run` or `#track` calls.
88
+ exp.context(project_id: project.id)
89
+ exp.use { toggle_button_interface } # control
90
+ exp.try { cancel_button_interface } # candidate
91
+ exp.run
92
+
93
+ # track an event on the experiment we've defined.
94
+ exp.track(:clicked_button)
95
+ ```
96
+
97
+ </details>
98
+
99
+ <details>
100
+ <summary>You can define use custom classes...</summary>
101
+
102
+ ### Custom class
103
+
104
+ ```ruby
105
+ class CancellationExperiment < Gitlab::GrowthExperiment
106
+ def initialize(variant_name = nil, **context, &block)
107
+ super(:subscription_cancellation, variant_name, **context, &block)
108
+ end
109
+ end
110
+
111
+ exp = CancellationExperiment.run(user_id: user.id) do |e|
112
+ # context can be passed to `.run`, or after the fact like here.
113
+ # context must be added before `#run` or `#track` calls.
114
+ e.context(project_id: project.id)
115
+
116
+ e.use { toggle_button_interface } # control
117
+ e.try { cancel_button_interface } # candidate
118
+ end
119
+
120
+ # track an event on the experiment we've defined.
121
+ exp.track(:clicked_button)
122
+ ```
123
+
124
+ </details>
125
+
126
+ <details>
127
+ <summary>You can hard specify the variant to use...</summary>
128
+
129
+ ### Specifying which variant to use
130
+
131
+ This should generally be discouraged, as it can change the experience users have during rollout, and may confuse generating reports from the tracking calls. It is possible however, and may be useful if you understand the implications.
132
+
133
+ ```ruby
134
+ experiment(:subscription_cancellation, :no_interface, user_id: user.id) do |e|
135
+ e.use { toggle_button_interface } # control
136
+ e.try { cancel_button_interface } # candidate
137
+ e.try(:no_interface) { no_interface! } # variant
138
+ end
139
+ ```
140
+
141
+ Or you can set the variant within the block.
142
+
143
+ ```ruby
144
+ experiment(:subscription_cancellation, user_id: user.id) do |e|
145
+ e.variant(:variant) # set the variant
146
+ # ...
147
+ end
148
+ ```
149
+
150
+ </details>
151
+
152
+ The `experiment` method, and the underlying `Gitlab::GrowthExperiment` is an implementation on top of [Scientist](https://github.com/github/scientist). Generally speaking you can use the DSL that Scientist defines, but for experiments we use `experiment` instead of `science`, and specify the variant on initialization (or via `#variant` and not in the call to `#run`. The interface is otherwise the same, even though not every aspect of Scientist makes sense for experiments.
153
+
154
+ ### Context migrations
155
+
156
+ There are times when we may need to add new values or change something that we're providing in context while an experiment is running. We make this possible by passing the `migrated_from` context key.
157
+
158
+ Take for instance, that you might be using `version: 1` in your context. If you want to migrate this to `version: 2`, you just need to provide the context that you provided prior, in a `migrated_from` context key. In doing this, a given experiment experience can be resolved back through any number of migrations.
159
+
160
+ ```ruby
161
+ experiment(:my_experiment, version: 2, migrated_from: { version: 1 })
162
+ ```
163
+
164
+ It's important to understand that this can bucket a user in a new experience (depending on the rollout strategy being used and what is changing in the context), so you should investigate how this might impact your experiment before using it.
165
+
166
+ ### When there isn't a user
167
+
168
+ When there isn't a user, we typically have to fall back to another concept to provide a consistent experiment experience. What this means, is that once we bucket someone in a certain bucket, we always bucket them in that bucket.
169
+
170
+ We do this by using cookies.... [document more]
171
+
172
+ ## Tracking, anonymity and GDPR
173
+
174
+ We intentionally don't, and shouldn't, track things like user ids. What we can and do track is what we consider an "experiment experience" key. This key is generated by the context we pass to the experiment implementation. If we consistently pass the same context to an experiment, we're able to consistently track events generated in that experience. A context can contain things like user, or project -- so, if you only included a user in the context that user would get the same experience across all projects they view, but if you include the currently viewed project in the context the user would potentially have a different experience on each of their projects. Each can be desirable given the objectives of the experiment.
175
+
176
+ ## Code quality expectations
177
+
178
+ Since experimental code is inherently short lived, an intentionally stated goal is to iterate quickly to generate and evaluate performance data.
179
+
180
+ This goal prioritizes iteration and resolution over code quality, which means that experiment code may not always meet our code standards guidelines, but should also not negatively impact the availability of GitLab nor contribute to bad data. Even though experiments will be deployed to a minority of users, we still expect a flawless experience for those users, therefore, good test coverage is still required.
181
+
182
+ Reviewers and maintainers are encouraged to note when code doesn't meet our code standards guidelines. Please mention your concerns and include or link to them on the experiment tracking issue. The experiment author(s) are responsible for addressing these concerns when the experiment is resolved.
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'scientist'
4
+
5
+ require 'gitlab/experiment/configuration'
6
+ require 'gitlab/experiment/context'
7
+ require 'gitlab/experiment/dsl'
8
+ require 'gitlab/experiment/variant'
9
+ require 'gitlab/experiment/version'
10
+
11
+ module Gitlab
12
+ class Experiment
13
+ include Scientist::Experiment
14
+
15
+ class << self
16
+ def configure
17
+ yield Configuration
18
+ end
19
+
20
+ def run(name, variant_name = nil, **context, &block)
21
+ instance = new(name, variant_name, **context, &block)
22
+ return instance unless block_given?
23
+
24
+ instance.context.frozen? ? instance.run : instance.tap(&:run)
25
+ end
26
+ end
27
+
28
+ def initialize(name, variant_name = nil, **context)
29
+ @name = name
30
+ @variant_name = variant_name
31
+ @context = Context.new(self)
32
+
33
+ context(context)
34
+
35
+ ignore { true }
36
+ compare { false }
37
+
38
+ yield self if block_given?
39
+ end
40
+
41
+ def context(value = nil)
42
+ return @context if value.nil?
43
+
44
+ @context.value(value)
45
+ @context
46
+ end
47
+
48
+ def variant(value = nil)
49
+ @variant_name = value unless value.nil?
50
+ instance_exec(@variant_name, &Configuration.variant_resolver)
51
+ end
52
+
53
+ def run
54
+ @result ||= super(variant.name)
55
+ end
56
+
57
+ def publish(_result)
58
+ track(:assignment)
59
+ end
60
+
61
+ def track(action, **event_args)
62
+ instance_exec(action, event_args, &Configuration.tracking_behavior)
63
+ end
64
+
65
+ def name
66
+ [Configuration.name_prefix, @name].compact.join('_')
67
+ end
68
+
69
+ def variant_names
70
+ @variant_names = behaviors.keys.tap { |keys| keys.delete('control') }.map(&:to_sym)
71
+ end
72
+
73
+ def enabled?
74
+ true
75
+ end
76
+
77
+ protected
78
+
79
+ def generate_result(variant_name)
80
+ observation = Scientist::Observation.new(variant_name, self, &behaviors[variant_name])
81
+ Scientist::Result.new(self, [observation], observation)
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'singleton'
4
+ require 'logger'
5
+ require 'digest'
6
+
7
+ module Gitlab
8
+ class Experiment
9
+ class Configuration
10
+ include Singleton
11
+
12
+ # Prefix all experiment names with a given value. Use `nil` for none.
13
+ @name_prefix = 'gitlab_experiment'
14
+
15
+ # The logger is used to log various details of the experiments.
16
+ @logger = Logger.new(STDOUT)
17
+
18
+ # Logic this project uses to resolve a variant for a given experiment.
19
+ @variant_resolver = lambda do |requested_variant|
20
+ # An example of running the control, unless a variant was requested:
21
+ Variant.new(name: requested_variant || 'control')
22
+
23
+ # Always run candidate, unless a variant was requested, with fallback:
24
+ # Variant.new(name: variant_name || variant_names.first || 'control')
25
+
26
+ # Using unleash to determine the variant:
27
+ # fallback = Unleash::Variant.new(name: 'control', enabled: true)
28
+ # Unleash.get_variant(name, context.value, fallback)
29
+
30
+ # Using Flipper to determine the variant:
31
+ # TODO: provide example
32
+ # Variant.new(name: resolved)
33
+ end
34
+
35
+ # Tracking behavior can be implemented to link an event to an experiment.
36
+ @tracking_behavior = lambda do |action, event_args|
37
+ # An example of using a generic logger to track events:
38
+ (event_args[:context] ||= []) << context.signature.merge(group: variant.name)
39
+ Configuration.logger.info "EXPERIMENT[#{name}] #{action}: #{event_args}"
40
+
41
+ # Using snowplow to track events:
42
+ # (event_args[:context] ||= []) << SnowplowTracker::SelfDescribingJson.new(
43
+ # 'iglu:com.gitlab/gitlab_experiment/jsonschema/1-0-0',
44
+ # experiment: context.signature.merge(group: variant.name)
45
+ # )
46
+ #
47
+ # Tracking.event(name, action, **event_args)
48
+ end
49
+
50
+ # Algorithm that consistently generates a hash key for a given hash map.
51
+ @context_hash_strategy = lambda do |context|
52
+ values = context.values.map { |v| (v.respond_to?(:to_global_id) ? v.to_global_id : v).to_s }
53
+ Digest::MD5.hexdigest((context.keys + values).join('|'))
54
+ end
55
+
56
+ class << self
57
+ attr_accessor :name_prefix, :logger
58
+ attr_accessor :variant_resolver, :tracking_behavior, :context_hash_strategy
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gitlab
4
+ class Experiment
5
+ class Context
6
+ def initialize(experiment)
7
+ @experiment = experiment
8
+ @value = {}
9
+ @migrations_from = []
10
+ @migrations_with = []
11
+ end
12
+
13
+ def value(value = nil)
14
+ return @value if value.nil?
15
+
16
+ value = value.dup # dup so we don't mutate
17
+ @signature = nil # clear memoized signature
18
+
19
+ @migrations_from << value.delete(:migrated_from) if value[:migrated_from]
20
+ @migrations_with << value.delete(:migrated_with) if value[:migrated_with]
21
+ @value.merge!(migrate_cookie_to_user_id(value))
22
+ end
23
+
24
+ def freeze
25
+ signature # ensure we're done before being frozen
26
+ super
27
+ end
28
+
29
+ def signature
30
+ @signature ||= { key: key_for(@value), migration_keys: migration_keys }.compact
31
+ end
32
+
33
+ private
34
+
35
+ def migrate_cookie_to_user_id(value)
36
+ return value unless (request = value.delete(:request))
37
+ return value if request.headers['DNT'].to_s.match?(/^(true|t|yes|y|1|on)$/i)
38
+
39
+ cookie_name = [@experiment.name, 'id'].join('_')
40
+ cookie_value = request.cookie_jar.signed[cookie_name]
41
+
42
+ if value[:user_id].blank? && cookie_value.blank?
43
+ request.cookie_jar.permanent.signed[cookie_name] = {
44
+ value: value[:user_id] = SecureRandom.uuid, secure: true, domain: :all, httponly: true
45
+ }
46
+ elsif cookie_value # we know via the cookie
47
+ if value[:user_id].blank?
48
+ value[:user_id] = cookie_value
49
+ else
50
+ @migrations_with << { user_id: cookie_value }
51
+ request.cookie_jar.delete(cookie_name, domain: :all)
52
+ end
53
+ end
54
+
55
+ value
56
+ end
57
+
58
+ def migration_keys
59
+ return nil if @migrations_from.empty? && @migrations_with.empty?
60
+
61
+ @migrations_from.map { |m| key_for(m) } +
62
+ @migrations_with.map { |m| key_for(@value.merge(m)) }
63
+ end
64
+
65
+ def key_for(context)
66
+ Configuration.context_hash_strategy.call(context)
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gitlab
4
+ class Experiment
5
+ module Dsl
6
+ def experiment(name, variant_name = nil, **context, &block)
7
+ Experiment.run(name, variant_name, **context, &block)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gitlab
4
+ class Experiment
5
+ Variant = Struct.new(:name, :payload, keyword_init: true)
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gitlab
4
+ class Experiment
5
+ VERSION = '0.2.0'
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gitlab-experiment
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - GitLab
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-09-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: scientist
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.5.0
20
+ - - "~>"
21
+ - !ruby/object:Gem::Version
22
+ version: '1.5'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 1.5.0
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '1.5'
33
+ description:
34
+ email:
35
+ - gitlab_rubygems@gitlab.com
36
+ executables: []
37
+ extensions: []
38
+ extra_rdoc_files: []
39
+ files:
40
+ - LICENSE.txt
41
+ - README.md
42
+ - lib/gitlab/experiment.rb
43
+ - lib/gitlab/experiment/configuration.rb
44
+ - lib/gitlab/experiment/context.rb
45
+ - lib/gitlab/experiment/dsl.rb
46
+ - lib/gitlab/experiment/variant.rb
47
+ - lib/gitlab/experiment/version.rb
48
+ homepage: https://gitlab.com/gitlab-org/gitlab-experiment
49
+ licenses:
50
+ - MIT
51
+ metadata: {}
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '2.6'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubygems_version: 3.0.3
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: GitLab experiment library built on top of scientist.
71
+ test_files: []