英文:
AWS Cloud Watch: How to specify which field to use for timestamp in json?
问题
我有以下代码:
datetime_format = "%Y-%m-%dT%H:%M:%S.%f%z"
在 /etc/awslogs/awslogs.conf 文件中。
而且我有这样的日志:
{
    "level": "info",
    "ts": "2023-01-08T21:46:03.381067Z",
    "caller": "bot/bot.go:172",
    "msg": "Creating test subscription declined",
    "user_id": "0394c017-2a94-416c-940c-31b1aadb12ee"
}
但是时间戳无法解析。
我在日志中看到了警告:
2023-01-08 21:46:03,423 - cwlogs.push.reader - WARNING - 9500 - Thread-4 - Fall back to previous event time: {'timestamp': 1673211877689, 'start_position': 6469L, 'end_position': 6640L}, previousEventTime: 1673211877689, reason: timestamp could not be parsed from message.
更新:
尝试移除 level:
{
    "ts": "2023-01-08T23:15:00.518545Z",
    "caller": "bot/bot.go:172",
    "msg": "Creating test subscription declined",
    "user_id": "0394c017-2a94-416c-940c-31b1aadb12ee"
}
但仍然无法工作。
英文:
I have
datetime_format = "%Y-%m-%dT%H:%M:%S.%f%z"
in /etc/awslogs/awslogs.conf
And I have log like this:
{
    "level": "info",
    "ts": "2023-01-08T21:46:03.381067Z",
    "caller": "bot/bot.go:172",
    "msg": "Creating test subscription declined",
    "user_id": "0394c017-2a94-416c-940c-31b1aadb12ee"
}
However timestamp does not parsed
I see warning in logs
2023-01-08 21:46:03,423 - cwlogs.push.reader - WARNING - 9500 - Thread-4 - Fall back to previous event time: {'timestamp': 1673211877689, 'start_position': 6469L, 'end_position': 6640L}, previousEventTime: 1673211877689, reason: timestamp could not be parsed from message.
upd:
tried to remove level
{
    "ts": "2023-01-08T23:15:00.518545Z",
    "caller": "bot/bot.go:172",
    "msg": "Creating test subscription declined",
    "user_id": "0394c017-2a94-416c-940c-31b1aadb12ee"
}
and still does not work.
答案1
得分: 1
- https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/AgentReference.html. 这已经被弃用,如页面警告部分所述。
 - https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-Agent-Configuration-File-Details.html. 这是新统一CloudWatch代理的配置,不包含
datetime_format参数。相反,它有timestamp_format。 
由于您提到了datetime_format,我假设您正在使用旧代理。在这种情况下,%z表示UTC偏移形式+HHMM或-HHMM。+0000,-0400,+1030,如链接文档[1上]所述。您的时间戳没有提到偏移,因此您的格式应为%Y-%m-%dT%H:%M:%S.%fZ。这里的Z类似于T,只表示一个字符。还请将time_zone指定为UTC。
英文:
There 2 different formats of cloudwatch log configurations:
- https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/AgentReference.html. This is deprecated as mentioned in the alert section of the page.
 - https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-Agent-Configuration-File-Details.html. This is the configuration for new unified cloudwatch agent and it doesn't have the parameter 
datetime_formatto configure. Instead it has thetimestamp_format. 
Since you have mentioned the datetime_format, I'm assuming you are using the old agent. In that case, the %z refers to UTC offset in the form +HHMM or -HHMM. +0000, -0400, +1030 as per the linked documentation[1 above]. Your timestamp doesn't have an offset mentioned hence your format should be %Y-%m-%dT%H:%M:%S.%fZ. There the Z is similar to T where it just represents a character. Also, specify the time_zone as UTC.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论