Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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. 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.
Prerequisites
- The variant feature flag with telemetry enabled from Enable telemetry for feature flags.
- The application from Use variant feature flags.
Add telemetry to your Python application
Install the required packages using pip:
pip install azure-appconfiguration-provider pip install featuremanagement["AzureMonitor"] pip install azure-monitor-opentelemetry
Open
app.py
and configure your code to connect to Application Insights to publish telemetry.import os from azure.monitor.opentelemetry import configure_azure_monitor # Configure Azure Monitor configure_azure_monitor(connection_string=os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING"))
Also in
app.py
load your feature flags from App Configuration and load them into feature management.FeatureManager
uses thepublish_telemetry
callback function to publish telemetry to Azure Monitor.from featuremanagement.azuremonitor import publish_telemetry feature_manager = FeatureManager(config, on_feature_evaluated=publish_telemetry)
Open
routes.py
and update your code to track your own events in your application. Whentrack_event
is called, a custom event is published to Azure Monitor with the provided user.from featuremanagement import track_event @bp.route("/heart", methods=["POST"]) def heart(): if current_user.is_authenticated: user = current_user.username # Track the appropriate event based on the action track_event("Liked", user) return jsonify({"status": "success"})
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.<script> function heartClicked(button) { var icon = button.querySelector('i'); // Toggle the heart icon appearance icon.classList.toggle('far'); icon.classList.toggle('fas'); // Only send a request to the dedicated heart endpoint when it's a like action if (icon.classList.contains('fas')) { fetch('/heart', { method: 'POST', headers: { 'Content-Type': 'application/json', } }); } } </script>
Build and run the app
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.setx APPLICATIONINSIGHTS_CONNECTION_STRING "applicationinsights-connection-string"
If you use PowerShell, run the following command:
$Env:APPLICATIONINSIGHTS_CONNECTION_STRING = "applicationinsights-connection-string"
If you use macOS or Linux, run the following command:
export APPLICATIONINSIGHTS_CONNECTION_STRING='applicationinsights-connection-string'
Run the application, see step 2 of Use variant feature flags .
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!".
With some of the users select the Like button to trigger the telemetry event.
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:
// Total users let total_users = customEvents | where name == "FeatureEvaluation" | summarize TotalUsers = count() by Variant = tostring(customDimensions.Variant); // Hearted users let hearted_users = customEvents | where name == "FeatureEvaluation" | extend TargetingId = tostring(customDimensions.TargetingId) | join kind=inner ( customEvents | where name == "Liked" | extend TargetingId = tostring(customDimensions.TargetingId) ) on TargetingId | summarize HeartedUsers = count() by Variant = tostring(customDimensions.Variant); // Calculate the percentage of hearted users over total users let combined_data = total_users | join kind=leftouter (hearted_users) on Variant | extend HeartedUsers = coalesce(HeartedUsers, 0) | extend PercentageHearted = strcat(round(HeartedUsers * 100.0 / TotalUsers, 1), "%") | project Variant, TotalUsers, HeartedUsers, PercentageHearted; // Calculate the sum of total users and hearted users of all variants let total_sum = combined_data | summarize Variant="All", TotalUsers = sum(TotalUsers), HeartedUsers = sum(HeartedUsers); // Display the combined data along with the sum of total users and hearted users combined_data | union (total_sum)
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 calledTargetingId
with the name of the user that liked the quote.