Skip to content

Commit

Permalink
record metrics only if necessary (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanposhiho authored Oct 13, 2023
1 parent 905a80b commit e10b036
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
13 changes: 9 additions & 4 deletions pkg/hpa/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
10 changes: 8 additions & 2 deletions pkg/vpa/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e10b036

Please sign in to comment.