forked from goodrain/rainbond
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.go
72 lines (61 loc) · 1.81 KB
/
monitor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package app
import (
"github.com/prometheus/client_golang/prometheus"
)
// Metric name parts.
const (
// Namespace for all metrics.
namespace = "webcli"
// Subsystem(s).
exporter = "exporter"
)
//Exporter collects entrance metrics. It implements prometheus.Collector.
type Exporter struct {
ExecuteCommandTotal prometheus.Counter
ExecuteCommandFailed prometheus.Counter
}
var healthDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, exporter, "health_status"),
"health status.",
[]string{"service_name"}, nil,
)
//NewExporter new a exporter
func NewExporter() *Exporter {
return &Exporter{
ExecuteCommandTotal:prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: exporter,
Name: "execute_command_total",
Help: "Total number of execution commands",
}),
ExecuteCommandFailed:prometheus.NewCounter(prometheus.CounterOpts{
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) {
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)
}