Open13

Fluentdのドキュメントを読む

sgotandsgotand

1. Overview

https://docs.fluentd.org/quickstart

Fluentd treats logs as JSON

written primarily in C with a thin-Ruby wrapper

argest user currently collects logs from 50,000+ servers.

  • Step 1: Installing Fluentd
    NOTE: デフォルトのインストール方法にdockerの記載がない
  • Step 2: Use Cases
  • Step 3: Learn More
sgotandsgotand

1.1 Life of a Fluentd event

1.1.1 Basic Setup

Inputs =Event=> Output

in_http , out_stdout config 例

input
<source>
  @type http
  port 8888
  bind 0.0.0.0
</source>
output

Tagtest.cycleな incoming eventにマッチ

<match test.cycle>
  @type stdout
</match>
  • QUESTION: @typeを portなどより後ろに書くとどうなるか?
  • QUESTION: tagに使える文字集合は?
  • QUESTION: . 区切りの意義は?
  • QUESTION: コメントは?
テスト
$ curl -i -X POST -d 'json={"action":"login","user":2}' http://localhost:8888/test.cycle
  • in_httpではパスが Tag っぽい
  • json

1.1.2 Event Structure

  • tag : eventのorigin, routingに使われる

  • time

  • record: json

  • input pluginはevent生成の役割を担う

1.1.3 Processing Events

  • Router Engine 事前ルールがいくつかある
  • Eventは内部でchain of procedureを通過

Filter

<source>
  @type http
  port 8888
  bind 0.0.0.0
</source>

<filter test.cycle>
  @type grep
  <exclude>
    key action
    pattern ^logout$
  </exclude>
</filter>

<match test.cycle>
  @type stdout
</match>

ルールは上から下に順次適用

Label

  • topdown適用に起因するコンフィグファイルの複雑性低減のために使われる
  • topdownに縛られない新たなRoutingセクションの追加
    • linked reference
<source>
  @type http
  bind 0.0.0.0
  port 8888
  @label @STAGING
</source>

<filter test.cycle>
  @type grep
  <exclude>
    key action
    pattern ^login$
  </exclude>
</filter>

<label @STAGING>
  <filter test.cycle>
    @type grep
    <exclude>
      key action
      pattern ^logout$
    </exclude>
  </filter>

  <match test.cycle>
    @type stdout
  </match>
</label>

<source>の末尾に @labelがあると、そちらに処理が移る

  • QUESTION: 分岐や合流は可能か?
  • QUESTION: <filter> などにも@labelをつけられるか?
  • QUESTION: @labelの後に何か書くとどうなるのか?

Buffers

out_stdout は non-buffered

NOTE: indexを見る限りBufferはInput, Output, Filterと同レベルでpluginとして実装されているっぽい

sgotandsgotand

Configuration

Config File Syntax

https://docs.fluentd.org/configuration/config-file

file location

  • インストール方法によって異なる
    • package, Gem, Docker

Charcter set

  • UTF-8
  • ASCII

Directives

  • source
  • match
  • filter
  • system
  • label
  • @include

source (input plugin)

@type (必須)でプラグインを指定
標準で以下を含む

  • http
  • forward (TCP)

tagは. 区切りの ^[a-z0-9_]+$ を推奨

match (output plugin)

@type (必須)でプラグインを指定
標準で以下を含む

  • file
  • forward
    • QUESTION: TCP?
# Match events tagged with "myapp.access" and
# store them to /var/log/fluent/access.%Y-%m-%d
# Of course, you can control how you partition your data
# with the time_slice_format option.
<match myapp.access>
  @type file
  path /var/log/fluent/access
</match>

filter

<filter myapp.access>
  @type record_transformer
  <record>
    host_param "#{Socket.gethostname}"
  </record>
</filter>

system

システムワイドな設定

  • log_level
  • suppress_repeated_stacktrace
  • emit_error_log_interval
  • suppress_config_dump
  • without_source
  • process_name

label

label 自体は組み込みのパラメタなので @label

The label parameter is useful for event flow separation without the tag prefix.

@ERROR label

プラグインの emit_error_event APIで吐かれたものに適用

@ROOT label

v1.14.0から
プラグインの event_emitter_router API
This label is introduced since v1.14.0 to assign a label back to the default route. For example, timed-out event records are handled by the concat filter can be sent to the default route.

@include

# absolute path
@include /path/to/config.conf

# if using a relative path, the directive will use
# the dirname of this config file to expand the path
@include extra.conf

# glob match pattern
@include config.d/*.conf

# http
@include http://example.com/fluent.conf

globは辞書順に展開。だがこれに依存するのは良くない

セクション内部での共通パラメタの読み込みとかにも使える

sgotandsgotand

How do the match patterns work?

  • * matches a single tag part.
    • a.* = a.b
  • ** matches zero or more tag parts.
    • a.** = a, a.b, a.b.c
  • {a,b} = a, b
    • a.{b,c}.* a.{b,c.**}
  • /regular expression/
    • v1.11.2+
  • A B = A, B

Note on Match Order

  • コンフィグファイルの登場順
    • => longestは上に定義すべき
  • 複数の出力先=> out_copy pluginを使う
  • #{...} でルビーコードを埋め込める
    • v1.4.0

Supported Data Types for Values

skip

Common Plugin Parameters

  • @type
  • @id
  • @label
  • @log_level

Check Configuration File

$ fluentd --dry-run -c fluent.conf

でvalidationできる

Format Tips

文字列("...")、配列、ハッシュによる 複数行サポート

埋め込みRuby

sgotandsgotand

https://docs.fluentd.org/configuration/routing-examples

Simple: Input -> Filter -> Output

ソケット名取得、良さげ

略
<filter app.**>
  @type record_transformer
  <record>
    hostname "#{Socket.gethostname}"
  </record>
</filter>
略

Two Inputs: forward and tail

With Label: Input -> Filter -> Output

dstat input plugin良さげ
とはいえ、prometheusのnode exporterを使う場合が多いので、差別化が難しい
push/pullの違いはある

Reroute Event by Tag

fluent-plugin-route plugin
Outputプラグインだが、タグを書き換えてイベント再送できる
fan-outに使える

Re-route Event by Record Content

fluent-plugin-rewrite-tag-filter プラグイン

Re-route Event to Other Label

out_relabel プラグイン
タグの書き換えなしで別ラベルに送る

sgotandsgotand

https://docs.fluentd.org/configuration/buffer-section

<match>セクションで使われそう

  • buf_file
  • buf_memory

@typeを指定しない場合、output pluginで指定されたものが使われる
指定されていない場合、memory

Chunk Keys

output プラグインはイベントを chunk に分割する.
bufferセクションでどのようにチャンクわけするか決められる

  • 未指定の場合: 規定チャンクサイズを超えるまで貯める
  • []: アウトプットプラグインが指定したものを使える
  • tag: タグごとにグループ化
  • time: timekey で指定された秒数ごとにグループ化
    • QUESTION: バッファオーバーフローしないのか?
    • デフォルトではrangeの終わりから10分待ってflush
  • その他: イベントのキーをchunk keyとして指定できる
    • $.nest.fieldなどのように奥にあるキーも指定できる

これらの複合も可能 <buffer tag, time>
chunk key数のハードリミットはない

Placeholders

Parameters

sgotandsgotand

https://docs.fluentd.org/configuration/format-section

<match>, <filter> セクションにかける
NOTE: 基本はjsonなのでその互換ならできそう

  • out_file
    • QUESTION: とは?
  • json
  • ltsv
  • csv
  • msgpack
  • hash
    • QUESTION: hash mapではなくハッシュ化?
  • single_value

https://docs.fluentd.org/configuration/extract-section

<match>, <source>, or <filter>

1つの値を取り出すっぽい?

  • QUESTION: 部分取り出しとかはできないのかな?

https://docs.fluentd.org/configuration/inject-section

<match> or <filter>

値を入れ込む

<inject>
  time_key fluentd_time
  time_type string
  time_format %Y-%m-%dT%H:%M:%S.%NZ
  tag_key fluentd_tag
</inject>

QUESTION: fluentd_tag で fluendの tagを取得?? fluentd_ プレフィックス?

sgotandsgotand

https://docs.fluentd.org/configuration/transport-section

<match>, <source>, and <filter>

トランスポートプロトコル, バージョン, (TLS)証明書を指定。
in, outはわかるが、filter???

https://docs.fluentd.org/configuration/storage-section
<source>, <match> or <filter>

<storage>をサポートしているプラグインがある
同じく、<filter>は?

<storage>
  @type local
</storage>

https://docs.fluentd.org/configuration/service_discovery-section
Output プラグインで使う
<service_discovery>

  • static
  • file
  • srv
    • SRV レコード
sgotandsgotand

https://docs.fluentd.org/deployment/system-config

<system> セクション or コマンドラインオプション

  • workers
  • root_dir
  • log_level
  • suppress_repeated_stacktrace
  • emit_error_log_interval
  • ignore_repeated_log_interval
  • ignore_same_log_interval
  • suppress_config_dump
  • log_event_verbose
  • without_source
    • input プラグインなしでfluendをinvoke
  • rpc_endpoint
    • QUESTION: rpcをどこで使う?
    • pluginを別プロセスで動かす?
  • enable_get_dump
    • QEUSTION: なんのget?
  • process_name
  • enable_msgpack_time_support
  • file_permission
  • dir_permission
  • strict_config_value
  • disable_shared_socket

<log> section

  • format
  • time_format
  • rotate_age
  • rotate_size
  • enable_input_metrics
  • enable_size_metrics

https://docs.fluentd.org/deployment/logging

https://docs.fluentd.org/deployment/signals

SIGINT, SIGTERM

  • デーモンをgracefulに止める
    • memoryバッファをflushする
    • ただし、失敗してもretryしない
    • fileバッファの場合はデフォルトでpersistするのでflushしない

SIGUSR1

  • バッファをflushしてlogをreopenする
    • memory, fileバッファをflushする
    • flush_intervalでフラッシュし続ける

SIGUSR2

  • コンフィグファイルをリロードし、gracefulにdatapipelineを再構築する
    • SIGINT, SIGTERMと同じbuffer flush仕様

SIGHUP

  • v1.19以降はSIGUSR2を使うべき

SIGCONT

  • 内部状態をダンプする

https://docs.fluentd.org/deployment/rpc

signalの代わりに使える
-- no-superiser なしで起動する必要がある