fluentd学习——fluent-plugin-rewrite插件重写


fluent-plugin-rewrite

 https://github.com/kentaro/fluent-plugin-rewrite#fluent-plugin-rewrite

所谓的插件重写,以为现在做的项目理解来说:客户端要向服务器端提交数据,重写是指在我把数据(一般指固定的格式,这个由正则表达式来匹配)上传之前,我要把数据的格式重写(增加删除字段就要用到rewrite插件)。

Component组成

<match apache.log.**>
  type rewrite

  remove_prefix apache.log
  add_prefix    filtered

  <rule>
    key     path
    pattern \\?.+$
    replace
  </rule>
  <rule>
    key     path
    pattern (/[^/]+)\\?([^=]+)=(\\d)
    replace \\1/\\2/\\3
  </rule>
  <rule>
    key     status
    pattern ^500$
    ignore  true
  </rule>
  <rule>
    key           path
    pattern       ^\/(users|entries)
    append_to_tag true
    fallback      others
  </rule>
  <rule>
    key           is_loggged_in
    pattern       1
    append_to_tag true
    tag           user
  </rule>
</match>

Configuration

配置

 

<match apache.log.**>
RewriteOutput 
重写输出

Output plugin to rewrite messages' tags/values along with pattern matching and re-emit them.

输出插件改写消息:tag/values 以及模式匹配并且回送他们。

Synopsis概要

 

remove_prefix / add_prefix         (@******重点)

remove_prefix apache.log
add_prefix    filtered
  • remove_prefix: removes the string from a prefix of tag.
  • add_prefix: prepend the string to a tag.
  • 删除前缀:删除一个tag前缀的字符串。(什么意思:删除一个tag的前缀,tag的前缀是什么,删除它。就是 tag 要匹配度的 在source 中 tag lj.test 这个文件,)
    增加前缀:预先考虑到标签的字符串。(什么意思)

      type rewrite   rewrite 重写插件,猜测(要重写,你必须是要把原先要匹配的东西tag删了,然后在forward之前,从新写入一个新文件,这就是下面的两行代码的实现)
      remove_prefix apache.log   
      add_prefix    filtered

rule: replace     (@******重点)

规则:代替

For example, if you want to filter out query string form URL string:

例如,如果你想从URL字符串过滤查询字符串的形式:
<rule>
  key     path
  pattern \\?.+$
  replace
</rule>

It executes pattern matching against a value related with key and replaces it with empty string if it matches.

它执行对相关key值的模式匹配和如果它匹配用空字符串代替。(说明:如果它匹配。是说,如果它和key值匹配,就用空字符串来代替。replace翻译成:代替。空字符串是什么意思:就是replace后面决定是要用什么来代替)

/foo?bar=baz -> /foo

This time, if you want to rewrite path string along with some pattern:

这一次,如果你想修改路径字符串和一些模式

<rule>
  key     path
  pattern (/[^/]+)\\?([^=]+)=(\\d)
  replace \\1/\\2/\\3
</rule>

It executes pattern matching against a value related with key and replaces it with replace if it matches.

它执行对相关key值模式匹配和如果它匹配用replace来代替。
(注意:key后的值是什么,key 可以理解为关键字,关键字是否和path匹配,如果匹配用replace这个来代替 ,深入了解)
/foo?bar=1 -> /foo/bar/1

rule: ignore

规则:忽略

For example, if you want to skip a message which matches some pattern:

例如:如果你想跳过匹配一些模式的一个消息。

<rule>
  key     status
  pattern ^500$
  ignore  true
</rule>

It executes pattern matching against a value related with key and skip emitting the message if it matches.

它执行对相关key值得模式匹配和如果它匹配跳过发射。(跳过发射:就是ignore)

rule: append_to_tag

规则:添加到标签
<rule>
  key           path
  pattern       ^\/(users|entries)
  append_to_tag true
</rule>

It executes pattern matching against a value related with key and append mathed strings to message tag. For example:

它执行对相关key值得模式匹配和把matched (匹配)的字符串附加到消息标签。
apache.log { "path" : "/users/antipop" }

the messabe above will be re-emmited as the message below:

上面的消息将会重新按照下面的消息发送:

apache.log.users { "path" : "/users/antipop" }   (说明:为什么append_to_tag true 增加后的显示是在apache.log后会加上user)

If you set fallback option like below:

如果你按下面的后备选项设置:

<rule>
  key           path
  pattern       ^\/(users|entries)
  append_to_tag true
  fallback      others
</rule>

the value of fallback option will be appended to message tag.

fallback 选项的值将会增加到消息日志中。

apache.log { "path" : "/foo/bar" }

This time, the messabe above will be re-emmited as the message below:

上面的消息将会重新按照下面的消息发送:(加入fallbach others 后在apache.log后也会加入一个后缀)

apache.log.others { "path" : "/foo/bar" }

If tag option set,(如果设置tag选项)

<rule>
  key           is_loggged_in
  pattern       1
  append_to_tag true  
  tag           user
</rule>

the value designated by tag will be appended to the original tag, that is:

这个被指定的tag值将会被附加到原始的tag,即:

test { "is_logged_in" => "1" }

will be

test.user { "is_logged_in" => "1" }  (看到这里前面的疑惑才豁然开朗了,是这么来的)

rule: last

规则:last

If you set last option to true, rewriting chain stops applying rule where the pattern matches first.

如果你把last选项设置为true,在第一次模式匹配重写链停止应用规则。(不知所云)

<rule>
  key     path
  pattern ^/foo$
  replace /bar
  last    true
</rule>
<rule>
  key     path
  pattern ^/bar$
  replace /baz
</rule>

This rules will be applied like below:

这个规则将应用如下:

{ "path" => "/foo" }(这说的是什么:key path  然后path指向 pattern,这个在上面没注意到)

will be replaced with

{ "path" => "/bar" }

and the chain stops here. Therefore, the second rule is never applied.  下面的两句也是来说明这个的。

{ "path" => "/bar" }

will be replaced by the second rule as usual.

{ "path" => "/baz" }
和链停止在这里。因此,第二个规则是从来没有用的。(明白了:第二个规则指:"path" => "/foo" 这个规则是被"path" => "/baz"取代了)

Installation

安装

Add this line to your application's Gemfile:

gem 'fluent-plugin-rewrite'

And then execute:

$ bundle

Or install it yourself as:

$ gem install fluent-plugin-rewrite

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值