From 363e22126ff80cda511531c05079d8cc2a77d0ac Mon Sep 17 00:00:00 2001 From: Lucas Walter Date: Mon, 25 Nov 2024 09:29:28 -0800 Subject: [PATCH] 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 | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/main.go b/main.go index 8fc07f3..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]