forked from hawkular/hawkular-openshift-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
store metric units in a fixed tag (hawkular#58)
- Loading branch information
1 parent
8fcc9de
commit 9dfce3f
Showing
4 changed files
with
180 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package collector | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type MetricUnits struct { | ||
Symbol string | ||
Custom bool | ||
} | ||
|
||
const customMetricUnitsPrefix = "custom:" | ||
|
||
type standardMetricUnits []MetricUnits | ||
|
||
// standardMetricUnitsList is a list of standard metric units | ||
// See https://en.wikipedia.org/wiki/International_System_of_Units and http://metrics20.org/spec/ | ||
var standardMetricUnitsList = standardMetricUnits{ | ||
// absolute sizes in bytes | ||
{Symbol: "B"}, | ||
{Symbol: "kB"}, | ||
{Symbol: "MB"}, | ||
{Symbol: "GB"}, | ||
{Symbol: "TB"}, | ||
{Symbol: "PB"}, | ||
{Symbol: "KiB"}, | ||
{Symbol: "MiB"}, | ||
{Symbol: "GiB"}, | ||
{Symbol: "TiB"}, | ||
{Symbol: "PiB"}, | ||
|
||
// absolute sizes in bits | ||
{Symbol: "b"}, | ||
{Symbol: "kb"}, | ||
{Symbol: "Mb"}, | ||
{Symbol: "Gb"}, | ||
{Symbol: "Tb"}, | ||
{Symbol: "Pb"}, | ||
{Symbol: "Kib"}, | ||
{Symbol: "Mib"}, | ||
{Symbol: "Gib"}, | ||
{Symbol: "Tib"}, | ||
{Symbol: "Pib"}, | ||
|
||
// relative time | ||
{Symbol: "jiff"}, | ||
{Symbol: "ns"}, | ||
{Symbol: "us"}, | ||
{Symbol: "ms"}, | ||
{Symbol: "s"}, | ||
{Symbol: "M"}, | ||
{Symbol: "h"}, | ||
{Symbol: "d"}, | ||
{Symbol: "w"}, | ||
|
||
// frequency | ||
{Symbol: "Hz"}, | ||
{Symbol: "kHz"}, | ||
{Symbol: "MHz"}, | ||
{Symbol: "GHz"}, | ||
|
||
// temperature | ||
{Symbol: "C"}, | ||
{Symbol: "F"}, | ||
{Symbol: "K"}, | ||
|
||
// current | ||
{Symbol: "uA"}, | ||
{Symbol: "mA"}, | ||
{Symbol: "A"}, | ||
{Symbol: "kA"}, | ||
{Symbol: "MA"}, | ||
{Symbol: "GA"}, | ||
|
||
// voltage | ||
{Symbol: "uV"}, | ||
{Symbol: "mV"}, | ||
{Symbol: "V"}, | ||
{Symbol: "kV"}, | ||
{Symbol: "MV"}, | ||
{Symbol: "GV"}, | ||
|
||
// percentage | ||
{Symbol: "%"}, | ||
|
||
// none (no metric units are applicable) | ||
{Symbol: ""}, | ||
} | ||
|
||
// GetMetricUnits will check to see if the given string is a valid units identifier. | ||
// If it is valid, this returns the units string as a MetricUnits. | ||
// If it is not a valid units identifier, an error is returned. | ||
// If it is a custom units identifier, it is returned minus the custom units prefix. | ||
func GetMetricUnits(u string) (MetricUnits, error) { | ||
if len(u) > len(customMetricUnitsPrefix) && strings.HasPrefix(u, customMetricUnitsPrefix) { | ||
mu := MetricUnits{ | ||
Symbol: strings.TrimPrefix(u, customMetricUnitsPrefix), | ||
Custom: true, | ||
} | ||
return mu, nil | ||
} | ||
for _, x := range standardMetricUnitsList { | ||
if x.Symbol == u { | ||
return x, nil | ||
} | ||
} | ||
return MetricUnits{}, fmt.Errorf("invalid metric units: %v", u) | ||
} |
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,51 @@ | ||
package collector | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestGetMetricUnits(t *testing.T) { | ||
// make sure valid metrics are retrieved successfully | ||
u, e := GetMetricUnits("ms") | ||
if e != nil { | ||
t.Errorf("Should not have failed. Error=%v", e) | ||
} | ||
if u.Symbol != "ms" { | ||
t.Errorf("Should have matched 'ms'. u=%v", u) | ||
} | ||
if u.Custom != false { | ||
t.Errorf("Should not have been custom. u=%v", u) | ||
} | ||
|
||
u, e = GetMetricUnits("custom:foobars") | ||
if e != nil { | ||
t.Errorf("Should not have failed. Error=%v", e) | ||
} | ||
if u.Symbol != "foobars" { | ||
t.Errorf("Should have matched 'foobars'. u=%v", u) | ||
} | ||
if u.Custom != true { | ||
t.Errorf("Should have been custom. u=%v", u) | ||
} | ||
|
||
u, e = GetMetricUnits("") | ||
if e != nil { | ||
t.Errorf("Should not have failed. Empty units means 'none'. Error=%v", e) | ||
} | ||
if u.Symbol != "" { | ||
t.Errorf("Should have matched the 'none' units (an empty string). u=%v", u) | ||
} | ||
if u.Custom != false { | ||
t.Errorf("Should not have been custom. u=%v", u) | ||
} | ||
|
||
// make sure errors are generated properly | ||
u, e = GetMetricUnits("millis") | ||
if e == nil { | ||
t.Errorf("Should have failed - not a standard metric. u=%v", u) | ||
} | ||
u, e = GetMetricUnits("foobars") | ||
if e == nil { | ||
t.Errorf("Should have failed - not a standard metric. u=%v", u) | ||
} | ||
} |
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