CloudWatch Insights 查询:格式化日期时间字符串以进行分组

ama*_*ert 5 amazon-web-services amazon-cloudwatch amazon-cloudwatchlogs aws-cloudwatch-log-insights

我有 json 格式的 CloudWatch 日志,其条目类似于以下内容:

{
    "message": "resource_liked",
    "context": {
        "date": {
            "date": "2021-05-07 16:52:11.000000",
            "timezone_type": 3,
            "timezone": "UTC"
        },
    ...
Run Code Online (Sandbox Code Playgroud)

我正在尝试编写一个 CloudWatch Insights 查询来制作一个简单的直方图:每小时日志中的事件数。

但是,我无法使用@timestamp日志条目的属性。我需要context.date.date在条目的消息正文中使用。

使用以下命令编写此查询@timestamp非常简单:

stats count(*) by datefloor(@timestamp, 1h)
Run Code Online (Sandbox Code Playgroud)

但是,我不确定如何使用该消息context.date.date

我认为我需要将日期时间格式化2021-05-07 16:52:11.000000为 aws 理解的日期时间,但我找不到如何操作。


到目前为止我尝试过的

stats count(*) by datefloor(context.date.date, 1h)->“无效日期”

stats count(*) by datefloor(toMillis(context.date.date), 1h)->“无效日期”

stats count(*) by datefloor(substr(context.date.date, 0, 19), 1h)->“无效日期”

stats count(*) by datefloor(concat(replace(substr(context.date.date, 0, 23), ' ', 'T'), '-00:00'), 1h)-> 无效日期。这使得该字段看起来与 @timestamp 的显示方式完全相同。

Jeh*_*Ahn 6

| parse @message '"date": "*:' as hour
| stats count() as cnt by hour
Run Code Online (Sandbox Code Playgroud)

解析时间戳以与 CW Log Insights 函数一起使用


小智 5

可能有助于将字符串日期时间转换为数值毫秒,请注意,由于闰年计算,最大年份为 2100。

fields "2021-05-07 16:52:11.000000" as reqDateTime
| parse reqDateTime "*-*-* *:*:*.*" as reqYear, reqM, reqD, reqH, reqMin, reqSec, reqMilliSec
| fields reqYear - 1970 as reqYearDiff, reqYear % 4 == 0 as reqIsLeapYear, reqM/1 as reqMonth, reqD/1 as reqDay, reqH/1 as reqHour, reqMin/1 as reqMinute, reqSec/1 as reqSecond, reqMilliSec/1 as reqMilliSecond
| fields ((reqYearDiff * 365) + ((reqYear % 4 == 1) * 1) + floor(reqYearDiff / 4) # as yearsToDays
         + ((reqMonth == 2) * 31) # 
         + ((reqMonth == 3) * 59) #
         + ((reqMonth == 4) * 90) #
         + ((reqMonth == 5) * 120) #
         + ((reqMonth == 6) * 151) #
         + ((reqMonth == 7) * 181) #
         + ((reqMonth == 8) * 212) #
         + ((reqMonth == 9) * 243) #
         + ((reqMonth == 10) * 273) #
         + ((reqMonth == 11) * 304) #
         + ((reqMonth == 12) * 334) #
         + ((reqMonth > 2) and (reqIsLeapYear == 1)) # as monthsToDays
         + reqDay - 1) * 24 * 60 * 60 * 1000 # as daysToMilliSeconds
         + reqHour * 60 * 60 * 1000 # as hoursToMilliSeconds
         + reqMinute * 60 * 1000 # as minutesToMilliSeconds
         + reqSecond * 1000 # as secondsToMilliSeconds
         + reqMilliSecond  
         as reqMilliSeconds
| display reqMilliSeconds, fromMillis(reqMilliSeconds), reqYear, reqMonth, reqDay, reqHour, reqMinute, reqSecond, reqMilliSecond
| limit 1
Run Code Online (Sandbox Code Playgroud)