|
| 1 | +--- |
| 2 | +title: Enable telemetry for feature flags in a Python application (preview) |
| 3 | +titleSuffix: Azure App Configuration |
| 4 | +description: Learn how to use telemetry in python for feature flags in Azure App Configuration. |
| 5 | +ms.service: azure-app-configuration |
| 6 | +author: mrm9084 |
| 7 | +ms.author: mametcal |
| 8 | +ms.topic: how-to |
| 9 | +ms.date: 05/06/2025 |
| 10 | +--- |
| 11 | + |
| 12 | +# Enable telemetry for feature flags in a Python application |
| 13 | + |
| 14 | +In this tutorial, you use telemetry in your Python application to track feature flag evaluations and custom events. Telemetry allows you to make informed decisions about your feature management strategy. You utilize the feature flag with telemetry enabled created in [Enable telemetry for feature flags](./howto-telemetry.md). Before proceeding, ensure that you create a feature flag named *Greeting* in your Configuration store with telemetry enabled. This tutorial builds on top of [use variant feature flags](./howto-variant-feature-flags-python.md). |
| 15 | + |
| 16 | +## Prerequisites |
| 17 | + |
| 18 | +- The variant feature flag with telemetry enabled from [Enable telemetry for feature flags](./howto-telemetry.md). |
| 19 | +- The application from [Use variant feature flags](./howto-variant-feature-flags-python.md). |
| 20 | + |
| 21 | +## Add telemetry to your Python application |
| 22 | + |
| 23 | +1. Install the required packages using pip: |
| 24 | + |
| 25 | + ```bash |
| 26 | + pip install azure-appconfiguration-provider |
| 27 | + pip install featuremanagement["AzureMonitor"] |
| 28 | + pip install azure-monitor-opentelemetry |
| 29 | + ``` |
| 30 | + |
| 31 | +1. Open `app.py` and configure your code to connect to Application Insights to publish telemetry. |
| 32 | + |
| 33 | + ```python |
| 34 | + import os |
| 35 | + from azure.monitor.opentelemetry import configure_azure_monitor |
| 36 | +
|
| 37 | + # Configure Azure Monitor |
| 38 | + configure_azure_monitor(connection_string=os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING")) |
| 39 | + ``` |
| 40 | + |
| 41 | +1. Also in `app.py` load your feature flags from App Configuration and load them into feature management. `FeatureManager` uses the `publish_telemetry` callback function to publish telemetry to Azure Monitor. |
| 42 | + |
| 43 | + ```python |
| 44 | + from featuremanagement.azuremonitor import publish_telemetry |
| 45 | +
|
| 46 | + feature_manager = FeatureManager(config, on_feature_evaluated=publish_telemetry) |
| 47 | + ``` |
| 48 | + |
| 49 | +1. Open `routes.py` and update your code to track your own events in your application. When `track_event` is called, a custom event is published to Azure Monitor with the provided user. |
| 50 | + |
| 51 | + ```python |
| 52 | + from featuremanagement import track_event |
| 53 | + |
| 54 | + @bp.route("/heart", methods=["POST"]) |
| 55 | + def heart(): |
| 56 | + if current_user.is_authenticated: |
| 57 | + user = current_user.username |
| 58 | + |
| 59 | + # Track the appropriate event based on the action |
| 60 | + track_event("Liked", user) |
| 61 | + return jsonify({"status": "success"}) |
| 62 | + ``` |
| 63 | +
|
| 64 | +1. Open `index.html` and update the code to implement the like button. The like button sends a POST request to the `/heart` endpoint when clicked. |
| 65 | +
|
| 66 | + ```html |
| 67 | + <script> |
| 68 | + function heartClicked(button) { |
| 69 | + var icon = button.querySelector('i'); |
| 70 | + |
| 71 | + // Toggle the heart icon appearance |
| 72 | + icon.classList.toggle('far'); |
| 73 | + icon.classList.toggle('fas'); |
| 74 | + |
| 75 | + // Only send a request to the dedicated heart endpoint when it's a like action |
| 76 | + if (icon.classList.contains('fas')) { |
| 77 | + fetch('/heart', { |
| 78 | + method: 'POST', |
| 79 | + headers: { |
| 80 | + 'Content-Type': 'application/json', |
| 81 | + } |
| 82 | + }); |
| 83 | + } |
| 84 | + } |
| 85 | + </script> |
| 86 | + ``` |
| 87 | +
|
| 88 | +## Build and run the app |
| 89 | +
|
| 90 | +1. Application insights requires a connection string to connect to your Application Insights resource. Set the `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable to the connection string for your Application Insights resource. |
| 91 | +
|
| 92 | + ```cmd |
| 93 | + setx APPLICATIONINSIGHTS_CONNECTION_STRING "applicationinsights-connection-string" |
| 94 | + ``` |
| 95 | +
|
| 96 | + If you use PowerShell, run the following command: |
| 97 | +
|
| 98 | + ```powershell |
| 99 | + $Env:APPLICATIONINSIGHTS_CONNECTION_STRING = "applicationinsights-connection-string" |
| 100 | + ``` |
| 101 | +
|
| 102 | + If you use macOS or Linux, run the following command: |
| 103 | +
|
| 104 | + ```bash |
| 105 | + export APPLICATIONINSIGHTS_CONNECTION_STRING='applicationinsights-connection-string' |
| 106 | + ``` |
| 107 | +
|
| 108 | +1. Run the application, [see step 2 of Use variant feature flags](./howto-variant-feature-flags-python.md#build-and-run-the-app) . |
| 109 | +1. Create 10 different users and log into the application. As you log in with each user, you get a different message variant for some of them. ~50% of the time you get no message. 25% of the time you get the message "Hello!" and 25% of the time you get "I hope this makes your day!". |
| 110 | +1. With some of the users select the **Like** button to trigger the telemetry event. |
| 111 | +1. Open your Application Insights resource in the Azure portal and select **Logs** under **Monitoring**. In the query window, run the following query to see the telemetry events: |
| 112 | +
|
| 113 | + ```kusto |
| 114 | + // Total users |
| 115 | + let total_users = |
| 116 | + customEvents |
| 117 | + | where name == "FeatureEvaluation" |
| 118 | + | summarize TotalUsers = count() by Variant = tostring(customDimensions.Variant); |
| 119 | + |
| 120 | + // Hearted users |
| 121 | + let hearted_users = |
| 122 | + customEvents |
| 123 | + | where name == "FeatureEvaluation" |
| 124 | + | extend TargetingId = tostring(customDimensions.TargetingId) |
| 125 | + | join kind=inner ( |
| 126 | + customEvents |
| 127 | + | where name == "Liked" |
| 128 | + | extend TargetingId = tostring(customDimensions.TargetingId) |
| 129 | + ) on TargetingId |
| 130 | + | summarize HeartedUsers = count() by Variant = tostring(customDimensions.Variant); |
| 131 | + |
| 132 | + // Calculate the percentage of hearted users over total users |
| 133 | + let combined_data = |
| 134 | + total_users |
| 135 | + | join kind=leftouter (hearted_users) on Variant |
| 136 | + | extend HeartedUsers = coalesce(HeartedUsers, 0) |
| 137 | + | extend PercentageHearted = strcat(round(HeartedUsers * 100.0 / TotalUsers, 1), "%") |
| 138 | + | project Variant, TotalUsers, HeartedUsers, PercentageHearted; |
| 139 | + |
| 140 | + // Calculate the sum of total users and hearted users of all variants |
| 141 | + let total_sum = |
| 142 | + combined_data |
| 143 | + | summarize Variant="All", TotalUsers = sum(TotalUsers), HeartedUsers = sum(HeartedUsers); |
| 144 | + |
| 145 | + // Display the combined data along with the sum of total users and hearted users |
| 146 | + combined_data |
| 147 | + | union (total_sum) |
| 148 | + ``` |
| 149 | +
|
| 150 | + > [!div class="mx-imgBorder"] |
| 151 | + >  |
| 152 | +
|
| 153 | + You see one "FeatureEvaluation" for each time the quote page was loaded and one "Liked" event for each time the like button was clicked. The "FeatureEvaluation" event have a custom property called `FeatureName` with the name of the feature flag that was evaluated. Both events have a custom property called `TargetingId` with the name of the user that liked the quote. |
| 154 | +
|
| 155 | +## Additional resources |
| 156 | +- [Flask Quote of the Day sample](https://github.com/Azure-Samples/quote-of-the-day-python) |
0 commit comments