-
Notifications
You must be signed in to change notification settings - Fork 5
Implement 'open feature' feature flagging #1371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2cee12d
Install openfeature-sdk
gpeng 0c117f7
Add DEPLOYED_TO environment variable
gpeng 0db3e5d
Basic implementation of openfeature
gpeng bf864b3
Implement gateway_images_enabled flag
gpeng 6ab7dec
Add docs/feature-flags.md
gpeng 8531d32
COPY flags files
gpeng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| # Feature flags | ||
|
|
||
| Feature flags are implemented using [OpenFeature](https://openfeature.dev/) with an `InMemoryProvider`. Flags are declared in YAML files and loaded at startup. | ||
|
|
||
| ## Flag files | ||
|
|
||
| Each environment has its own flag file. The application selects the file based on the `DEPLOYED_TO` environment variable, falling back to `flags.yml` for local development where `DEPLOYED_TO` is not set. | ||
|
|
||
| | Environment | File | | ||
| | ---------------------------- | ---------------------- | | ||
| | dev | `flags.dev.yml` | | ||
| | review | `flags.review.yml` | | ||
| | preprod | `flags.preprod.yml` | | ||
| | production | `flags.production.yml` | | ||
| | local (no `DEPLOYED_TO` set) | `flags.yml` | | ||
|
|
||
| ## Adding a flag | ||
|
|
||
| **1. Declare the flag in each environment's file.** | ||
|
|
||
| Add an entry to all five flag files (`flags.yml`, `flags.dev.yml`, `flags.review.yml`, `flags.preprod.yml`, `flags.production.yml`). Set the value to `true` to enable or `false` to disable in that environment. | ||
|
|
||
| ```yaml | ||
| flags: | ||
| my_flag: false | ||
| ``` | ||
|
|
||
| **2. Check the flag in application code.** | ||
|
|
||
| ```python | ||
| from manage_breast_screening.core.feature_flags import FeatureFlag | ||
|
|
||
| if FeatureFlag.is_enabled("my_flag"): | ||
| # feature is enabled | ||
| ``` | ||
|
|
||
| `FeatureFlag.is_enabled` returns `False` if the flag is missing or the provider is unavailable. If a flag name is not found in the YAML file, OpenFeature silently returns the fallback rather than raising an error — so a typo in a flag name will return `False` without any indication something is wrong. | ||
|
|
||
| ## Enabling a flag in tests | ||
|
|
||
| Use the `with_flag_enabled` fixture to turn a flag on for the duration of a single test: | ||
|
|
||
| ```python | ||
| def test_something(with_flag_enabled): | ||
| with_flag_enabled("my_flag") | ||
| # flag is enabled for this test only | ||
| ``` | ||
|
|
||
| Multiple flags can be enabled by calling it more than once: | ||
|
|
||
| ```python | ||
| def test_something(with_flag_enabled): | ||
| with_flag_enabled("my_flag") | ||
| with_flag_enabled("another_flag") | ||
| ``` | ||
|
|
||
| In class-based tests the fixture must be relayed via an `autouse` fixture so it is accessible to helper methods: | ||
|
|
||
| ```python | ||
| @pytest.fixture(autouse=True) | ||
| def flags(self, with_flag_enabled): | ||
| self.with_flag_enabled = with_flag_enabled | ||
|
|
||
| def and_my_flag_is_enabled(self): | ||
| self.with_flag_enabled("my_flag") | ||
| ``` | ||
|
|
||
| After each test the flags are reset to the defaults from the YAML file. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| flags: | ||
| gateway_images: false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| flags: | ||
| gateway_images: false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| flags: | ||
| gateway_images: false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| flags: | ||
| gateway_images: false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| flags: | ||
| gateway_images: false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| from pathlib import Path | ||
|
|
||
| import yaml | ||
| from openfeature import api | ||
| from openfeature.provider.in_memory_provider import InMemoryFlag, InMemoryProvider | ||
|
|
||
|
|
||
| class FeatureFlag: | ||
| @staticmethod | ||
| def is_enabled(name: str) -> bool: | ||
| return api.get_client().get_boolean_value(name, False) | ||
|
|
||
|
|
||
| def setup_feature_flags(flags_yaml_path: Path) -> None: | ||
| """Initialise the OpenFeature InMemoryProvider from a YAML flags file. | ||
|
|
||
| Call from AppConfig.ready(). The YAML file must follow the structure:: | ||
|
|
||
| flags: | ||
| my_flag: true # true to enable, false to disable | ||
| """ | ||
| with flags_yaml_path.open() as f: | ||
| data = yaml.safe_load(f) | ||
|
|
||
| raw_flags = data.get("flags", {}) | ||
| provider_flags = { | ||
| name: InMemoryFlag( | ||
| default_variant="on" if enabled else "off", | ||
| variants={"on": True, "off": False}, | ||
| ) | ||
| for name, enabled in raw_flags.items() | ||
| } | ||
| api.set_provider(InMemoryProvider(provider_flags)) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import pytest | ||
| from openfeature import api | ||
|
|
||
| from manage_breast_screening.core.apps import _FLAGS_YAML | ||
| from manage_breast_screening.core.feature_flags import setup_feature_flags | ||
|
|
||
|
|
||
| class TestSetupFeatureFlags: | ||
| @pytest.fixture(autouse=True) | ||
| def reset_flags(self): | ||
| yield | ||
| setup_feature_flags(_FLAGS_YAML) | ||
|
|
||
| def test_enabled_flag_returns_true(self, tmp_path): | ||
| flags_file = tmp_path / "flags.yml" | ||
| flags_file.write_text("flags:\n my_flag: true\n") | ||
| setup_feature_flags(flags_file) | ||
|
|
||
| assert api.get_client().get_boolean_value("my_flag", False) is True | ||
|
|
||
| def test_disabled_flag_returns_false(self, tmp_path): | ||
| flags_file = tmp_path / "flags.yml" | ||
| flags_file.write_text("flags:\n my_flag: false\n") | ||
| setup_feature_flags(flags_file) | ||
|
|
||
| assert api.get_client().get_boolean_value("my_flag", False) is False | ||
|
|
||
| def test_missing_flag_returns_fallback(self, tmp_path): | ||
| flags_file = tmp_path / "flags.yml" | ||
| flags_file.write_text("flags: {}\n") | ||
| setup_feature_flags(flags_file) | ||
|
|
||
| assert api.get_client().get_boolean_value("nonexistent_flag", False) is False | ||
|
|
||
|
|
||
| class TestWithFlagEnabledFixture: | ||
| def test_enabled_flag_returns_true(self, with_flag_enabled): | ||
| with_flag_enabled("my_flag") | ||
|
|
||
| assert api.get_client().get_boolean_value("my_flag", False) is True | ||
|
|
||
| def test_unenabled_flag_returns_fallback(self, with_flag_enabled): | ||
| assert api.get_client().get_boolean_value("my_flag", False) is False | ||
|
|
||
| def test_multiple_flags_can_be_enabled(self, with_flag_enabled): | ||
| with_flag_enabled("flag_one") | ||
| with_flag_enabled("flag_two") | ||
|
|
||
| assert api.get_client().get_boolean_value("flag_one", False) is True | ||
| assert api.get_client().get_boolean_value("flag_two", False) is True | ||
|
|
||
| def test_enabling_one_flag_does_not_enable_others(self, with_flag_enabled): | ||
| with_flag_enabled("flag_one") | ||
|
|
||
| assert api.get_client().get_boolean_value("flag_two", False) is False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,9 @@ | ||
| import os | ||
|
|
||
| from manage_breast_screening.core.feature_flags import FeatureFlag | ||
| from manage_breast_screening.gateway.models import Relay | ||
|
|
||
|
|
||
| def gateway_images_enabled(appointment): | ||
| """Check if automatic gateway image retrieval is enabled.""" | ||
| if os.getenv("GATEWAY_IMAGES_ENABLED", "false").lower() == "true": | ||
| if FeatureFlag.is_enabled("gateway_images"): | ||
| return Relay.for_appointment(appointment) is not None | ||
| return False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.