mackerel-plugin-jsonl

module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 13, 2025 License: MIT

README

mackerel-plugin-jsonl

JSON Lines 形式のログからメトリックを抽出し、Mackerelに投稿するためのカスタムプラグインです。

インストール方法

mkrを使うと楽です

mkr plugin --upgrade kazeburo/mackerel-plugin-jsonl

基本的な使い方

./mackerel-plugin-jsonl \
	--key-name count \
	--json-key foo.bar \
	--aggregator count \
	--log-file /path/to/your.log \
	--prefix custom.jsonl

複数のキーや集計方法を指定する場合は、--key-name, --json-key, --aggregator を同じ数だけ指定してください。

mackerel-agent.conf 設定例

[plugin.metrics.jsonl]
command = "/path/to/mackerel-plugin-jsonl --key-name count --json-key foo.bar --aggregator count --log-file /var/log/app.log --prefix custom.jsonl"

aggregatorの種類と説明

--aggregator には以下の種類を指定できます。

  • count : 対象となるキーが存在する行をカウントします
    • 例: json.total.count 80000.000000 1760281381
  • group_by : 指定したキーの値ごとに件数を集計します。
    • 例: json.status.2xx 24769.733333 1760281381
  • group_by_with_percentage : group_byの各値ごとの割合(%)も出力します。
    • 例: json.status_percentage.5xx 23.139667 1760281381
  • percentile : 指定した数値キーのパーセンタイル(mean, p90, p95, p99)を出力します。
    • 例: json.latency.p95 0.030000 1760281381

複数のaggregatorを同時に指定することで、複数のメトリクスを一度に出力できます。


json-keyの変換関数

--json-key にはパイプ(|)区切りで変換関数を指定できます。

例: foo.bar|tolower|replace('a','b')|trimspace

利用可能な関数:

  • tolower : 小文字化
  • toupper : 大文字化
  • trimspace : 前後の空白を除去
  • replace('pattern','repl') : 正規表現で置換
  • have('key1','key2','key3') : 初期状態のキーのリスト

例:

--json-key "user.name|trimspace|tolower"
--json-key "message|replace('error','warn')"

JSONによるアクセスログのメトリクス作成例

% /path/to/mackerel-plugin-jsonl --prefix json --log-file json.log \
  -k total.count -j time -a count \
  -k status -j 'status|replace("^(?:([1235])\d{2}|(4)(?:[0-8]\d|9[0-8]))$","${1}${2}xx")|have("2xx","3xx","4xx","499","5xx")' -a group_by_with_percentage \
  -k latency -j reqtime -a percentile
json.total.count        4800000.000000  1760339314
json.status.3xx 684256.000000   1760339314
json.status.4xx 1370792.000000  1760339314
json.status.499 344392.000000   1760339314
json.status.5xx 1031020.000000  1760339314
json.status.2xx 1369540.000000  1760339314
json.status_percentage.2xx      28.532083       1760339314
json.status_percentage.3xx      14.255333       1760339314
json.status_percentage.4xx      28.558167       1760339314
json.status_percentage.499      7.174833        1760339314
json.status_percentage.5xx      21.479583       1760339314
json.latency.mean       0.030000        1760339314
json.latency.p99        0.030000        1760339314
json.latency.p90        0.030000        1760339314
json.latency.p95        0.030000        1760339314

ライセンス

MIT License

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL