如何根据自定义图表中的azure仪表板中的选定时间段计算时间粒度

Ric*_*ick 1 azure azure-application-insights kql

在编写 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;      
Run Code Online (Sandbox Code Playgroud)

该查询返回我想要实现的时间粒度,但我无法在其他查询中使用此变量。

traces
...
| summarize percentile(DurationInMs, 50) by bin(timestamp, timeGrain), CommandType
| render areachart with (ytitle = "Duration In Ms", xtitle = "Timestamp");
Run Code Online (Sandbox Code Playgroud)

(我知道跟踪不是存储有关持续时间的数据的最佳位置,我们会将其更改为指标,但这不是问题的范围)

这给了我以下错误:“summarize”运算符:无法解析名为“timeGrain”的标量表达式

有没有办法修复这个错误或者有更好的方法来创建动态时间粒度?

Pet*_*ons 5

显然,我的跟踪中没有相同的字段,但您应该使用时间跨度而不是字符串来定义timeGrain

另外,要将查询结果用作timeGrain变量,请使用toscalardocs):

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       
Run Code Online (Sandbox Code Playgroud)

这很好用。