Class: Gitlab::Experiment
- Inherits:
-
Object
- Object
- Gitlab::Experiment
show all
- Includes:
- ActiveModel::Model, BaseInterface, Cache, Callbacks, Nestable
- Defined in:
- lib/gitlab/experiment.rb,
lib/gitlab/experiment/dsl.rb,
lib/gitlab/experiment/cache.rb,
lib/gitlab/experiment/rspec.rb,
lib/gitlab/experiment/engine.rb,
lib/gitlab/experiment/errors.rb,
lib/gitlab/experiment/context.rb,
lib/gitlab/experiment/cookies.rb,
lib/gitlab/experiment/rollout.rb,
lib/gitlab/experiment/variant.rb,
lib/gitlab/experiment/version.rb,
lib/gitlab/experiment/nestable.rb,
lib/gitlab/experiment/callbacks.rb,
lib/gitlab/experiment/middleware.rb,
lib/gitlab/experiment/configuration.rb,
lib/gitlab/experiment/base_interface.rb,
lib/gitlab/experiment/rollout/random.rb,
lib/gitlab/experiment/rollout/percent.rb,
lib/gitlab/experiment/rollout/round_robin.rb,
lib/gitlab/experiment/cache/redis_hash_store.rb,
lib/gitlab/experiment/test_behaviors/trackable.rb
Defined Under Namespace
Modules: BaseInterface, Cache, Callbacks, Cookies, Dsl, Nestable, RSpecHelpers, RSpecMatchers, RSpecMocks, Rollout, TestBehaviors
Classes: Configuration, Context, Engine, Middleware, NestingError, Variant, WrappedExperiment
Constant Summary
collapse
- Error =
Class.new(StandardError)
- InvalidRolloutRules =
Class.new(Error)
- UnregisteredExperiment =
Class.new(Error)
- ExistingBehaviorError =
Class.new(Error)
- BehaviorMissingError =
Class.new(Error)
- VERSION =
'0.9.1'
Class Method Summary
collapse
-
.after_run(*filter_list, **options, &block) ⇒ Object
-
.around_run(*filter_list, **options, &block) ⇒ Object
-
.before_run(*filter_list, **options, &block) ⇒ Object
-
.candidate(*filter_list, **options, &block) ⇒ Object
-
.control(*filter_list, **options, &block) ⇒ Object
Class level behavior registration methods.
-
.default_rollout(rollout = nil, options = {}) ⇒ Object
Class level definition methods.
-
.exclude(*filter_list, **options, &block) ⇒ Object
Class level callback registration methods.
-
.model_name ⇒ Object
Used for generating routes.
-
.published_experiments ⇒ Object
Class level accessor methods.
-
.segment(*filter_list, variant:, **options, &block) ⇒ Object
-
.variant(variant, *filter_list, **options, &block) ⇒ Object
Instance Method Summary
collapse
Methods included from Nestable
#nest_experiment
Methods included from Cache
#cache, #cache_key, #cache_variant
#id, #initialize, #inspect, #key_for, #process_redirect_url, #variant_names
Class Method Details
.after_run(*filter_list, **options, &block) ⇒ Object
64
65
66
|
# File 'lib/gitlab/experiment.rb', line 64
def after_run(*filter_list, **options, &block)
build_run_callback(filter_list.unshift(:after, block), **options)
end
|
.around_run(*filter_list, **options, &block) ⇒ Object
60
61
62
|
# File 'lib/gitlab/experiment.rb', line 60
def around_run(*filter_list, **options, &block)
build_run_callback(filter_list.unshift(:around, block), **options)
end
|
.before_run(*filter_list, **options, &block) ⇒ Object
56
57
58
|
# File 'lib/gitlab/experiment.rb', line 56
def before_run(*filter_list, **options, &block)
build_run_callback(filter_list.unshift(:before, block), **options)
end
|
.candidate(*filter_list, **options, &block) ⇒ Object
38
39
40
|
# File 'lib/gitlab/experiment.rb', line 38
def candidate(*filter_list, **options, &block)
variant(:candidate, *filter_list, **options, &block)
end
|
.control(*filter_list, **options, &block) ⇒ Object
Class level behavior registration methods.
34
35
36
|
# File 'lib/gitlab/experiment.rb', line 34
def control(*filter_list, **options, &block)
variant(:control, *filter_list, **options, &block)
end
|
.default_rollout(rollout = nil, options = {}) ⇒ Object
Class level definition methods.
70
71
72
73
74
|
# File 'lib/gitlab/experiment.rb', line 70
def default_rollout(rollout = nil, options = {})
return @_rollout ||= Configuration.default_rollout if rollout.blank?
@_rollout = Rollout.resolve(rollout, options)
end
|
.exclude(*filter_list, **options, &block) ⇒ Object
Class level callback registration methods.
48
49
50
|
# File 'lib/gitlab/experiment.rb', line 48
def exclude(*filter_list, **options, &block)
build_exclude_callback(filter_list.unshift(block), **options)
end
|
.model_name ⇒ Object
Used for generating routes. We’ve included the method and ‘ActiveModel::Model` here because these things don’t make sense outside of Rails environments.
11
12
13
|
# File 'lib/gitlab/experiment/engine.rb', line 11
def self.model_name
ActiveModel::Name.new(self, Gitlab)
end
|
.published_experiments ⇒ Object
Class level accessor methods.
78
79
80
|
# File 'lib/gitlab/experiment.rb', line 78
def published_experiments
RequestStore.store[:published_gitlab_experiments] || {}
end
|
.segment(*filter_list, variant:, **options, &block) ⇒ Object
52
53
54
|
# File 'lib/gitlab/experiment.rb', line 52
def segment(*filter_list, variant:, **options, &block)
build_segment_callback(filter_list.unshift(block), variant, **options)
end
|
.variant(variant, *filter_list, **options, &block) ⇒ Object
42
43
44
|
# File 'lib/gitlab/experiment.rb', line 42
def variant(variant, *filter_list, **options, &block)
build_behavior_callback(filter_list, variant, **options, &block)
end
|
Instance Method Details
#assigned(value = nil) ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/gitlab/experiment.rb', line 109
def assigned(value = nil)
@_assigned_variant_name = cache_variant(value) if value.present?
return Variant.new(name: @_assigned_variant_name || :unresolved) if @_assigned_variant_name || @_resolving_variant
if enabled?
@_resolving_variant = true
@_assigned_variant_name = cached_variant_resolver(@_assigned_variant_name)
end
run_callbacks(segmentation_callback_chain) do
@_assigned_variant_name ||= :control
Variant.new(name: @_assigned_variant_name)
end
ensure
@_resolving_variant = false
end
|
#behaviors ⇒ Object
172
173
174
|
# File 'lib/gitlab/experiment.rb', line 172
def behaviors
@_behaviors ||= registered_behavior_callbacks
end
|
#candidate(&block) ⇒ Object
91
92
93
|
# File 'lib/gitlab/experiment.rb', line 91
def candidate(&block)
variant(:candidate, &block)
end
|
#context(value = nil) ⇒ Object
102
103
104
105
106
107
|
# File 'lib/gitlab/experiment.rb', line 102
def context(value = nil)
return @_context if value.blank?
@_context.value(value)
@_context
end
|
#control(&block) ⇒ Object
87
88
89
|
# File 'lib/gitlab/experiment.rb', line 87
def control(&block)
variant(:control, &block)
end
|
#enabled? ⇒ Boolean
154
155
156
|
# File 'lib/gitlab/experiment.rb', line 154
def enabled?
rollout.enabled?
end
|
#exclude! ⇒ Object
132
133
134
|
# File 'lib/gitlab/experiment.rb', line 132
def exclude!
@_excluded = true
end
|
#excluded? ⇒ Boolean
158
159
160
161
162
|
# File 'lib/gitlab/experiment.rb', line 158
def excluded?
return @_excluded if defined?(@_excluded)
@_excluded = !run_callbacks(exclusion_callback_chain) { :not_excluded }
end
|
#publish(result = nil) ⇒ Object
142
143
144
145
146
|
# File 'lib/gitlab/experiment.rb', line 142
def publish(result = nil)
instance_exec(result, &Configuration.publishing_behavior)
(RequestStore.store[:published_gitlab_experiments] ||= {})[name] = signature.merge(excluded: excluded?)
end
|
#rollout(rollout = nil, options = {}) ⇒ Object
126
127
128
129
130
|
# File 'lib/gitlab/experiment.rb', line 126
def rollout(rollout = nil, options = {})
return @_rollout ||= self.class.default_rollout(nil, options).for(self) if rollout.blank?
@_rollout = Rollout.resolve(rollout, options).for(self)
end
|
#run(variant_name = nil) ⇒ Object
136
137
138
139
140
|
# File 'lib/gitlab/experiment.rb', line 136
def run(variant_name = nil)
return @_result if context.frozen?
@_result = run_callbacks(run_callback_chain) { super(assigned(variant_name).name) }
end
|
#should_track? ⇒ Boolean
164
165
166
|
# File 'lib/gitlab/experiment.rb', line 164
def should_track?
enabled? && context.trackable? && !excluded?
end
|
#signature ⇒ Object
168
169
170
|
# File 'lib/gitlab/experiment.rb', line 168
def signature
{ variant: assigned.name.to_s, experiment: name }.merge(context.signature)
end
|
#track(action, **event_args) ⇒ Object
148
149
150
151
152
|
# File 'lib/gitlab/experiment.rb', line 148
def track(action, **event_args)
return unless should_track?
instance_exec(action, tracking_context(event_args).try(:compact) || {}, &Configuration.tracking_behavior)
end
|
#variant(name, &block) ⇒ Object
95
96
97
98
99
100
|
# File 'lib/gitlab/experiment.rb', line 95
def variant(name, &block)
raise ArgumentError, 'name required' if name.blank?
raise ArgumentError, 'block required' unless block.present?
behaviors[name] = block
end
|