From b81e4048508e5c244736c79ccccc978e64d7f25d Mon Sep 17 00:00:00 2001 From: Zubair Haque Date: Mon, 2 Oct 2023 16:37:30 -0500 Subject: [PATCH] chore: updating getCanaryConfigId to be more efficient with better error handling (#3070) updating getCanaryConfigId to be more efficient with better error handling Signed-off-by: zhaque44 --- metricproviders/kayenta/kayenta.go | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/metricproviders/kayenta/kayenta.go b/metricproviders/kayenta/kayenta.go index 79f47c5f28..b623aa7730 100644 --- a/metricproviders/kayenta/kayenta.go +++ b/metricproviders/kayenta/kayenta.go @@ -67,26 +67,20 @@ func (p *Provider) GetMetadata(metric v1alpha1.Metric) map[string]string { } func getCanaryConfigId(metric v1alpha1.Metric, p *Provider) (string, error) { - configIdLookupURL := fmt.Sprintf(configIdLookupURLFormat, metric.Provider.Kayenta.Address, metric.Provider.Kayenta.Application, metric.Provider.Kayenta.StorageAccountName) response, err := p.client.Get(configIdLookupURL) - if err != nil || response.Body == nil || response.StatusCode != 200 { - if err == nil { - err = errors.New("Invalid Response") - } + if err != nil { return "", err } + defer response.Body.Close() - data, err := io.ReadAll(response.Body) - if err != nil { - return "", err + if response.StatusCode != 200 { + return "", fmt.Errorf("Invalid Response: HTTP %d", response.StatusCode) } var cc []canaryConfig - - err = json.Unmarshal(data, &cc) - if err != nil { + if err := json.NewDecoder(response.Body).Decode(&cc); err != nil { return "", err } @@ -96,7 +90,7 @@ func getCanaryConfigId(metric v1alpha1.Metric, p *Provider) (string, error) { } } - return "", err + return "", errors.New("Canary config not found") } // Run queries kayentd for the metric