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. メトリクスの記述子を作る 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 }
  2. メトリクスの記述子を作る 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生成
  3. 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の部 分は適宜実装を変えて下さい
  4. 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)) }