Skip to content

Feature Toggles

Brad Keryan edited this page Sep 28, 2023 · 5 revisions

Overview

New features that are not yet ready for release should be guarded by a feature toggle.

Feature toggles are defined in the _featuretoggles.py submodule.

XY_DATA = FeatureToggle("XY_DATA", CodeReadiness.RELEASE)
NEW_SESSION_API = FeatureToggle("NEW_SESSION_API", CodeReadiness.INCOMPLETE)

You can use the requires_feature decorator to indicate that a function requires the specified feature toggle. This wraps the function with a feature toggle check which will throw FeatureNotSupportedError if the feature is not enabled.

@requires_feature(NEW_SESSION_API)
def do_session_stuff(...): ...

Code Readiness

To determine whether a feature is enabled by default, the feature toggle code compares the feature's "code readiness" against the global "code readiness level" for the process. By default, the code readiness level is RELEASE, so only features with a code readiness of RELEASE are enabled by default.

You can change the code readiness level by setting environment variables:

  • MEASUREMENTLINK_ALLOW_NEXT_RELEASE=1: allow features with code readiness of NEXT_RELEASE or RELEASE
  • MEASUREMENTLINK_ALLOW_INCOMPLETE=1: allow features with code readiness of INCOMPLETE, NEXT_RELEASE, or RELEASE

You can set these environment variables in the OS environment or in a .env file. See .env.sample for a sample configuration file that you can use as a starting point.

Enabling Specific Features

Regardless of the code readiness level, you can enable specific features by setting environment variables:

  • MEASUREMENTLINK_ENABLE_NEW_SESSION_API=1: enable the NEW_SESSION_API feature

Testing

In automated tests, the default code readiness level is PROTOTYPE. Note that this applies only to the pytest process, not child processes.

You can use marks to enable or disable specific features for a specific test function or test module:

  • @pytest.mark.enable_feature_toggle(NEW_SESSION_API): enable the NEW_SESSION_API feature
  • @pytest.mark.disable_feature_toggle(XY_DATA): disable the XY_DATA feature

You can also use marks to set the code readiness level for a specific test function or test module:

  • @pytest.mark.use_code_readiness(CodeReadiness.NEXT_RELEASE): allow features with code readiness of NEXT_RELEASE or RELEASE
Clone this wiki locally