Skip to content

Commit

Permalink
[exporter/utils] mem conversion float err (#72)
Browse files Browse the repository at this point in the history
* float64 map conversion

* handle empty squeue outputs
  • Loading branch information
abhinavDhulipala authored Jun 22, 2024
1 parent 01aa0e5 commit cd27134
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
11 changes: 9 additions & 2 deletions exporter/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,15 @@ func (nat *NAbleTime) UnmarshalJSON(data []byte) error {

func parseCliFallback(squeue []byte, errorCounter prometheus.Counter) ([]JobMetric, error) {
jobMetrics := make([]JobMetric, 0)
// convert our custom format to the openapi format we expect
for i, line := range bytes.Split(bytes.Trim(squeue, "\n"), []byte("\n")) {
// clean input
squeue = bytes.TrimSpace(squeue)
squeue = bytes.Trim(squeue, "\n")
if len(squeue) == 0 {
// handle no jobs returned
return nil, nil
}

for i, line := range bytes.Split(squeue, []byte("\n")) {
var metric struct {
Account string `json:"a"`
JobId float64 `json:"id"`
Expand Down
15 changes: 15 additions & 0 deletions exporter/jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,18 @@ func TestNAbleTimeJson_NA(t *testing.T) {
assert.Nil(err)
assert.True(nat.Equal(time.Time{}))
}

func TestParseCliFallbackEmpty(t *testing.T) {
assert := assert.New(t)
counter := prometheus.NewCounter(prometheus.CounterOpts{
Name: "validation_counter",
})
metrics, err := parseCliFallback([]byte(""), counter)
assert.NoError(err)
assert.Empty(metrics)
assert.Zero(CollectCounterValue(counter))
metrics, err = parseCliFallback([]byte("\n "), counter)
assert.NoError(err)
assert.Empty(metrics)
assert.Zero(CollectCounterValue(counter))
}
4 changes: 2 additions & 2 deletions exporter/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func MemToFloat(mem string) (float64, error) {
if num, err := strconv.ParseFloat(mem, 64); err == nil {
return num, nil
}
memUnits := map[string]int{
memUnits := map[string]float64{
"M": 1e+6,
"G": 1e+9,
"T": 1e+12,
Expand All @@ -182,5 +182,5 @@ func MemToFloat(mem string) (float64, error) {
// err here should be impossible due to regex
num, err := strconv.ParseFloat(matches[re.SubexpIndex("num")], 64)
memunit := memUnits[matches[re.SubexpIndex("memunit")]]
return num * float64(memunit), err
return num * memunit, err
}

0 comments on commit cd27134

Please sign in to comment.