🔎

Logstashでfilter処理中にデバッグプリントする

に公開

はじめに

  • Logstashのfilterを開発中にデバッグプリントが欲しくなる
    • 標準のruby filter pluginでやってやれないことはない
      • rb読み込みで毎回path指定やコード直書きコピペを避けたい
    • カスタムプラグインを使いfilter { debug_print {} }で呼べるようにした

概要

設定例

input {...}
filter {
  debug_print {
    # 出力時にフィールドを追加する(非必須:デフォルトnil)
    tag => "debug"
    # 出力時にインデント、改行をつける(非必須:デフォルトfalse)
    pretty => true
  }
}
output {...}

出力例

# pretty: falseの場合は1行で出る
{
  "tag": "debug",  # 引数tagで指定した値
  "data": {        # 加工中の各フィールドはdataの子として出力
    "event": {
      "original": "hello world"
    },
    "@version": "1",
    "@timestamp": "2025-03-29T09:31:04.587228185Z",
    "message": "hello world",
    "host": {
      "hostname": "3b064c4f9911"
    },
    "foo": "bar"
  }
}

起動コマンド例

docker run --rm -it \
  -v "$(pwd)/logstash.conf:/usr/share/logstash/pipeline/logstash.conf" \
  -v "$(pwd)/debug_print.rb:/opt/plugins/logstash/filters/debug_print.rb" \
  docker.elastic.co/logstash/logstash:8.17.4 \
  --config.reload.automatic \
  --path.plugins /opt/plugins
  • --path.plugins <path>でパスを指定する
    • <path>/logstash/<typename>の下を見に行く
    • typenameの種類はinputs, filters, outputs, codecs
    • rbのまま実行してくれるので、テスト用に色々できそう

実装

https://github.com/necoskijanen/logstash-filter-debug_print

Discussion