-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
mem.go
50 lines (41 loc) · 1.19 KB
/
mem.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
package prometheus
import (
"github.com/datarhei/core/monitor/metric"
"github.com/prometheus/client_golang/prometheus"
)
type memCollector struct {
core string
collector metric.Reader
memLimitDesc *prometheus.Desc
memFreeDesc *prometheus.Desc
}
func NewMemCollector(core string, c metric.Reader) prometheus.Collector {
return &memCollector{
core: core,
collector: c,
memLimitDesc: prometheus.NewDesc(
"mem_total_bytes",
"Total available memory in bytes",
[]string{"core"}, nil),
memFreeDesc: prometheus.NewDesc(
"mem_free_bytes",
"Free memory in bytes",
[]string{"core"}, nil),
}
}
func (c *memCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.memLimitDesc
ch <- c.memFreeDesc
}
func (c *memCollector) Collect(ch chan<- prometheus.Metric) {
metrics := c.collector.Collect([]metric.Pattern{
metric.NewPattern("mem_total"),
metric.NewPattern("mem_free"),
})
for _, m := range metrics.Values("mem_total") {
ch <- prometheus.MustNewConstMetric(c.memLimitDesc, prometheus.GaugeValue, m.Val(), c.core)
}
for _, m := range metrics.Values("mem_free") {
ch <- prometheus.MustNewConstMetric(c.memFreeDesc, prometheus.GaugeValue, m.Val(), c.core)
}
}