This is the third part in a series.
EDIT: my Feature Management Pluralsight training course is now available.
So far in this series, the configured feature flags have either been set to on (true) or off (false):
"FeatureManagement": {
"Printing": true,
"LiveChat": false
}
In addition to setting an absolute value, you can also make use of feature filters.
Essentially feature filters allow you to create conditional feature flags.
There are currently two feature filters built into the library and you can also create your own.
Percentage Feature Filter
If you want a feature enabled only for a percentage of users you can apply the Microsoft.Percentage feature filter.
To configure a feature filter in appsettings.json, instead of specifying a value of true/false, instead you add an EnabledFor section. For example to configure the Printing feature to be enabled 50% of the time:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"FeatureManagement": {
"Printing": {
"EnabledFor": [
{
"Name": "Microsoft.Percentage",
"Parameters": {
"Value": 50
}
}
]
},
"LiveChat": false
}
}
If you run the app now you’ll get an error because feature filters have to be registered. In an ASP.NET Core app this can be done by chaining on the AddFeatureFilter method, for example in the Startup.cs as follows:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddFeatureManagement()
.AddFeatureFilter<PercentageFilter>();
}
To get access to the PercentageFilter you’ll need to add a using directive for Microsoft.FeatureManagement.FeatureFilters.
Now if you run the app (e.g. an ASP.NET Core web app) the printing feature will be available for 50% of requests.
Time Window Feature Filter
The second built-in feature filter, Microsoft.TimeWindow, allows a feature to be enabled for a specified range of time.
The first thing is to register the feature filter:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddFeatureManagement()
.AddFeatureFilter<TimeWindowFilter>();
}
Next the configuration can be changed.
The time window feature filter accepts two parameters in the config, a Start date/time and an End date/time, for example the following feature would be enabled at 1 second past midnight for New Year’s Day 2021 and be disable at the end of the 1st Jan:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"FeatureManagement": {
"Printing": {
"EnabledFor": [
{
"Name": "Microsoft.TimeWindow",
"Parameters": {
"Start": "Fri, 01 Jan 2021 00:00:01 GMT",
"End": "Fri, 01 Jan 2021 23:59:59 GMT"
}
}
]
},
"LiveChat": false
}
}
You can also specify that a feature should be enable after a specific date/time forever after by not specifying an end date, for example to enable a feature from the 1st may 2020, 1PM GMT:
"FeatureManagement": {
"Printing": {
"EnabledFor": [
{
"Name": "Microsoft.TimeWindow",
"Parameters": {
"Start": "Fri, 01 May 2020 13:00:00 GMT"
}
}
]
},
If you want a feature to be on until a date/time and then be off after that then you can just specify the end date, for example to have a feature enabled and then automatically turn off 5:10 AM on 15th may 2020:
"FeatureManagement": {
"Printing": {
"EnabledFor": [
{
"Name": "Microsoft.TimeWindow",
"Parameters": {
"End": "Fri, 15 May 2020 05:10:00 GMT"
}
}
]
},
There is another advanced type of feature filtering called “targeting” that can be used to roll out new features based on a complex set of rules – I’ll cover this in a future part of this series.
You can also define your own feature filters which we’ll look at in the next part in this series.
Be sure to check out my Microsoft Feature Management Pluralsight course get started watching with a free trial.
SHARE: