-
Notifications
You must be signed in to change notification settings - Fork 157
/
prom.go
242 lines (215 loc) · 7.59 KB
/
prom.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright 2017 ETH Zurich
// Copyright 2018 ETH Zurich, Anapaya Systems
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package prom contains some utility functions for dealing with prometheus
// metrics.
package prom
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
// Common label values.
const (
// LabelResult is the label for result classifications.
LabelResult = "result"
// LabelStatus for latency status classifications, possible values are prefixed with Status*.
LabelStatus = "status"
// LabelOperation is the label for the name of an executed operation.
LabelOperation = "op"
// LabelSrc is the label for the source.
LabelSrc = "src"
// LabelDst is the label for the destination.
LabelDst = "dst"
// LabelNeighIA is label for the neighboring IA.
LabelNeighIA = "neighbor_isd_as"
)
// Common result values.
const (
// Success is no error.
Success = "ok_success"
// ErrCrypto is used for crypto related errors.
ErrCrypto = "err_crypto"
// ErrDB is used for db related errors.
ErrDB = "err_db"
// ErrInternal is an internal error.
ErrInternal = "err_internal"
// ErrInvalidReq is an invalid request.
ErrInvalidReq = "err_invalid_request"
// ErrNotClassified is an error that is not further classified.
ErrNotClassified = "err_not_classified"
// ErrParse failed to parse request.
ErrParse = "err_parse"
// ErrProcess is an error during processing e.g. parsing failed.
ErrProcess = "err_process"
// ErrTimeout is a timeout error.
ErrTimeout = "err_timeout"
// ErrValidate is used for validation related errors.
ErrValidate = "err_validate"
// ErrVerify is used for verification related errors.
ErrVerify = "err_verify"
// ErrNetwork is used for errors when sending something over the network.
ErrNetwork = "err_network"
// ErrNotFound is used for errors where a resource is not found.
ErrNotFound = "err_not_found"
// ErrUnavailable is used for errors where a resource is not available.
ErrUnavailable = "err_unavailable"
)
// FIXME(roosd): remove when moving messenger to new metrics style.
const (
StatusErr = "err"
StatusTimeout = "err_timeout"
)
var (
// DefaultLatencyBuckets 10ms, 20ms, 40ms, ... 5.12s, 10.24s.
DefaultLatencyBuckets = []float64{0.01, 0.02, 0.04, 0.08, 0.16, 0.32, 0.64,
1.28, 2.56, 5.12, 10.24}
// DefaultSizeBuckets 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384
DefaultSizeBuckets = []float64{32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384}
)
// Labels allows to safely pass label values into prometheus.
type Labels interface {
Labels() []string
Values() []string
}
// ExportElementID exports the element ID as configured in the config file.
func ExportElementID(id string) {
promauto.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "scion",
Subsystem: "",
Name: "elem_id",
Help: "The element ID from the config file",
},
[]string{"cfg"},
).WithLabelValues(id).Set(1)
}
// SafeRegister registers c and returns the registered collector. If c was
// already registered the already registered collector is returned. In case of
// any other error this method panicks (as MustRegister).
func SafeRegister(c prometheus.Collector) prometheus.Collector {
if err := prometheus.Register(c); err != nil {
if are, ok := err.(prometheus.AlreadyRegisteredError); ok {
return are.ExistingCollector
}
panic(err)
}
return c
}
// NewCounter creates a new prometheus counter that is registered with the default registry.
func NewCounter(namespace, subsystem, name, help string) prometheus.Counter {
return promauto.NewCounter(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: name,
Help: help,
},
)
}
// NewCounterVecWithLabels creates a prometheus counter vec that is registered with
// the default registry and with a default init values for labels to avoid missing metrics.
func NewCounterVecWithLabels(ns, sub, name, help string, label Labels) *prometheus.CounterVec {
opts := prometheus.CounterOpts{
Namespace: ns,
Subsystem: sub,
Name: name,
Help: help,
}
c := prometheus.NewCounterVec(opts, label.Labels())
ret := SafeRegister(c).(*prometheus.CounterVec)
return ret
}
// NewCounterVec creates a new prometheus counter vec that is registered with the default registry.
func NewCounterVec(namespace, subsystem, name, help string,
labelNames []string) *prometheus.CounterVec {
// Not to be used https://github.com/scionproto/scion/issues/3274
return NewCounterVecWithLabels(namespace, subsystem, name, help,
initLabels{labelNames: labelNames})
}
// NewGauge creates a new prometheus gauge that is registered with the default registry.
func NewGauge(namespace, subsystem, name, help string) prometheus.Gauge {
return promauto.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: name,
Help: help,
},
)
}
// NewGaugeVecWithLabels creates a new prometheus gauge vec that is registered
// with the default registry.
func NewGaugeVecWithLabels(namespace, subsystem, name, help string,
label Labels) *prometheus.GaugeVec {
opts := prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: name,
Help: help,
}
c := prometheus.NewGaugeVec(opts, label.Labels())
ret := SafeRegister(c).(*prometheus.GaugeVec)
return ret
}
// NewGaugeVec creates a new prometheus gauge vec that is registered with the default registry.
func NewGaugeVec(namespace, subsystem, name, help string,
labelNames []string) *prometheus.GaugeVec {
// Not to be used https://github.com/scionproto/scion/issues/3274
return NewGaugeVecWithLabels(namespace, subsystem, name, help,
initLabels{labelNames: labelNames})
}
// NewHistogram creates a new prometheus histogram that is registered with the default registry.
func NewHistogram(namespace, subsystem, name, help string, buckets []float64) prometheus.Histogram {
return promauto.NewHistogram(
prometheus.HistogramOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: name,
Help: help,
Buckets: buckets,
},
)
}
// NewHistogramVecWithLabels creates a new prometheus histogram vec
// that is registered with the default registry.
func NewHistogramVecWithLabels(namespace, subsystem, name, help string,
label Labels, buckets []float64) *prometheus.HistogramVec {
opts := prometheus.HistogramOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: name,
Help: help,
Buckets: buckets,
}
c := prometheus.NewHistogramVec(opts, label.Labels())
ret := SafeRegister(c).(*prometheus.HistogramVec)
return ret
}
// NewHistogramVec creates a new prometheus histogram vec
// that is registered with the default registry.
func NewHistogramVec(namespace, subsystem, name, help string,
labelNames []string, buckets []float64) *prometheus.HistogramVec {
// Not to be used https://github.com/scionproto/scion/issues/3274
return NewHistogramVecWithLabels(namespace, subsystem, name, help,
initLabels{labelNames: labelNames}, buckets)
}
type initLabels struct {
labelNames []string
}
func (l initLabels) Labels() []string {
return l.labelNames
}
func (l initLabels) Values() []string {
return nil
}