-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
An attempt to clean up the code that converts Artifactory strings to numbers accepted in Prometheus.
- Loading branch information
1 parent
8dfb2ca
commit 2221844
Showing
6 changed files
with
180 additions
and
93 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package collector | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
// convArtiBoolToProm is a very interesting appendix. | ||
// Something needs it, but what and why? | ||
// It would be quite nice if it was written here why such a thing is needed. | ||
func convArtiBoolToProm(b bool) float64 { | ||
if b { | ||
return 1 | ||
} | ||
return 0 | ||
} | ||
|
||
const ( | ||
pattOneNumber = `^(?P<number>[[:digit:]]{1,3}(?:,[[:digit:]]{3})*)(?:\.[[:digit:]]{1,2})? ?(?P<multiplier>%|bytes|[KMGT]B)?$` | ||
) | ||
|
||
var ( | ||
reOneNumber = regexp.MustCompile(pattOneNumber) | ||
) | ||
|
||
func (e *Exporter) convNumArtiToProm(artiNum string) (float64, error) { | ||
e.logger.Debug( | ||
"Attempting to convert a string from artifactory representing a number.", | ||
"artifactory.number", artiNum, | ||
) | ||
|
||
if !reOneNumber.MatchString(artiNum) { | ||
e.logger.Debug( | ||
"The arti number did not match known templates.", | ||
"artifactory.number", artiNum, | ||
) | ||
err := fmt.Errorf( | ||
`The string '%s' does not match pattern '%s'.`, | ||
artiNum, | ||
pattOneNumber, | ||
) | ||
return 0, err | ||
} | ||
|
||
groups := extractNamedGroups(artiNum, reOneNumber) | ||
|
||
// The following `strings.replace` is for those cases that contain a comma | ||
// thousands separator. In other cases, unnecessary, but cheaper than if. | ||
// Sorry. | ||
f, err := e.convNumber( | ||
strings.Replace(groups["number"], `,`, ``, -1), | ||
) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
mAsString, mIsPresent := groups["multiplier"] | ||
if !mIsPresent { | ||
return f, nil | ||
} | ||
m, err := e.convMultiplier(mAsString) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return f * m, nil | ||
} | ||
|
||
const ( | ||
pattTBytesPercent = `^(?P<tbytes>[[:digit:]]+(?:\.[[:digit:]]{1,2})?) TB \((?P<percent>[[:digit:]]{1,2}(?:\.[[:digit:]]{1,2})?)%\)$` | ||
) | ||
|
||
var ( | ||
reTBytesPercent = regexp.MustCompile(pattTBytesPercent) | ||
) | ||
|
||
func (e *Exporter) convTwoNumsArtiToProm(artiSize string) (float64, float64, error) { | ||
e.logger.Debug( | ||
"Attempting to convert a string from artifactory representing a number.", | ||
"artifactory.number", artiSize, | ||
) | ||
|
||
if !reTBytesPercent.MatchString(artiSize) { | ||
e.logger.Debug( | ||
"The arti number did not match known templates.", | ||
"artifactory.number", artiSize, | ||
) | ||
err := fmt.Errorf( | ||
`The string '%s' does not match '%s' pattern.`, | ||
artiSize, | ||
pattTBytesPercent, | ||
) | ||
return 0, 0, err | ||
} | ||
groups := extractNamedGroups(artiSize, reTBytesPercent) | ||
b, err := e.convNumber(groups["tbytes"]) | ||
if err != nil { | ||
return 0, 0, err | ||
} | ||
mulTB, _ := e.convMultiplier(`TB`) | ||
p, err := e.convNumber(groups["percent"]) | ||
if err != nil { | ||
return 0, 0, err | ||
} | ||
mulPercent, _ := e.convMultiplier(`%`) | ||
return b * mulTB, p * mulPercent, nil | ||
} |
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,54 @@ | ||
package collector | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
"regexp" | ||
"strconv" | ||
) | ||
|
||
var mulConvDriver = map[string]float64{ | ||
`%`: 0.01, | ||
`bytes`: 1, | ||
`KB`: math.Exp2(10), | ||
`MB`: math.Exp2(20), | ||
`GB`: math.Exp2(30), | ||
`TB`: math.Exp2(40), | ||
} | ||
|
||
func (e *Exporter) convMultiplier(m string) (float64, error) { | ||
mul, present := mulConvDriver[m] | ||
if present { | ||
return mul, nil | ||
} | ||
e.logger.Error( | ||
"The string was not recognized as a known multiplier.", | ||
"artifactory.number.multiplier", m, | ||
) | ||
return 0, fmt.Errorf(`Could not recognise '%s; as multiplier`, m) | ||
} | ||
|
||
func (e *Exporter) convNumber(n string) (float64, error) { | ||
f, err := strconv.ParseFloat(n, 64) | ||
if err != nil { | ||
e.logger.Error( | ||
"String not convertible to float64", | ||
"string", n, | ||
) | ||
return 0, err | ||
} | ||
return f, nil | ||
} | ||
|
||
type reCaptureGroups map[string]string | ||
|
||
func extractNamedGroups(artiNum string, re *regexp.Regexp) reCaptureGroups { | ||
match := re.FindStringSubmatch(artiNum) | ||
groupsFound := make(reCaptureGroups) | ||
for i, name := range re.SubexpNames() { | ||
if i != 0 && name != "" { | ||
groupsFound[name] = match[i] | ||
} | ||
} | ||
return groupsFound | ||
} |
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 was deleted.
Oops, something went wrong.