Amazon CloudWatch Insights Query



我有这样的日志:

I, [2020-06-17T09:32:48.100103 #9]  INFO -- : [54b35e04-9c19-443d-adff-b2c3192b5590] Completed 500 Internal Server Error in 7ms (ActiveRecord: 2.3ms | Allocations: 1705)
I, [2020-06-17T10:37:27.169909 #9]  INFO -- : [c800e9ce-fba3-4e1a-a19f-526f32746925] Completed 401 Unauthorized in 0ms (ActiveRecord: 0.0ms | Allocations: 115)

因此,正如您在消息中看到的那样,它总是休耕模式:已完成 [错误代码] [错误消息] ...

我使用此查询检索具有某些错误代码的日志:

fields @timestamp, @message
| filter @message like /401/
| sort @timestamp desc
| limit 20

但是如何解析消息以获取错误代码和消息的单独字段?

您可以将parse函数与正则表达式语法一起使用:https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_QuerySyntax.html

例如,像这样:

parse @message /Completed (?<errorCode>d+) (?<errorMessage>.+) in (?<timeMilis>d+)ms /
| filter isPresent(errorCode)

结果将是这样的

-------------------------------------------------
| errorCode |     errorMessage      | timeMilis |
|-----------|-----------------------|-----------|
| 500       | Internal Server Error | 7         |
| 401       | Unauthorized          | 0         |
-------------------------------------------------

这将仅过滤和提取字段,您可以从那里进行进一步处理。

最新更新