简体   繁体   English

使用 fluentd JSON 解析器的多种时间格式

[英]Multiple time formats using fluentd JSON parser

I'm currently reading container logs as my source in fluentd, and I'm parsing all of our log files which is in JSON format.我目前正在阅读容器日志作为我在 fluentd 中的源,并且我正在解析我们所有的日志文件,这些文件采用 JSON 格式。 The JSON parser is working as expected based on our configuration, but the issue is the time format. JSON 解析器根据我们的配置按预期工作,但问题在于时间格式。 There are multiple time formats depending on the log file.根据日志文件,有多种时间格式。

As an example, one log file might store time as 2020-05-28T21:00:07Z, but another will store it as 2020-04-28T20:07:52.013557931Z.例如,一个日志文件可能将时间存储为 2020-05-28T21:00:07Z,但另一个日志文件将其存储为 2020-04-28T20:07:52.013557931Z。 The time is stored in a key named "time" in all cases, too, so fluentd by default attempts to parse this key.在所有情况下,时间都存储在一个名为“time”的键中,因此默认情况下 fluentd 会尝试解析该键。

Is it possible to use the same filter to parse all JSON data but have it handle multiple time formats?是否可以使用相同的过滤器来解析所有 JSON 数据,但它是否可以处理多种时间格式? Would we need to create different filters for each different time format?我们是否需要为每种不同的时间格式创建不同的过滤器?

This is more of a 'fluentd' question, than a straight 'Ruby' question.这更像是一个“流利”的问题,而不是一个直接的“Ruby”问题。

However, to your specific question about filters, generally speaking it's impossible to tell specific date formats based on the value alone.但是,对于您关于过滤器的具体问题,一般来说,仅根据值无法判断具体的日期格式。 For example, does '2020-03-04' mean March 4 (yyyy-MM-dd), or April 3 (yyyy-dd-MM)?例如,“2020-03-04”是指 3 月 4 日 (yyyy-MM-dd) 还是 4 月 3 日 (yyyy-dd-MM)? Unfortunately there's no way to tell from values alone.不幸的是,没有办法仅从价值观来判断。

The best course of action is generally to verify the format per source (by reading documentation for that source or otherwise), and make sure identifying information is available to tie the date value back to the source.最好的做法通常是验证每个来源的格式(通过阅读该来源的文档或其他方式),并确保可以使用识别信息将日期值与来源联系起来。 Without that, neither fluentd nor any other program will be able to determine what time it is (although you can do tricks in some situations to determine what time it isn't .)否则,fluentd 和任何其他程序都无法确定现在是什么时间(尽管您可以在某些情况下使用技巧来确定不是什么时间。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM