-
Notifications
You must be signed in to change notification settings - Fork 1
/
prometheus.go
122 lines (98 loc) · 2.62 KB
/
prometheus.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package output
import (
"fmt"
"github.com/gfleury/gstreamtop/tablestream"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var ordinalNumbers = []string{"First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eighth", "Ninth", "Tenth"}
type PrometheusOutput struct {
StreamOutput
metrics []*metric
}
type metric struct {
metricData prometheus.Collector
vdIdx int
v *tablestream.View
}
func (o *metric) metric() prometheus.Collector {
return o.metricData
}
func (o *metric) setMetric(m prometheus.Collector) {
o.metricData = m
}
func (o *metric) viewDataIdx() int {
return o.vdIdx
}
func (o *metric) setViewDataIdx(vd int) {
o.vdIdx = vd
}
func (o *metric) view() *tablestream.View {
return o.v
}
func (o *metric) setView(v *tablestream.View) {
o.v = v
}
func (o *PrometheusOutput) Loop() {
pTicker := time.NewTicker(time.Second * 2)
o.createMetrics()
go PrometheusHTTP()
for *o.InputExists() {
<-pTicker.C
o.publishMetrics()
// Just print the normal SimpleTable.
for _, view := range o.stream.GetViews() {
tablestream.TableWrite(view, os.Stdout)
}
}
}
func (o *PrometheusOutput) Configure() error {
o.errors = make(chan error)
return nil
}
func (o *PrometheusOutput) Shutdown() {
}
func (o *PrometheusOutput) publishMetrics() {
for _, metric := range o.metrics {
keys := metric.view().OrderedKeys()
result := metric.view().IntViewData(metric.viewDataIdx(), keys)
for keyIdx, value := range result {
labels := prometheus.Labels{"row": keys[keyIdx]}
metric.metric().(*prometheus.GaugeVec).With(labels).Set(float64(value))
}
}
}
func (o *PrometheusOutput) createMetrics() {
for vIdx, view := range o.stream.GetViews() {
for idx, vd := range view.ViewDatas() {
if vd.VarType() == tablestream.VARCHAR {
continue
}
metric := &metric{}
switch vd.Modifier() {
default:
promMetric := promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: metricName(fmt.Sprintf("%sView_%s", ordinalNumbers[vIdx], vd.Name())),
Help: fmt.Sprintf("Metrics %s are from view create by SQL: %s", vd.Name(), view.Name()),
}, []string{"row"})
metric.setMetric(promMetric)
metric.setView(view)
metric.setViewDataIdx(idx)
}
o.metrics = append(o.metrics, metric)
}
}
}
func PrometheusHTTP() {
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(":9090", nil))
}
func metricName(name string) string {
return strings.Replace(strings.Replace(name, "(", "_", -1), ")", "_", -1)
}