You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

计算失败API调用占比的KQL脚本异常问题排查

KQL失败占比计算异常问题修复

你的代码中失败占比显示为0的核心原因是整数除法导致的精度丢失:KQL中整数之间的除法会直接舍去小数部分,比如10(失败次数)除以174(成功次数)会被计算为0,最终百分比自然为0。

修正后的代码

let Lst = ago(30m); 
let N = ago(1m); 

AzureDiagnostics  
| where strlen(requestUri_s) >0  
| project splitted=split(requestUri_s, '/'), TimeGenerated, timeTaken_d, l=toint(serverResponseLatency_s), requestUri_s, httpStatus_d 
| mv-expand col1=splitted[0], col2=splitted[1], col3=splitted[2], col4=splitted[3], col5=splitted[4], col6=splitted[5], col7=splitted[6], col8=splitted[7], col9=splitted[8], col10=splitted[9], col11=splitted[10], col12=splitted[11], col13=splitted[12]  
| project-away splitted  
| where col2 =="API"  
and TimeGenerated between (Lst .. N) 
| extend a= case(col3=='token', col3, 
strlen(col11)==0, col7,  
strlen(col12) ==0 or strlen(col12)>30, col11,  
strlen(col13)==0, col12,  
col12 =='limitRules', 'limitRules',  
col13)   
, cc1 = 1 
| summarize work = sumif(toint(cc1), httpStatus_d <500)  
            , Fail = sumif(toint(cc1), httpStatus_d >=500)  
by a 
| summarize percentage = sum(toreal(Fail))/sum(toreal(work))*100, sum(Fail), sum(work) by a

关键修正说明

将原代码中的sum(Fail)/sum(work)*100改为sum(toreal(Fail))/sum(toreal(work))*100,通过toreal()函数将整数类型的统计结果转换为浮点数,强制KQL执行浮点除法,保留小数部分,从而得到正确的失败占比计算结果。

内容的提问来源于stack exchange,提问作者Symon Turner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 21:25:11