forked from bakins/gearman-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector.go
107 lines (94 loc) · 2.78 KB
/
collector.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package exporter
import (
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
)
const metricsNamespace = "gearman"
type collector struct {
exporter *Exporter
gearman *gearman
up *prometheus.Desc
versionInfo *prometheus.Desc
statusJobs *prometheus.Desc
statusJobsRunning *prometheus.Desc
statusJobsWaiting *prometheus.Desc
statusWorkers *prometheus.Desc
}
// based on https://github.com/hnlq715/nginx-vts-exporter/
func newFuncMetric(metricName string, docString string, labels []string) *prometheus.Desc {
return prometheus.NewDesc(
prometheus.BuildFQName(metricsNamespace, "", metricName),
docString, labels, nil,
)
}
func (e *Exporter) newCollector(g *gearman) *collector {
return &collector{
exporter: e,
gearman: g,
up: newFuncMetric("up", "is gearman up", []string{}),
versionInfo: newFuncMetric("version_info", "gearman version", []string{"version"}),
statusJobs: newFuncMetric("jobs", "number of jobs queued or running", []string{"function"}),
statusJobsRunning: newFuncMetric("jobs_running", "number of running jobs", []string{"function"}),
statusJobsWaiting: newFuncMetric("jobs_waiting", "number of jobs waiting for an available worker", []string{"function"}),
statusWorkers: newFuncMetric("workers", "number of capable workers", []string{"function"}),
}
}
func (c *collector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.up
ch <- c.versionInfo
ch <- c.statusJobs
ch <- c.statusJobsRunning
ch <- c.statusJobsWaiting
ch <- c.statusWorkers
}
func (c *collector) collectVersion(ch chan<- prometheus.Metric) {
up := 1.0
version, err := c.gearman.getVersion()
if err != nil {
c.exporter.logger.Error("failed to get gearman version", zap.Error(err))
up = 0.0
version = "unknown"
}
ch <- prometheus.MustNewConstMetric(
c.up,
prometheus.GaugeValue,
up)
ch <- prometheus.MustNewConstMetric(
c.versionInfo,
prometheus.GaugeValue,
1,
version)
}
func (c *collector) collectStatus(ch chan<- prometheus.Metric) {
s, err := c.gearman.getStatus()
if err != nil {
c.exporter.logger.Error("failed to get gearman status", zap.Error(err))
return
}
for k, v := range s {
ch <- prometheus.MustNewConstMetric(
c.statusJobs,
prometheus.GaugeValue,
float64(v.total),
k)
ch <- prometheus.MustNewConstMetric(
c.statusJobsRunning,
prometheus.GaugeValue,
float64(v.running),
k)
ch <- prometheus.MustNewConstMetric(
c.statusJobsWaiting,
prometheus.GaugeValue,
float64(v.total-v.running),
k)
ch <- prometheus.MustNewConstMetric(
c.statusWorkers,
prometheus.GaugeValue,
float64(v.workers),
k)
}
}
func (c *collector) Collect(ch chan<- prometheus.Metric) {
c.collectVersion(ch)
c.collectStatus(ch)
}