Sitecore and Feature Flags using LaunchDarkly

Feature flags are a software development best practice of gating functionality. Functionality can be deployed “off”, then turned on via the feature flag, separate from deployment. With feature flags, you can manage the entire lifecycle of a feature.

  • Launchdarkly.com

In essence, a feature flag is a if..then..else statement, where the outcome of the statement is controlled by feeding a flag into a feature flag system:

if (GetFeatureFlag("feature-flag") == true)
{
  // enable my feature
}
else
{
  // disable my feature
}

Feature flags are user specific and can be used cross platform, and is therefore a great way to release new features:

  • At the same time on all platforms
  • To 10% of all users
  • To all users from a specific segment
  • To website only

Feature flags should not be confused with Sitecore Personalization, although they both share similarities. The purpose of Personalization is to tailor the user experience to the end users needs, whilst feature flags (or feature toggling) is enabling or disabling features based on rules separate from deployment or code.

A feature flag is often short-lived, and the flag is removed when the gated feature is considered stable.

 

ABOUT THE FEATURE FLAG PLATFORM

I have lately worked with a feature flag platform called “Launchdarkly“. They offer API’s for every possible programming language and for every possible platform. Their code is blazingly fast, and you do not notice any performance loss when checking for a flag.

Feature Flags Overview

Feature Flags Overview

In Launchdarkly, you can enable features by flipping a switch, or by segmenting on user data:

Feature Flags Targeting

Feature Flags Targeting

 

IMPLEMENTING LAUNCHDARKLY IN SITECORE

In order to check for a feature flag, we need to send user data and a feature flag name to Launchdarkly. The Launchdarkly API uses a Launchdarkly.Client.User and you can fill any property from your Sitecore User to the Launchdarkly Client User.

This is a simple example where I create a Launchdarkly user containing a unique ID, the email address and a name:

using System;
using LaunchDarkly.Client;

namespace MyNamespace
{
  static internal class LaunchDarklyFactory
  {
    static internal User CreateUser()
    {
      var user = Sitecore.Context.User;
      var key = user.IsAuthenticated ? user.Name : Guid.NewGuid().ToString();
      var ldUser = User.WithKey(key).AndAnonymous(!user.IsAuthenticated).AndEmail(user.Profile.Email).AndFirstName(user.Profile.FullName).AndLastName("."); //LaunchDarkly won't update display name unless both first and last is set.
      return ldUser;
    }
  }
}

With the user in hand it is easy to create a feature flag repository:

using System;
using LaunchDarkly.Client;
using Sitecore.Configuration;
using Sitecore.Diagnostics;

namespace MyNamespace
{
  public static class LaunchDarklyRepository
  {
    // My Launchdarkly key is stored in a setting in a config file
    private static readonly LdClient _client = new LdClient(Settings.GetSetting("LaunchDarkly.Client"));

    public static bool GetFeatureFlag(string flag, bool defaultValue)
    {
      try
      {
        var ldUser = LaunchDarklyFactory.CreateUser();
        return _client.BoolVariation(flag, ldUser, defaultValue);
      }
      catch (Exception ex)
      {
        Log.Error(typeof (LaunchDarklyRepository) + ".GetFeatureFlag() failed to get feature flag '" + flag + "', returning default value " + defaultValue + ". Message:" + ex.Message, typeof (LaunchDarklyRepository));
        return defaultValue;
      }
    }
  }
}

The repository can now be used to enable or disable features:

if (LaunchDarklyRepository.GetFeatureFlag("my-feature-flag", false))
{
  // feature enabled
}
else
{  
  // feature disabled
}

 

INTEGRATING LAUNCHDARKLY INTO THE EXPERIENCE EDITOR

Experience Editor

Experience Editor

With the new feature flag framework in place, it could be nice with a Sitecore Rule. This rule will allow me to enable and disable components on my website by flipping a feature flag switch. The rule itself is very simple:

using System;
using Sitecore.Diagnostics;
using Sitecore.Rules;
using Sitecore.Rules.Conditions;

namespace MyNamespace
{
  public class FeatureFlagHasBoolVariation<T> : WhenCondition<T> where T : RuleContext
  {
    public string FeatureFlag
    {
      get;
      set;
    }

    public bool BoolVariation
    {
      get;
      set;
    }

    protected override bool Execute(T ruleContext)
    {
      try
      {
        LaunchDarklyUser user = LaunchDarklyUserFactory.GetLaunchDarklyUser();
        var boolVariation = LaunchDarklyRepository.GetFeatureFlag(FeatureFlag, false);
        return boolVariation == BoolVariation;
      }
      catch (Exception ex)
      {
        Log.Error(string.Format("{0}: {1} ", GetType(), ex.Message), ex, this);
        return false;
      }
    }
  }
}

To enable the rule in Sitecore, add an item under /sitecore/system/Settings/Rules/Definitions/Elements/…

Feature Flag Personalisation Rule

Feature Flag Personalization Rule

The rule can now applied to a component:

Rule Set Editor

Rule Set Editor

And the component can be visible if the flag is set:

Personalize Content

Personalize Content

MORE TO READ

Advertisements

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in c#, General .NET, Sitecore, Sitecore 8 and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s