【问题标题】:Fluent-bit - Splitting json log into structured fields in ElasticsearchFluent-bit - 将 json 日志拆分为 Elasticsearch 中的结构化字段
【发布时间】:2019-11-12 11:15:53
【问题描述】:

我正在尝试在 Fluent-bit 配置中找到一种方法来告诉/强制 ES 存储纯 json 格式的日志(下面的 log 位来自 docker stdout/stderror em>) 以结构化的方式 - 请参阅底部的图像以获得更好的解释。例如,除了(或同时)将日志存储为 log 字段下的普通 json 条目之外,我还想单独存储每个属性,如 red 所示。

过滤器和解析器的文档真的很差而且不清楚。除此之外,forward 输入没有“解析器”选项。我尝试了json/docker/regex 解析器,但没有运气。如果我必须使用正则表达式,我的正则表达式是 here。目前使用 ES (7.1)、Fluent-bit (1.1.3) 和 Kibana (7.1) - 而不是 Kubernetes。

如果有人能指导我举一个例子或给出一个例子,我将不胜感激。

谢谢

{
  "_index": "hello",
  "_type": "logs",
  "_id": "T631e2sBChSKEuJw-HO4",
  "_version": 1,
  "_score": null,
  "_source": {
    "@timestamp": "2019-06-21T21:34:02.000Z",
    "tag": "php",
    "container_id": "53154cf4d4e8d7ecf31bdb6bc4a25fdf2f37156edc6b859ba0ddfa9c0ab1715b",
    "container_name": "/hello_php_1",
    "source": "stderr",
    "log": "{\"time_local\":\"2019-06-21T21:34:02+0000\",\"client_ip\":\"-\",\"remote_addr\":\"192.168.192.3\",\"remote_user\":\"\",\"request\":\"GET / HTTP/1.1\",\"status\":\"200\",\"body_bytes_sent\":\"0\",\"request_time\":\"0.001\",\"http_referrer\":\"-\",\"http_user_agent\":\"curl/7.38.0\",\"request_id\":\"91835d61520d289952b7e9b8f658e64f\"}"
  },
  "fields": {
    "@timestamp": [
      "2019-06-21T21:34:02.000Z"
    ]
  },
  "sort": [
    1561152842000
  ]
}

谢谢

配置

[SERVICE]
    Flush        5
    Daemon       Off
    Log_Level    debug
    Parsers_File parsers.conf

[INPUT]
    Name   forward
    Listen 0.0.0.0
    Port   24224

[OUTPUT]
    Name  es
    Match hello_*
    Host  elasticsearch
    Port  9200
    Index hello
    Type  logs
    Include_Tag_Key On
    Tag_Key tag

【问题讨论】:

    标签: elasticsearch logging fluentd fluent-bit


    【解决方案1】:

    解决方法如下。

    [SERVICE]
        Flush        5
        Daemon       Off
        Log_Level    debug
        Parsers_File parsers.conf
    
    [INPUT]
        Name         forward
        storage.type filesystem
        Listen       my_fluent_bit_service
        Port         24224
    
    [FILTER]
        Name         parser
        Parser       docker
        Match        hello_*
        Key_Name     log
        Reserve_Data On
        Preserve_Key On
    
    [OUTPUT]
        Name            es
        Host            my_elasticsearch_service
        Port            9200
        Match           hello_*
        Index           hello
        Type            logs
        Include_Tag_Key On
        Tag_Key         tag
    
    [PARSER]
        Name         docker
        Format       json
        Time_Key     time
        Time_Format  %Y-%m-%dT%H:%M:%S.%L
        Time_Keep    On
        # Command      |  Decoder | Field | Optional Action
        # =============|==================|=================
        Decode_Field_As   escaped_utf8    log    do_next
        Decode_Field_As   json       log
    

    【讨论】:

    • 非常感谢您的回答。文档简直太可怕了。一个问题:您的log 条目最终是如何解码的?我得到一行 key=value(例如name=john age=27 city=paris)而不是解码结构(它不再是 JSON 字符串,但也不是 Kibana 可见的结构)
    • 不确定我是否理解您的确切意思,但我的应用程序日志默认为 JSON 格式。因此,如果是我的应用程序,您的示例将是 {"name":"john","age":"27","city":"paris"}。之后,整个字符串在 Kibana 中的 log 键下也看起来相同,如上图所示。我希望它有所帮助。还可以查看this 以获得更详细的示例。
    • 抱歉没有说清楚。我曾经在我的日志中将{"name":"john","age":"27","city":"paris"} 作为message 条目,由 Kibana 显示。我希望这个条目可以被 Fluent Bit 解码,以便它作为一个真正的 JSON 条目进入 Elasticsearch,这样我就可以将键 nameagecity 作为字段(与你的入口tagsource
    • (续)我所拥有的仍然是 message 条目,现在是 name=john age=27 city=paris(而不是之前的 JSON 字符串表示形式)。我想知道这是否是预期的行为(这使得解码器无用,因为我无法搜索键 city 例如)
    • 换句话说,message 下的条目已经从字符串{"name":"john","age":"27","city":"paris"} 重写为字符串name=john age=27 city=paris,这不是我期望的解析(→ 以“分解”JSON 字符串到 Kibana 的实际字段中)
    【解决方案2】:

    您可以为此使用 Fluent Bit Nest 过滤器,请参阅以下文档:

    https://docs.fluentbit.io/manual/filter/nest

    【讨论】:

    • OP - “过滤器和解析器的文档真的很差而且不清楚。”。我在文档上花了足够多的时间,因此原因最终得到了这个问题。
    • 文档极度缺乏
    猜你喜欢
    • 2022-08-19
    • 1970-01-01
    • 2021-02-19
    • 2020-11-08
    • 1970-01-01
    • 2018-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多