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

Add template invalidity metric #1242

Merged
merged 3 commits into from
Mar 24, 2025
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
8 changes: 4 additions & 4 deletions internal/controller/clusterdeployment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,10 +591,10 @@ func (r *ClusterDeploymentReconciler) updateServices(ctx context.Context, cd *kc
return ctrl.Result{}, fmt.Errorf("failed to reconcile Profile: %w", err)
}

metrics.TrackMetricTemplateUsageSet(ctx, kcm.ClusterTemplateKind, cd.Spec.Template, kcm.ClusterDeploymentKind, cd.ObjectMeta)
metrics.TrackMetricTemplateUsage(ctx, kcm.ClusterTemplateKind, cd.Spec.Template, kcm.ClusterDeploymentKind, cd.ObjectMeta, true)

for _, svc := range cd.Spec.ServiceSpec.Services {
metrics.TrackMetricTemplateUsageSet(ctx, kcm.ServiceTemplateKind, svc.Template, kcm.ClusterDeploymentKind, cd.ObjectMeta)
metrics.TrackMetricTemplateUsage(ctx, kcm.ServiceTemplateKind, svc.Template, kcm.ClusterDeploymentKind, cd.ObjectMeta, true)
}

// NOTE:
Expand Down Expand Up @@ -659,10 +659,10 @@ func (r *ClusterDeploymentReconciler) Delete(ctx context.Context, cd *kcm.Cluste

defer func() {
if err == nil {
metrics.TrackMetricTemplateUsageDelete(ctx, kcm.ClusterTemplateKind, cd.Spec.Template, kcm.ClusterDeploymentKind, cd.ObjectMeta)
metrics.TrackMetricTemplateUsage(ctx, kcm.ClusterTemplateKind, cd.Spec.Template, kcm.ClusterDeploymentKind, cd.ObjectMeta, false)

for _, svc := range cd.Spec.ServiceSpec.Services {
metrics.TrackMetricTemplateUsageDelete(ctx, kcm.ServiceTemplateKind, svc.Template, kcm.ClusterDeploymentKind, cd.ObjectMeta)
metrics.TrackMetricTemplateUsage(ctx, kcm.ServiceTemplateKind, svc.Template, kcm.ClusterDeploymentKind, cd.ObjectMeta, false)
}
}
}()
Expand Down
4 changes: 2 additions & 2 deletions internal/controller/multiclusterservice_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (r *MultiClusterServiceReconciler) reconcileUpdate(ctx context.Context, mcs
}

for _, svc := range mcs.Spec.ServiceSpec.Services {
metrics.TrackMetricTemplateUsageSet(ctx, kcm.ServiceTemplateKind, svc.Template, kcm.MultiClusterServiceKind, mcs.ObjectMeta)
metrics.TrackMetricTemplateUsage(ctx, kcm.ServiceTemplateKind, svc.Template, kcm.MultiClusterServiceKind, mcs.ObjectMeta, true)
}

// NOTE:
Expand Down Expand Up @@ -383,7 +383,7 @@ func (r *MultiClusterServiceReconciler) reconcileDelete(ctx context.Context, mcs
defer func() {
if err == nil {
for _, svc := range mcs.Spec.ServiceSpec.Services {
metrics.TrackMetricTemplateUsageDelete(ctx, kcm.ServiceTemplateKind, svc.Template, kcm.MultiClusterServiceKind, mcs.ObjectMeta)
metrics.TrackMetricTemplateUsage(ctx, kcm.ServiceTemplateKind, svc.Template, kcm.MultiClusterServiceKind, mcs.ObjectMeta, false)
}
}
}()
Expand Down
4 changes: 4 additions & 0 deletions internal/controller/template_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (

kcm "github.com/K0rdent/kcm/api/v1alpha1"
"github.com/K0rdent/kcm/internal/helm"
"github.com/K0rdent/kcm/internal/metrics"
"github.com/K0rdent/kcm/internal/utils"
"github.com/K0rdent/kcm/internal/utils/ratelimit"
)
Expand Down Expand Up @@ -355,6 +356,9 @@ func (r *TemplateReconciler) updateStatus(ctx context.Context, template template
if err != nil {
return fmt.Errorf("failed to update status for template %s/%s: %w", template.GetNamespace(), template.GetName(), err)
}

metrics.TrackMetricTemplateInvalidity(ctx, template.GetObjectKind().GroupVersionKind().Kind, template.GetNamespace(), template.GetName(), status.Valid)

return nil
}

Expand Down
61 changes: 41 additions & 20 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ import (
)

const (
metricLabelTemplateKind = "template_kind"
metricLabelTemplateName = "template_name"
metricLabelParentKind = "parent_kind"
metricLabelParentNamespace = "parent_namespace"
metricLabelParentName = "parent_name"
metricLabelTemplateKind = "template_kind"
metricLabelTemplateNamespace = "template_namespace"
metricLabelTemplateName = "template_name"
metricLabelParentKind = "parent_kind"
metricLabelParentNamespace = "parent_namespace"
metricLabelParentName = "parent_name"
)

var metricTemplateUsage = prometheus.NewGaugeVec(
Expand All @@ -42,44 +43,64 @@ var metricTemplateUsage = prometheus.NewGaugeVec(
[]string{metricLabelTemplateKind, metricLabelTemplateName, metricLabelParentKind, metricLabelParentNamespace, metricLabelParentName},
)

var metricTemplateInvalidity = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: kcm.CoreKCMName,
Name: "template_invalidity",
Help: "Number of invalid templates",
},
[]string{metricLabelTemplateKind, metricLabelTemplateNamespace, metricLabelTemplateName},
)

func init() {
metrics.Registry.MustRegister(
metricTemplateUsage,
metricTemplateInvalidity,
)
}

func TrackMetricTemplateUsageSet(ctx context.Context, templateKind, templateName, parentKind string, parent metav1.ObjectMeta) {
//nolint:revive // false-positive
func TrackMetricTemplateUsage(ctx context.Context, templateKind, templateName, parentKind string, parent metav1.ObjectMeta, inUse bool) {
var value float64
if inUse {
value = 1
}

metricTemplateUsage.With(prometheus.Labels{
metricLabelTemplateKind: templateKind,
metricLabelTemplateName: templateName,
metricLabelParentKind: parentKind,
metricLabelParentNamespace: parent.Namespace,
metricLabelParentName: parent.Name,
}).Set(1)
}).Set(value)

ctrl.LoggerFrom(ctx).V(1).Info("Tracking template usage metric (set to 1)",
ctrl.LoggerFrom(ctx).V(1).Info("Tracking template usage metric",
metricLabelTemplateKind, templateKind,
metricLabelTemplateName, templateName,
metricLabelParentKind, parentKind,
metricLabelParentNamespace, parent.Namespace,
metricLabelParentName, parent.Name,
"value", value,
)
}

func TrackMetricTemplateUsageDelete(ctx context.Context, templateKind, templateName, parentKind string, parent metav1.ObjectMeta) {
metricTemplateUsage.Delete(prometheus.Labels{
metricLabelTemplateKind: templateKind,
metricLabelTemplateName: templateName,
metricLabelParentKind: parentKind,
metricLabelParentNamespace: parent.Namespace,
metricLabelParentName: parent.Name,
})
//nolint:revive // false-positive
func TrackMetricTemplateInvalidity(ctx context.Context, templateKind, templateNamespace, templateName string, valid bool) {
var value float64
if !valid {
value = 1
}

metricTemplateInvalidity.With(prometheus.Labels{
metricLabelTemplateKind: templateKind,
metricLabelTemplateNamespace: templateNamespace,
metricLabelTemplateName: templateName,
}).Set(value)

ctrl.LoggerFrom(ctx).V(1).Info("Tracking template usage metric (delete)",
ctrl.LoggerFrom(ctx).V(1).Info("Tracking template invalidity metric",
metricLabelTemplateKind, templateKind,
metricLabelTemplateNamespace, templateNamespace,
metricLabelTemplateName, templateName,
metricLabelParentKind, parentKind,
metricLabelParentNamespace, parent.Namespace,
metricLabelParentName, parent.Name,
"value", value,
)
}