|
| 1 | +# Python |
| 2 | +See [here](https://learn.microsoft.com/en-us/azure/azure-app-configuration/howto-variant-feature-flags-python) for the full tutorial. |
| 3 | + |
| 4 | +## Install SDK |
| 5 | + |
| 6 | +``` |
| 7 | +pip install azure-identity |
| 8 | +pip install azure-appconfiguration-provider |
| 9 | +pip install featuremanagement[AzureMonitor] |
| 10 | +``` |
| 11 | + |
| 12 | +## Define a targeting context accessor method |
| 13 | + |
| 14 | +The accessor method is called from both Feature Management and Azure Monitor to identify the user for the current request. |
| 15 | + |
| 16 | +```python |
| 17 | +async def my_targeting_accessor() -> TargetingContext: |
| 18 | + user_id = request.headers.get('user-id', str(uuid.uuid4()) |
| 19 | + return TargetingContext(user_id=user_id) |
| 20 | +``` |
| 21 | + |
| 22 | +## Initialize Application Insights |
| 23 | + |
| 24 | +[Connect to Application Insights](https://learn.microsoft.com/en-us/azure/azure-monitor/app/asp-net-core) to instrument your application. Include the `TargetingSpanProcessor` to allow request and dependency data to be available for metrics. |
| 25 | + |
| 26 | +```python |
| 27 | +from azure.monitor.opentelemetry import configure_azure_monitor |
| 28 | +from featuremanagement.azuremonitor import TargetingSpanProcessor |
| 29 | + |
| 30 | +configure_azure_monitor( |
| 31 | + connection_string=os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING"), |
| 32 | + span_processors=[TargetingSpanProcessor(targeting_context_accessor=my_targeting_accessor)] |
| 33 | +) |
| 34 | +``` |
| 35 | + |
| 36 | +## Initialize App Configuration and Feature Manager |
| 37 | + |
| 38 | +[Connect to App Configuration](https://learn.microsoft.com/azure/azure-app-configuration/quickstart-feature-flag-python#console-applications), which supplies feature flags and other configurations to your application. Use `DefaultAzureCredential` to authenticate to your App Configuration store. Follow the [instructions](https://learn.microsoft.com/en-us/azure/azure-app-configuration/concept-enable-rbac#authentication-with-token-credentials) to assign your credential the App Configuration Data Reader role. |
| 39 | + |
| 40 | +```python |
| 41 | +from azure.appconfiguration.provider import load |
| 42 | +from azure.identity import DefaultAzureCredential |
| 43 | +from featuremanagement import FeatureManager |
| 44 | +from featuremanagement.azuremonitor import publish_telemetry, track_event |
| 45 | + |
| 46 | +azure_app_config = load(endpoint="<your-endpoint>", credential=DefaultAzureCredential(), feature_flag_enabled=True, feature_flag_refresh_enabled=True) |
| 47 | +feature_manager = FeatureManager(azure_app_config, on_feature_evaluated=publish_telemetry, targeting_context_accessor=my_targeting_accessor) |
| 48 | +``` |
| 49 | + |
| 50 | +## Use Feature Flag |
| 51 | + |
| 52 | +Use feature manager to get the feature variant assigned to the user. |
| 53 | + |
| 54 | +```python |
| 55 | +my_feature_flag = feature_manager.get_variant("<placeholder-feature-flag-name>"): |
| 56 | +if my_feature_flag: |
| 57 | + variant_name = my_feature_flag.name |
| 58 | + variant_value = my_feature_flag.configuration |
| 59 | + # do something with the variant value |
| 60 | +``` |
| 61 | + |
| 62 | +## Log event |
| 63 | +``` |
| 64 | +# The user_id from the targeting context needs to be manually added for now due to an missing Azure Monitor support. |
| 65 | +track_event("<placeholder-event-name>", my_targeting_accessor().user_id) |
| 66 | +``` |
| 67 | + |
| 68 | +## Validate Event Logs |
| 69 | +- Run your application and trigger one or more get_variant() calls for the test feature flag. |
| 70 | +- Go to the Log Analytics workspace (add link to LA workspace/log page) and execute the following query. After a short ingestion delay, you should see the log events for the feature evaluation calls. |
| 71 | + |
| 72 | +``` |
| 73 | +AppEvents | where Name == "FeatureEvaluation" |
| 74 | +``` |
0 commit comments