Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Hello, Prometheus!! Goで作るexporter自作入門 / 180727 LT

Hello, Prometheus!! Goで作るexporter自作入門 / 180727 LT

kaznishi

July 27, 2018
Tweet

More Decks by kaznishi

Other Decks in Programming

Transcript

  1. Hello, Prometheus!!
    Goで作るexporter自作入門
    2018-07-27 吉祥寺 .pm #15 LT
    by kaznishi

    View Slide

  2. 自己紹介
    Twitter: @kaznishi1246
    主な守備範囲: サーバーサイド,インフラ
    主な使用言語: PHP, Scala
    最近Goをプライベートで勉強してます

    View Slide

  3. 今回の話
    Prometheusのサポートライブラリ(client_golang)
    を使ってexporterを自作してみる

    View Slide

  4. Prometheusとは
    モニタリングツールの一種
    OSS
    クラウド時代向けのツールであり、強力なサービ
    スディスカバリによってAutoScaling対応も簡単

    View Slide

  5. Promethesのアーキテクチャ
    https://prometheus.io/docs/introduction/overview/
    より図を引用

    View Slide

  6. Promethesのアーキテクチャ
    Prometheusサーバがexporterにスクレイピングを
    行い、データ収集をする。
    ↑ node_exporterの出力例

    View Slide

  7. exporterの種類
    既に多くの種類のexporterがオフィシャル、サー
    ドパーティ問わず存在
    https://prometheus.io/docs/instrumenting/export
    ers/
    代表的なものはサーバのメトリクスモニタリング
    を行うnode_exporterや、外形監視を行う
    blackbox_exporterなど

    View Slide

  8. 自作するには

    View Slide

  9. 自作するには
    最終的にPrometheusサーバがスクレイピングでき
    る形式の出力を行うWebサーバになっていればよ
    いので、好きな言語で実装OK
    サポートライブラリが公開されている言語も
    https://prometheus.io/docs/instrumenting/clientli
    bs/
    今回はGoのサポートライブラリを使用する

    View Slide

  10. Hello,World的に固定値を吐くだけの
    exporterを作ってみます
    repo:
    https://github.com/kaznishi/prometheus_expo
    rter_example

    View Slide

  11. 手順
    メトリクスの記述子を作る
    Collectorを実装する
    Describeを実装する
    Collectを実装する
    メトリクス収集の中身を実装
    CollectorをRegisterする
    エンドポイントを実装する

    View Slide

  12. メトリクスの記述子を作る

    View Slide

  13. メトリクスの記述子を作る
    https://github.com/prometheus/client_golang/blob/
    master/prometheus/desc.go
    type Desc struct {
    fqName string
    help string
    constLabelPairs []*dto.LabelPair
    variableLabels []string
    id uint64
    dimHash uint64
    err error
    }

    View Slide

  14. メトリクスの記述子を作る
    var (
    exampleCount = prometheus.NewCounter(
    prometheus.CounterOpts{
    Namespace: namespace,
    Name: "example_count",
    Help: "example counter help",
    })
    exampleGauge = prometheus.NewGauge(
    prometheus.GaugeOpts{
    Namespace: namespace,
    Name: "example_gauge",
    Help: "example gauge help",
    })
    )
    NewCounter,NewGauge処理内でDesc生成

    View Slide

  15. メトリクスの記述子を作る
    ちなみに
    プロメテウスのデータ形式は
    Counter,Gauge,Histogram,Summaryの4種類。
    入門としてはCounter,Gaugeを押さえておけばよ
    い。
    Counter...単調増加する値(ex. 累計アクセス数)
    Gauge...上下する値(ex. リアルタイムアクセス数)

    View Slide

  16. Collectorを実装する

    View Slide

  17. Collectorインタフェース
    https://github.com/prometheus/client_golang/blob/
    master/prometheus/collector.go
    type Collector interface {
    Describe(chan<- *Desc)
    Collect(chan<- Metric)
    }
    Describe
    メトリクスの記述子(Desc)を送信する
    Collect
    メトリクスを送信する

    View Slide

  18. Collectorを実装する
    type myCollector struct {}

    View Slide

  19. Collectorを実装する(Describe)
    func (c myCollector) Describe(ch chan<- *prometheus.Desc){
    ch <- exampleCount.Desc()
    ch <- exampleGauge.Desc()
    }

    View Slide

  20. Collectorを実装する(Collect)
    func (c myCollector) Collect(ch chan<- prometheus.Metric){
    dummyStaticNumber := float64(1234)
    ch <- prometheus.MustNewConstMetric(
    exampleCount.Desc(),
    prometheus.CounterValue,
    float64(dummyStaticNumber),
    )
    ch <- prometheus.MustNewConstMetric(
    exampleGauge.Desc(),
    prometheus.GaugeValue,
    float64(dummyStaticNumber),
    )
    }
    exporterの要件によってdummyStaticNumberの部
    分は適宜実装を変えて下さい

    View Slide

  21. CollectorをRegisterする

    View Slide

  22. エンドポイントを実装する

    View Slide

  23. CollectorをRegisterする & エンドポイントを実装する
    var addr = flag.String(
    "listen-address",
    ":8080",
    "The address to listen on for HTTP requests."
    )
    func main() {
    flag.Parse()
    var c myCollector
    prometheus.MustRegister(c)
    http.Handle("/metrics", promhttp.Handler())
    log.Fatal(http.ListenAndServe(*addr, nil))
    }

    View Slide

  24. 結果

    View Slide

  25. 結果
    Prometheusがスクレイピングできる形式になって
    る!

    View Slide

  26. とりあえず固定値を吐くexporterは実装できた

    View Slide

  27. が、それだけでは面白くないので
    別のexporterを作ってきた

    View Slide

  28. openweathermap_exporter

    View Slide

  29. OpenWeatherMap
    全世界の天気情報を公開
    JSONを吐くAPIが存在

    View Slide

  30. openweathermap_exporter
    APIから取得した気温等を出力するexporterを実装
    した
    https://github.com/kaznishi/openweathermap_exp
    orter
    (実は車輪の再発明だが他に良いネタが思いつかな
    かった)

    View Slide

  31. 結果

    View Slide

  32. ここ数日の気温がモニタリングできた!
    (可視化にはGrafanaを使用)

    View Slide

  33. まとめ
    exporterの自作は難しくない!
    みなさんも機会があればexporter作ってみて下さい

    View Slide

  34. おわり

    View Slide