Screaming Loud

日々是精進

Sentryにログを送るfluent pluginを作った

fluentからsentryに送るプラグインを作りました。

github.com

こちらのプラグインのメンテが止まっていた感じだったので、新しく作りました。

github.com

利用イメージ

自分が欲しかったものは、fluent自体のログをsentryに送るという機能です。 なので、まずはすごく薄く作りました。

fluentのログを流すためのサンプルのconfigはこんな感じですね。

<filter fluent.**>
  @type record_transformer
  enable_ruby
  <record>
    level ${tag.sub("fluent.","")}
  </record>
</filter>

<match fluent.**>
  @type       raven
  dsn         https://12345@sentry.io/12345
  environment development
  <buffer tag>
    @type file
    path  fluentd/log/error.*.buffer
    timekey 5m
    timekey_wait 1m
    flush_interval 1s
  </buffer>
</match>

各configの値

  • dsn : Sentryの管理画面から取得するDSNの値
  • environment : Sentryに送るenvironmentの値
  • default_level : Sentryに送るログレベルのデフォルトの値。初期値は errorです。

bufferで tag を指定するとSentryのタグとして送られます。

recordの値

recordの以下の値が使われます。

  • message : 実際に送るログメッセージ
  • level : この値を設定するとsentryに送られるログレベルの値がその値になります。recordにその値がなければ、configのdefault_levelが使われます。

上記の例では、levelの値を record_transformer pluginを使って書き換えることで、sentry上でのログレベルが設定されます。

f:id:yuutookun:20191231012119p:plain
実際のタグはこんな感じ

infoは送らないようにする

null pluginを使って実現するのが良いかな

<match fluent.info>
  @type null
</match>

まとめ

もしsentryを使っていれば、使ってみてください。 何かあればPRやIssueを投げてくださいm( )m