Prometheus用の自作Exporterを作成する


自作Exporterの構築方法

Motivation

シェルなどを用いて独自のメトリクスを収集したい

Prometheus用のExporterを自作しメトリクスを確認したい

本当に最低限exporterを作る

metricsというファイル名で、prometheus形式でデータを登録する

❯ cat metrics 
# HELP osushi これはお寿司の数です
# TYPE osushi counter
osushi 1.101010101e+10
# HELP yakiniku これは焼肉の数
# TYPE yakiniku counter
yakiniku 1.101010101e+10

上記ファイルを公開するために、pythonを用いて可児的なwebサーバを立てる

python3 -m http.server

8000番ポートでwebサーバが動作して、先ほどのファイルの内容を参照することができる

❯ curl localhost:8000/metrics
# HELP osushi これはお寿司の数です
# TYPE osushi counter
osushi 1.101010101e+10
# HELP yakiniku これは焼肉の数
# TYPE yakiniku counter
yakiniku 1.101010101e+10

Prometheusの設定(prometheus.yml)内のscrape_config内に、exporterのurlを記載する

❯ cat prometheus.yml
# my global config
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

scrape_configs:
  - job_name: 'simple_file'
    static_configs:
      - targets: ['localhost:8000']

prometheusのUIからosushiというメトリクスが取得できることを確認できる

あとは、該当のファイルをシェル等で更新していくことで、シェルなどの既存資産を用いてexporterを簡易的に自作できる。

[補足]exporter作成用のライブラリを用いて自作exporter作成

下記に各言語用のExporter作成用ライブラリが存在する