【问题标题】:Parse python multiline traceback to one line with fluentbit使用fluentbit解析python多行回溯到一行
【发布时间】:2021-11-12 12:09:42
【问题描述】:

我的 python 应用程序将日志写入 STDOUT,我正在使用 fluentbit 代理收集日志。我的日志示例

 
Checking for future activity
 
Activity Time: 1636487814
 
Current Time: 1636490831
 
Not the future activity request for athlete: 505
  
[2021-11-09 20:47:11,781] ERROR in app: Exception on /api/v1/lala/activity-request [POST]
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/usr/local/lib/python3.7/site-packages/newrelic/hooks/framework_flask.py", line 64, in _nr_wrapper_handler_
    return wrapped(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/flask_restful/__init__.py", line 467, in wrapper
    resp = resource(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/flask/views.py", line 89, in view
    return self.dispatch_request(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/flask_restful/__init__.py", line 582, in dispatch_request
    resp = meth(*args, **kwargs)
  File "./test-app/api/resources/lala/v1/resources.py", line 111, in post
    lalaService.send_activity_request_to_broker(athlete, activity_request)
  File "./test-app/api/resources/lala/services.py", line 94, in send_activity_request_to_broker
    ThirdPartyService.produce_activity(activity_data)
  File "./test-app/api/resources/common/services.py", line 91, in produce_activity
    get_or_create_eventloop().run_until_complete(produce_activity(activity_data))
  File "/usr/local/lib/python3.7/asyncio/base_events.py", line 587, in run_until_complete
    return future.result()
  File "./test-app/producer.py", line 12, in produce_activity
    await result
RuntimeError: Task <Task pending coro=<produce_activity() running at ./test-app/producer.py:12> cb=[remove_from_cache() at /usr/local/lib/python3.7/site-packages/newrelic/hooks/coroutines_asyncio.py:20, _run_until_complete_cb() at /usr/local/lib/python3.7/asyncio/base_events.py:157]> got Future <Future pending cb=[Topic._on_published(message=<FutureMessage pending>, state={}, producer=<Producer: running >)()]> attached to a different loop
 
[pid: 10|app: 0|req: 15952/63804] 10.0.225.22 () {60 vars in 2181 bytes} [Tue Nov  9 20:47:11 2021] POST /api/v1/lala/activity-request => generated 37 bytes in 506 msecs (HTTP/1.1 500) 2 headers in 90 bytes (1 switches on core 0)
 
Duplicate request.id = 505 workout time = 2021-08-28T08:27:21.000Z

对于回溯,我想用一行发送完整的回溯。

是否可以使用 fluentbit 解析器做到这一点?

【问题讨论】:

    标签: python django newrelic fluentd fluent-bit


    【解决方案1】:

    Fluent Bit 支持解析多行消息,你只需要识别:

    1. 标识日志消息第一行的内容。在您的情况下,所有消息都以空格开头,后跟换行符 (\n) 字符。正则表达式:^\s\n
    2. 是什么使日志消息中的其他行。很简单:它不是空格换行符 (^(?!\s\n).+)。

    现在只需编辑documentation 中的示例代码。

    parsers_multiline.conf:

    [MULTILINE_PARSER]
        name          multiline-regex-test
        type          regex
        flush_timeout 1000
        # rules |   state name  | regex pattern         | next state
        # ------|---------------|-----------------------------------
        rule      "start_state"   "/^\s\n/"               "cont"
        rule      "cont"          "/^(?!\s\n).+/"         "cont"
    

    fluent-bit.conf:

    [SERVICE]
        (...)
        parsers_file parsers_multiline.conf
    
    [INPUT]
        name             tail
        path             test.log
        read_from_head   true
        multiline.parser multiline-regex-test
    
    [OUTPUT]
        (...)
    

    【讨论】:

    • 我已经阅读了文档。你确定这会奏效吗?对于 fluentd,我们可以给出一个起始模式和一个结束模式来检测多行回溯。问题是我对多行结束模式没有信心。对于 fluentbit,已经有一些用于此目的的内置解析器,但它们无法正确解析多行。顺便说一句,我通过将回溯转换为 JSON 并解析它来解决它。这是我认为最简单的方法。
    • @Thekira 是的,我在 django 项目的流利位边车的生产中也有类似的模式(start_state 识别[timestamp] rest of the line 格式,而cont 模式是“任何不启动的东西带括号” (/^[^\[].+/)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-16
    • 2019-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-30
    相关资源
    最近更新 更多