forked from czerwonk/ping_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rttscale.go
57 lines (48 loc) · 1.4 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
// 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
}
func (s *scaledMetrics) Describe(ch chan<- *prometheus.Desc) {
if rttMetricsScale == rttInMills || rttMetricsScale == rttBoth {
ch <- s.Millis
}
if rttMetricsScale == rttInSeconds || rttMetricsScale == rttBoth {
ch <- s.Seconds
}
}
func (s *scaledMetrics) Collect(ch chan<- prometheus.Metric, value float32, labelValues ...string) {
if rttMetricsScale == rttInMills || rttMetricsScale == rttBoth {
ch <- prometheus.MustNewConstMetric(s.Millis, prometheus.GaugeValue, float64(value), labelValues...)
}
if rttMetricsScale == rttInSeconds || rttMetricsScale == rttBoth {
ch <- prometheus.MustNewConstMetric(s.Seconds, prometheus.GaugeValue, float64(value)/1000, labelValues...)
}
}
func newScaledDesc(name, help string, variableLabels []string) scaledMetrics {
return scaledMetrics{
Millis: newDesc(name+"_ms", help+" in millis (deprecated)", variableLabels, nil),
Seconds: newDesc(name+"_seconds", help+" in seconds", variableLabels, nil),
}
}