使用本指南筛选 Azure Monitor Application Insights 中的 OpenTelemetry (OTel) 数据。 筛选有助于排除不必要的遥测数据,并防止收集敏感数据以优化性能和支持合规性。
对遥测进行筛选的原因包括:
- 筛出运行状况检查遥测,以减少噪音。
- 确保不会收集个人数据和凭据。
- 筛出低价值的遥测以优化性能。
若要了解有关 OpenTelemetry 概念的详细信息,请查看 OpenTelemetry 概述。
注意
有关 Azure 函数应用程序,请参阅 在 Azure Functions 中使用 OpenTelemetry。
使用检测库筛选 OpenTelemetry
要查看 Azure Monitor OpenTelemetry 发行版中包含的所有工具库列表,请参阅 配置 Azure Monitor OpenTelemetry 的自动数据收集和资源检测器。
许多检测库都提供筛选选项。 有关指导,请查看相应的 README 文件:
注意
目前,Azure Monitor OpenTelemetry 发行版包含 SqlClient 检测源代码的副本,以确保在上游 OpenTelemetry SqlClient 库保持试验性的同时保持稳定性。
此类 SetDbStatementForStoredProcedure 选项在我们的分发中不可用,因为代码是嵌入的,而不是引用外部包。
SqlClient 监控工具达到稳定版本后,Azure Monitor 会切换至引用官方包,并通过 builder.AddSqlClientInstrumentation(options => { ... }) 进行自定义。
使用范围处理器筛选遥测
使用自定义处理器:
提示
添加此处显示的处理器之前添加Azure Monitor。
// Create an ASP.NET Core application builder. var builder = WebApplication.CreateBuilder(args); // Configure the OpenTelemetry tracer provider to add a new processor named ActivityFilteringProcessor. builder.Services.ConfigureOpenTelemetryTracerProvider((sp, builder) => builder.AddProcessor(new ActivityFilteringProcessor())); // Configure the OpenTelemetry tracer provider to add a new source named "ActivitySourceName". builder.Services.ConfigureOpenTelemetryTracerProvider((sp, builder) => builder.AddSource("ActivitySourceName")); // Add the Azure Monitor telemetry service to the application. This service will collect and send telemetry data to Azure Monitor. builder.Services.AddOpenTelemetry().UseAzureMonitor(); // Build the ASP.NET Core application. var app = builder.Build(); // Start the ASP.NET Core application. app.Run();使用以下代码将
ActivityFilteringProcessor.cs添加到你的项目:public class ActivityFilteringProcessor : BaseProcessor<Activity> { // The OnStart method is called when an activity is started. This is the ideal place to filter activities. public override void OnStart(Activity activity) { // prevents all exporters from exporting internal activities if (activity.Kind == ActivityKind.Internal) { activity.IsAllDataRequested = false; } } }
如果没有使用 AddSource("ActivitySourceName") 显式添加特定源,则不会导出使用该源创建的任何活动。
在引入时使用数据收集规则筛选遥测数据
在Azure Monitor将其存储在Log Analytics工作区之前,请减少干扰或标准化遥测数据。 若要实现这一目标,请使用数据收集规则(DCR)中的引入时间转换在 Azure Monitor收到遥测数据后筛选或修改遥测数据。
转换操作使用在每条引入记录上运行的 Kusto 查询语言(KQL)语句。
在 Application Insights 表中使用工作区转换 DCR。 Log Analytics工作区支持一个工作区转换 DCR。 将该工作区的所有转换置于同一 DCR 中。
更新后最多可能需要 60 分钟激活。
使用这些资源了解详细信息:
- 查看数据收集规则(DCR)。
- 查看 Azure Monitor 中的转换。
- 查看 Azure 门户工作区转换教程。
- 查看关于Resource Manager 模板的工作区转换教程。
- 查看 在 Azure Monitor 中创建转换。
- 查看 转换中支持的 KQL 功能。
- 查看支持引入时间转换的 Application Insights 表。
将 OpenTelemetry 信号映射到Log Analytics表
Application Insights 在这些表中存储常见的 OpenTelemetry (OTel) 信号:
-
AppTraces(日志) -
AppRequests(传入请求) -
AppDependencies(传出依赖项) -
AppExceptions(例外) -
AppMetrics(指标)
在工作区转换 DCR 中,对每个表使用流名称格式 Microsoft-Table-<TableName> 。 例如,可以使用Microsoft-Table-AppRequests用于AppRequests表。
注意
工作区转换 DCR 适用于引入到 Log Analytics 工作区所选表中的所有数据。 如果多个应用程序共享同一工作区,请使用筛选器来限定每个转换的范围,例如 AppRoleName 或 ResourceGUID。
使用工作区转换 DCR 示例
以下示例显示了工作区变换 DCR JSON。 使用 dataFlows 为每个表定义一个转换。 在 DCR 定义中保留 transformKql 一行。
创建工作区转换 DCR 模板
使用此模板创建工作区转换 DCR。 将 <workspace-location> 和 <workspace-resource-id> 替换为Log Analytics工作区中的值。
{
"kind": "WorkspaceTransforms",
"location": "<workspace-location>",
"properties": {
"dataSources": {},
"destinations": {
"logAnalytics": [
{
"workspaceResourceId": "<workspace-resource-id>",
"name": "laDest"
}
]
},
"dataFlows": [
{
"streams": ["Microsoft-Table-AppRequests"],
"destinations": ["laDest"],
"transformKql": "source"
}
]
}
}
通过删除表来删除遥测类型
使用此示例可屏蔽整个遥测类型,例如所有跟踪或所有请求。
为每个要删除的表添加一个数据流。
[
{
"streams": ["Microsoft-Table-AppTraces"],
"destinations": ["laDest"],
"transformKql": "source | where 1 == 0"
},
{
"streams": ["Microsoft-Table-AppRequests"],
"destinations": ["laDest"],
"transformKql": "source | where 1 == 0"
},
{
"streams": ["Microsoft-Table-AppDependencies"],
"destinations": ["laDest"],
"transformKql": "source | where 1 == 0"
},
{
"streams": ["Microsoft-Table-AppExceptions"],
"destinations": ["laDest"],
"transformKql": "source | where 1 == 0"
},
{
"streams": ["Microsoft-Table-AppMetrics"],
"destinations": ["laDest"],
"transformKql": "source | where 1 == 0"
}
]
删除运行状况检查请求
使用此示例可删除常见运行状况、就绪性和实时性终结点。
{
"streams": ["Microsoft-Table-AppRequests"],
"destinations": ["laDest"],
"transformKql": "source | extend url = tolower(tostring(Url)) | where not(url contains '/health' or url contains '/healthz' or url contains '/ready' or url contains '/readyz' or url contains '/live' or url contains '/livez') | project-away url"
}
仅保留失败的运行状况检查请求
使用此示例可保留失败的运行状况检查,并丢弃成功的运行状况检查。
{
"streams": ["Microsoft-Table-AppRequests"],
"destinations": ["laDest"],
"transformKql": "source | extend url = tolower(tostring(Url)) | where not((url contains '/health' or url contains '/healthz' or url contains '/ready' or url contains '/readyz' or url contains '/live' or url contains '/livez') and Success == true) | project-away url"
}
仅保留失败或缓慢的请求
使用此示例来保留失败或超出延迟阈值的请求。
{
"streams": ["Microsoft-Table-AppRequests"],
"destinations": ["laDest"],
"transformKql": "source | where Success == false or DurationMs >= 1000"
}
仅保留警告及更高级别的日志
使用此示例可保留 SeverityLevel 为 Warning (2)、Error (3) 或 Critical (4) 的跟踪记录。
{
"streams": ["Microsoft-Table-AppTraces"],
"destinations": ["laDest"],
"transformKql": "source | where SeverityLevel >= 2"
}
仅保留失败或缓慢的依赖项
使用此示例可以保留失败或超出延迟阈值的依赖项调用。
{
"streams": ["Microsoft-Table-AppDependencies"],
"destinations": ["laDest"],
"transformKql": "source | where Success == false or DurationMs >= 500"
}
从依赖项数据中删除 SQL 语句
使用此示例删除存储在列中的 Data SQL 语句,同时保留依赖项计时和成功信息。
{
"streams": ["Microsoft-Table-AppDependencies"],
"destinations": ["laDest"],
"transformKql": "source | extend dependencyType = tolower(tostring(DependencyType)) | extend Data = iff(dependencyType == 'sql', '', Data) | project-away dependencyType"
}
丢弃合成流量
使用此示例删除包含值的 SyntheticSource 记录。
{
"streams": ["Microsoft-Table-AppRequests"],
"destinations": ["laDest"],
"transformKql": "source | where isempty(SyntheticSource)"
}
删除取消异常
使用此示例可删除常见的取消异常类型。
{
"streams": ["Microsoft-Table-AppExceptions"],
"destinations": ["laDest"],
"transformKql": "source | where not(ExceptionType contains 'OperationCanceledException' or ExceptionType contains 'TaskCanceledException')"
}
删除客户端 IP 地址和查询字符串
使用此示例删除客户端 IP 地址,并存储不带查询字符串的 URL。
[
{
"streams": ["Microsoft-Table-AppRequests"],
"destinations": ["laDest"],
"transformKql": "source | extend Url = tostring(split(tostring(Url), '?')[0]) | project-away ClientIP"
},
{
"streams": ["Microsoft-Table-AppDependencies"],
"destinations": ["laDest"],
"transformKql": "source | extend Data = tostring(split(tostring(Data), '?')[0]) | project-away ClientIP"
}
]
将转换范围限定为单个服务
当多个应用程序共享工作区时,使用此示例可将转换范围限定为单个服务。
{
"streams": ["Microsoft-Table-AppRequests"],
"destinations": ["laDest"],
"transformKql": "source | where AppRoleName == '<app-role-name>' | where Success == false or DurationMs >= 1000"
}
故障排除、反馈和支持
提示
以下部分适用于所有 OpenTelemetry 发行版文章。
故障排除
有关故障排除信息,请参阅 排查 .NET 中的 OpenTelemetry 问题 ,以及 排查 Azure Monitor Application Insights 中缺少的应用程序遥测问题。
OpenTelemetry 反馈
若要提供反馈,请查看以下内容:
- 加入 OpenTelemetry 早期采用者社区,告诉 Microsoft 有关你本人的信息。
- 在 Microsoft 技术社区与其他 Azure Monitor 用户联系。
- 在 Azure 反馈论坛中发出功能请求。
支持
选择所选语言的选项卡,以发现支持选项。
- 如有 Azure 支持方面的问题,请提交 Azure 支持工单。
- 有关 OpenTelemetry 问题,请直接与 OpenTelemetry .NET 社区联系。
- 有关与 Azure Monitor 导出程序相关的未解决问题列表,请参阅 GitHub 问题页。
后续步骤
- 若要查看源代码,请参阅 Azure Monitor ASP.NET Core GitHub 存储库。
- 若要查看示例应用程序,请参阅 适用于 ASP.NET Core 的 Azure Monitor OpenTelemetry。
- 若要安装 NuGet 包、检查更新或查看发行说明,请参阅 Azure Monitor AspNetCore NuGet 包页面。
- 若要了解有关 OpenTelemetry 及其社区的详细信息,请参阅 OpenTelemetry .NET GitHub 存储库。
- 若要启用使用体验, 请启用 Web 或浏览器用户监视。