From 612e273de56133f9c6dd6861908f3cb8073d4518 Mon Sep 17 00:00:00 2001 From: Lucas Walter Date: Wed, 27 Nov 2024 00:04:28 -0800 Subject: [PATCH] Fix panic for case when no metrics are returned (#110) * 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. --- main.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 0295872..3c88485 100644 --- a/main.go +++ b/main.go @@ -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] @@ -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) @@ -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