From 0d2a84ea222479e42e57f19656b6b3275c6c0a4d Mon Sep 17 00:00:00 2001 From: isbakurskii Date: Sun, 28 Dec 2025 12:14:23 +0100 Subject: [PATCH 1/2] Added ytop_update_strategy_on_delete_wait_seconds prometheus metric --- pkg/apiproxy/ytsaurus.go | 3 +++ pkg/metrics/update_strategy.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 pkg/metrics/update_strategy.go diff --git a/pkg/apiproxy/ytsaurus.go b/pkg/apiproxy/ytsaurus.go index 264029c9d..500ce1f1d 100644 --- a/pkg/apiproxy/ytsaurus.go +++ b/pkg/apiproxy/ytsaurus.go @@ -18,6 +18,7 @@ import ( ytv1 "github.com/ytsaurus/ytsaurus-k8s-operator/api/v1" "github.com/ytsaurus/ytsaurus-k8s-operator/pkg/consts" + "github.com/ytsaurus/ytsaurus-k8s-operator/pkg/metrics" ) type Ytsaurus struct { @@ -241,8 +242,10 @@ func (c *Ytsaurus) UpdateOnDeleteComponentsSummary(ctx context.Context, waitingO summaryParts = append(summaryParts, fmt.Sprintf("{%s (waiting %s)}", componentName, waitingDuration.Truncate(time.Second).String())) + metrics.ObserveOnDeleteWait(c.GetResource().Name, componentName, &condition.LastTransitionTime) } else { summaryParts = append(summaryParts, fmt.Sprintf("{%s}", componentName)) + metrics.ObserveOnDeleteWait(c.GetResource().Name, componentName, nil) } } } diff --git a/pkg/metrics/update_strategy.go b/pkg/metrics/update_strategy.go new file mode 100644 index 000000000..4a204ed25 --- /dev/null +++ b/pkg/metrics/update_strategy.go @@ -0,0 +1,33 @@ +package metrics + +import ( + "time" + + "github.com/prometheus/client_golang/prometheus" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics" +) + +//nolint:gochecknoglobals // Prometheus metrics are package-level for registration and reuse. +var onDeleteWaitSeconds = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Namespace: "ytop", + Subsystem: "update_strategy", + Name: "on_delete_wait_seconds", + Help: "Seconds a component has been waiting in OnDelete mode since the mode started.", + }, + []string{"cluster", "component"}, +) + +func init() { + ctrlmetrics.Registry.MustRegister(onDeleteWaitSeconds) +} + +func ObserveOnDeleteWait(cluster, component string, startedAt *metav1.Time) { + lbl := []string{cluster, component} + if startedAt == nil || startedAt.IsZero() { + onDeleteWaitSeconds.DeleteLabelValues(lbl...) + return + } + onDeleteWaitSeconds.WithLabelValues(lbl...).Set(time.Since(startedAt.Time).Seconds()) +} From 05eebbbda03350e72816954e00ccd8e4220cd90a Mon Sep 17 00:00:00 2001 From: isbakurskii Date: Fri, 9 Jan 2026 20:52:43 +0100 Subject: [PATCH 2/2] Improved strategyOnDeleteWatingTimeSeconds metric, migrated to labeller --- pkg/apiproxy/ytsaurus.go | 29 ++++++++++++++--------------- pkg/labeller/labeller.go | 4 ++++ pkg/metrics/update_strategy.go | 18 +++++++++--------- 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/pkg/apiproxy/ytsaurus.go b/pkg/apiproxy/ytsaurus.go index 500ce1f1d..f82ceee59 100644 --- a/pkg/apiproxy/ytsaurus.go +++ b/pkg/apiproxy/ytsaurus.go @@ -18,6 +18,7 @@ import ( ytv1 "github.com/ytsaurus/ytsaurus-k8s-operator/api/v1" "github.com/ytsaurus/ytsaurus-k8s-operator/pkg/consts" + "github.com/ytsaurus/ytsaurus-k8s-operator/pkg/labeller" "github.com/ytsaurus/ytsaurus-k8s-operator/pkg/metrics" ) @@ -203,23 +204,21 @@ func buildComponentsSummary(components []ytv1.Component) string { } var componentNames []string for _, comp := range components { - // TODO: we better use labeller here after support deployment names in it. - name := getComponentName(comp) + name := getLabelerComponentName(comp) componentNames = append(componentNames, name) } return "{" + strings.Join(componentNames, " ") + "}" } -func getComponentName(component ytv1.Component) string { - // TODO: we better use labeller here after support deployment names in it. - name := consts.GetShortName(component.Type) - if name == "" { - name = strings.ToLower(string(component.Type)) - } - if component.Name != "" { - name += fmt.Sprintf("-%s", component.Name) - } - return name +func getComponentLabeler(component ytv1.Component) *labeller.Labeller { + l := (&labeller.Labeller{}).ForComponent(component.Type, component.Name) + return l +} + +func getLabelerComponentName(component ytv1.Component) string { + l := getComponentLabeler(component) + l.UseShortNames = true + return l.GetComponentShortName() } // UpdateOnDeleteComponentsSummary updates the UpdatingComponentsSummary with waiting time information @@ -236,16 +235,16 @@ func (c *Ytsaurus) UpdateOnDeleteComponentsSummary(ctx context.Context, waitingO condition := meta.FindStatusCondition(c.GetResource().Status.UpdateStatus.Conditions, waitingOnDeleteConditionType) if condition != nil && condition.Status == "True" { - componentName := getComponentName(component) + componentName := getLabelerComponentName(component) if includeWaitinDuration { waitingDuration := time.Since(condition.LastTransitionTime.Time) summaryParts = append(summaryParts, fmt.Sprintf("{%s (waiting %s)}", componentName, waitingDuration.Truncate(time.Second).String())) - metrics.ObserveOnDeleteWait(c.GetResource().Name, componentName, &condition.LastTransitionTime) + metrics.ObserveOnDeleteWait(c.GetResource().Name, c.GetResource().Namespace, componentName, &condition.LastTransitionTime) } else { summaryParts = append(summaryParts, fmt.Sprintf("{%s}", componentName)) - metrics.ObserveOnDeleteWait(c.GetResource().Name, componentName, nil) + metrics.ObserveOnDeleteWait(c.GetResource().Name, c.GetResource().Namespace, componentName, nil) } } } diff --git a/pkg/labeller/labeller.go b/pkg/labeller/labeller.go index e6e275166..684d2c471 100644 --- a/pkg/labeller/labeller.go +++ b/pkg/labeller/labeller.go @@ -89,6 +89,10 @@ func (l *Labeller) GetInstanceGroup() string { return l.InstanceGroup } +func (l *Labeller) GetComponentShortName() string { + return l.getName(consts.GetShortName(l.ComponentType), "") +} + func (l *Labeller) GetServerStatefulSetName() string { return l.getName(consts.ComponentStatefulSetPrefix(l.ComponentType), "") } diff --git a/pkg/metrics/update_strategy.go b/pkg/metrics/update_strategy.go index 4a204ed25..2a771dfb4 100644 --- a/pkg/metrics/update_strategy.go +++ b/pkg/metrics/update_strategy.go @@ -9,25 +9,25 @@ import ( ) //nolint:gochecknoglobals // Prometheus metrics are package-level for registration and reuse. -var onDeleteWaitSeconds = prometheus.NewGaugeVec( +var strategyOnDeleteWatingTimeSeconds = prometheus.NewGaugeVec( prometheus.GaugeOpts{ Namespace: "ytop", - Subsystem: "update_strategy", - Name: "on_delete_wait_seconds", + Subsystem: "strategy_on_delete", + Name: "waiting_time_seconds", Help: "Seconds a component has been waiting in OnDelete mode since the mode started.", }, - []string{"cluster", "component"}, + []string{"cluster", "cluster_namespace", "component_name"}, ) func init() { - ctrlmetrics.Registry.MustRegister(onDeleteWaitSeconds) + ctrlmetrics.Registry.MustRegister(strategyOnDeleteWatingTimeSeconds) } -func ObserveOnDeleteWait(cluster, component string, startedAt *metav1.Time) { - lbl := []string{cluster, component} +func ObserveOnDeleteWait(cluster, cluster_namespace, component_name string, startedAt *metav1.Time) { + lbl := []string{cluster, cluster_namespace, component_name} if startedAt == nil || startedAt.IsZero() { - onDeleteWaitSeconds.DeleteLabelValues(lbl...) + strategyOnDeleteWatingTimeSeconds.DeleteLabelValues(lbl...) return } - onDeleteWaitSeconds.WithLabelValues(lbl...).Set(time.Since(startedAt.Time).Seconds()) + strategyOnDeleteWatingTimeSeconds.WithLabelValues(lbl...).Set(time.Since(startedAt.Time).Seconds()) }