Search code examples
kql

Calucating failed API percentage KQL


I have some very odd code that works out the count of successful and failed API calls by API call type.

THe counts work fund, but when I get to the percentage, I can't get it to work.

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(Fail)/sum(work)*100, sum(Fail), sum(work) by a

One of the results is 10 failed Vs 174 succeeded, but the percentage shows 0 which is not right


Solution

  • My guess is that you're dividing 2 integers, and thus getting an integer (while 'losing' everything after the decimal point).

    try this instead:

    | summarize percentage = 100.0 * sum(Fail) / sum(work), sum(Fail), sum(work) by a