在编写 kusto 查询以在我的 azure 仪表板上创建自定义图表时,我希望能够根据用户在仪表板上选择的时间段来计算时间粒度。
例如:最后 4 小时 => 时间粒度 2 分钟,最后 24 小时 => 10 分钟
我尝试了以下方法来计算时间段,因为我们仍然无法访问它(据我在互联网上可以找到的信息)。
let timeGrain = traces
| summarize min_time = min(timestamp), max_time = max(timestamp)
| extend timeWindow = max_time - min_time // days / hrs/ min / seconds
| project timeWindow
| extend timeGrain = case(timeWindow <= 4h, "2m",
timeWindow <= 12h, "5m",
timeWindow <= 24h, "10m",
"2h")
| project timeGrain;
该查询返回我想要实现的时间粒度,但我无法在其他查询中使用此变量。
traces
...
| summarize percentile(DurationInMs, 50) by bin(timestamp, timeGrain), CommandType
| render areachart with (ytitle = "Duration In Ms", xtitle = "Timestamp");
(我知道跟踪不是存储有关持续时间的数据的最佳位置,我们会将其更改为指标,但这不是问题的范围)
这给了我以下错误:“summarize”运算符:无法解析名为“timeGrain”的标量表达式
有没有办法修复这个错误或者有更好的方法来创建动态时间粒度?
最佳答案
显然,我的跟踪中没有相同的字段,但您应该使用时间跨度而不是字符串来定义 timeGrain
。
此外,要将查询结果 timeGrain
用作变量,请使用 toscalar
( docs ):
let timeGrain = toscalar(traces
| summarize min_time = min(timestamp), max_time = max(timestamp)
| extend timeWindow = max_time - min_time // days / hrs/ min / seconds
| project timeWindow
| extend timeGrain = case(timeWindow <= 4h, 2m,
timeWindow <= 12h, 5m,
timeWindow <= 24h, 10m,
2h)
| project timeGrain);
traces
| summarize count() by bin(timestamp, timeGrain)
| order by timestamp desc
这工作得很好。
关于azure - 如何根据自定义图表中的azure仪表板中的选定时间段计算时间粒度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63668438/