Feature flagging with azure app configurations.

Feature flagging with azure app configurations.

What is feature flagging?

Feature flags (also commonly known as feature toggles) is a software engineering technique that turns select functionality on and off during runtime, without deploying new code.

Advantages:

Feature management helps developers address the following problems:

  • The business owner can toggle a particular feature flag at run time without restarting a service.
  • The feature flag solution supports A/B testing. It supports the gradual rollout of a feature to a group/percentage of users.
  • The feature flag toggling is available per environment. For instance, we can enable a particular feature only on the DEV environment but disable that on the QA environment.
  • It can support access control(Targetting) for toggling a feature flag.

Feature Management Platforms:

There are widely recognised feature management platforms (FMP) for maintaining the feature flags. LaunchDarkly, Flagsmith, Flagship, Unleash, BulletTrain and Split are a few of the major feature management solution providers that can be hosted in aks clusture.

Third Party SAAS based solution's are also available which provides JAVA SDK and Android SDK for backend and frontend services and also provides user segmentation facilities but they have cost associated with them. e.g Harness, Optimizely

Azure application configuration for Feature flagging:

In this blog post we will be mainly focussing on azure app config for feature flagging.

Azure App Config is recommended as it has lower cost and provides all the desired features and functionalities for feature flagging such as user segmentation and audit trail. Also Azure App Config is also used by any platform for storing application configurations.

Manage feature flags in Azure App Configuration

We can store all feature flags in Azure App Configuration and administer them from a single place. App Configuration has a portal UI named Feature Manager that's designed specifically for feature flags.

We can add feature flags from below screen:

No alt text provided for this image

We can also edit the feature flag and enable filtering/targetting on them from below screen:

No alt text provided for this image

Here we have set targetting on Users with email: [email protected] and Groups: contoso.com

These settings result in the following behavior:

  • The feature flag is always enabled for user [email protected], because [email protected] is listed in the Users section.
  • The feature flag is enabled for 50% of other users in the contoso.com group, because contoso.com is listed in the Groups section with a Percentage of 50.
  • The feature is always disabled for all other users, because the Default percentage is set to 0.


Integrating Feature flagging in spring boot applications:

1) Create a spring boot project with Spring Initializr and choose maven as build tool.

2) Open the pom.xml file in a text editor and add the following to the list of <dependencies>:

<dependency
    <groupId>com.azure.spring</groupId>
    <artifactId>spring-cloud-azure-appconfiguration-config-web</artifactId>
    <version>5.2.0</version>
</dependency>
<dependency>
    <groupId>com.azure.spring</groupId>
    <artifactId>spring-cloud-azure-feature-management-web</artifactId>
    <version>5.2.0</version>
</dependency>>        

3) Navigate to the resources directory of your app and open bootstrap.properties. If the file does not exist, create it. Add the following line to the file.

spring.cloud.azure.appconfiguration.stores[0].connection-string= ${APP_CONFIGURATION_CONNECTION_STRING}
spring.cloud.azure.appconfiguration.stores[0].feature-flags.enabled=true        

Set an environment variable named APP_CONFIGURATION_CONNECTION_STRING, and set it to the connection string to your App Configuration store.

4) Create a new Java file named HelloController.java in the package directory of your app.

@RestController
public class HelloController {

?@Autowired
 private FeatureManager featureManager;


 @GetMapping("/demo")
 public String demo() {
     if (Boolean.TRUE.equals(featureManager.isEnabledAsync("FeatureA").block())) {
            return "enabled"; // actual code base that needs to be enabled
     }
        return "disabled";
  }
};        


Enabling FF for Targeting:

Referring to our above image, lets say we want feature flags to be enabled only for user with email: [email protected] and group contoso.com

Then, We have to register this bean:

@Configuration
public class TargetConfiguration {

    @Bean("Microsoft.Targeting")
    public TargetingFilter targetingFilter(TargetingContextAccessor contextAccessor) {
        return new TargetingFilter(contextAccessor, new TargetingEvaluationOptions().setIgnoreCase(true));
    }
}        

Also have to create another component class:

@Component
public class TargetingContextAccessorImpl implements TargetingContextAccessor {

    @Override
    public void configureTargetingContext(TargetingContext context) {
            String email = "[email protected]"
            context.setUserId(email);
            ArrayList<String> groups = new ArrayList<String>();
            groups.add(email.split("@")[1]);
            context.setGroups(groups);
        }
}        

This way targetting is enabled in our spring code.

Apart from Targetting, we can do filtering on time and can other custom filters too. Hence, feature flagging with azure is an easy, efficient and effective way to manage our code.


References:

https://learn.microsoft.com/en-us/azure/azure-app-configuration/manage-feature-flags

https://learn.microsoft.com/en-us/azure/azure-app-configuration/quickstart-feature-flag-spring-boot?tabs=spring-boot-3%2Cwindowscommandprompt

Deepak Sharma

Head Of Engineering

1 年

She is continuously filling gap which generally required in micro service project. She has built lot of cross cutting libraries which accelerate MS project.

要查看或添加评论,请登录

Charu Jain的更多文章

社区洞察

其他会员也浏览了