首页 > 解决方案 > AWS CloudWatch Insights - 简单三元 IF 或一些类似函数

问题描述

考虑到我的日志中有许多带有时间测量的属性。我想计算它们每个比我的超时限制大多少次,例如 10 秒。

我目前能够多次运行非常相似的查询,如下所示:

fields context
| filter context.time_to_prepare > 10
| count(*) as count_slow_time_to_prepare by bin(10m)
fields context
| filter context.time_to_shine > 10
| count(*) as count_slow_time_to_shine by bin(10m)

.. 每个属性一个查询

如果我们可以在同一个查询中提取所有这些指标,那么绘图会更好也更容易。为此,所缺少的只是一些三元运算符或类似的东西。

我希望这样的事情会起作用:

fields context
| filter context.total_time > 0
| stats sum(if(context.time_to_prepare > 10,1,0)) as slow_time_to_prepare,
|       sum(if(context.time_to_shine   > 10,1,0)) as slow_time_to_shine  ,
# .. one row for each attribute
|       sum(if(context.time_to_move    > 10,1,0)) as slow_time_to_move   by bin(10m)

但这没有用。“如果”函数不存在。

那么,有什么办法可以制作三元if呢?

标签: amazon-cloudwatchaws-cloudwatch-log-insights

解决方案


我很接近,

这段代码应该可以完成这项工作:

fields context
| filter context.total_time > 0
| stats sum(context.time_to_prepare > 10) as slow_time_to_prepare,
|       sum(context.time_to_shine   > 10) as slow_time_to_shine  ,
# .. one row for each attribute
|       sum(context.time_to_move    > 10) as slow_time_to_move   by bin(10m)

True 和 Falsea > b变为 1 和 0,可用于 sum、avg 等分组函数。


推荐阅读