-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from digineo/units
metrics: allow for best practices
- Loading branch information
Showing
4 changed files
with
113 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
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), | ||
} | ||
} |