我的lua脚本正在返回记录[键']

发布于 2025-01-21 12:07:45 字数 2508 浏览 3 评论 0原文

我正在尝试使用Lua使用Fluent Pit来更改日志来吹捧/掩模日志。该过滤器包含一个简单的数字替换为#使用GSUB。我正在以JSON格式的lua输入日志,无法访问钥匙值。

test.lua

        function modifyRecord(tag, timestamp, record)
        local new_record = record
        old_message = record["message"]
        digits = "[%d]+"
        new_message = old_message:gsub(digits, "#")
        new_record["message"] = new_message
        return 1, timestamp, new_record
        end

日志文件 - test.log

 echo '{"Test": "Logs","message":"This is a message 123432"}' >> test.log

fluent-bit.conf

    [SERVICE]
        # This is the main configuration block for fluent bit.
        # Ensure the follow line exists somewhere in the SERVICE block
        Plugins_File plugins.conf
    
    [INPUT]
        Name tail
        #Path /export/home/dummy/newrelic-infra/var/log/newrelic-infra/newrelic-infra.log
        Path /export/home/dummy/nrfb/test.log
   
    [OUTPUT]
        Name newrelic
        Match *
        licenseKey <key>
        proxy <proxy>
        endpoint <endpoint>
    
    [FILTER]
        Name modify
        Match *
        Add hostname <hostname>
        Add service_name newrelic-infra-fluent-bit
    
    [FILTER]
        Name    lua
        Match   *
        script  /export/home/dummy/nrfb/test.lua
        call    modifyRecord

fluent> flueent-bit logs

        -bash-4.2$ ./fluent-bit -c ./fluent-bit.conf
        Fluent Bit v1.6.3
        * Copyright (C) 2019-2020 The Fluent Bit Authors
        * Copyright (C) 2015-2018 Treasure Data
        * Fluent Bit is a CNCF sub-project under the umbrella of Fluentd
        * https://fluentbit.io
        
        [2022/04/15 11:09:20] [ info] [engine] started (pid=11454)
        [2022/04/15 11:09:20] [ info] [storage] version=1.0.6, initializing...
        [2022/04/15 11:09:20] [ info] [storage] in-memory
        [2022/04/15 11:09:20] [ info] [storage] normal synchronization mode, checksum disabled, max_chunks_up=128
        [2022/04/15 11:09:20] [ info] [sp] stream processor started
        [2022/04/15 11:09:20] [ info] [input:tail:tail.0] inotify_fs_add(): inode=194 watch_fd=1 name=/export/home/dummy/nrfb/test.log
        [2022/04/15 11:09:49] [error] [filter:lua:lua.1] error code 2: /export/home/dummy/nrfb/test.lua:5: attempt to index global 'old_message' (a nil value)

I am trying to fliter/mask logs by using fluent bit to change logs using lua. The filter contains a simple substitution of digits to # using gsub. I am entering logs in JSON format bit lua is not able to access key value.

test.lua

        function modifyRecord(tag, timestamp, record)
        local new_record = record
        old_message = record["message"]
        digits = "[%d]+"
        new_message = old_message:gsub(digits, "#")
        new_record["message"] = new_message
        return 1, timestamp, new_record
        end

Log file – test.log

 echo '{"Test": "Logs","message":"This is a message 123432"}' >> test.log

fluent-bit.conf

    [SERVICE]
        # This is the main configuration block for fluent bit.
        # Ensure the follow line exists somewhere in the SERVICE block
        Plugins_File plugins.conf
    
    [INPUT]
        Name tail
        #Path /export/home/dummy/newrelic-infra/var/log/newrelic-infra/newrelic-infra.log
        Path /export/home/dummy/nrfb/test.log
   
    [OUTPUT]
        Name newrelic
        Match *
        licenseKey <key>
        proxy <proxy>
        endpoint <endpoint>
    
    [FILTER]
        Name modify
        Match *
        Add hostname <hostname>
        Add service_name newrelic-infra-fluent-bit
    
    [FILTER]
        Name    lua
        Match   *
        script  /export/home/dummy/nrfb/test.lua
        call    modifyRecord

Fluent-bit logs

        -bash-4.2$ ./fluent-bit -c ./fluent-bit.conf
        Fluent Bit v1.6.3
        * Copyright (C) 2019-2020 The Fluent Bit Authors
        * Copyright (C) 2015-2018 Treasure Data
        * Fluent Bit is a CNCF sub-project under the umbrella of Fluentd
        * https://fluentbit.io
        
        [2022/04/15 11:09:20] [ info] [engine] started (pid=11454)
        [2022/04/15 11:09:20] [ info] [storage] version=1.0.6, initializing...
        [2022/04/15 11:09:20] [ info] [storage] in-memory
        [2022/04/15 11:09:20] [ info] [storage] normal synchronization mode, checksum disabled, max_chunks_up=128
        [2022/04/15 11:09:20] [ info] [sp] stream processor started
        [2022/04/15 11:09:20] [ info] [input:tail:tail.0] inotify_fs_add(): inode=194 watch_fd=1 name=/export/home/dummy/nrfb/test.log
        [2022/04/15 11:09:49] [error] [filter:lua:lua.1] error code 2: /export/home/dummy/nrfb/test.lua:5: attempt to index global 'old_message' (a nil value)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱格式化 2025-01-28 12:07:45

我在配置文件中遇到了错误,

[INPUT]
    Name tail
    #Path /export/home/dummy/newrelic-infra/var/log/newrelic-infra/newrelic-infra.log
    Path /export/home/dummy/nrfb/test.log

它应该设置为JSON的解析器设置/解析。
输入和添加解析器的变化可以解决问题。

[INPUT]
    Name tail
    #Path /export/home/dummy/newrelic-infra/var/log/newrelic-infra/newrelic-infra.log
    Path /export/home/dummy/nrfb/test.log
    Parser docker
[PARSER]
    Name        docker
    Format      json
    Time_Key    time
    Time_Format %Y-%m-%dT%H:%M:%S %z

I had mistake in config file

[INPUT]
    Name tail
    #Path /export/home/dummy/newrelic-infra/var/log/newrelic-infra/newrelic-infra.log
    Path /export/home/dummy/nrfb/test.log

It should have parser set to look/parse for json.
Changes in input and addition of parser did the trick.

[INPUT]
    Name tail
    #Path /export/home/dummy/newrelic-infra/var/log/newrelic-infra/newrelic-infra.log
    Path /export/home/dummy/nrfb/test.log
    Parser docker
[PARSER]
    Name        docker
    Format      json
    Time_Key    time
    Time_Format %Y-%m-%dT%H:%M:%S %z
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文