Skip to content

Commit

Permalink
Fix panic for case when no metrics are returned (#110)
Browse files Browse the repository at this point in the history
* Minor formatting changes

Remove extra whitespace

* Do not crash for queries where no metrics are returned

Depending on the configured query and particular timespan, it is
possible for no metrics to be returned. Which in the previous
implementation would cause a panic. This commit gracefully handles the
scenario by continuing to the next query job in this particular case.
  • Loading branch information
lwalter authored Nov 27, 2024
1 parent 10db7e4 commit 612e273
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,12 @@ func runAPIPolling(done chan error, url, token string, yamlConfig YamlConfig, re
return
}

// Handle cases where the metric may be missing for the given time range
if len(poll.Events) < 1 {
zap.L().Sugar().Debugf("No Events returned by query. Timespan: %v, MetricName: %s", job.Timespan, job.MetricName)
continue
}

var floatValue float64
for _, f := range supportedFunctions {
value, ok := poll.Events[0][f]
Expand All @@ -201,7 +207,6 @@ func runAPIPolling(done chan error, url, token string, yamlConfig YamlConfig, re
}
} else {
zap.L().Sugar().Debugf("Skipped value because query isn't done. Timespan: %v, Value: %v", job.Timespan, floatValue)

}
}
time.Sleep(5000 * time.Millisecond)
Expand All @@ -223,7 +228,6 @@ func (m *MetricMap) Register() error {
}

func (m *MetricMap) UpdateMetricValue(metricName, timespan, repo string, value float64, staticLabels []MetricLabel) error {

labels := make(map[string]string)
labels[intervalLabel] = timespan
labels[repoLabel] = repo
Expand Down

0 comments on commit 612e273

Please sign in to comment.