嗨 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,但两个日期之间没有明确的比较。
有什么方法可以在与具有相应计数的 count_ 列相邻的单独列上显示昨天失败的 API 计数今天失败的API? 我知道 project 运算符会添加额外的列,但我不知道如何将昨天失败的 API 计数合并并分配给项目运算符。
请添加我对 KQL 中可以实现此任务的任何相关函数或运算符的了解。
我尝试的另一种方法是定义两个变量,startDateTime和endDateTime来获取特定时间的数据,如下所示。 当我定义变量来定义选定的时间范围时,空白输出:
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)我想要实现的目标:
- Best way to show today Vs yesterday Vs week in KQL azure monitror
- kql query to compare the hour which has minimum number of TriggersStarted from last week to today past hour TriggersStarted
我在期待什么? 我想要一个查询,该查询可以在与具有相应 计数的 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
关于azure - 需要一个 KQL 查询来比较今天在特定时间失败的 API 计数与昨天同一时间失败的 API 计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75397235/