Feature flags in GitLab

GitLab has built-in support for feature flags. Since I've become a big supporter of this approach, I was happy to see this.

GitLab uses Unleash as a feature toggle service. It has a free, open-sourced client for many platforms and can be found on NuGet. However, it is worth noticing that its .Net implementation requires Newtonsoft dependency. So let's try to make use of it in a .Net Core API.

First, we need to configure it. You can find settings in the Operations -> Feature Flags sidebar menu. And then press the configure button. feature-flags-configure.png We are interested in the API URL and Instance ID settings from the dialog that pops up. Now it's time to create a new feature flag called feature-is-here setup for all users and all environments to keep it simple.

new-feature.png

It's worth examining the documentation because there are valuable strategies around the flags configuration.

Armed with these variables, we are ready to write some code.

public class FeatureFlagsReader {
    readonly DefaultUnleash _defaultUnleash;

    public FeatureFlagsReader() =>
        _defaultUnleash = new DefaultUnleash(new()
        {
              AppName = "whathever-app-it-is",
            InstanceTag = "{Instance ID}",
            UnleashApi = new("{API URL}")
        });

    public bool ReadFor(string flagName) => 
         _defaultUnleash.IsEnabled(flagName);
}

It makes sense to have only one instance of the feature flag client during the whole application lifetime. Therefore I register it as a singleton in default .Net Core DI container.

services.AddSingleton<FeatureFlagsReader>();

Now we can inject it into our controller and use it as we see fit:

    [ApiController]
    public class BeautifulController : ControllerBase {
        readonly FeatureFlagsReader _featureFlagsReader;

        public BeautifulController(FeatureFlagsReader featureFlagsReader) =>
            _featureFlagsReader = featureFlagsReader;

        [HttpGet("/featureFlag")]
        public string GetFeatureFlags() => 
            _featureFlagsReader.ReadFor("feature-is-here").ToString();
    }

The whole code is available on my GitHub.

I think you agree that it's straightforward. Yet, please be aware that Unleash cache the feature flags values, so it takes some time to reflect the changes.