Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve error handling when there is no metric type #58

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func ReportNotSupported(namespace string, hpaName string) {
errorsMetric.WithLabelValues(namespace, hpaName, "not_supported").Inc()
}

func ReportBadHpaState(namespace string, volumeType string) {
errorsMetric.WithLabelValues(namespace, volumeType, "hpa_state").Inc()
func ReportBadHpaState(namespace string, hpaName string) {
errorsMetric.WithLabelValues(namespace, hpaName, "hpa_state").Inc()
}
func ReportCustomMetricError(namespace string, volumeType string) {
errorsMetric.WithLabelValues(namespace, volumeType, "custom_metric").Inc()
Expand Down
18 changes: 14 additions & 4 deletions internal/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ func requestMetricValuesFromSpec(ctx hpaScopedContext) (*[]bool, error) {
} else {
metricValues <- isZero
}
} else if metric.Type == "" {
metrics.ReportBadHpaState(ctx.hpa.Namespace, ctx.hpa.Name)
ctx.logger.Error(nil, fmt.Sprintf("unexpected response: hpa returned metric definition with no type"))
} else {
metrics.ReportNotSupported(ctx.hpa.Namespace, ctx.hpa.Name)
ctx.logger.Error(nil, fmt.Sprintf("not supported metric type '%s'", metric.Type))
Expand Down Expand Up @@ -203,6 +206,9 @@ func extractMetricValuesFromCurrentMetrics(hpa *autoscaling.HorizontalPodAutosca
isZero = metric.Object.CurrentValue.IsZero()
} else if metric.Type == "External" {
isZero = metric.External.CurrentValue.IsZero()
} else if metric.Type == "" {
metrics.ReportBadHpaState(hpa.Namespace, hpa.Name)
return nil, fmt.Errorf("unexpected response: hpa returned metric with no type")
} else {
metrics.ReportNotSupported(hpa.Namespace, hpa.Name)
return nil, fmt.Errorf("not supported metric type %q", metric.Type)
Expand Down Expand Up @@ -262,12 +268,16 @@ func actualizeHpaTargetState(ctx hpaScopedContext) error {
if ctx.hpa.Status.CurrentReplicas == 0 {
//kube will not return current values if amount of replicas is 0, so we need to check every metric manually
metricValues, err = requestMetricValuesFromSpec(ctx)

if err != nil {
return fmt.Errorf("unable to determine if scaling from 0 is required: %s", err)
}
} else {
metricValues, err = extractMetricValuesFromCurrentMetrics(ctx.hpa)
}

if err != nil {
return err
if err != nil {
return fmt.Errorf("unable to determine if scaling to 0 is required: %s", err)
}
}

allAreZero := checkAllAreZero(metricValues)
Expand Down Expand Up @@ -336,7 +346,7 @@ func actualizeHpaState(ctx context.Context,
if hpa.ObjectMeta.CreationTimestamp.Time.Add(3 * time.Minute).After(time.Now()) {
ctx.logger.Info(fmt.Sprintf("Not able to process newly-created HPA: %s", err))
} else {
ctx.logger.Error(err, "not able to process HPA: %s")
ctx.logger.Error(err, "not able to process HPA")
}
}
}
Expand Down