2
votes

I have a Kusto Query that I am using to query Application Insights. The goal is to get number of failed requests in 5 min buckets / and divide that by total number of requests in the same 5 min bucket. I will eventually build an alert to trigger if this percentage is greater than a certain value. But, I can't seem to get the query right.

In the example below, I hardcode a specific timestamp to make sure I get some failures.

Here is the query:

let fn = "APP_NAME";
requests
| where success == "False" and cloud_RoleName == fn
| summarize failed=sum(itemCount) by bin(timestamp, 5m)
| where timestamp == "2021-05-17T20:20:00Z"
| join (
    requests
    | where cloud_RoleName == fn
    | summarize reqs=sum(itemCount) by bin(timestamp, 5m)
    | where timestamp == "2021-05-17T20:20:00Z"
  ) on timestamp
| project timestamp, failed, reqs
| extend p=round(failed/reqs, 2)

It currently returns:

timestamp [UTC]             |p  |failed  |reqs
5/17/2021, 8:20:00.000 PM   0   1,220   6,649

If anyone can give me insight into how to get the decimal value (~0.18) I expect for p?

2

2 Answers

2
votes

Had to cast values to Doubles to get it to return something other than 0.

let fn = "APP_NAME";
requests
| where success == "False" and cloud_RoleName == fn
| summarize failed=sum(itemCount) by bin(timestamp, 5m)
| join (
    requests
    | where cloud_RoleName == fn
    | summarize reqs=sum(itemCount) by bin(timestamp, 5m)
  ) on timestamp
| project timestamp, failedReqs=failed, totalRequests=reqs, percentage=(todouble(failed)  / todouble(reqs) * 100)
2
votes

another option that is a bit less verbose is to multiply by a 100.0 (which is a double literal)

percentage = failed * 100.0 / reqs

Note that the multiplication has to happen before division