你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
在本教程中,你将在 Python 应用程序中使用遥测来跟踪功能标志评估和自定义事件。 遥测允许你对功能管理策略做出明智的决策。 使用在启用功能标志遥测中创建的已启用遥测的功能标志。 在继续作之前,请确保在配置存储中创建一个名为 Greeting 的功能标志,并启用遥测。 本教程是在使用变体功能标志的基础上构建的。
先决条件
将遥测添加到 Python 应用程序
使用 pip 安装所需的包:
pip install azure-appconfiguration-provider pip install featuremanagement["AzureMonitor"] pip install azure-monitor-opentelemetry
打开
app.py
并配置代码以连接到 Application Insights 以发布遥测数据。import os from azure.monitor.opentelemetry import configure_azure_monitor # Configure Azure Monitor configure_azure_monitor(connection_string=os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING"))
另外,在
app.py
中从应用程序配置加载功能标志并将其加载到功能管理中。FeatureManager
publish_telemetry
使用回调函数将遥测数据发布到 Azure Monitor。from featuremanagement.azuremonitor import publish_telemetry feature_manager = FeatureManager(config, on_feature_evaluated=publish_telemetry)
打开
routes.py
并更新代码以跟踪应用程序中自己的事件。 调用track_event
时,会将自定义事件与提供的用户一起发布到 Azure Monitor。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"})
打开
index.html
并更新代码以实现类似按钮。 单击后,like 按钮会将 POST 请求发送到/heart
终结点。<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>
生成并运行应用
Application Insights 需要连接字符串才能连接到 Application Insights 资源。 将
APPLICATIONINSIGHTS_CONNECTION_STRING
环境变量设置为 Application Insights 资源的连接字符串。setx APPLICATIONINSIGHTS_CONNECTION_STRING "applicationinsights-connection-string"
如果使用 PowerShell,请运行以下命令:
$Env:APPLICATIONINSIGHTS_CONNECTION_STRING = "applicationinsights-connection-string"
如果使用 macOS 或 Linux,则请运行以下命令:
export APPLICATIONINSIGHTS_CONNECTION_STRING='applicationinsights-connection-string'
运行应用程序, 请参阅使用变体功能标志的步骤 2 。
创建 10 个不同的用户并登录到应用程序。 当你以每个用户是身份登录时,会收到不同的消息变体。 大约 50% 的时间不会收到消息。 25% 的时间会收到消息“你好!”,25% 的时间会收到“我希望这能让你开心!”。
某些用户选择“ 赞 ”按钮以触发遥测事件。
在 Azure 门户中打开 Application Insights 资源,然后选择“监视”下的“日志”。 在查询窗口中,运行以下查询以查看遥测事件:
// 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)
每次加载引文页面时,你都会看到一个“FeatureEvaluation”,每次单击点赞按钮时,您都会看到一个“Liked”事件。 “FeatureEvaluation”事件具有一个自定义属性
FeatureName
,该属性的名称为已评估的功能标志。 这两个事件都有一个自定义TargetingId
属性,其中包含喜欢引号的用户的名称。