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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章