2

我在 sumologic 上完成了日志记录。日志 JSON 包含请求的响应时间。让它成为一个名为“response_time”的 JSON 键。每个请求都由唯一 ID 标识,由 JSON 键“request_id”表示。和一个由 JSON 键“url”表示的 URL。我需要根据以下条件在松弛通道上发出警报。

1) 在 10 分钟的窗口内,如果有 100 个请求,并且超过 5% 的请求的响应时间超过 100ms,则提醒所有这些请求的“url”、“request_id”和“response_time”。2) 如果小于或等于 5% 的请求的响应时间超过 100 毫秒,则根本不要发出警报。我写了一个这样的查询。

_sourceName=<my_source_name> 
| json field=_raw "response_time" as response_time 
| json field=_raw "request_id" as request_id 
| if (num(response_time) > 100, 1, 0) as higher 
| if (num(response_time) <= 100, 1, 0) as lower 
| count as total_requests, sum(higher) as 
response_time_greater_than_100, sum(lower) as 
response_time_less_than_100 
| (response_time_greater_than_100/total_requests) as failure_ratio 
| where (failure_ratio > 0.05)

当超过 5% 的请求的 response_time 超过 100 毫秒时,上面的查询给了我所有的请求。但无论响应时间如何,它都会给我所有请求。否则不返回任何结果。

除了这个结果,我想进一步过滤上面的查询,请求“response_time”> 100 ms。只要有结果,它就会提供两个选项卡。一个用于“消息”,另一个用于“聚合”。我想将“消息”选项卡中的字段发送到松弛频道。如何做到这一点?

4

1 回答 1

2

选项卡 - 聚合与消息

首先,让我们澄清这两个选项卡。第一个(消息)包含所有这些产生结果的原始日志行。第二个(聚合)是您的实际查询与分组的结果。请注意,您正在使用| countwhich 是一个分组运算符(类似于GROUP BYSQL)。

任何传出交互始终基于查询的实际结果(聚合)。原始行仅在用户界面中可见以供检查(在 API 中也可见)。

实际查询

如果您只想获取响应时间>100 的所有请求,那么有这样的查询就足够了:

_sourceName=<my_source_name> 
| json field=_raw "response_time" as response_time 
| json field=_raw "request_id" as request_id 
| where response_time > 100

声明式地说,我知道您想要一些不同的东西:让所有响应超过 100,但前提是超过 100 的请求占总请求的 >5%,否则为空结果集。

_sourceName=<my_source_name> 
| 1 as expected_failure_ratio_violation
| where [subquery:
  _sourceName=<my_source_name> 
  | json field=_raw "response_time" as response_time 
  | json field=_raw "request_id" as request_id
  | if (num(response_time) > 100, 1, 0) as higher 
  | if (num(response_time) <= 100, 1, 0) as lower 
  | count as total_requests, sum(higher) as response_time_greater_than_100, 
    sum(lower) as response_time_less_than_100 
  | (response_time_greater_than_100/total_requests) as failure_ratio 
  | where (failure_ratio > 0.05)
  | count as expected_failure_ratio_violation 
  | compose expected_failure_ratio_violation        
]
| json field=_raw "response_time" as response_time 
| json field=_raw "request_id" as request_id
| where response_time > 100

它使用了一种技巧,将(一个常数)1与子查询()中的违规计数相匹配expected_failure_ratio_violation

另外,作为一个提示 - 你没有| timeslice在这里使用,根据我的经验,这是人们通常在这样的场景中使用的。你可能想看看它。

免责声明:我目前受雇于 Sumo Logic

于 2019-06-04T11:05:18.733 回答