-
Notifications
You must be signed in to change notification settings - Fork 0
/
prometheus.go
94 lines (87 loc) · 3.03 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
package main
import (
"fmt"
"strings"
"github.com/prometheus/client_golang/prometheus"
io_prometheus_client "github.com/prometheus/client_model/go"
)
const (
promNamespace = "xmpp"
)
var (
promInfo = prometheus.NewDesc(
promNamespace+"_info",
"constant metric with value=1. Various information about the XMPP connection.",
[]string{"encrypted", "jid"}, nil)
promAlertTriggeredMetric = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: promNamespace,
Name: "alert_trigger_total",
Help: "Number of successful calls to the /alert webhook.",
})
promAlertsProcessedMetric = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: promNamespace,
Name: "alert_processed_total",
Help: "Number of alerts processed in the /alert webhook.",
})
promSendTriggeredMetric = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: promNamespace,
Name: "send_trigger_total",
Help: "Number of successful calls to the /send webhook.",
})
promMessagesSentMetric = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: promNamespace,
Name: "messages_sent_total",
Help: "Number of messages sent.",
}, []string{"recipient", "recipient_type", "format"})
promMessagesReceivedMetric = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: promNamespace,
Name: "messages_received_total",
Help: "Number of messages received.",
}, []string{"recipient"})
)
func init() {
prometheus.MustRegister(promAlertTriggeredMetric)
prometheus.MustRegister(promAlertsProcessedMetric)
prometheus.MustRegister(promSendTriggeredMetric)
prometheus.MustRegister(promMessagesSentMetric)
prometheus.MustRegister(promMessagesReceivedMetric)
}
func getMetrics() ([]string, error) {
metricFamilies, err := prometheus.DefaultGatherer.Gather()
if err != nil {
return nil, err
}
var result []string = nil
var labelsSlice []string = nil
for _, metricFamily := range metricFamilies {
for _, metric := range metricFamily.GetMetric() {
if labelsSlice != nil {
labelsSlice = labelsSlice[:0]
}
for _, label := range metric.GetLabel() {
labelsSlice = append(labelsSlice, fmt.Sprintf(`%s="%s"`, label.GetName(), label.GetValue()))
}
metricType, value := getMetricsValue(metric)
result = append(result, fmt.Sprintf("%s %s{%s}: %f", metricType, metricFamily.GetName(), strings.Join(labelsSlice, ","), value))
}
}
return result, nil
}
func getMetricsValue(metric *io_prometheus_client.Metric) (string, float64) {
if counter := metric.GetCounter(); counter != nil {
return "counter", counter.GetValue()
}
if gauge := metric.GetGauge(); gauge != nil {
return "gauge", gauge.GetValue()
}
if summary := metric.GetSummary(); summary != nil {
return "summary", summary.GetSampleSum() / float64(summary.GetSampleCount())
}
if untyped := metric.GetUntyped(); untyped != nil {
return "untyped", untyped.GetValue()
}
if histogram := metric.GetHistogram(); histogram != nil {
return "histogram", histogram.GetSampleSum() / float64(histogram.GetSampleCount())
}
return "unknown", 0
}