Prometheusの運用を効率化させる管理用エンドポイントの紹介
Prometheusには、運用を効率化させるいくつかのエンドポイントが存在します。これらを活用することで、例えば構成ファイルのオンラインリロードや、インスタンスのステータスを取得できます。今回は、これらの管理用エンドポイントをご紹介します。
利用できるエンドポイント
Promethues Version 2.17時点で用意されているエンドポイントは以下の4つです。
/-/healthy
/-/ready
/-/reload
/-/quit
Health
healthyエンドポイントは常に200を返します。Promethuesのインスタンスの正常を確認するために利用します。このエンドポイントがFailする場合は、Promethuesが正常に起動していません。
$ curl localhost:9090/-/healthy -v
* Trying 127.0.0.1:9090...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 9090 (#0)
> GET /-/healthy HTTP/1.1
> Host: localhost:9090
> User-Agent: curl/7.65.3
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Wed, 15 Apr 2020 09:08:17 GMT
< Content-Length: 23
< Content-Type: text/plain; charset=utf-8
<
Prometheus is Healthy.
* Connection #0 to host localhost left intact
Ready
readyエンドポイントは、Promethuesがクエリを処理できる場合に200を返します。PromethuesのAPIを利用したアプリケーションなどが、Promethuesのインスタンスにリクエストを送ってよいか判断する場合などに利用します。
$ curl localhost:9090/-/ready -v
* Trying 127.0.0.1:9090...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 9090 (#0)
> GET /-/ready HTTP/1.1
> Host: localhost:9090
> User-Agent: curl/7.65.3
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Wed, 15 Apr 2020 09:07:52 GMT
< Content-Length: 21
< Content-Type: text/plain; charset=utf-8
<
Prometheus is Ready.
* Connection #0 to host localhost left intact
Reload
reloadエンドポイントは、Promethuesの構成ファイル(prometheus.yml)をインスタンスを停止せずに即時リロードします。HealthやReadyと異なりPOSTリクエストを送る必要があります。また、起動時に--web.enable-lifecycle
オプションを有効にする必要があります。
# curl -X POST localhost:9090/-/reload -v
* Trying 127.0.0.1:9090...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 9090 (#0)
> POST /-/reload HTTP/1.1
> Host: localhost:9090
> User-Agent: curl/7.65.3
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Wed, 15 Apr 2020 09:06:27 GMT
< Content-Length: 0
<
* Connection #0 to host localhost left intact
構成ファイルが正常な場合はContent-Lengthが0になります。誤りがあればエラーが返ります。また、誤りがあった場合は構成ファイルのリロードは行われません。
Quit
quitエンドポイントは、Prometheusを正常にシャットダウンさせます。小規模なインスタンスであれば問題になることは少ないですが、大量のターゲットを監視しているPromethuesを突然停止させると、たとえば内部のTSDBに障害を引き起こすことがあります。こういった事象を避けるため、quitエンドポイントによって安全なシャットダウンをリクエストできます。quitエンドポイントもPOSTリクエストを要求し、起動時に--web.enable-lifecycle
オプションを有効にする必要があります。
$ curl -X POST localhost:9090/-/quit -v
* Trying 127.0.0.1:9090...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 9090 (#0)
> POST /-/quit HTTP/1.1
> Host: localhost:9090
> User-Agent: curl/7.65.3
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Wed, 15 Apr 2020 09:14:07 GMT
< Content-Length: 34
< Content-Type: text/plain; charset=utf-8
<
* Connection #0 to host localhost left intact
Requesting termination... Goodbye!%
併せて、以下のようにPromethues側のログにもリクエストの受信と、各種サービスの停止が出力され、正常にシャットダウンされました。
level=warn ts=2020-04-15T09:14:07.484Z caller=main.go:510 msg="Received termination request via web service, exiting gracefully..."
level=info ts=2020-04-15T09:14:07.484Z caller=main.go:530 msg="Stopping scrape discovery manager..."
level=info ts=2020-04-15T09:14:07.484Z caller=main.go:544 msg="Stopping notify discovery manager..."
level=info ts=2020-04-15T09:14:07.484Z caller=main.go:566 msg="Stopping scrape manager..."
level=info ts=2020-04-15T09:14:07.484Z caller=main.go:526 msg="Scrape discovery manager stopped"
level=info ts=2020-04-15T09:14:07.484Z caller=main.go:540 msg="Notify discovery manager stopped"
level=info ts=2020-04-15T09:14:07.484Z caller=manager.go:845 component="rule manager" msg="Stopping rule manager..."
level=info ts=2020-04-15T09:14:07.484Z caller=manager.go:851 component="rule manager" msg="Rule manager stopped"
level=info ts=2020-04-15T09:14:07.484Z caller=main.go:560 msg="Scrape manager stopped"
level=info ts=2020-04-15T09:14:07.492Z caller=notifier.go:598 component=notifier msg="Stopping notification manager..."
level=info ts=2020-04-15T09:14:07.492Z caller=main.go:731 msg="Notifier manager stopped"
level=info ts=2020-04-15T09:14:07.492Z caller=main.go:743 msg="See you next time!"
終わりに
今回ご紹介したのはPromethuesのAPIのほんの一部ですが、特に /-/reload
は重宝するので活用をおすすめします。また、今回記事を執筆するに当たって、改めて /-/healthy
と /-/ready
の違いを知りました。