forked from goodrain/rainbond
/
monitor.go
83 lines (71 loc) · 1.99 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
73
74
75
76
77
78
79
80
81
82
83
package monitor
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/goodrain/rainbond/builder/discover"
"github.com/goodrain/rainbond/builder/exector"
)
// Metric name parts.
const (
// Namespace for all metrics.
namespace = "builder"
// Subsystem(s).
exporter = "exporter"
)
//Exporter collects entrance metrics. It implements prometheus.Collector.
type Exporter struct {
taskNum prometheus.Counter
taskError prometheus.Counter
}
var healthDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, exporter, "health_status"),
"builder service health status.",
[]string{"service_name"}, nil,
)
//NewExporter new a exporter
func NewExporter() *Exporter {
return &Exporter{
taskNum:prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: exporter,
Name: "builder_task_number",
Help: "builder number of tasks",
}),
taskError:prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: exporter,
Name: "builder_task_error",
Help: "builder number of task errors",
}),
}
}
//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) {
healthInfo := discover.HealthCheck()
healthStatus := healthInfo["status"]
var val float64
if healthStatus == "health" {
val = 1
} else {
val = 0
}
ch <- prometheus.MustNewConstMetric(healthDesc, prometheus.GaugeValue, val, "builder")
ch <- prometheus.MustNewConstMetric(e.taskNum.Desc(), prometheus.CounterValue, exector.TaskNum)
ch <- prometheus.MustNewConstMetric(e.taskError.Desc(), prometheus.CounterValue, exector.ErrorNum)
}