使用 record_transformer 插件修改 fluentd json

Modify fluentd json with record_transformer plugin

我有这样的日志:

{
    "posts": {
        "key1": "value123",
        "key2": "abcdge123",
        "key3": "abcdge345",
        "....": "....",
        "something": "something"
    },
    "execute_time": 123,
    "code": 200,
}

这是 record_transformer 的流畅配置:

<filter tag.hellow>
  @type record_transformer
  enable_ruby true
  <record>
    posts ${ if record['posts'].has_key?('key1'); Base64.strict_encode64(record['posts']['key1'].to_s); end }
  </record>
</filter>

这会删除 posts 字段并重新创建,这样 posts 字段中的所有其他键都将丢失,即:

{
    "posts": {
        "key1": "base64XXXXXXX"
    },
    "execute_time": 123,
    "code": 200,
}

这是所需的日志输出:

{
    "posts": {
        "key1": "base64XXXXXXX",
        "key2": "abcdge123",
        "key3": "abcdge345",
        "....": "....",
        "something": "something"
    },
    "execute_time": 123,
    "code": 200,
}

这个用例有什么方法吗?

谢谢!

您需要 return 修改后的完整 posts 对象,如下所示:

posts ${ if record['posts'].has_key?('key1'); record['posts']['key1'] = Base64.strict_encode64(record['posts']['key1'].to_s); record['posts']; end }

当前形式即:

posts ${ if record['posts'].has_key?('key1'); Base64.strict_encode64(record['posts']['key1'].to_s); end }

它 return 是修改后分配给 posts 的第一个对象,这就是您所看到的。