じゃあ、おうちで学べる

思考を呼び覚ます このコードに、君は抗えるか。

Prometheus でGoアプリケーションでの値の取得について

インストール

Prometheusには、Goアプリケーションにメトリクスを組み込むために利用可能な公式Goクライアントライブラリがある。 INSTRUMENTING A GO APPLICATION FOR PROMETHEUSでは、HTTPでPrometheusメトリクスを出力する簡単なGoアプリケーションを作成する。

環境情報

# Docker のVersion: 18.09.7 を利用
$ docker version
# Golang の
$ go version
go version go1.14 linux/amd64

実行

GoアプリケーションでPrometheusメトリクスを出力するには、HTTPエンドポイント/metricsを提供する必要。ちなみにライブラリを利用しなくても/metricsに出力さえされていればよい

package main

import (
        "net/http"

        "github.com/prometheus/client_golang/prometheus/promhttp"
)

func main() {
        http.Handle("/metrics", promhttp.Handler())
        http.ListenAndServe(":2112", nil)
}

アプリケーションの実行

# `go mod init` など必要 
go run main.go

アクセス確認

curl http://localhost:2112/metrics
# HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 0
go_gc_duration_seconds{quantile="0.25"} 0
go_gc_duration_seconds{quantile="0.5"} 0
go_gc_duration_seconds{quantile="0.75"} 0
go_gc_duration_seconds{quantile="1"} 0
go_gc_duration_seconds_sum 0
go_gc_duration_seconds_count 0
# HELP go_goroutines Number of goroutines that currently exist.
# TYPE go_goroutines gauge
go_goroutines 8
-----

prometheus での確認

./Dockerfile

Dockerfile を記述して

FROM prom/prometheus
ADD prometheus.yml /etc/prometheus/

./prometheus.yml

prometheus.yml の設定

scrape_configs:
- job_name: myapp
  scrape_interval: 10s
  static_configs:
  - targets:
    - 127.0.0.1:2112

Docker のビルド

ビルドします

docker build -t my-prometheus .

Docker run

docker run -p 9090:9090 my-prometheus

prometheus での確認

http://localhost:9090/targets で ターゲットを確認することができます

独自メトリクスの追加

上記のアプリケーションは、デフォルトのGoメトリクスのみを出力する。 独自のアプリケーション固有のメトリクスを登録することもできる。 この例では、その時点までに処理された操作数を数えるカウンターmyapp_processed_ops_totalを出力する。 このカウンターは、2秒ごとに1ずつ増加する。

package main

import (
        "net/http"
        "time"

        "github.com/prometheus/client_golang/prometheus"
        "github.com/prometheus/client_golang/prometheus/promauto"
        "github.com/prometheus/client_golang/prometheus/promhttp"
)

func recordMetrics() {
        go func() {
                for {
                        opsProcessed.Inc()
                        time.Sleep(2 * time.Second)
                }
        }()
}

var (
        opsProcessed = promauto.NewCounter(prometheus.CounterOpts{
                Name: "myapp_processed_ops_total",
                Help: "The total number of processed events",
        })
)

func main() {
        recordMetrics()

        http.Handle("/metrics", promhttp.Handler())
        http.ListenAndServe(":2112", nil)
}

アプリケーションの実行

# `go mod init` など必要 
go run main.go

アクセス確認

curl http://localhost:2112/metrics

データの取得

# HELP myapp_processed_ops_total The total number of processed events
# TYPE myapp_processed_ops_total counter
myapp_processed_ops_total <カウンター>

Promerrheus での確認

<localhost>:9090/graph 上で myapp_processed_ops_total を確認します

この後、値が確認できたと思います。

そのあとに

この値でAlert を投げたい場合は下記を実施していきます。アプリケーションでの値の取得については別途ブログでやっていこうと思います。

#### AlertManagerのインストール
#### アラート設定
#### ルール設定
#### prometheus に組み込み