【问题标题】:fluentd loses milliseconds and now log messages are stored out of order in elasticsearchfluentd 丢失毫秒,现在日志消息在 elasticsearch 中乱序存储
【发布时间】:2015-03-11 18:47:31
【问题描述】:

我正在使用 fluentd 将日志消息集中在 elasticsearch 中,并使用 kibana 进行查看。当我查看日志消息时,同一秒内发生的消息是乱序的,@timestamp 中的毫秒数全为零

2015-01-13T11:54:01.000-06:00   DEBUG   my message

如何流利地存储毫秒?

【问题讨论】:

    标签: elasticsearch kibana fluentd


    【解决方案1】:

    fluentd 目前不支持亚秒级分辨率: https://github.com/fluent/fluentd/issues/461

    我通过使用 record_reformer 向所有日志消息添加一个新字段来解决此问题,以存储自纪元以来的纳秒

    例如,如果您的 fluentd 有这样的输入:

    #
    # Syslog
    #
    <source>
        type syslog
        port 5140
        bind localhost
        tag syslog
    </source>
    
    #
    # Tomcat log4j json output
    #
    <source>
        type tail
        path /home/foo/logs/catalina-json.out
        pos_file /home/foo/logs/fluentd.pos
        tag tomcat
        format json
        time_key @timestamp
        time_format "%Y-%m-%dT%H:%M:%S.%L%Z"
    </source>
    

    然后将它们更改为如下所示并添加一个添加纳秒字段的record_reformer

    #
    # Syslog
    #
    <source>
        type syslog
        port 5140
        bind localhost
        tag cleanup.syslog
    </source>
    
    #
    # Tomcat log4j json output
    #
    <source>
        type tail
        path /home/foo/logs/catalina-json.out
        pos_file /home/foo/logs/fluentd.pos
        tag cleanup.tomcat
        format json
        time_key @timestamp
        time_format "%Y-%m-%dT%H:%M:%S.%L%Z"
    </source>
    
    <match cleanup.**>
        type record_reformer
        time_nano ${t = Time.now; ((t.to_i * 1000000000) + t.nsec).to_s}
        tag ${tag_suffix[1]}
    </match>
    

    然后将 time_nano 字段添加到您的 kibana 仪表板并使用它而不是 @timestamp 进行排序,一切都会井井有条。

    【讨论】:

    • 感谢您的回答。 Fluentd 维护者在这里。当我们更多地考虑亚秒级时间戳支持时,我会牢记这个问题(这是一个已知问题/设计决策)。
    • 感谢田村清人对本期的关注。解决方法不太理想,因为时间戳是从 fluentd 生成的,而不是从可能已经具有至少毫秒精度的日志文件生成的。最好先对时间格式使用毫秒精度,然后在解析时将当前秒中的当前纳秒值添加到流利中,以将顺序保持在同一毫秒内。或者第二个,如果解析的日志消息只有 1 秒的分辨率,例如 syslog。
    • 嗨@DavidWartell,你认为使用fluent-plugin-record-reformer中的变量${time}会更好吗?所以我们可以获取事件的时间而不是 fluentd 的时间,而不是 Time.now
    • @DavidWartell,感谢您分享您的解决方案。有同样的问题。 @clarete,我最初只使用${time},这显然不起作用。我尝试在 David Wartell 的解决方案中用 ${time} 替换 Time.now,但这也不起作用。最后几位数字全为零。我认为这是因为${time} 的存储时间不会超过几秒。
    • 您将其转换为字符串是否有特定原因?为什么不直接将值返回为?
    猜你喜欢
    • 2018-07-18
    • 1970-01-01
    • 2023-01-03
    • 2021-01-05
    • 2020-05-01
    • 2015-01-10
    • 2021-12-14
    • 2017-02-14
    • 1970-01-01
    相关资源
    最近更新 更多