-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
205 lines (164 loc) · 6.18 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package metrics
import (
"errors"
"net/http"
"strconv"
"github.com/bottlenose-inc/go-common-tools/logger" // go-common-tools logger package
"github.com/prometheus/client_golang/prometheus" // Official Prometheus golang library
)
type PrometheusId struct {
Name string
Address string
Port int
ID string
}
var (
histogramBuckets = []float64{0.001, 0.0025, 0.005, 0.0075, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 20, 30, 45, 60, 90}
)
func StartPrometheusMetricsServer(name string, logger *logger.Logger, port int) error {
// name for identifying the service
// logger - Logger object from go-common-tools#logger.go
// port for Prometheus to report metrics to
// Returns an error or nil upon successful setup
// Start HTTP server
http.Handle("/metrics", prometheus.Handler())
err := http.ListenAndServe(":"+strconv.Itoa(port), nil)
if err != nil {
logger.Error("Error starting Prometheus metrics server: " + err.Error())
return err
}
return nil
}
func CreateHistogram(name string, namespace string, subsystem string, help string, labels map[string]string, buckets ...[]float64) (histogram prometheus.Histogram, err error) {
// "name" and "help" are required by Prometheus to create a histogram
// all other fields are optional
// Returns a prometheus histogram object
useBuckets := histogramBuckets
if len(buckets) > 0 {
useBuckets = buckets[0]
}
constLabels := prometheus.Labels(labels)
if name == "" || help == "" {
err = errors.New("Prometheus histogram requires both name and help fields to initialize - missing one or both of those fields")
return nil, err
}
histogram = prometheus.NewHistogram(prometheus.HistogramOpts{
Name: name,
Help: help,
Namespace: namespace,
Subsystem: subsystem,
ConstLabels: constLabels,
Buckets: useBuckets,
})
prometheus.MustRegister(histogram)
return histogram, nil
}
func CreateHistogramVector(name string, namespace string, subsystem string, help string, labels map[string]string, labelNames []string, buckets ...[]float64) (histogramVec *prometheus.HistogramVec, err error) {
// "name" and "help" are required by Prometheus to create a histogram
// all other fields are optional
// Returns a prometheus histogram object
useBuckets := histogramBuckets
if len(buckets) > 0 {
useBuckets = buckets[0]
}
constLabels := prometheus.Labels(labels)
if name == "" || help == "" {
err = errors.New("Prometheus histogram requires both name and help fields to initialize - missing one or both of those fields")
return nil, err
}
histogramVec = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: name,
Help: help,
Namespace: namespace,
Subsystem: subsystem,
ConstLabels: constLabels,
Buckets: useBuckets,
}, labelNames)
prometheus.MustRegister(histogramVec)
return histogramVec, nil
}
func CreateCounterVector(name string, namespace string, subsystem string, help string, labels map[string]string, labelNames []string) (counterVec *prometheus.CounterVec, err error) {
// "name" and "help" are required by Prometheus to create a counter vector
// all other fields are optional
// Returns a prometheus counter vector object
constLabels := prometheus.Labels(labels)
if name == "" || help == "" {
err = errors.New("Prometheus counter vector requires both name and help fields to initialize - missing one or both of those fields")
return nil, err
}
counterVec = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: name,
Help: help,
Namespace: namespace,
Subsystem: subsystem,
ConstLabels: constLabels,
}, labelNames)
prometheus.MustRegister(counterVec)
return counterVec, nil
}
// initialize a counter vector with given labels, setting values to 0
func InitCounterVector(counterVec *prometheus.CounterVec, labels []string) {
for _, label := range labels {
counter, err := counterVec.GetMetricWithLabelValues(label)
if err == nil {
counter.Add(0)
}
}
}
func CreateCounter(name string, namespace string, subsystem string, help string, labels map[string]string) (counter prometheus.Counter, err error) {
// "name" and "help" are required by Prometheus to create a counter
// all other fields are optional
// Returns a prometheus counter object
constLabels := prometheus.Labels(labels)
if name == "" || help == "" {
err = errors.New("Prometheus counter requires both name and help fields to initialize - missing one or both of those fields")
return nil, err
}
counter = prometheus.NewCounter(prometheus.CounterOpts{
Name: name,
Help: help,
Namespace: namespace,
Subsystem: subsystem,
ConstLabels: constLabels,
})
prometheus.MustRegister(counter)
return counter, nil
}
func CreateGauge(name string, namespace string, subsystem string, help string, labels map[string]string) (gauge prometheus.Gauge, err error) {
// "name" and "help" are required by Prometheus to create a gauge
// all other fields are optional
// Returns a prometheus gauge object
constLabels := prometheus.Labels(labels)
if name == "" || help == "" {
err = errors.New("Prometheus gauge requires both name and help fields to initialize - missing one or both of those fields")
return nil, err
}
gauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: name,
Help: help,
Namespace: namespace,
Subsystem: subsystem,
ConstLabels: constLabels,
})
prometheus.MustRegister(gauge)
return gauge, nil
}
func CreateGaugeVector(name string, namespace string, subsystem string, help string, labels map[string]string, labelNames []string) (gaugeVec *prometheus.GaugeVec, err error) {
// "name" and "help" are required by Prometheus to create a gauge vector
// all other fields are optional
// Returns a prometheus gauge vector object
constLabels := prometheus.Labels(labels)
if name == "" || help == "" {
err = errors.New("Prometheus gauge vector requires both name and help fields to initialize - missing one or both of those fields")
return nil, err
}
gaugeVec = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: name,
Help: help,
Namespace: namespace,
Subsystem: subsystem,
ConstLabels: constLabels,
}, labelNames)
prometheus.MustRegister(gaugeVec)
return gaugeVec, nil
}