6

我有以下查询,输出如下:

Query:
filter @message like /A:|B:/ 

Output:
[INFO] 2020-07-28T09:20:48.406Z requestid A: [{'Delivery': OK, 'Entry': 12323 }]
[INFO] 2020-07-28T09:20:48.407Z requestid B: {'MyValue':0}

我只想在 B 消息' MyValue ' = 0中打印 A 消息。对于上面的例子,我必须有以下输出

Output:
[INFO] 2020-07-28T09:20:48.406Z requestid A: [{'Delivery': OK, 'Entry': 12323 }]

对于下一个示例

[INFO] 2020-07-28T09:20:48.406Z requestid A: [{'Delivery': OK, 'Entry': 12323 }]
[INFO] 2020-07-28T09:20:48.407Z requestid B: {'MyValue':12}

输出应该为空

我不能这样做,因为我错过了 A 消息:

filter @message like /A:|B:/ 
filter MyValue = 0

有任何想法吗?

4

1 回答 1

0

如果有人仍然感兴趣,有办法从按字段分组中获得第一个和最后一个。因此,如果您可以将数据放入成对的消息中,它可能会有所帮助。

例如,给定 API Gateway 访问日志(每一行是一个@message):

2021-09-14T14:09:00.452+03:00   (01c53288-5d25-*******) Extended Request Id: ***************
2021-09-14T14:09:00.452+03:00   (01c53288-5d25-*******) Verifying Usage Plan for request: 01c53288-5d25-*******. API Key: API Stage: **************/dev
2021-09-14T14:09:00.454+03:00   (01c53288-5d25-*******) API Key authorized because method 'ANY /path/{proxy+}' does not require API Key. Request will not contribute to throttle or quota limits
2021-09-14T14:09:00.454+03:00   (01c53288-5d25-*******) Usage Plan check succeeded for API Key and API Stage **************/dev
2021-09-14T14:09:00.454+03:00   (01c53288-5d25-*******) Starting execution for request: 01c53288-5d25-*******
2021-09-14T14:09:00.454+03:00   (01c53288-5d25-*******) HTTP Method: GET, Resource Path: /path/json.json
2021-09-14T14:09:00.468+03:00   (01c53288-5d25-*******) Method completed with status: 304

我们可以从最后 2 行获取方法、uri 和返回码。为此,我将相关数据解析为参数,然后通过请求 id 进行聚合来获取它们(我也解析)

神奇之处在于:使用 和 之类的统计sortsFirst()数据sortsLast()并按@reqid. (AWS 文档

注意:IMO,不要使用earliest()latest()因为它们依赖于内置@timestamp并且对我来说很奇怪,其中 2 条连续消息具有相同的时间戳

因此,例如,使用此查询:

filter @message like "Method"
| parse @message /\((?<@reqid>.*?)\) (.*?) (Method: (?<@method>.*?), )?(.*?:)* (?<@data>[^\ ]*)/ 
| sort @timestamp desc
| stats sortsFirst(@method) as @reqMethod, sortsFirst(@data) as @reqPath, sortsLast(@data) as @reqCode by @reqid
| limit 20

我们将得到以下所需的输出:

@reqid                                  @reqMethod   @reqPath    @reqCode
f42e2b44-b858-45cb-*****************    GET          /path-******.json  304
fecddb03-3804-4ff5-*****************    OPTIONS      /path-******.json  200
e8e47185-6280-4e1e-*****************    GET          /path-******.json  304
e4fa9a0c-6d75-4e26-*****************    GET          /path-******.json  304
于 2021-09-14T12:57:37.723 回答