Application Insights交叉表查询:如何计算异常数与请求数的比值
Application Insights 异常率查询正确写法
你原来的写法问题在于Kusto中let定义的计数结果默认是表格类型,无法直接和另一个表格的字段做算术运算,需要先把单个计数结果转为标量值才能运算。
全量统计异常率写法
直接用toscalar()函数将两个计数结果转为单个数值后运算即可:
// 统计符合要求的异常总数,转为标量 let exceptionCount = toscalar( exceptions | where type == "ShiftDigital.InventoryServices.API.GetVehicleException" and outerMessage !contains("Invalid zip code") | count ); // 统计请求总数,转为标量 let requestCount = toscalar(requests | count); // 输出结果,支持自定义精度和格式 print 异常总数 = exceptionCount, 请求总数 = requestCount, 异常率 = round(1.0 * exceptionCount / requestCount, 4), 异常率百分比 = strcat(round(100.0 * exceptionCount / requestCount, 2), "%")
按时间维度分段统计异常率写法
如果需要按天/按小时统计周期内的异常率,可以用join关联两个指标的聚合结果:
requests | where timestamp >= ago(7d) // 统计最近7天数据,可自行调整时间范围 | summarize 请求数 = count() by bin(timestamp, 1d) // 按天聚合,可改为1h按小时聚合 | join kind=leftouter ( exceptions | where type == "ShiftDigital.InventoryServices.API.GetVehicleException" and outerMessage !contains("Invalid zip code") | where timestamp >= ago(7d) | summarize 异常数 = count() by bin(timestamp, 1d) ) on timestamp | extend 异常数 = coalesce(异常数, 0) // 无异常的时段异常数补0,避免运算出错 | extend 异常率 = round(1.0 * 异常数 / 请求数, 4) | project timestamp, 请求数, 异常数, 异常率
内容的提问来源于stack exchange,提问作者James Alexander
相关产品推荐
相关产品推荐

