You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
6
6
7
-
### Prerequisites
7
+
##Get Started
8
8
9
-
* Python 3.7 or later is required to use this package.
9
+
[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.
10
10
11
-
### Install the package
11
+
[API Reference](https://microsoft.github.io/FeatureManagement-Python/): This API reference details the API surface of the libraries contained within this repository.
12
12
13
-
Install the Python feature management client library for Python with [pip][pip]:
13
+
## Examples
14
14
15
-
```bash
16
-
pip install featuremanagement
17
-
```
18
-
19
-
## Usage
20
-
21
-
You can use feature flags from the Azure App Configuration service, a json file, or a dictionary.
22
-
23
-
### Use feature flags from Azure App Configuration
f =open(script_directory +"/my_json_file.json", "r")
76
-
77
-
feature_flags = json.load(f)
78
-
79
-
feature_manager = FeatureManager(feature_flags)
80
-
81
-
# Returns the value of Alpha, based on the result of the feature filter
82
-
print("Alpha is ", feature_manager.is_enabled("Alpha"))
83
-
```
84
-
85
-
### Use feature flags from a dictionary
86
-
87
-
```python
88
-
from featuremanagement import FeatureManager
89
-
90
-
feature_flags = {
91
-
"feature_management": {
92
-
"feature_flags": [
93
-
{
94
-
"id": "Alpha",
95
-
"description": "",
96
-
"enabled": "true",
97
-
"conditions": {
98
-
"client_filters": []
99
-
}
100
-
}
101
-
]
102
-
}
103
-
}
104
-
105
-
feature_manager = FeatureManager(feature_flags)
106
-
107
-
# Is always true
108
-
print("Alpha is ", feature_manager.is_enabled("Alpha"))
109
-
```
110
-
111
-
## Key concepts
112
-
113
-
### FeatureManager
114
-
115
-
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.
116
-
117
-
### Feature Flags
118
-
119
-
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.
120
-
121
-
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).
122
-
123
-
```javascript
124
-
{
125
-
"id":"Alpha",
126
-
"enabled":"true",
127
-
"conditions": {
128
-
"client_filters": [
129
-
{
130
-
"name":"MyFilter",
131
-
"parameters": {
132
-
...
133
-
}
134
-
}
135
-
]
136
-
}
137
-
}
138
-
```
139
-
140
-
This object is passed into the `FeatureManager` when it is initialized.
141
-
142
-
### Feature Filters
143
-
144
-
Feature filters enable dynamic evaluation of feature flags. The Python feature management library includes two built-in filters:
145
-
146
-
-`Microsoft.TimeWindow` - Enables a feature flag based on a time window.
147
-
-`Microsoft.Targeting` - Enables a feature flag based on a list of users, groups, or rollout percentages.
148
-
149
-
#### Time Window Filter
150
-
151
-
The Time Window Filter enables a feature flag based on a time window. It has two parameters:
152
-
153
-
-`Start` - The start time of the time window.
154
-
-`End` - The end time of the time window.
155
-
156
-
```json
157
-
{
158
-
"name": "Microsoft.TimeWindow",
159
-
"parameters": {
160
-
"Start": "2020-01-01T00:00:00Z",
161
-
"End": "2020-12-31T00:00:00Z"
162
-
}
163
-
}
164
-
```
165
-
166
-
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.
167
-
168
-
#### Targeting Filter
169
-
170
-
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.
171
-
172
-
The following steps demonstrate an example of a progressive rollout for a new 'Beta' feature:
173
-
174
-
1. Individual users Jeff and Alicia are granted access to the Beta
175
-
1. Another user, Mark, asks to opt-in and is included.
176
-
1. Twenty percent of a group known as "Ring1" users are included in the Beta.
177
-
1. The number of "Ring1" users included in the beta is bumped up to 100 percent.
178
-
1. Five percent of the user base is included in the beta.
179
-
1. The rollout percentage is bumped up to 100 percent and the feature is completely rolled out.
180
-
181
-
This strategy for rolling out a feature is built in to the library through the included Microsoft.Targeting feature filter.
182
-
183
-
##### Defining a Targeting Feature Filter
184
-
185
-
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:
186
-
187
-
-`Users` - A list of users that the feature flag is enabled for.
188
-
-`Groups` - A list of groups that the feature flag is enabled for and a rollout percentage for each group.
189
-
-`Name` - The name of the group.
190
-
-`RolloutPercentage` - A percentage value that the feature flag is enabled for in the given group.
191
-
-`DefaultRolloutPercentage` - A percentage value that the feature flag is enabled for.
192
-
-`Exclusion` - An object that contains a list of users and groups that the feature flag is disabled for.
193
-
-`Users` - A list of users that the feature flag is disabled for.
194
-
-`Groups` - A list of groups that the feature flag is disabled for.
195
-
196
-
```json
197
-
{
198
-
"name": "Microsoft.Targeting",
199
-
"parameters": {
200
-
"Audience": {
201
-
"Users": ["user1", "user2"],
202
-
"Groups": [
203
-
{
204
-
"Name": "group1",
205
-
"RolloutPercentage": 100
206
-
}
207
-
],
208
-
"DefaultRolloutPercentage": 50,
209
-
"Exclusion": {
210
-
"Users": ["user3"],
211
-
"Groups": ["group2"]
212
-
}
213
-
}
214
-
}
215
-
}
216
-
```
217
-
218
-
##### Using Targeting Feature Filter
219
-
220
-
You can provide the current user info through `kwargs` when calling `isEnabled`.
221
-
222
-
```python
223
-
from featuremanagement import FeatureManager, TargetingContext
224
-
225
-
# Returns true, because user1 is in the Users list
You can also create your own feature filters by implementing the `FeatureFilter` interface.
238
-
239
-
```python
240
-
classMyCustomFilter(FeatureFilter):
241
-
242
-
defevaluate(self, context, **kwargs):
243
-
...
244
-
returnTrue
245
-
```
246
-
247
-
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.
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`.
255
-
256
-
```javascript
257
-
{
258
-
"name":"CustomFilter",
259
-
"parameters": {
260
-
...
261
-
}
262
-
}
263
-
```
264
-
265
-
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.
0 commit comments