From 6808469f81871fff4e0a9a69a83373746f182b34 Mon Sep 17 00:00:00 2001 From: Matt Metcalf Date: Tue, 25 Jun 2024 16:56:22 -0700 Subject: [PATCH 1/3] GA Prep --- README.md | 275 ++-------------------------------- featuremanagement/_version.py | 2 +- 2 files changed, 11 insertions(+), 266 deletions(-) diff --git a/README.md b/README.md index f59dade..d4a3645 100644 --- a/README.md +++ b/README.md @@ -1,274 +1,21 @@ # Microsoft Feature Management for Python -Feature Management is a library for enabling/disabling features at runtime. Developers can use feature flags in simple use cases like conditional statement to more advanced scenarios like conditionally adding routes. +[![FeatureManagement](https://img.shields.io/pypi/v/FeatureManagement?label=FeatureManagement)](https://pypi.org/project/FeatureManagement/) -## Getting started +Feature management provides a way to develop and expose application functionality based on features. Many applications have special requirements when a new feature is developed such as when the feature should be enabled and under what conditions. This library provides a way to define these relationships, and also integrates into common Python code patterns to make exposing these features possible. -### Prerequisites +## Get Started -* Python 3.7 or later is required to use this package. +[Quickstart](https://learn.microsoft.com/azure/azure-app-configuration/quickstart-feature-flag-python): A quickstart guide is available to learn how to integrate feature flags from Azure App Configuration into your Python applications. -### Install the package +[API Reference](https://microsoft.github.io/FeatureManagement-Python/): This API reference details the API surface of the libraries contained within this repository. -Install the Python feature management client library for Python with [pip][pip]: +## Examples -```bash -pip install featuremanagement -``` - -## Usage - -You can use feature flags from the Azure App Configuration service, a json file, or a dictionary. - -### Use feature flags from Azure App Configuration - -```python -from featuremanagement import FeatureManager -from azure.appconfiguration.provider import load -from azure.identity import DefaultAzureCredential -import os - -endpoint = os.environ.get("APPCONFIGURATION_ENDPOINT_STRING") - -# If no setting selector is set then feature flags with no label are loaded. -selects = {SettingSelector(key_filter=".appconfig.featureflag*")} - -config = load(endpoint=endpoint, credential=DefaultAzureCredential(), selects=selects) - -feature_manager = FeatureManager(config) - -# Prints the value of the feature flag Alpha -print("Alpha is ", feature_manager.is_enabled("Alpha")) -``` - -### Use feature flags from a json file - -A Json file with the following format can be used to load feature flags. - -```json -{ - "feature_management": { - "feature_flags": [ - { - "id": "Alpha", - "description": "", - "enabled": "true", - "conditions": { - "client_filters": [] - } - } - ] - } - } -``` - -Load feature flags from a json file. - -```python -from featuremanagement import FeatureManager -import json -import os -import sys - -script_directory = os.path.dirname(os.path.abspath(sys.argv[0])) - -f = open(script_directory + "/my_json_file.json", "r") - -feature_flags = json.load(f) - -feature_manager = FeatureManager(feature_flags) - -# Returns the value of Alpha, based on the result of the feature filter -print("Alpha is ", feature_manager.is_enabled("Alpha")) -``` - -### Use feature flags from a dictionary - -```python -from featuremanagement import FeatureManager - -feature_flags = { - "feature_management": { - "feature_flags": [ - { - "id": "Alpha", - "description": "", - "enabled": "true", - "conditions": { - "client_filters": [] - } - } - ] - } -} - -feature_manager = FeatureManager(feature_flags) - -# Is always true -print("Alpha is ", feature_manager.is_enabled("Alpha")) -``` - -## Key concepts - -### FeatureManager - -The `FeatureManager` is the main entry point for using feature flags. It is initialized with a dictionary of feature flags, and optional feature filters. The `FeatureManager` can then be used to check if a feature is enabled or disabled. - -### Feature Flags - -Feature Flags are objects that define how Feature Management enables/disables a feature. It contains an `id` and `enabled` property. The `id` is a string that uniquely identifies the feature flag. The `enabled` property is a boolean that indicates if the feature flag is enabled or disabled. The `conditions` object contains a property `client_filters` which is a list of `FeatureFilter` objects that are used to determine if the feature flag is enabled or disabled. The Feature Filters only run if the feature flag is enabled. - -The full schema for a feature Flag can be found [here](https://github.com/Azure/AppConfiguration/blob/main/docs/FeatureManagement/FeatureFlag.v1.1.0.schema.json). - -```javascript -{ - "id": "Alpha", - "enabled": "true", - "conditions": { - "client_filters": [ - { - "name": "MyFilter", - "parameters": { - ... - } - } - ] - } -} -``` - -This object is passed into the `FeatureManager` when it is initialized. - -### Feature Filters - -Feature filters enable dynamic evaluation of feature flags. The Python feature management library includes two built-in filters: - -- `Microsoft.TimeWindow` - Enables a feature flag based on a time window. -- `Microsoft.Targeting` - Enables a feature flag based on a list of users, groups, or rollout percentages. - -#### Time Window Filter - -The Time Window Filter enables a feature flag based on a time window. It has two parameters: - -- `Start` - The start time of the time window. -- `End` - The end time of the time window. - -```json -{ - "name": "Microsoft.TimeWindow", - "parameters": { - "Start": "2020-01-01T00:00:00Z", - "End": "2020-12-31T00:00:00Z" - } -} -``` - -Both parameters are optional, but at least one is required. The time window filter is enabled after the start time and before the end time. If the start time is not specified, it is enabled immediately. If the end time is not specified, it will remain enabled after the start time. - -#### Targeting Filter - -Targeting is a feature management strategy that enables developers to progressively roll out new features to their user base. The strategy is built on the concept of targeting a set of users known as the target audience. An audience is made up of specific users, groups, excluded users/groups, and a designated percentage of the entire user base. The groups that are included in the audience can be broken down further into percentages of their total members. - -The following steps demonstrate an example of a progressive rollout for a new 'Beta' feature: - -1. Individual users Jeff and Alicia are granted access to the Beta -1. Another user, Mark, asks to opt-in and is included. -1. Twenty percent of a group known as "Ring1" users are included in the Beta. -1. The number of "Ring1" users included in the beta is bumped up to 100 percent. -1. Five percent of the user base is included in the beta. -1. The rollout percentage is bumped up to 100 percent and the feature is completely rolled out. - -This strategy for rolling out a feature is built in to the library through the included Microsoft.Targeting feature filter. - -##### Defining a Targeting Feature Filter - -The Targeting Filter provides the capability to enable a feature for a target audience. The filter parameters include an `Audience` object which describes users, groups, excluded users/groups, and a default percentage of the user base that should have access to the feature. The `Audience` object contains the following fields: - -- `Users` - A list of users that the feature flag is enabled for. -- `Groups` - A list of groups that the feature flag is enabled for and a rollout percentage for each group. - - `Name` - The name of the group. - - `RolloutPercentage` - A percentage value that the feature flag is enabled for in the given group. -- `DefaultRolloutPercentage` - A percentage value that the feature flag is enabled for. -- `Exclusion` - An object that contains a list of users and groups that the feature flag is disabled for. - - `Users` - A list of users that the feature flag is disabled for. - - `Groups` - A list of groups that the feature flag is disabled for. - -```json -{ - "name": "Microsoft.Targeting", - "parameters": { - "Audience": { - "Users": ["user1", "user2"], - "Groups": [ - { - "Name": "group1", - "RolloutPercentage": 100 - } - ], - "DefaultRolloutPercentage": 50, - "Exclusion": { - "Users": ["user3"], - "Groups": ["group2"] - } - } - } -} -``` - -##### Using Targeting Feature Filter - -You can provide the current user info through `kwargs` when calling `isEnabled`. - -```python -from featuremanagement import FeatureManager, TargetingContext - -# Returns true, because user1 is in the Users list -feature_manager.is_enabled("Beta", TargetingContext(user_id="user1", groups=["group1"])) - -# Returns false, because group2 is in the Exclusion.Groups list -feature_manager.is_enabled("Beta", TargetingContext(user_id="user1", groups=["group2"])) - -# Has a 50% chance of returning true, but will be conisistent for the same user -feature_manager.is_enabled("Beta", TargetingContext(user_id="user4")) -``` - -#### Custom Filters - -You can also create your own feature filters by implementing the `FeatureFilter` interface. - -```python -class MyCustomFilter(FeatureFilter): - - def evaluate(self, context, **kwargs): - ... - return True -``` - -They can then be passed into the `FeatureManager` when it is initialized. By default, the name of a feature filter is the name of the class. You can override this by setting a class attribute `alias` to the modified class name. - -```python - -feature_manager = FeatureManager(feature_flags, feature_filters={MyCustomFilter(), MyOtherFilter()}) -``` - -The `evaluate` method is called when checking if a feature flag is enabled. The `context` parameter contains information about the feature filter from the `parameters` field of the feature filter. Any additional parameters can be passed in as keyword arguments when calling `is_enabled`. - -```javascript -{ - "name": "CustomFilter", - "parameters": { - ... - } -} -``` - -You can modify the name of a feature flag by using the `@FeatureFilter.alias` decorator. The alias overrides the name of the feature filter and needs to match the name of the feature filter in the feature flag json. - -```python -@FeatureFilter.alias("AliasFilter") -class MyCustomFilter(FeatureFilter): - ... -``` +* [Python Application](https://github.com/microsoft/FeatureManagement-Python/blob/main/samples/feature_flag_sample.py) +* [Python Application with Azure App Configuration](https://github.com/microsoft/FeatureManagement-Python/blob/main/samples/feature_flag_with_azure_app_configuration_sample.py) +* [Djano Application](https://github.com/Azure/AppConfiguration/tree/main/examples/Python/python-django-webapp-sample) +* [Flask Application](https://github.com/Azure/AppConfiguration/tree/main/examples/Python/python-flask-webapp-sample) ## Contributing @@ -291,5 +38,3 @@ trademarks or logos is subject to and must follow [Microsoft's Trademark & Brand Guidelines](https://www.microsoft.com/en-us/legal/intellectualproperty/trademarks/usage/general). Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies. - -[pip]: https://pypi.org/project/FeatureManagement/ diff --git a/featuremanagement/_version.py b/featuremanagement/_version.py index 839ee5f..f0d8f67 100644 --- a/featuremanagement/_version.py +++ b/featuremanagement/_version.py @@ -4,4 +4,4 @@ # license information. # ------------------------------------------------------------------------- -VERSION = "1.0.0b1" +VERSION = "1.0.0" From 6a41cb2adffbe1b51d63449606d2c3c939bd3dc2 Mon Sep 17 00:00:00 2001 From: Matt Metcalf Date: Tue, 25 Jun 2024 16:57:26 -0700 Subject: [PATCH 2/3] Update CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 30d8665..9866269 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Release History +## 1.0.0 (06/26/2024) + +Updated version to 1.0.0. + ## 1.0.0b1 (05/22/2024) New Feature Management library. From c38d88ef5001f0e268fed36b6aa131309fbf380e Mon Sep 17 00:00:00 2001 From: Matthew Metcalf Date: Wed, 26 Jun 2024 10:21:20 -0700 Subject: [PATCH 3/3] Update README.md Co-authored-by: Ross Grambo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d4a3645..1fc3dc3 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Feature management provides a way to develop and expose application functionalit * [Python Application](https://github.com/microsoft/FeatureManagement-Python/blob/main/samples/feature_flag_sample.py) * [Python Application with Azure App Configuration](https://github.com/microsoft/FeatureManagement-Python/blob/main/samples/feature_flag_with_azure_app_configuration_sample.py) -* [Djano Application](https://github.com/Azure/AppConfiguration/tree/main/examples/Python/python-django-webapp-sample) +* [Django Application](https://github.com/Azure/AppConfiguration/tree/main/examples/Python/python-django-webapp-sample) * [Flask Application](https://github.com/Azure/AppConfiguration/tree/main/examples/Python/python-flask-webapp-sample) ## Contributing