From e10b036f34057eddef7efa3ac41506d356557fda Mon Sep 17 00:00:00 2001 From: Kensei Nakada Date: Fri, 13 Oct 2023 12:24:59 +0900 Subject: [PATCH] record metrics only if necessary (#186) --- pkg/hpa/service.go | 13 +++++++++---- pkg/vpa/service.go | 10 ++++++++-- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/pkg/hpa/service.go b/pkg/hpa/service.go index 9bfd1237..2fe5bd87 100644 --- a/pkg/hpa/service.go +++ b/pkg/hpa/service.go @@ -393,8 +393,10 @@ func (c *Service) ChangeHPAFromTortoiseRecommendation(tortoise *autoscalingv1bet if err != nil { return nil, tortoise, fmt.Errorf("get minReplicas recommendation: %w", err) } - metrics.AppliedHPAMinReplicas.WithLabelValues(tortoise.Name, tortoise.Namespace, hpa.Name).Set(float64(recommendMin)) - metrics.AppliedHPAMaxReplicas.WithLabelValues(tortoise.Name, tortoise.Namespace, hpa.Name).Set(float64(recommendMax)) + if recordMetrics { + metrics.ProposedHPAMinReplicas.WithLabelValues(tortoise.Name, tortoise.Namespace, hpa.Name).Set(float64(recommendMin)) + metrics.ProposedHPAMaxReplicas.WithLabelValues(tortoise.Name, tortoise.Namespace, hpa.Name).Set(float64(recommendMax)) + } // the minReplicas to be applied is not always the same as the recommended one. var minToActuallyApply int32 @@ -416,8 +418,11 @@ func (c *Service) ChangeHPAFromTortoiseRecommendation(tortoise *autoscalingv1bet minToActuallyApply = recommendMin } hpa.Spec.MinReplicas = &minToActuallyApply - metrics.AppliedHPAMinReplicas.WithLabelValues(tortoise.Name, tortoise.Namespace, hpa.Name).Set(float64(*hpa.Spec.MinReplicas)) - metrics.AppliedHPAMaxReplicas.WithLabelValues(tortoise.Name, tortoise.Namespace, hpa.Name).Set(float64(hpa.Spec.MaxReplicas)) + if tortoise.Spec.UpdateMode != autoscalingv1beta2.UpdateModeOff && recordMetrics { + // We don't want to record applied* metric when UpdateMode is Off. + metrics.AppliedHPAMinReplicas.WithLabelValues(tortoise.Name, tortoise.Namespace, hpa.Name).Set(float64(*hpa.Spec.MinReplicas)) + metrics.AppliedHPAMaxReplicas.WithLabelValues(tortoise.Name, tortoise.Namespace, hpa.Name).Set(float64(hpa.Spec.MaxReplicas)) + } return hpa, tortoise, nil } diff --git a/pkg/vpa/service.go b/pkg/vpa/service.go index 39844d63..19f00e80 100644 --- a/pkg/vpa/service.go +++ b/pkg/vpa/service.go @@ -212,11 +212,17 @@ func (c *Service) UpdateVPAFromTortoiseRecommendation(ctx context.Context, torto for resourcename, value := range r.RecommendedResource { if resourcename == corev1.ResourceCPU { metrics.ProposedCPURequest.WithLabelValues(tortoise.Name, tortoise.Namespace, r.ContainerName, tortoise.Spec.TargetRefs.ScaleTargetRef.Name, tortoise.Spec.TargetRefs.ScaleTargetRef.Kind).Set(float64(value.MilliValue())) - metrics.AppliedCPURequest.WithLabelValues(tortoise.Name, tortoise.Namespace, r.ContainerName, tortoise.Spec.TargetRefs.ScaleTargetRef.Name, tortoise.Spec.TargetRefs.ScaleTargetRef.Kind).Set(float64(value.MilliValue())) + if tortoise.Spec.UpdateMode == autoscalingv1beta2.UpdateModeOff { + // We don't want to record applied* metric when UpdateMode is Off. + metrics.AppliedCPURequest.WithLabelValues(tortoise.Name, tortoise.Namespace, r.ContainerName, tortoise.Spec.TargetRefs.ScaleTargetRef.Name, tortoise.Spec.TargetRefs.ScaleTargetRef.Kind).Set(float64(value.MilliValue())) + } } if resourcename == corev1.ResourceMemory { metrics.ProposedMemoryRequest.WithLabelValues(tortoise.Name, tortoise.Namespace, r.ContainerName, tortoise.Spec.TargetRefs.ScaleTargetRef.Name, tortoise.Spec.TargetRefs.ScaleTargetRef.Kind).Set(float64(value.Value())) - metrics.AppliedMemoryRequest.WithLabelValues(tortoise.Name, tortoise.Namespace, r.ContainerName, tortoise.Spec.TargetRefs.ScaleTargetRef.Name, tortoise.Spec.TargetRefs.ScaleTargetRef.Kind).Set(float64(value.Value())) + if tortoise.Spec.UpdateMode == autoscalingv1beta2.UpdateModeOff { + // We don't want to record applied* metric when UpdateMode is Off. + metrics.AppliedMemoryRequest.WithLabelValues(tortoise.Name, tortoise.Namespace, r.ContainerName, tortoise.Spec.TargetRefs.ScaleTargetRef.Name, tortoise.Spec.TargetRefs.ScaleTargetRef.Kind).Set(float64(value.Value())) + } } } metricsRecorded = true