azure - 需要一个 KQL 查询来比较今天在特定时间失败的 API 计数与昨天同一时间失败的 API 计数

标签 azure comparison azure-application-insights kql

嗨 Kusto 查询语言 (KQL) 爱好者,

我正在尝试使用 Kusto 查询语言 (KQL) 编写一个查询,该查询可以比较今天失败的 API 的计数相对于同一时间范围内(下午 2:30 到下午 3 点)昨天失败的 API 的计数具体时间(假设下午 2:30 到下午 3 点)。

例如,如果今天在过去 30 分钟内操作 X 失败了 10 次,失败代码为 400,我需要查看今天在过去 30 分钟(同一时间范围)内哪个操作 X 失败了。

为此,我使用标量函数 bin() 并编写了以下查询,从 request 表中提取数据:

requests
|where timestamp > ago(1d)
| where client_Type != "Browser"
| where (cloud_RoleName == 'X')
| where name != 'HTTP GET'
| where success == false
| summarize count() by bin (timestamp, 1d), name, resultCode
|sort by timestamp

这是我使用 timestamp > ago(1d) 时得到的输出。这样,我就看到了今天和昨天失败的 API,但两个日期之间没有明确的比较。 Output

有什么方法可以在与具有相应计数的 count_ 列相邻的单独列上显示昨天失败的 API 计数今天失败的API? 我知道 project 运算符会添加额外的列,但我不知道如何将昨天失败的 API 计数合并并分配给项目运算符。

请添加我对 KQL 中可以实现此任务的任何相关函数或运算符的了解。

我尝试的另一种方法是定义两个变量,startDateTimeendDateTime来获取特定时间的数据,如下所示。 当我定义变量来定义选定的时间范围时,空白输出: Blank output

let startDateTime = todatetime("2023-02-07 06:35:00.0");
let endDateTime = todatetime("2023-02-07 06:35:00.0");
requests
|where timestamp > startDateTime and timestamp < endDateTime
| where client_Type != "Browser"
| where (cloud_RoleName == 'web-memberappservice-weu-prod')
| where name != 'HTTP GET'
| where success == false
| summarize count() by bin (timestamp, 1d), name, resultCode
|sort by timestamp

我搜索了 KQL 查询,将今天失败的 API 数量与昨天失败的 API 数量进行比较,并检查了 Stack Overflow 中的一些结果,这些结果并不能帮助我实现所需的结果。 我尝试了这些链接,但对这些链接的查询没有反射(reflect)我想要实现的目标:

我在期待什么? 我想要一个查询,该查询可以在与具有相应 计数的 count_ 列相邻的单独列上显示昨天失败的 API 计数今天失败的 API。 我知道 project 运算符会添加额外的列,但我不知道如何将昨天失败的 API 计数合并并分配给项目运算符。 请指出任何可以在这方面提供帮助的相关功能或操作。

最佳答案

* 出于性能原因添加了 where 子句。

// Sample data generation. Not part of the solution.
let requests = materialize(range i from 1 to 100000 step 1 | extend timestamp = ago(2d * rand()), name = tostring(dynamic(["PUT", "POST", "PATCH", "GET"])[toint(rand(4))]), resultCode = 400 + toint(rand(3)));
// Solution starts here.
let _period = 30m;
requests
| where     timestamp between (ago(_period)      .. _period)
        or  timestamp between (ago(_period + 1d) .. _period)   
| summarize todayCount      = countif(timestamp between (ago(_period)      .. _period)) 
           ,YesterdayCount  = countif(timestamp between (ago(_period + 1d) .. _period))
            by name, resultCode
 |sort by name asc, resultCode asc
<表类=“s-表”> <标题> 姓名 结果代码 今天计数 昨天计数 <正文> 获取 400 91 100 获取 401 98 98 获取 402 109 89 补丁 400 93 77 补丁 401 84 85 补丁 402 74 82 发布 400 78 85 发布 401 96 77 发布 402 85 102 放置 400 98 81 放置 401 97 85 放置 402 77 83

Fiddle

关于azure - 需要一个 KQL 查询来比较今天在特定时间失败的 API 计数与昨天同一时间失败的 API 计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75397235/

相关文章:

Azure Cosmos DB 一致性保证

Azure 数据工厂 Web API 事件

c - 我如何比较收到的答案?

types - 如何检查元素是否属于一个子类型或另一个子类型?

Azure 函数监视器警报,其中执行计数 < 1 从未触发

azure - 如何在Azure Application Insights中使用cloud/roleName上的过滤器?

asp.net - Azure DevOps 构建/发布管道转换

python - 在Python中使用Pandas将多表Excel文件保存到Azure Blob存储

C:比较两个元素没有产生正确的结果?

Azure Application Insights 查询 - 如何计算总数的百分比