Pushgatewayでメトリクスをプッシュする【メトリクス収集・監視】

今回はPushgatewayを利用して、Prometheus serverから直接pullできないメトリクスを収集する方法をご紹介します。
なお、【メトリクス収集・監視】シリーズと題して他にも記事を投稿していきますので、併せてご確認ください。

Pushgatewayとは

ジョブ/バッチ実行など何らかの理由で生存期間の短いサーバーやアプリケーションに対して、プッシュ型のメトリクス収集を提供するコンポーネントです。
前述の生存期間の短いサーバーやアプリケーション自体がこのPushgatewayにメトリクス情報を送ることによりPushgatewayで一時的に保管が行われ、Prometheus serverがその情報の収集を行います。

目的

本記事では、任意の短命なアプリケーションからPushgatewayを介して、Prometheus serverにメトリクスを収集させる方法をご紹介します。

前提

下記の記事を参考に、Prometeus server、監視対象サーバー(Node exporter)、Grafanaが構築されていること。
Prometheus+Grafanaでメトリクスを監視する【メトリクス収集・監視】

環境

今回の構成図・検証環境サーバーは以下の通りです。
短命なアプリケーションの代わりとして、vm-target1からcurlでメトリクスをプッシュします。

・構成図
※黄色背景が新たに構築するサーバー

用途 ホスト名 IPアドレス
Grafanaサーバー vm-grafana1 172.24.1.5/24
Prometheusサーバー vm-prom1 172.24.1.9/24
短命アプリ vm-target1 172.24.1.10/24
Pushgateway vm-pushgw 172.24.1.10/24

上記サーバーのOS/SWは以下の通りです。

OS/SW バージョン
OS Ubuntu 22.04 LTS
Grafana grafana-enterprise 9.5.13
Prometheus 2.53.2 LTS
Node exporter 1.8.2
Pushgateway 1.9.0

Pushgatewayの構築・動作確認

それではPushgatewayの構築を行っていきます。
ここでは特段記載のない限りvm-pushgwで操作を行います。

バイナリダウンロード・解凍

まず作業ディレクトリを作成して移動します。

# 作業ディレクトリ作成
sudo mkdir /work; sudo chown azureuser:azureuser /work; sudo chmod 777 /work
# 移動・確認
cd /work
ls -la

実行結果

azureuser@vm-pushgw:~$ sudo mkdir /work; sudo chown azureuser:azureuser /work; sudo chmod 777 /work
azureuser@vm-pushgw:~$ 
azureuser@vm-pushgw:~$ cd /work
azureuser@vm-pushgw:/work$ ls -la
total 8
drwxrwxrwx  2 azureuser azureuser 4096 Sep 19 15:47 .
drwxr-xr-x 20 root      root      4096 Sep 19 15:47 ..
azureuser@vm-pushgw:/work$ 

次にPushgatewayのバイナリダウンロード、および、解凍をします。

# バージョン指定
VERSION=1.9.0
# ダウンロード・確認
curl -sSOL https://github.com/prometheus/pushgateway/releases/download/v${VERSION}/pushgateway-${VERSION}.linux-amd64.tar.gz
ls -la
# 解凍・確認
tar xf pushgateway-${VERSION}.linux-amd64.tar.gz
ls -la
# 解凍先ディレクトリへの移動・確認
cd pushgateway-${VERSION}.linux-amd64/
ls -la

実行結果

azureuser@vm-pushgw:/work$ VERSION=1.9.0
azureuser@vm-pushgw:/work$ curl -sSOL https://github.com/prometheus/pushgateway/releases/download/v${VERSION}/pushgateway-${VERSION}.linux-amd64.tar.gz
azureuser@vm-pushgw:/work$ 
azureuser@vm-pushgw:/work$ ls -la
total 10324
drwxrwxrwx  2 azureuser azureuser     4096 Sep 19 15:48 .
drwxr-xr-x 20 root      root          4096 Sep 19 15:47 ..
-rw-rw-r--  1 azureuser azureuser 10563386 Sep 19 15:48 pushgateway-1.9.0.linux-amd64.tar.gz
azureuser@vm-pushgw:/work$ 
azureuser@vm-pushgw:/work$ tar xf pushgateway-${VERSION}.linux-amd64.tar.gz
azureuser@vm-pushgw:/work$ ls -la
total 10328
drwxrwxrwx  3 azureuser azureuser     4096 Sep 19 15:49 .
drwxr-xr-x 20 root      root          4096 Sep 19 15:47 ..
drwxr-xr-x  2 azureuser azureuser     4096 Jun  9 00:05 pushgateway-1.9.0.linux-amd64
-rw-rw-r--  1 azureuser azureuser 10563386 Sep 19 15:48 pushgateway-1.9.0.linux-amd64.tar.gz
azureuser@vm-pushgw:/work$ 
azureuser@vm-pushgw:/work$ cd pushgateway-${VERSION}.linux-amd64/
azureuser@vm-pushgw:/work/pushgateway-1.9.0.linux-amd64$ ls -la
total 18332
drwxr-xr-x 2 azureuser azureuser     4096 Jun  9 00:05 .
drwxrwxrwx 3 azureuser azureuser     4096 Sep 19 15:49 ..
-rw-r--r-- 1 azureuser azureuser    11357 Jun  9 00:05 LICENSE
-rw-r--r-- 1 azureuser azureuser      487 Jun  9 00:05 NOTICE
-rwxr-xr-x 1 azureuser azureuser 18745578 Jun  9 00:04 pushgateway
azureuser@vm-pushgw:/work/pushgateway-1.9.0.linux-amd64$ 

Pushgatewayのインストール

今回はPushgatewayバイナリ実行用のユーザーを作成して実行することにします。
そのため、ユーザー作成と取得したバイナリを適切なディレクトリへ配置します。

まずはユーザー/グループ作成を行います。

# グループ追加
sudo groupadd prometheus
# ユーザー追加・ホームディレクトリ確認
sudo useradd prometheus -g prometheus -d /var/lib/prometheus -s /usr/sbin/nologin -m
sudo ls -la /var/lib/prometheus/

実行結果

azureuser@vm-pushgw:/work/pushgateway-1.9.0.linux-amd64$ sudo groupadd prometheus
azureuser@vm-pushgw:/work/pushgateway-1.9.0.linux-amd64$ 
azureuser@vm-pushgw:/work/pushgateway-1.9.0.linux-amd64$ sudo useradd prometheus -g prometheus -d /var/lib/prometheus -s /usr/sbin/nologin -m
azureuser@vm-pushgw:/work/pushgateway-1.9.0.linux-amd64$ sudo ls -la /var/lib/prometheus/
total 20
drwxr-x---  2 prometheus prometheus 4096 Sep 19 15:50 .
drwxr-xr-x 41 root       root       4096 Sep 19 15:50 ..
-rw-r--r--  1 prometheus prometheus  220 Jan  7  2022 .bash_logout
-rw-r--r--  1 prometheus prometheus 3771 Jan  7  2022 .bashrc
-rw-r--r--  1 prometheus prometheus  807 Jan  7  2022 .profile
azureuser@vm-pushgw:/work/pushgateway-1.9.0.linux-amd64$ 

次に取得したバイナリファイルを配置していきます。

# 実行バイナリの配置・確認
sudo cp pushgateway /usr/local/bin/
ls -la /usr/local/bin/pushgateway*

実行結果

azureuser@vm-pushgw:/work/pushgateway-1.9.0.linux-amd64$ sudo cp pushgateway /usr/local/bin/
azureuser@vm-pushgw:/work/pushgateway-1.9.0.linux-amd64$ ls -la /usr/local/bin/pushgateway*
-rwxr-xr-x 1 root root 18745578 Sep 19 15:51 /usr/local/bin/pushgateway
azureuser@vm-pushgw:/work/pushgateway-1.9.0.linux-amd64$ 

Pushgatewayのサービス化と起動確認

こちらはオプションとなりますが、今回はsystemctlコマンドで管理できるよう、Unitファイルを作成してSystemdサービス化を行います。

# Unitファイル作成
cat <<"EOF" | sudo tee /etc/systemd/system/prometheus-pushgateway.service
[Unit]
Description=Prometheus PushGateway
Documentation=https://github.com/prometheus/pushgateway
After=network-online.target

[Service]
User=prometheus
WorkingDirectory=/var/lib/prometheus
ExecStart=/usr/local/bin/pushgateway
ExecStop=/bin/kill -TERM ${MAINPID}
ExecReload=/bin/kill -HUP ${MAINPID}
[Install]
WantedBy=multi-user.target
EOF

実行結果

azureuser@vm-pushgw:/work/pushgateway-1.9.0.linux-amd64$ cat <<"EOF" | sudo tee /etc/systemd/system/prometheus-pushgateway.service
[Unit]
Description=Prometheus PushGateway
Documentation=https://github.com/prometheus/pushgateway
After=network-online.target

[Service]
User=prometheus
WorkingDirectory=/var/lib/prometheus
ExecStart=/usr/local/bin/pushgateway
ExecStop=/bin/kill -TERM ${MAINPID}
ExecReload=/bin/kill -HUP ${MAINPID}
[Install]
WantedBy=multi-user.target
EOF
[Unit]
Description=Prometheus PushGateway
Documentation=https://github.com/prometheus/pushgateway
After=network-online.target

[Service]
User=prometheus
WorkingDirectory=/var/lib/prometheus
ExecStart=/usr/local/bin/pushgateway
ExecStop=/bin/kill -TERM ${MAINPID}
ExecReload=/bin/kill -HUP ${MAINPID}
[Install]
WantedBy=multi-user.target
azureuser@vm-pushgw:/work/pushgateway-1.9.0.linux-amd64$ 

それでは正常に起動することを確認するため、サービス起動を行います。

# Unitファイルの反映・ステータス確認
sudo systemctl daemon-reload
sudo systemctl status prometheus-pushgateway
# サービス起動・ステータス確認
sudo systemctl start prometheus-pushgateway
sudo systemctl status prometheus-pushgateway
# サービスの自動起動有効化
sudo systemctl enable prometheus-pushgateway

実行結果

azureuser@vm-pushgw:/work/pushgateway-1.9.0.linux-amd64$ sudo systemctl daemon-reload
azureuser@vm-pushgw:/work/pushgateway-1.9.0.linux-amd64$ sudo systemctl status prometheus-pushgateway
○ prometheus-pushgateway.service - Prometheus PushGateway
     Loaded: loaded (/etc/systemd/system/prometheus-pushgateway.service; disabled; vendor preset: enabled)
     Active: inactive (dead)
       Docs: https://github.com/prometheus/pushgateway
azureuser@vm-pushgw:/work/pushgateway-1.9.0.linux-amd64$ 
azureuser@vm-pushgw:/work/pushgateway-1.9.0.linux-amd64$ sudo systemctl start prometheus-pushgateway
azureuser@vm-pushgw:/work/pushgateway-1.9.0.linux-amd64$ sudo systemctl status prometheus-pushgateway
● prometheus-pushgateway.service - Prometheus PushGateway
     Loaded: loaded (/etc/systemd/system/prometheus-pushgateway.service; disabled; vendor preset: enabled)
     Active: active (running) since Thu 2024-09-19 15:53:17 JST; 3s ago
       Docs: https://github.com/prometheus/pushgateway
   Main PID: 23328 (pushgateway)
      Tasks: 6 (limit: 2263)
     Memory: 4.7M
        CPU: 6ms
     CGroup: /system.slice/prometheus-pushgateway.service
             └─23328 /usr/local/bin/pushgateway

Sep 19 15:53:17 vm-pushgw systemd[1]: Started Prometheus PushGateway.
Sep 19 15:53:17 vm-pushgw pushgateway[23328]: ts=2024-09-19T06:53:17.910Z caller=main.go:87 level=info msg="starting pushgateway" version="(version=1.9.0, branch=HEAD, revision=d1ca1a6a426126a09a21f745e8ffbaba>
Sep 19 15:53:17 vm-pushgw pushgateway[23328]: ts=2024-09-19T06:53:17.911Z caller=main.go:88 level=info build_context="(go=go1.22.4, platform=linux/amd64, user=root@2167597b1e9c, date=20240608-15:04:08, tags=un>
Sep 19 15:53:17 vm-pushgw pushgateway[23328]: ts=2024-09-19T06:53:17.912Z caller=tls_config.go:313 level=info msg="Listening on" address=[::]:9091
Sep 19 15:53:17 vm-pushgw pushgateway[23328]: ts=2024-09-19T06:53:17.912Z caller=tls_config.go:316 level=info msg="TLS is disabled." http2=false address=[::]:9091
azureuser@vm-pushgw:/work/pushgateway-1.9.0.linux-amd64$ 
azureuser@vm-pushgw:/work/pushgateway-1.9.0.linux-amd64$ sudo systemctl enable prometheus-pushgateway
Created symlink /etc/systemd/system/multi-user.target.wants/prometheus-pushgateway.service → /etc/systemd/system/prometheus-pushgateway.service.
azureuser@vm-pushgw:/work/pushgateway-1.9.0.linux-amd64$ 

Pushgatewayへメトリクス情報のプッシュ・PushgatewayのWebUI確認

短命なアプリケーションからPushgatewayへメトリクス情報をプッシュする手段として、今回は簡易的にvm-target1でcurlコマンドを実行して確認します。
vm-target1でcurlコマンドを実行することでPushgatewayへPOST送信を行い、PushgatewayのWebUIでメトリクス情報を受信したことを確認します。
ここではPushgatewayのWebUI操作を除き、vm-target1で操作を行います。

PushgatewayのWebUIの事前確認

下記のURLでアクセスしWebUI画面が表示され、メトリクス情報が何も表示されていないことを確認します。
http://<vm-pushgwのホスト名/IPアドレス>:9091/

Pushgatewayへメトリクス情報のプッシュ

ここではシンプルなメトリクスと複雑なメトリクスの2パターンのメトリクス情報をプッシュすることにします。
参考:pushgateway v1.9.0#command-line

# シンプルなメトリクスのプッシュ
echo "some_metric 3.14" | curl --data-binary @- http://vm-pushgw:9091/metrics/job/some_job
# 複雑なメトリクスのプッシュ
cat <<EOF | curl --data-binary @- http://vm-pushgw:9091/metrics/job/some_job/instance/some_instance
# TYPE some_metric counter
some_metric2{label="val1"} 42
# TYPE another_metric gauge
# HELP another_metric Just an example.
another_metric 2398.283
EOF

実行結果

azureuser@vm-target1:~$ echo "some_metric 3.14" | curl --data-binary @- http://vm-pushgw:9091/metrics/job/some_job
azureuser@vm-target1:~$ 
azureuser@vm-target1:~$ cat <<EOF | curl --data-binary @- http://vm-pushgw:9091/metrics/job/some_job/instance/some_instance
# TYPE some_metric counter
some_metric2{label="val1"} 42
# TYPE another_metric gauge
# HELP another_metric Just an example.
another_metric 2398.283
EOF
azureuser@vm-target1:~$ 

PushgatewayのWebUI確認

先ほどと同じく下記のURLでアクセスし、今度はメトリクス情報が2件表示されていることを確認します。
なお、この時点ではPushgatewayに一時保存されており、Prometheus serverにメトリクス収集されていないことにご留意ください。
http://<vm-pushgwのホスト名/IPアドレス>:9091/

Prometheus serverのメトリクス収集先にPushgatewayを追加

それではPushgatewayからメトリクスを収集するため、Prometheus serverに収集先を追加します。
そのため、ここではvm-prom1で操作を行います。

prometheus.ymlの変更

scrape_configsブロックに新たにjob_nameブロックを追加し、Pushgatewayであるvm-pushgwの情報を下記の通り追記します。

  - job_name: pushgw
    honor_labels: true
    static_configs:
      - targets: ['vm-pushgw:9091']

追記後の内容確認

azureuser@vm-prom1:~$ tail -n 20 /etc/prometheus/prometheus.yml 
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090"]

  - job_name: target01
    static_configs:
      - targets: ['vm-target1:9100']

  - job_name: pushgw
    honor_labels: true
    static_configs:
      - targets: ['vm-pushgw:9091']

azureuser@vm-prom1:~$ 

ユースケースによりますが、ここで注意する点として基本的にhonor_labels: trueを指定することが挙げられます。
この指定はプッシュ送信したラベルとPushgatewayが付与するラベルとで重複が発生した場合、プッシュ送信したラベルを優先させる指定です。
例えばinstanceラベルにはPushgateway自体を表すvm-pushgw:9091ではなく、プッシュ送信した際に付与した値であるsome_instanceを保持したい場合、honor_labels: trueを指定する必要があります。
Pushgatewayはメトリクス情報を中間で保持するサーバーに過ぎないため、ほとんどの場合、この例のようにはPushgatewayで付与されるラベルより、送信元(今回ではvm-target1からcurlで送信した内容)のラベルを優先することが望ましいでしょう。
参考:pushgateway v1.9.0#about-the-job-and-instance-labels

prometheus.ymlの反映

変更内容を反映するためsystemctlでサービスを再起動します。

# サービス再起動・ステータス確認
sudo systemctl restart prometheus
sudo systemctl status prometheus

実行結果

azureuser@vm-prom1:~$ sudo systemctl restart prometheus
azureuser@vm-prom1:~$ sudo systemctl status prometheus
● prometheus.service - Prometheus Server
     Loaded: loaded (/etc/systemd/system/prometheus.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2024-09-19 18:18:10 JST; 3s ago
       Docs: https://prometheus.io/docs/introduction/overview/
   Main PID: 2081 (prometheus)
      Tasks: 5 (limit: 2260)
     Memory: 43.6M
        CPU: 150ms
     CGroup: /system.slice/prometheus.service
             └─2081 /usr/local/bin/prometheus --config.file=/etc/prometheus/prometheus.yml --web.console.templates=/etc/prometheus/consoles --web.console.libraries=/etc/prometheus/console_libraries

Sep 19 18:18:10 vm-prom1 prometheus[2081]: ts=2024-09-19T09:18:10.356Z caller=head.go:793 level=info component=tsdb msg="WAL segment loaded" segment=5 maxSegment=6
Sep 19 18:18:10 vm-prom1 prometheus[2081]: ts=2024-09-19T09:18:10.357Z caller=head.go:793 level=info component=tsdb msg="WAL segment loaded" segment=6 maxSegment=6
Sep 19 18:18:10 vm-prom1 prometheus[2081]: ts=2024-09-19T09:18:10.357Z caller=head.go:830 level=info component=tsdb msg="WAL replay completed" checkpoint_replay_duration=4.120173ms wal_replay_duration=98.78385>
Sep 19 18:18:10 vm-prom1 prometheus[2081]: ts=2024-09-19T09:18:10.360Z caller=main.go:1169 level=info fs_type=EXT4_SUPER_MAGIC
Sep 19 18:18:10 vm-prom1 prometheus[2081]: ts=2024-09-19T09:18:10.360Z caller=main.go:1172 level=info msg="TSDB started"
Sep 19 18:18:10 vm-prom1 prometheus[2081]: ts=2024-09-19T09:18:10.360Z caller=main.go:1354 level=info msg="Loading configuration file" filename=/etc/prometheus/prometheus.yml
Sep 19 18:18:10 vm-prom1 prometheus[2081]: ts=2024-09-19T09:18:10.366Z caller=main.go:1391 level=info msg="updated GOGC" old=100 new=75
Sep 19 18:18:10 vm-prom1 prometheus[2081]: ts=2024-09-19T09:18:10.366Z caller=main.go:1402 level=info msg="Completed loading of configuration file" filename=/etc/prometheus/prometheus.yml totalDuration=6.65351>
Sep 19 18:18:10 vm-prom1 prometheus[2081]: ts=2024-09-19T09:18:10.367Z caller=main.go:1133 level=info msg="Server is ready to receive web requests."
Sep 19 18:18:10 vm-prom1 prometheus[2081]: ts=2024-09-19T09:18:10.367Z caller=manager.go:164 level=info component="rule manager" msg="Starting rule manager..."
azureuser@vm-prom1:~$ 

GrafanaでPushgatewayで収集したメトリクスを確認

前回の記事で作成したデータソースに対し、some_metricsome_metric2another_metricメトリクスを指定してクエリすると、Pushgateway経由で収集されたメトリクスが表示されることを確認します。

Pushgateway利用時の注意点

最後にPushgateway利用時の注意点を挙げさせて頂きます。
Prometheus公式からアナウンスのある通り、Pushgatewayは限られた場合にのみ利用することが推奨されています。
公式HPから抜粋すると、

  • PushgatewayはSPOF(単一障害点)とボトルネックになる可能性がある
  • NW構成の問題でPrometheus serverからpull出来ない場合、安易にPushgatewayを利用するのではなく、
    NW構成またはPrometheus serverの配置を見直すべきである
  • 特定のマシン/インスタンスに意味的に紐づかないバッチ/ジョブの結果を取得するために利用するには有用である

とある通り、非常に限定された状況下でのみ推奨されていることが分かります。
参考:Pushgatewayを使用すべきか?

上記の通りPushgatewayの利用には注意が必要であるものの、
特定の状況下では有効なため、利用シーンを見極めながらご活用ください。

ご覧いただきありがとうございます! この投稿はお役に立ちましたか?

役に立った 役に立たなかった

0人がこの投稿は役に立ったと言っています。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です