How do I implement a Feature Toggle?


116
views
0
8 months ago by

Thanks Graham, this was very helpful. I have a question, as it looks like just two options are available - default and experimental. Is that correct? Also, how do we enable the experimental feature set locally please?

written 7 months ago by Ed Robinson  

1 Answer


0
8 months ago by

This is a summary of work done to implement a feature toggle, with help from Bash.

Before starting, read all about Feature Toggling. All the infrastructure work has already been done by the Platform guys on behalf of Travel, so implementation is pretty simple.

Next, an example. Here's one for a feature that lists all flight numbers for a given search result, to show where multiple operators are selling tickets on the same flight. The feature is experimental until it can be properly verified.

The first thing is to add the new feature to Travel's own Feature Service implementation. Here's the class with the new feature in place:

package com.clicktravel.services.travel.features;

import java.util.HashSet;
import java.util.Set;

import com.clicktravel.cheddar.features.Feature;
import com.clicktravel.services.features.service.api.FeatureService;

public class TravelFeatureService extends FeatureService {

    public enum TravelFeature implements Feature {
        SHOW_ALL_FLIGHT_NUMBERS
    }

    @Override
    public Set<Feature> enabledFeatures(final String featureSetId) {
        final Set<Feature> enabledFeatures = new HashSet<>();
        if (FeatureService.EXPERIMENTAL_FEATURE_SET.equals(featureSetId)) {
            enabledFeatures.add(TravelFeature.SHOW_ALL_FLIGHT_NUMBERS);
        }
        return enabledFeatures;
    }
}

Two things have been done here; one was to add the name of the feature into the Enum; the other was to define the conditions under which it will be enabled. Here it's to be made available to anyone authorised to see experimental features.

Now you have to ensure that FeatureService is available to the code that needs to use it, by autowiring it in the class constructor. Use the base class (FeatureService), not your implementation class. The Cheddar infrastructure will visit your class, pull out the feature name and apply it at the appropriate time.

Finally, here's the feature toggle wrapped round the experimental method call:

        if (featureService.isEnabled(TravelFeatureService.TravelFeature.SHOW_ALL_FLIGHT_NUMBERS)) {
            applyCodeshares(flightSearchResults, codeshares);
        }

Thanks Graham, this was very helpful. I have a question, as it looks like just two options are available - default and experimental. Is that correct? Also, how do we enable the experimental feature set locally please?

written 7 months ago by Ed Robinson  

Looks like there are definitely only the two options available atm, just default and experimental.

To enable the experimental feature locally, add a header to Postman requests with a name of 'Feature-Set-Id' and a value of 'EXPERIMENTAL'.

written 7 months ago by Ed Robinson  
Please login to add an answer/comment or follow this question.

Similar posts:
Search »