-
Notifications
You must be signed in to change notification settings - Fork 115
/
rttscale.go
59 lines (50 loc) · 1.38 KB
/
rttscale.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
// SPDX-License-Identifier: MIT
package main
import "github.com/prometheus/client_golang/prometheus"
type rttUnit int
const (
rttInvalid rttUnit = iota
rttInMills
rttInSeconds
rttBoth
)
func rttUnitFromString(s string) rttUnit {
switch s {
case "s":
return rttInSeconds
case "ms":
return rttInMills
case "both":
return rttBoth
default:
return rttInvalid
}
}
type scaledMetrics struct {
Millis *prometheus.Desc
Seconds *prometheus.Desc
scale rttUnit
}
func (s *scaledMetrics) Describe(ch chan<- *prometheus.Desc) {
if s.scale == rttInMills || s.scale == rttBoth {
ch <- s.Millis
}
if s.scale == rttInSeconds || s.scale == rttBoth {
ch <- s.Seconds
}
}
func (s *scaledMetrics) Collect(ch chan<- prometheus.Metric, value float32, labelValues ...string) {
if s.scale == rttInMills || s.scale == rttBoth {
ch <- prometheus.MustNewConstMetric(s.Millis, prometheus.GaugeValue, float64(value), labelValues...)
}
if s.scale == rttInSeconds || s.scale == rttBoth {
ch <- prometheus.MustNewConstMetric(s.Seconds, prometheus.GaugeValue, float64(value)/1000, labelValues...)
}
}
func newScaledDesc(name, help string, scale rttUnit, variableLabels []string) scaledMetrics {
return scaledMetrics{
scale: scale,
Millis: newDesc(name+"_ms", help+" in millis (deprecated)", variableLabels, nil),
Seconds: newDesc(name+"_seconds", help+" in seconds", variableLabels, nil),
}
}