Feature Flags for ASP.Net Core Applications: Using Complex Feature Flags with Feature Filters

Feature Flags for ASP.Net Core Applications: Using Complex Feature Flags with Feature Filters

Feature Flags for ASP.Net Core Applications Series

Up till now we were working with simple feature flags that has either a true/false value that evaluates to feature flag On or Off. But there are some other scenarios that needs more complex conditions to be evaluated to determine the state of the feature flag. This is done using Feature Filters and there are couple of built in feature filters you can use, and you can create your own custom feature filters. Let’s first distinguish between a feature flag and a feature filter

Feature Flag

A feature flag is a combination of a feature name and a list of feature filters that is used to turn the feature on/off. This filter can be a simple value like a Boolean or a complex filter with multiple conditions that needs to be met.

Feature Filter

A feature filter defines a scenario when a feature should be enabled. When a feature flag is evaluated the defined feature filters are traversed and evaluated until it finds a filter that decides the feature should be enabled. And when that happens the traversal stops.

Built-In Feature Filters

Let’s look at some of the built-in feature filters. There are 3 as of now.

AlwaysOn

As the name suggests, if this feature filter is applied the feature is enabled always. You can define the AlwaysOn feature filter like this in your configuration.

{
  ...

  "FeatureManagement": {
    ...

    "Suggestion.User": {
      "EnabledFor": [
        {
          "Name": "AlwaysOn"
        }
      ]
    }
  }
}

Microsoft.Percentage

This filter is a useful feature filter when it comes to scenarios like A|B testing or in a situation you want to route traffic to features according to some percentage. Like the name suggests, you can define a percentage as the value for the feature filter and only that percentage of requests are able to see the feature enabled. In the following example, I want to enable Suggested Albums feature in our Music Store application for 50% of the requests that is coming in.

{
  ...

  "FeatureManagement": {
    ...

    "Suggestion.User": {
      "EnabledFor": [
        {
          "Name": "Microsoft.Percentage",
          "Parameters": {
            "Value": 50
          }
        }
      ]
    }
  }
}

Once the appsettings.json is configured with the percentage value you want, next is to register the feature filter in Startup.cs

using Microsoft.FeatureManagement;
using Microsoft.FeatureManagement.FeatureFilters;

namespace MusicStore.Web
{
  public class Startup
  {
    ...
    public void ConfigureServices(IServiceCollection services)
    {
        ...

        // Configure Feature Management
        services
          .AddFeatureManagement()
          .AddFeatureFilter<PercentageFilter>(); // register PercentageFilter
        ...
    }
  }
}

Microsoft.TimeWindow

With this you can enable features based on a time window. You can provide 2 parameters for this feature filter. Start and End times. If you provide only the Start time the feature will be activated after the defined time and will be kept activated. If you provide the End time as well, once the End time is reached, the feature will be deactivated. This is how you define the Microsoft.TimeWindow feature filter on your appsettings.json

{
  ...
  "FeatureManagement": {
    ...

    "Suggestion.User": {
      "EnabledFor": [
        {
          "Name": "Microsoft.TimeWindow",
          "Parameters": {
            "Start": "Mon, 13 January 2020 00:00:00 GMT",
            "End": "Mon, 20 January 2020 00:00:00 GMT"
          }
        }
      ]
    }
  }
}

Once the appsettings.json is configured with the time window values you want, next is to register the feature filter in Startup.cs

using Microsoft.FeatureManagement;
using Microsoft.FeatureManagement.FeatureFilters;

namespace MusicStore.Web
{
  public class Startup
  {
    ...

    public void ConfigureServices(IServiceCollection services)
    {
      ...

      // Configure Feature Management
      services
        .AddFeatureManagement()
        .AddFeatureFilter<TimeWindowFilter>(); // register PercentageFilter

      ...
    }
  }
}

Summary

We looked at some of the useful feature filters already built into Microsoft.FeatureManagement library that you can use. We can also create our custom Feature Filters as well. In an upcoming article we will look at creating custom Feature Filters.

You Might Also Like
Comments