/
mtail_collector.go
88 lines (79 loc) · 1.87 KB
/
mtail_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
package main
import (
"encoding/json"
"io"
"log"
"strings"
"sync"
"github.com/google/mtail/metrics"
"github.com/google/mtail/mtail"
"github.com/prometheus/client_golang/prometheus"
)
type mtailCollector struct {
mtail *mtail.Mtail
metrics []*metrics.Metric
mu *sync.Mutex
}
func newMtailCollector(m *mtail.Mtail) *mtailCollector {
c := &mtailCollector{mtail: m, mu: &sync.Mutex{}}
err := c.mtail.StartTailing()
if err != nil {
log.Fatal(err)
}
return c
}
func (c *mtailCollector) process() {
r, w := io.Pipe()
go func(w *io.PipeWriter) {
err := c.mtail.WriteMetrics(w)
if err != nil {
log.Println(err)
}
}(w)
err := json.NewDecoder(r).Decode(&c.metrics)
if err != nil {
log.Println(err)
}
}
func newDesc(m *metrics.Metric, l *metrics.LabelSet) *prometheus.Desc {
labels := prometheus.Labels{}
for k, v := range l.Labels {
labels[k] = v
}
name := m.Name
help := strings.ToLower(m.Kind.String())
return prometheus.NewDesc(name, help, []string{}, labels)
}
func newMetric(m *metrics.Metric, l *metrics.LabelSet) (prometheus.Metric, error) {
var value float64
var valueType prometheus.ValueType
switch m.Kind {
case metrics.Counter:
valueType = prometheus.CounterValue
value = float64(l.Datum.Get())
case metrics.Gauge:
valueType = prometheus.GaugeValue
value = float64(l.Datum.Get())
}
return prometheus.NewConstMetric(newDesc(m, l), valueType, value)
}
// Collect implements prometheus.Collector.
func (c *mtailCollector) Collect(ch chan<- prometheus.Metric) {
c.process()
for _, metric := range c.metrics {
lc := make(chan *metrics.LabelSet)
go metric.EmitLabelSets(lc)
for l := range lc {
m, err := newMetric(metric, l)
if err != nil {
log.Println(err)
continue
}
ch <- m
}
}
}
// Describe implements prometheus.Collector.
func (c *mtailCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- collected.Desc()
}