Feature Flags - Percent Filter Demo
Feature Flags - Percent Filter Demo

Conditional Feature Flags with Azure App Configuration Service

In this article, we are going to discuss briefly about what are conditional feature flags and how to enable them via Azure Portal.

What are conditional feature flags ?

Feature flags allow to conditionally enable or disable an execution path. The feature flags can help to hide features from end users. This can be handy from various perspectives. It means the features in development can be merged to the main branch. They can also be deployed as long as they are behind the feature toggles.

In some cases, we may want the feature toggle to be enabled on specific date. Let’s say, mortgage interest rates are changing from new financial year. So, a bank website may want to show some notice for few days, to convey that interest rates are going to change soon. Also, as soon as, the new year commences, from that midnight, the bank website is supposed to show the latest rate. For such scenarios, we can define feature flags, which gets enabled when certain condition is met (in our case, date and time based condition).

The feature flags which are based on certain configured condition, which should be evaluated to decide if the flag is enabled or disable, are called as conditional feature flags. The condition can be either to enable the flag and / or to disable to the flag.

The feature flags can be enabled only for short duration (let’s say only for last two weeks of the year) or the feature flag may be applicable only after certain date is passed. Or feature flag may be used only for enabling a feature from a specific region / country.

Does Azure App Configuration Service Support Conditional Flags ?

We have create a feature flag in one of the previous posts. At that time, we just specified feature flag identifier and whether it is enabled or not. We did not specify any filters.

Actually, the feature filter input is one way to create conditional feature flags. Feature filters determine the state of the feature flag each time it’s evaluated. The Microsoft.FeatureManagement library includes three feature filters:

  • PercentageFilter enables the feature flag based on a percentage.
  • TimeWindowFilter enables the feature flag during a specified window of time.
  • TargetingFilter enables the feature flag for specified users and groups.

You can also create your own feature filter that implements the Microsoft.FeatureManagement.IFeatureFilter interface.

How to enable filter via Azure Portal ?

In this section, let’s see how to add the conditional feature flags.

Create Azure App Configuration Service

We will need an instance of Azure App Configuration Service. You can refer this article to create one using Azure Portal. Alternatively, you can run the bicep script from this article to create one for you. Refer my GitHub repository to create the resource if you want to use the Bicep files.

Add Feature Flag

You can refer previous article to view how to add a new feature flag. We are going to need feature flag with name “StorageFeature“. The previous article adds the flag with same identifier.

We need to enable the flag and enter the text “first” in the label field.

Azure Portal – Adding a new feature flag in Azure App Service Configuration

Add Filters

Now, that we have the feature flag, let’s see how to add conditions to it. Select the feature flag that we have created, click on the right click on three dots and select Edit from context menu. It should open the edit panel as shown below.

The Use feature filter checkbox should be selected. Then there are three radio buttons. We can select any one of the three options – Targeting or Time Window or Custom. Let’s select custom. Then a new textbox will appear. There, we need to specify the filter name.

Let’s enter Microsoft.Percentage in the text box. Then select the three dots from the right side of the text box and select Edit filter parameters option. This option is to pass additional parameters.

Azure Portal – Edit feature flag and add a custom feature filter

Enter a Name of Value and a Value of 50. This Value indicates the percentage of requests for which to enable the feature filter.

Azure Portal – App Configuration Service – Edit parameter values of the feature flag filters

Then click on Apply button to close the Edit filter parameters panel. Then click on Apply to click the feature flag edit panel. Now, you should be able to view that the feature filter option on the list of feature flag list screen, as shown in the snapshot given below.

Azure Portal – App Configuration Service – Feature filter is set to Custom

Let’s verify that it works !

Now, let’s go to the console application which consumes the feature flag. We have created one in the previous article. You can refer that article for creating one. But, we need to verify that the feature flag is not enabled for 50% of the requests. That’s what we have configured above. So, to verify that, let’s modify the console application a bit.

First modification is – to include PercentageFilter class, while registering for the feature management. Then, the second modification is, we need to call the IsEnabledAsync method inside a loop so that it gets called multiple times and then we can verify what was the value of feature flag every time this method was called.

The complete code from Program.cs file is shown in the code snippet given below.

Let’s run the console application and it should print the feature flag values on the console. As we can see in the snapshot given below, I have tried 20 attempts and out of those, almost 50% times the feature flag was enabled.

I tried to run this application multiple times, the result was more or less similar. Only one time, I got a result in which exactly 50% times the feature flag was enabled. Given sufficiently large number of attempts, the actual percentage would come more closer to exact 50%.

Console Application Output – Feature flag is enabled almost 50% times

I hope you find this information helpful. Let me know your thoughts.

Leave a ReplyCancel reply