如何使用Kusto Query正确统计命令的执行成功率
问题诊断
你原来的查询存在两类错误,直接导致报错和逻辑不成立:
- 语法逻辑错误:
percentiles()函数仅支持统计数值型字段的百分位分布,你传入的command是字符串类型,无法计算百分位,因此触发识别报错 - 业务逻辑错误:你仅筛选了
Table_Step = "CommandReceived"的触发记录,未关联Table_Step = "STOP"的执行结果记录,且缺少命令唯一标识id的关联,无法统计成功/失败的对应关系
正确统计方案
成功率统计逻辑为:指定时间段内,call类命令的成功执行数 / call类命令的总触发数,其中执行结果以STOP步骤的Table_status字段为准,同一id对应同一条命令的触发和执行记录。
参考查询语句如下:
// 提取所有call类命令的触发记录 let call_trigger = Table | where Table_Name == "call_command" and Table_Step == "CommandReceived" | parse Table_MetaData with * "command = " command: string ",id = " id: string "," * | where command == "call" | project id, trigger_time = Event_Time; // 提取所有call类命令的执行结果记录 let call_result = Table | where Table_Name == "call_command" and Table_Step == "STOP" | parse Table_MetaData with * "command = " command: string ",id = " id: string "," * | where command == "call" | project id, exec_status = Table_status; // 关联触发和结果,统计成功率 call_trigger | join kind=leftouter call_result on id // 无结果记录默认判定为执行失败 | extend is_success = iif(exec_status == "OK", 1, 0) | summarize total_trigger = count(), success_count = sum(is_success), success_rate = round(todouble(sum(is_success)) * 100 / count(), 2) // 若要制作时间趋势仪表盘,可放开下方注释,按指定时间粒度分组统计 // by bin(trigger_time, 1h)
补充说明
- 时间粒度可按需调整,比如
bin(trigger_time, 30m)就是按30分钟分组统计各时段成功率 - 若需要排除重试命令的干扰,可以在parse元数据时额外提取
retryAttempt字段,筛选首次执行的记录再做统计
内容的提问来源于stack exchange,提问作者Angus
相关产品推荐
相关产品推荐

