fluentd filter的record_transformer插件(v0.12)

https://docs.fluentd.org/v0.12/articles/filter_record_transformer

The filter_record_transformer filter plugin mutates/transforms incoming event streams in a versatile manner. If there is a need to add/delete/modify events, this plugin is the first filter to try.

Table of Contents

Example Configurations

filter_record_transformer is included in Fluentd’s core. No installation required.

<filter foo.bar>
  @type record_transformer
  <record>
    hostname "#{Socket.gethostname}"
    tag ${tag}
  </record>
</filter>

The above filter adds the new field “hostname” with the server’s hostname as its value (It is taking advantage of Ruby’s string interpolation) and the new field “tag” with tag value. So, an input like

{"message":"hello world!"}

is transformed into

{"message":"hello world!", "hostname":"db001.internal.example.com", "tag":"foo.bar"}

Here is another example where the field “total” is divided by the field “count” to create a new field “avg”:

<filter foo.bar>
  @type record_transformer
  enable_ruby
  <record>
    avg ${record["total"] / record["count"]}
  </record>
</filter>

It transforms an event like

{"total":100, "count":10}

into

{"total":100, "count":10, "avg":"10"}

With the enable_ruby option, an arbitrary Ruby expression can be used inside ${...}. Note that the “avg” field is typed as string in this example. You may use auto_typecast true option to treat the field as a float.

You can also use this plugin to modify your existing fields as

<filter foo.bar>
  @type record_transformer
  <record>
    message yay, ${record["message"]}
  </record>
</filter>

An input like

{"message":"hello world!"}

is transformed into

{"message":"yay, hello world!"}

Finally, this configuration embeds the value of the second part of the tag in the field “service_name”. It might come in handy when aggregating data across many services.

<filter web.*>
  @type record_transformer
  <record>
    service_name ${tag_parts[1]}
  </record>
</filter>

So, if an event with the tag “web.auth” and record {"user_id":1, "status":"ok"} comes in, it transforms it into {"user_id":1, "status":"ok", "service_name":"auth"}.

Parameters

<record> directive

Parameters inside <record> directives are considered to be new key-value pairs:

<record>
  NEW_FIELD NEW_VALUE
</record>

For NEW_FIELD and NEW_VALUE, a special syntax ${} allows the user to generate a new field dynamically. Inside the curly braces, the following variables are available:

  • The incoming event’s existing values can be referred by their field names. So, if the record is {"total":100, "count":10}, then record["total"]=100 and record["count"]=10.
  • tag_parts[N] refers to the Nth part of the tag. It works like the usual zero-based array accessor.
  • tag_prefix[N] refers to the first N parts of the tag. It works like the usual zero-based array accessor.
  • tag_suffix[N] refers to the last N parts of the tag. It works like the usual zero-based array accessor.
  • tag refers to the whole tag.
  • time refers to stringanized event time.
  • hostname refers to machine’s hostname. The actual value is result of Socket.gethostname.

enable_ruby (optional)

When set to true, the full Ruby syntax is enabled in the ${...} expression. The default value is false.

With true, additional variables could be used inside ${}.

  • record refers to the whole record.
  • time refers to event time as Time object, not stringanized event time.

Here is the examples:

jsonized_record ${record.to_json}
avg ${record["total"] / record["count"]}
formatted_time ${time.strftime('%Y-%m-%dT%H:%M:%S%z')}
escaped_tag ${tag.gsub('.', '-')}
last_tag ${tag_parts.last}
foo_${record["key"]} bar_${record["value"]}

auto_typecast (optional)

Automatically cast the field types. Default is false.

LIMITATION: This option is effective only for field values comprised of a single placeholder.

Effective Examples:

foo ${record["foo"]}

Non-Effective Examples:

foo ${record["foo"]}${record["bar"]}
foo ${record["foo"]}bar
foo 1

Internally, this keeps the original value type only when a single placeholder is used.

renew_record (optional)

By default, the record transformer filter mutates the incoming data. However, if this parameter is set to true, it modifies a new empty hash instead.

renew_time_key (optional, string type)

renew_time_key foo overwrites the time of events with a value of the record field foo if exists. The value of foo must be a unix time.

keep_keys (optional, array type)

A list of keys to keep. Only relevant if renew_record is set to true.

remove_keys (optional, array type)

A list of keys to delete.

Need more performance?

filter_record_modifier is light-weight and faster version of filter_record_transformerfilter_record_modifier doesn’t provide several filter_record_transformer features, but it covers popular cases. If you need better performace for mutating records, consider filter_record_modifier instead.

FAQ

What are the differences between ${record["key"]} and ${key}?

${key} is short-cut for ${record["key"]}. This is error prone because ${tag} is unclear for event tag or record["tag"]. So the ${key} syntax is now deprecated for avoiding this problem. Don’t use ${key} short-cut syntax on the production.

Since v0.14, ${key} short-cut syntax is removed.

Learn More

好的,您想了解如何使用Fluentd读写OSS,我可以为您提供一些基本信息。首先,Fluentd是一种开源的数据收集和转发引擎,它可以将来自不同数据源的数据进行聚合、过滤、转换和输出。而OSS(Object Storage Service)是阿里云提供的一种云端对象存储服务,可以用来存储和管理各种类型的非结构化数据,如图片、视频、文本等。下面是使用Fluentd读写OSS的基本步骤: 1. 安装Fluentd和相应的OSS插件。您可以在Fluentd的官网上下载和安装最新版本的Fluentd,然后使用以下命令安装OSS插件: ``` gem install fluent-plugin-oss ``` 2. 配置Fluentd。您需要在Fluentd的配置文件添加OSS输出插件的相关信息,如OSS的访问密钥、Bucket名称、Region等。以下是一个示例配置: ``` <match oss.*> @type oss oss_key_id YOUR_ACCESS_KEY_ID oss_access_key YOUR_ACCESS_KEY_SECRET oss_bucket YOUR_BUCKET_NAME oss_region YOUR_BUCKET_REGION <buffer> @type memory flush_interval 10s </buffer> </match> ``` 3. 使用Fluentd向OSS写入数据。您可以使用Fluentd的in_tail插件来监控日志文件,并将其输出到OSS。以下是一个示例配置: ``` <source> @type tail path /path/to/your/log/file pos_file /var/log/td-agent/your_log.pos tag oss.your_log format json <parse> @type json </parse> </source> <match oss.your_log> @type oss oss_key_id YOUR_ACCESS_KEY_ID oss_access_key YOUR_ACCESS_KEY_SECRET oss_bucket YOUR_BUCKET_NAME oss_region YOUR_BUCKET_REGION <buffer> @type memory flush_interval 10s </buffer> </match> ``` 4. 从OSS读取数据。您可以使用Fluentd的in_oss插件来读取OSS的数据,并将其输出到其他数据源。以下是一个示例配置: ``` <source> @type oss oss_key_id YOUR_ACCESS_KEY_ID oss_access_key YOUR_ACCESS_KEY_SECRET oss_bucket YOUR_BUCKET_NAME oss_prefix YOUR_OBJECT_PREFIX oss_endpoint YOUR_BUCKET_ENDPOINT format json tag oss.your_data </source> <match oss.your_data> @type stdout </match> ``` 这些是使用Fluentd读写OSS的基本步骤,您可以根据自己的需求进行配置和调整。需要注意的是,使用Fluentd读写OSS可能会产生一定的费用,具体费用和计费方式可以参考阿里云的官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值