Skip to content

Instantly share code, notes, and snippets.

@fzf
Created August 15, 2019 06:38
Show Gist options
  • Save fzf/ec1dba4b2f55cc49fcd80c585956d795 to your computer and use it in GitHub Desktop.
Save fzf/ec1dba4b2f55cc49fcd80c585956d795 to your computer and use it in GitHub Desktop.
class MyClass
include Scientist
def count
science('new_count') do |experiment|
experiment.context(user: user)
experiment.use { 1 }
# One Try
experiment.try { 2 }
# Multiple Tries
experiment.try('two') { 2 }
experiment.try('three') { 3 }
# Ignore, still runs all the tries
experiment.ignore { true }
# Similar to enabled?, does not run tries
experiment.run_if { Flipper['new_count'.to_sym].enabled?(user) }
end
end
end
require 'scientist'
class MyExperiment
include Scientist::Experiment
attr_reader :name
def initialize(name:)
@name = name
end
def enabled?
Flipper[name.to_sym].enabled?(user)
end
def publish(result)
Chime::Dog.histogram("#{name}.control.duration", result.control.duration)
result.candidates.each do |candidate|
Chime::Dog.histogram("#{name}.#{candidate.name}.duration", candidate.duration)
end
if result.matched?
Chime::Dog.increment "#{name}.matched"
elsif result.ignored?
Chime::Dog.increment "#{name}.ignored"
else
Chime::Dog.increment "#{name}.mismatched"
end
end
private
def user
context[:user]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment