elasticsearch Fluent Bit Filter将Unix纪元时间戳转换为人类可读的时间格式

c6ubokkw  于 2022-11-02  发布在  ElasticSearch
关注(0)|答案(2)|浏览(275)

我有一个Java应用程序,在其中我使用Log4j2在JSONLayout中打印日志,下面是日志格式的示例:

{
    "thread": "TopicStatusThreadPool-thread-1",
    "level": "INFO",
    "loggerName": "My Kafka Logger",
    "message": "Topic Status List is empty, returning from summarize method",
    "endOfBatch": false,
    "loggerFqcn": "org.apache.logging.slf4j.Log4jLogger",
    "instant": {
        "epochSecond": 1587980988,
        "nanoOfSecond": 214241000
    },
    "threadId": 37,
    "threadPriority": 5
  }

以这种格式打印的日志然后被Fluent Bit拾取并推送到Elasticsearch中。我使用Kibana来可视化日志,但是当我看到日志和时间为epochSecondnonOfSecond时,由于格式的原因,很难将其与实际的应用程序日志相关联。
是否有任何Fluent Bit Filter可用于将此时间格式修改为更易于阅读的格式。
目前,我正在Fluent Bit配置中使用基本JSON解析器和Kubernetes过滤器将Kubernetes信息添加到日志消息中。

更新日期:

我对Log4j2配置进行了更改,现在我得到了timeMillis字段,它在日志中以毫秒为单位打印时间。

{
    "thread": "TopicStatusThreadPool-thread-1",
    "level": "INFO",
    "loggerName": "My Kafka Logger",
    "message": "Topic Status List is empty, returning from summarize method",
    "endOfBatch": false,
    "loggerFqcn": "org.apache.logging.slf4j.Log4jLogger",
    "timeMillis": 1587980988213,
    "threadId": 37,
    "threadPriority": 5
  }

什么是lua过滤器将其转换为人类可读的格式。默认情况下,Fluent Bit中的时间转换不支持以毫秒为单位的时间,它需要以秒为单位的时间。
我试过这个:

[PARSER]
        Name   json
        Format json
        Time_Key timeMillis
        Time_Format %s
        Time_Keep On

但这并没有选择毫秒部分,而是以秒为单位。

jucafojl

jucafojl1#

您可以按如下方式使用lua过滤器:

-- test.lua
function append_converted_timestamp(tag, timestamp, record)
    new_record = record
    new_record["instant"]["recorded_time"] = os.date("%m/%d/%Y %I:%M:%S %p", record["instant"]["epochSecond"])
    return 2, timestamp, new_record
end

FluentBit配置:

[FILTER]
    Name    lua
    Match   *
    script  test.lua
    call    append_converted_timestamp

它会将带有可读日期的新字段 recorded_time 追加到记录中。

更新

timeMillis字段的Lua函数可以如下实现:

-- test.lua
function append_converted_timestamp(tag, timestamp, record)
    new_record = record
    new_record["recorded_time"] = os.date("%m/%d/%Y %I:%M:%S %p", record["timeMillis"]/1000)
    return 2, timestamp, new_record
end
5f0d552i

5f0d552i2#

正如作者所提到的,Lua is not supporting milliseconds format. @rmax的答案是好的,但它没有捕捉毫秒部分,并添加到记录。我也无法找到一个直接的方法,但钉了一个变通办法。

function append_converted_timestamp(tag, timestamp, record)
    new_record = record
    convertedToDecimal = string.format("%.3f", record["epoch_time"]/1000)
    local integer, decimal = string.match(convertedToDecimal, "([^.]+)%.(.+)");
    convertedDate = os.date("%Y-%m-%dT%I:%M:%S", integer)
    new_record["time"] = convertedDate .. "." .. decimal
    record["epoch_time"] = nil
    return 1, timestamp, record
end

epoch_time与您的timeMillis字段类似。我另外做的是,删除了原来的epoch_time字段,并添加了一个新的字段名称time。否则最终结果将有重复的时间字段。

相关问题