debian-mirror-gitlab/app/experiments/application_experiment.rb

71 lines
2.2 KiB
Ruby
Raw Normal View History

2021-02-22 17:27:13 +05:30
# frozen_string_literal: true
2021-03-11 19:13:27 +05:30
class ApplicationExperiment < Gitlab::Experiment # rubocop:disable Gitlab/NamespacedClass
def enabled?
return false if Feature::Definition.get(feature_flag_name).nil? # there has to be a feature flag yaml file
2021-04-17 20:07:23 +05:30
return false unless Gitlab.dev_env_or_com? # we have to be in an environment that allows experiments
2021-03-11 19:13:27 +05:30
2021-04-17 20:07:23 +05:30
# the feature flag has to be rolled out
2021-03-11 19:13:27 +05:30
Feature.get(feature_flag_name).state != :off # rubocop:disable Gitlab/AvoidFeatureGet
end
2021-04-17 20:07:23 +05:30
def publish(_result = nil)
return unless should_track? # don't track events for excluded contexts
2021-09-04 01:27:46 +05:30
record_experiment if @record # record the subject in the database if the context contains a namespace, group, project, actor or user
2021-02-22 17:27:13 +05:30
track(:assignment) # track that we've assigned a variant for this context
2021-04-17 20:07:23 +05:30
2021-09-04 01:27:46 +05:30
push_to_client
end
# push the experiment data to the client
def push_to_client
Gon.push({ experiment: { name => signature } }, true)
rescue NoMethodError
# means we're not in the request cycle, and can't add to Gon. Log a warning maybe?
2021-02-22 17:27:13 +05:30
end
def track(action, **event_args)
2021-04-17 20:07:23 +05:30
return unless should_track? # don't track events for excluded contexts
2021-02-22 17:27:13 +05:30
2021-04-17 20:07:23 +05:30
# track the event, and mix in the experiment signature data
2021-02-22 17:27:13 +05:30
Gitlab::Tracking.event(name, action.to_s, **event_args.merge(
context: (event_args[:context] || []) << SnowplowTracker::SelfDescribingJson.new(
2021-04-29 21:17:54 +05:30
'iglu:com.gitlab/gitlab_experiment/jsonschema/1-0-0', signature
2021-02-22 17:27:13 +05:30
)
))
end
2021-09-04 01:27:46 +05:30
def record!
@record = true
end
2021-04-17 20:07:23 +05:30
def exclude!
@excluded = true
2021-03-11 19:13:27 +05:30
end
2021-06-08 01:23:25 +05:30
def control_behavior
# define a default nil control behavior so we can omit it when not needed
end
2021-02-22 17:27:13 +05:30
private
2021-03-11 19:13:27 +05:30
def feature_flag_name
name.tr('/', '_')
end
2021-04-17 20:07:23 +05:30
def experiment_group?
Feature.enabled?(feature_flag_name, self, type: :experiment, default_enabled: :yaml)
2021-02-22 17:27:13 +05:30
end
2021-09-04 01:27:46 +05:30
def record_experiment
subject = context.value[:namespace] || context.value[:group] || context.value[:project] || context.value[:user] || context.value[:actor]
return unless ExperimentSubject.valid_subject?(subject)
variant = :experimental if @variant_name != :control
Experiment.add_subject(name, variant: variant || :control, subject: subject)
end
2021-02-22 17:27:13 +05:30
end