Skip to content

Commit

Permalink
chore: updating getCanaryConfigId to be more efficient with better er…
Browse files Browse the repository at this point in the history
…ror handling (#3070)

updating getCanaryConfigId to be more efficient with better error handling

Signed-off-by: zhaque44 <[email protected]>
  • Loading branch information
zhaque44 authored Oct 2, 2023
1 parent 5ac4a48 commit b81e404
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions metricproviders/kayenta/kayenta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand Down

0 comments on commit b81e404

Please sign in to comment.