Skip to content

Commit 716c798

Browse files
Merge pull request #290006 from mrm9084/EnableTelemetry
App Configuration Telemetry Enabled
2 parents 6b99693 + 819e23a commit 716c798

File tree

8 files changed

+218
-14
lines changed

8 files changed

+218
-14
lines changed

articles/azure-app-configuration/TOC.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,12 @@
220220
href: howto-variant-feature-flags-python.md
221221
- name: JavaScript
222222
href: howto-variant-feature-flags-javascript.md
223+
- name: Enable telemetry for feature flags
224+
items:
225+
- name: Overview
226+
href: howto-telemetry.md
227+
- name: Python
228+
href: howto-telemetry-python.md
223229
- name: CI/CD integration
224230
items:
225231
- name: Use configuration files
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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+
> ![Screenshot of Application Insights showing the results table with four rows; All, Simple, Long, and None with their respective user counts and percentages.](./media/howto-telemetry-python/telemetry-results.png)
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)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
title: Enable telemetry for feature flags (preview)
3+
titleSuffix: Azure App Configuration
4+
description: Learn how to enable telemetry 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: 03/05/2025
10+
---
11+
12+
# Enable telemetry for feature flags
13+
14+
Telemetry is the process of collecting, transmitting, and analyzing data about the usage and performance of your application. It helps you monitor feature flag behavior and make data-driven decisions. When a feature flag change is deployed, it's often important to analyze its effect on an application. For example, here are a few questions that may arise:
15+
16+
- Are my flags enabled/disabled as expected?
17+
- Are targeted users getting access to a certain feature as expected?
18+
- How does a variant affect customer engagement?
19+
20+
These types of questions can be answered through the emission and analysis of feature flag evaluation events.
21+
22+
## Prerequisites
23+
24+
- The feature flag created in [Use variant feature flags](./howto-variant-feature-flags.md).
25+
26+
## Connect to Application Insights (preview)
27+
28+
1. Open your App Configuration store in the Azure portal.
29+
1. In the **Telemetry** section, select the **Application Insights (preview)** blade.
30+
1. Select the subscription, resource group. Then either select your existing Application Insights resource you want to connect to your App Configuration store to, or select **Create new** to create a new Application Insights resource.
31+
1. Select the **Connect** button.
32+
33+
> [!div class="mx-imgBorder"]
34+
> ![Screenshot of the Azure portal, connecting application insights.](./media/howto-telemetry/connect-to-app-insights.png)
35+
36+
## Enable telemetry for a feature flag (preview)
37+
38+
1. Open your App Configuration store in the Azure portal and select the **Feature manager** blade under the **Operations** section.
39+
1. Select the feature flag named Greeting. If you don't have it, follow the [instructions to create it](./manage-feature-flags.md). Then, right-click on the feature flag and select **Edit**.
40+
41+
> [!div class="mx-imgBorder"]
42+
> ![Screenshot of the Azure portal, editing a feature flag.](./media/howto-telemetry/edit-feature-flag.png)
43+
44+
1. In the new view, select the **Telemetry** tab.
45+
1. Check the **Enable Telemetry** box and then select the **Review + update** button at the bottom of the page.
46+
47+
> [!div class="mx-imgBorder"]
48+
> ![Screenshot of the Azure portal, enabling telemetry.](./media/howto-telemetry/enable-telemetry.png)
49+
50+
1. Continue to the following instructions to use telemetry in your application for the language or platform you're using.
51+
52+
* [Python](./howto-telemetry-python.md)

articles/azure-app-configuration/howto-variant-feature-flags-python.md

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ms.author: mametcal
88
ms.service: azure-app-configuration
99
ms.devlang: python
1010
ms.topic: how-to
11-
ms.date: 12/02/2024
11+
ms.date: 05/06/2025
1212
---
1313

1414
# Use variant feature flags in a Python application
@@ -118,7 +118,7 @@ If you already have a Python Flask web app, you can skip to the [Use the variant
118118
119119
bp = Blueprint("pages", __name__)
120120
121-
@bp.route("/", methods=["GET", "POST"])
121+
@bp.route("/", methods=["GET"])
122122
def index():
123123
context = {}
124124
user = ""
@@ -127,8 +127,6 @@ If you already have a Python Flask web app, you can skip to the [Use the variant
127127
context["user"] = user
128128
else:
129129
context["user"] = "Guest"
130-
if request.method == "POST":
131-
return redirect(url_for("pages.index"))
132130
133131
quotes = [
134132
Quote("You cannot change what you are, only what you do.", "Philip Pullman"),
@@ -456,19 +454,11 @@ If you already have a Python Flask web app, you can skip to the [Use the variant
456454
feature_manager = FeatureManager(azure_app_config)
457455
```
458456
459-
1. Open `routes.py` and add the following code to the end of it to refresh configuration and get the feature variant.
457+
1. Open `routes.py` and update the following code for `greeting_message` to get the feature variant.
460458
461459
```python
462-
from featuremanagement.azuremonitor import track_event
463-
from . import azure_app_config, feature_manager
464-
465-
...
466-
# Update the post request to track liked events
467-
if request.method == "POST":
468-
track_event("Liked", user)
469-
return redirect(url_for("pages.index"))
460+
from . import feature_manager
470461
471-
...
472462
# Update greeting_message to variant
473463
greeting = feature_manager.get_variant("Greeting", user)
474464
greeting_message = ""
8.79 KB
Loading
46 KB
Loading
48 KB
Loading
15.7 KB
Loading

0 commit comments

Comments
 (0)