2019-10-01 14:06:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# == Experimentation
|
|
|
|
#
|
2020-04-21 11:21:10 -04:00
|
|
|
# Utility module for A/B testing experimental features. Define your experiments in the `EXPERIMENTS` constant.
|
|
|
|
# Experiment options:
|
|
|
|
# - tracking_category (optional, used to set the category when tracking an experiment event)
|
2021-01-19 13:11:04 -05:00
|
|
|
# - rollout_strategy: default is `:cookie` based rollout. We may also set it to `:user` based rollout
|
2020-12-04 04:09:36 -05:00
|
|
|
#
|
2020-04-21 11:21:10 -04:00
|
|
|
# The experiment is controlled by a Feature Flag (https://docs.gitlab.com/ee/development/feature_flags/controls.html),
|
2020-05-14 14:08:06 -04:00
|
|
|
# which is named "#{experiment_key}_experiment_percentage" and *must* be set with a percentage and not be used for other purposes.
|
2020-04-21 11:21:10 -04:00
|
|
|
#
|
2022-03-16 20:07:59 -04:00
|
|
|
# To enable the experiment for 10% of the time:
|
2020-05-14 14:08:06 -04:00
|
|
|
#
|
2022-03-16 20:07:59 -04:00
|
|
|
# chatops: `/chatops run feature set experiment_key_experiment_percentage 10 --random`
|
2020-06-05 11:08:23 -04:00
|
|
|
# console: `Feature.enable_percentage_of_time(:experiment_key_experiment_percentage, 10)`
|
2020-04-21 11:21:10 -04:00
|
|
|
#
|
|
|
|
# To disable the experiment:
|
|
|
|
#
|
2020-05-14 14:08:06 -04:00
|
|
|
# chatops: `/chatops run feature delete experiment_key_experiment_percentage`
|
2020-06-02 08:08:33 -04:00
|
|
|
# console: `Feature.remove(:experiment_key_experiment_percentage)`
|
2020-05-14 14:08:06 -04:00
|
|
|
#
|
|
|
|
# To check the current rollout percentage:
|
|
|
|
#
|
|
|
|
# chatops: `/chatops run feature get experiment_key_experiment_percentage`
|
|
|
|
# console: `Feature.get(:experiment_key_experiment_percentage).percentage_of_time_value`
|
2019-10-01 14:06:28 -04:00
|
|
|
#
|
2020-06-02 08:08:33 -04:00
|
|
|
|
2020-06-05 11:08:23 -04:00
|
|
|
# TODO: see https://gitlab.com/gitlab-org/gitlab/-/issues/217490
|
2019-10-01 14:06:28 -04:00
|
|
|
module Gitlab
|
|
|
|
module Experimentation
|
|
|
|
EXPERIMENTS = {
|
|
|
|
}.freeze
|
|
|
|
|
|
|
|
class << self
|
2020-12-02 19:09:53 -05:00
|
|
|
def get_experiment(experiment_key)
|
|
|
|
return unless EXPERIMENTS.key?(experiment_key)
|
|
|
|
|
|
|
|
::Gitlab::Experimentation::Experiment.new(experiment_key, **EXPERIMENTS[experiment_key])
|
2019-10-18 17:06:37 -04:00
|
|
|
end
|
|
|
|
|
2020-12-02 19:09:53 -05:00
|
|
|
def active?(experiment_key)
|
|
|
|
experiment = get_experiment(experiment_key)
|
|
|
|
return false unless experiment
|
2019-10-01 14:06:28 -04:00
|
|
|
|
2020-12-02 19:09:53 -05:00
|
|
|
experiment.active?
|
2019-10-23 11:06:29 -04:00
|
|
|
end
|
2019-10-01 14:06:28 -04:00
|
|
|
|
2020-12-02 19:09:53 -05:00
|
|
|
def in_experiment_group?(experiment_key, subject:)
|
|
|
|
return false if subject.blank?
|
|
|
|
return false unless active?(experiment_key)
|
|
|
|
|
2021-01-19 13:11:04 -05:00
|
|
|
log_invalid_rollout(experiment_key, subject)
|
|
|
|
|
2020-12-02 19:09:53 -05:00
|
|
|
experiment = get_experiment(experiment_key)
|
|
|
|
return false unless experiment
|
|
|
|
|
|
|
|
experiment.enabled_for_index?(index_for_subject(experiment, subject))
|
|
|
|
end
|
|
|
|
|
2021-01-19 13:11:04 -05:00
|
|
|
def rollout_strategy(experiment_key)
|
|
|
|
experiment = get_experiment(experiment_key)
|
|
|
|
return unless experiment
|
|
|
|
|
|
|
|
experiment.rollout_strategy
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_invalid_rollout(experiment_key, subject)
|
|
|
|
return if valid_subject_for_rollout_strategy?(experiment_key, subject)
|
|
|
|
|
|
|
|
logger = Gitlab::ExperimentationLogger.build
|
|
|
|
logger.warn message: 'Subject must conform to the rollout strategy',
|
|
|
|
experiment_key: experiment_key,
|
|
|
|
subject: subject.class.to_s,
|
|
|
|
rollout_strategy: rollout_strategy(experiment_key)
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_subject_for_rollout_strategy?(experiment_key, subject)
|
|
|
|
case rollout_strategy(experiment_key)
|
|
|
|
when :user
|
|
|
|
subject.is_a?(User)
|
|
|
|
when :group
|
|
|
|
subject.is_a?(Group)
|
|
|
|
when :cookie
|
|
|
|
subject.nil? || subject.is_a?(String)
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-02 19:09:53 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def index_for_subject(experiment, subject)
|
2021-08-19 20:09:17 -04:00
|
|
|
index = Zlib.crc32("#{experiment.key}#{subject_id(subject)}")
|
2020-12-02 19:09:53 -05:00
|
|
|
|
|
|
|
index % 100
|
2020-09-24 05:09:35 -04:00
|
|
|
end
|
|
|
|
|
2020-12-02 19:09:53 -05:00
|
|
|
def subject_id(subject)
|
|
|
|
if subject.respond_to?(:to_global_id)
|
|
|
|
subject.to_global_id.to_s
|
|
|
|
elsif subject.respond_to?(:to_s)
|
|
|
|
subject.to_s
|
|
|
|
else
|
2021-05-04 11:10:36 -04:00
|
|
|
raise ArgumentError, 'Subject must respond to `to_global_id` or `to_s`'
|
2020-12-02 19:09:53 -05:00
|
|
|
end
|
2019-10-01 14:06:28 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|