/
metrics.go
113 lines (98 loc) · 2.99 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package metrics
import (
"log"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
messageSent = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "telego_message_sent",
Help: "Telego sending a message",
})
messageReceive = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "telego_message_receive",
Help: "Telego receiving a message",
})
session = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "telego_session_counter",
Help: "Sessions that telego is keeping waiting for messages",
})
abstract = map[string]prometheus.Collector{}
)
// ExposeMetrics exposes the metrics so they can be queury by a prometheus server
func ExposeMetrics() {
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(":8080", nil))
}
// AddGauge adds and registers a Gauge metric to the prometheus client
func AddGauge(name, help string) {
abstract[name] = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: name,
Help: help,
},
)
prometheus.MustRegister(abstract[name])
}
// AddCounter adds and registers a Counter metric to the prometheus client
func AddCounter(name, help string) {
abstract[name] = prometheus.NewCounter(
prometheus.CounterOpts{
Name: name,
Help: help,
},
)
prometheus.MustRegister(abstract[name])
}
// AddHistogram adds and registers an Histogram metric to the prometheus client
func AddHistogram(name, help string) {
abstract[name] = prometheus.NewHistogram(
prometheus.HistogramOpts{
Name: name,
Help: help,
},
)
prometheus.MustRegister(abstract[name])
}
// AddSummary adds and registers an Summary metric to the prometheus client
func AddSummary(name, help string) {
abstract[name] = prometheus.NewSummary(
prometheus.SummaryOpts{
Name: name,
Help: help,
},
)
prometheus.MustRegister(abstract[name])
}
// Unregister removes the metric from the exposed metrics
func Unregister(name string) {
v, ok := abstract[name]
if ok {
prometheus.Unregister(v)
}
}
// GetGauge gets a gauge metric given the name. It returns two values,
// the metric and a boolean indicating if the metric exists
func GetGauge(name string) (prometheus.Gauge, bool) {
v, ok := abstract[name]
return v.(prometheus.Gauge), ok
}
// GetCounter gets a counter metric given the name. It returns two values,
// the metric and a boolean indicating if the metric exists
func GetCounter(name string) (prometheus.Counter, bool) {
v, ok := abstract[name]
return v.(prometheus.Counter), ok
}
// GetHistogram gets a historic metric given the name. It returns two values,
// the metric and a boolean indicating if the metric exists
func GetHistogram(name string) (prometheus.Histogram, bool) {
v, ok := abstract[name]
return v.(prometheus.Histogram), ok
}
// GetSummary gets a summary metric given the name. It returns two values,
// the metric and a boolean indicating if the metric exists
func GetSummary(name string) (prometheus.Summary, bool) {
v, ok := abstract[name]
return v.(prometheus.Summary), ok
}