Rainbond/webcli/app/monitor.go

73 lines
1.8 KiB
Go
Raw Normal View History

2018-07-12 18:00:10 +08:00
package app
import (
"github.com/prometheus/client_golang/prometheus"
)
// Metric name parts.
const (
// Namespace for all metrics.
namespace = "webcli"
// Subsystem(s).
exporter = "exporter"
)
2019-02-12 16:57:18 +08:00
//Exporter collects webcli metrics. It implements prometheus.Collector.
2018-07-12 18:00:10 +08:00
type Exporter struct {
2019-02-12 15:40:42 +08:00
ExecuteCommandTotal prometheus.Counter
2018-07-13 16:35:09 +08:00
ExecuteCommandFailed prometheus.Counter
2018-07-12 18:00:10 +08:00
}
2019-02-12 15:40:42 +08:00
2018-07-13 16:35:09 +08:00
var healthDesc = prometheus.NewDesc(
2018-07-29 21:22:42 +08:00
prometheus.BuildFQName(namespace, exporter, "health_status"),
2018-07-13 16:35:09 +08:00
"health status.",
[]string{"service_name"}, nil,
)
2018-07-12 18:00:10 +08:00
//NewExporter new a exporter
func NewExporter() *Exporter {
return &Exporter{
2019-02-12 15:40:42 +08:00
ExecuteCommandTotal: prometheus.NewCounter(prometheus.CounterOpts{
2018-07-12 18:00:10 +08:00
Namespace: namespace,
Subsystem: exporter,
Name: "execute_command_total",
Help: "Total number of execution commands",
}),
2019-02-12 15:40:42 +08:00
ExecuteCommandFailed: prometheus.NewCounter(prometheus.CounterOpts{
2018-07-12 18:00:10 +08:00
Namespace: namespace,
Subsystem: exporter,
Name: "execute_command_failed",
Help: "failed number of execution commands",
}),
}
}
//Describe implements prometheus.Collector.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
metricCh := make(chan prometheus.Metric)
doneCh := make(chan struct{})
go func() {
for m := range metricCh {
ch <- m.Desc()
}
close(doneCh)
}()
e.Collect(metricCh)
close(metricCh)
<-doneCh
}
// Collect implements prometheus.Collector.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.scrape(ch)
}
func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
2018-07-13 16:35:09 +08:00
ch <- prometheus.MustNewConstMetric(healthDesc, prometheus.GaugeValue, 1, "webcli")
ch <- prometheus.MustNewConstMetric(e.ExecuteCommandTotal.Desc(), prometheus.CounterValue, ExecuteCommandTotal)
ch <- prometheus.MustNewConstMetric(e.ExecuteCommandFailed.Desc(), prometheus.CounterValue, ExecuteCommandFailed)
2018-07-12 18:00:10 +08:00
}