Skip to content

jejacks0n/activeexperiment-unleash

Repository files navigation

Active Experiment – Unleash Adapter

Gem Version License Maintainability Test Coverage

Provides an Active Experiment compatible interface for Unleash.

Setup

This adapter requires an Unleash::Client to be operational. The adapter will attempt to find one, and if no valid client is found, a new one will be created using the global Unleash configuration.

The Unleash client can be manually provided to the adapter if desired:

ActiveExperiment::UnleashAdapter.client = Unleash::Client.new

Since Unleash implements its own caching and reporting logic, this adapter doesn't need to implement any additional caching or reporting.

Usage

To use this adapter, simply use the :unleash rollout in your experiment:

class MyExperiment < ActiveExperiment::Base
  variant(:red) { "red" }
  variant(:blue) { "blue" }

  use_rollout :unleash, toggle_name: "MyExperiment"
end

Given the above example, we can now create a feature toggle in Unleash with the name "MyExperiment", and add the "red" and "blue" variants to it. Now when our experiment is run, a variant will be assigned using Unleash.

MyExperiment.run(current_user) # => "red" or "blue"

When creating the variants in Unleash you might notice that a payload can be provided for each variant, which can be used in our experiment by using :unleash_variant_payload when registering the variants:

class MyExperiment < ActiveExperiment::Base
  variant(:red, :unleash_variant_payload)
  variant(:blue, :unleash_variant_payload)

  use_rollout :unleash, toggle_name: "MyExperiment"
end

Now if a payload has been provided for the variants in our Unleash feature toggle, that payload will be used as the result of the experiment:

MyExperiment.run(current_user) # => "red payload" or "blue payload"

A shortcut for the above, is to tell the rollout that we want to use the variants as they're defined in Unleash. For example, we don't need to register any variants if there are some defined on our Unleash feature toggle:

class MyExperiment < ActiveExperiment::Base
  use_rollout :unleash, toggle_name: "MyExperiment", variants: true
end

More complex variant scenarios can be crafted this way; for example:

class MyExperiment < ActiveExperiment::Base
  variant(:green) { "green" } # this variant is not in Unleash.
  variant(:red) { "red" } # this variant won't be overridden,

  # This will add the blue variant from Unleash, but won't override red.
  use_rollout :unleash, toggle_name: "MyExperiment", variants: true
end

Download and Installation

Add this line to your Gemfile:

gem "activeexperiment-unleash"

Or install the latest version with RubyGems:

gem install activeexperiment-unleash

Source code can be downloaded as part of the project on GitHub:

License

Active Experiment is released under the MIT license:

Copyright 2022 jejacks0n

Make Code Not War

About

Provides an Active Experiment compatible interface for Unleash.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages