LoginSignup
5

More than 5 years have passed since last update.

【Prometheus】remote writeの設定項目について調べてみた

Last updated at Posted at 2019-01-14

remote writeの設定項について詳しい説明が見当たらなかったので、
ソースコードを読んだりデバッグしてわかったことをまとめてみた。
※バージョンは2.6.0を使用

remote writeのデフォルト設定

prometheus.yml
remote_write:
- url: http://HOST:PORT/xxxxx
  remote_timeout: 30s
  queue_config:
    capacity: 10000
    max_shards: 1000
    min_shards: 1
    max_samples_per_send: 100
    batch_send_deadline: 5s
    max_retries: 3
    min_backoff: 30ms
    max_backoff: 100ms

urlから下は書かなくても自動で設定が追加される。

remote_writeの設定項目

url

外部ストレージへ書き込みを行うAdapterのURL。

remote_write:
- url: http://HOST01:PORT/receive
- url: http://HOST02:PORT/api/v1/prom/write?db=prometheus

↑のように複数指定できる。

remote_timeout

Adapterとの接続をタイムアウトさせる時間。

queue_config

Prometheusがスクレイプしたサンプルはキューに一旦格納され、順番に外部ストレージに書き込まれるようになっている。
queue_configではそのキューについて設定ができる。
またキューはurlで指定した書き込み先ごとに割り当てられる。

capacity

サンプルを格納できる最大容量。
スクレイピングで取得したサンプル数がcapacityを超えた場合、超えた分のサンプルは破棄されるので注意。
100のcapacityに対してスクレイピングしたサンプル数が130個だったとすると、30個のサンプルが送信されず消失することになる。
その際以下のようなエラーが出力される。

Remote storage queue full, discarding sample. Multiple subsequent messages of this kind may be suppressed.

max_shards/min_shards

remote writeの並列実行数の上限・下限。
remote writeはShardという単位で並列に実行することができ、max/min_shardsではその並列実行数の下限と上限を決めることができる。(起動時のShard数はmin_shards)
Prometheusは10秒置きに負荷を計算し、負荷に合わせてShardの数を増減させている。

詳細は省くが必要なShard数は以下の計算式で計算されていた。

desiredShards = (timePerSample * (samplesIn + samplesPending + t.integralAccumulator)) / float64(time.Second)

timePerSample: サンプル1つあたりの送信時間
samplesIn: 1秒あたりの取り込みサンプル数
samplesPending: samplesInから1秒あたりの送信サンプル数を引いた数( = 1秒あたりの送信待ちサンプル数)
t.integralAccumulator: t.integralAccumulator + (samplesPending * 0.1)

参考:https://github.com/prometheus/prometheus/blob/master/storage/remote/queue_manager.go
func (t *QueueManager) calculateDesiredShards()

remote_storage_integrations_01.png

max_samples_per_send

一度にまとめて送信するサンプルの数。データをPOSTする回数を調節することができる。
例えばキュー内に100個のサンプルがあり、max_samples_per_sendを20に設定している場合、サンプルを20個ずつ5回に分けて送信する。
(この場合実際にはサンプルが20個溜まった時点で外部ストレージに送信される。)

batch_send_deadline

キュー内に残ったサンプルを外部ストレージにフラッシュするまでの待ち時間。
外部ストレージへのサンプルの送信はmax_samples_per_send単位で送信されるため、キュー内のサンプル数が単位未満の場合は送信されず残ることになる。
サンプルがキューに残り続けないようbatch_send_deadlineに設定した時間が送信待ちサンプルの待機時間となり、設定した時間経過すると外部ストレージに書き込まれる。

remote_storage_integrations.png

max_retries

サンプルの送信が失敗した時にリトライする回数。

min_backoff/max_backoff

送信リトライの待ち時間の上限・下限。
送信が失敗した時すぐにはリトライせず、min_backoffに設定した時間待ってから行われる。
2回目は前回のbackoff時間を2倍した値が待ち時間になり、以降2倍ずつ待ち時間が増えていく。
ただし最大はmax_backoffまで。

参考

https://prometheus.io/docs/prometheus/latest/configuration/configuration/#remote_write
https://prometheus.io/docs/prometheus/latest/storage/#remote-storage-integrations
https://prometheus.io/docs/operating/integrations/#remote-endpoints-and-storage
https://github.com/prometheus/prometheus/tree/master/storage/remote

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5