-
Notifications
You must be signed in to change notification settings - Fork 9
/
metrics.go
95 lines (75 loc) · 3.12 KB
/
metrics.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
package http
import (
"strconv"
"github.com/prometheus/client_golang/prometheus"
"github.com/roadrunner-server/pool/fsm"
"github.com/roadrunner-server/pool/state/process"
)
type Informer interface {
Workers() []*process.State
}
func (p *Plugin) MetricsCollector() []prometheus.Collector {
return []prometheus.Collector{p.statsExporter}
}
func newWorkersExporter(stats Informer) *StatsExporter {
return &StatsExporter{
TotalWorkersDesc: prometheus.NewDesc("rr_http_total_workers", "Total number of workers used by the HTTP plugin", nil, nil),
TotalMemoryDesc: prometheus.NewDesc("rr_http_workers_memory_bytes", "Memory usage by HTTP workers.", nil, nil),
StateDesc: prometheus.NewDesc("rr_http_worker_state", "Worker current state", []string{"state", "pid"}, nil),
WorkerMemoryDesc: prometheus.NewDesc("rr_http_worker_memory_bytes", "Worker current memory usage", []string{"pid"}, nil),
WorkersReady: prometheus.NewDesc("rr_http_workers_ready", "HTTP workers currently in ready state", nil, nil),
WorkersWorking: prometheus.NewDesc("rr_http_workers_working", "HTTP workers currently in working state", nil, nil),
WorkersInvalid: prometheus.NewDesc("rr_http_workers_invalid", "HTTP workers currently in invalid,killing,destroyed,errored,inactive states", nil, nil),
Workers: stats,
}
}
type StatsExporter struct {
TotalMemoryDesc *prometheus.Desc
StateDesc *prometheus.Desc
WorkerMemoryDesc *prometheus.Desc
TotalWorkersDesc *prometheus.Desc
WorkersReady *prometheus.Desc
WorkersWorking *prometheus.Desc
WorkersInvalid *prometheus.Desc
Workers Informer
}
func (s *StatsExporter) Describe(d chan<- *prometheus.Desc) {
// send description
d <- s.TotalWorkersDesc
d <- s.TotalMemoryDesc
d <- s.StateDesc
d <- s.WorkerMemoryDesc
d <- s.WorkersReady
d <- s.WorkersWorking
d <- s.WorkersInvalid
}
func (s *StatsExporter) Collect(ch chan<- prometheus.Metric) {
// get the copy of the processes
workerStates := s.Workers.Workers()
// cumulative RSS memory in bytes
var cum float64
var ready float64
var working float64
var invalid float64
// collect the memory
for i := 0; i < len(workerStates); i++ {
cum += float64(workerStates[i].MemoryUsage)
ch <- prometheus.MustNewConstMetric(s.StateDesc, prometheus.GaugeValue, 0, workerStates[i].StatusStr, strconv.Itoa(int(workerStates[i].Pid)))
ch <- prometheus.MustNewConstMetric(s.WorkerMemoryDesc, prometheus.GaugeValue, float64(workerStates[i].MemoryUsage), strconv.Itoa(int(workerStates[i].Pid)))
// sync with sdk/worker/state.go
switch workerStates[i].Status {
case fsm.StateReady:
ready++
case fsm.StateWorking:
working++
default:
invalid++
}
}
ch <- prometheus.MustNewConstMetric(s.WorkersReady, prometheus.GaugeValue, ready)
ch <- prometheus.MustNewConstMetric(s.WorkersWorking, prometheus.GaugeValue, working)
ch <- prometheus.MustNewConstMetric(s.WorkersInvalid, prometheus.GaugeValue, invalid)
// send the values to the prometheus
ch <- prometheus.MustNewConstMetric(s.TotalWorkersDesc, prometheus.GaugeValue, float64(len(workerStates)))
ch <- prometheus.MustNewConstMetric(s.TotalMemoryDesc, prometheus.GaugeValue, cum)
}