From 4cca2a8dc2f99d0c94ad8121600d1c264f1d278b Mon Sep 17 00:00:00 2001 From: Alan Clucas Date: Mon, 19 Aug 2024 10:52:06 +0100 Subject: [PATCH] chore: add new enum for metrics workflow phases Signed-off-by: Alan Clucas --- workflow/controller/operator.go | 9 +++++---- workflow/metrics/counter_template.go | 30 +++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/workflow/controller/operator.go b/workflow/controller/operator.go index 1215d574b3b8..a1cac1564833 100644 --- a/workflow/controller/operator.go +++ b/workflow/controller/operator.go @@ -58,6 +58,7 @@ import ( controllercache "github.com/argoproj/argo-workflows/v3/workflow/controller/cache" "github.com/argoproj/argo-workflows/v3/workflow/controller/estimation" "github.com/argoproj/argo-workflows/v3/workflow/controller/indexes" + "github.com/argoproj/argo-workflows/v3/workflow/metrics" "github.com/argoproj/argo-workflows/v3/workflow/progress" argosync "github.com/argoproj/argo-workflows/v3/workflow/sync" "github.com/argoproj/argo-workflows/v3/workflow/templateresolution" @@ -2347,7 +2348,7 @@ func (woc *wfOperationCtx) recordWorkflowPhaseChange(ctx context.Context) { phase := string(woc.wf.Status.Phase) woc.controller.metrics.ChangeWorkflowPhase(ctx, phase, woc.wf.ObjectMeta.Namespace) if woc.wf.Spec.WorkflowTemplateRef != nil { // not-woc-misuse - woc.controller.metrics.CountWorkflowTemplate(ctx, phase, woc.wf.Spec.WorkflowTemplateRef.Name, woc.wf.ObjectMeta.Namespace, woc.wf.Spec.WorkflowTemplateRef.ClusterScope) // not-woc-misuse + woc.controller.metrics.CountWorkflowTemplate(ctx, metrics.ConvertWorkflowPhase(woc.wf.Status.Phase), woc.wf.Spec.WorkflowTemplateRef.Name, woc.wf.ObjectMeta.Namespace, woc.wf.Spec.WorkflowTemplateRef.ClusterScope) // not-woc-misuse switch woc.wf.Status.Phase { case wfv1.WorkflowSucceeded, wfv1.WorkflowFailed, wfv1.WorkflowError: duration := time.Since(woc.wf.Status.StartedAt.Time) @@ -3872,10 +3873,10 @@ func (woc *wfOperationCtx) fetchWorkflowSpec(ctx context.Context) (wfv1.Workflow woc.log.WithError(err).Error("clusterWorkflowTemplate RBAC is missing") return nil, fmt.Errorf("cannot get resource clusterWorkflowTemplate at cluster scope") } - woc.controller.metrics.CountWorkflowTemplate(ctx, "New", woc.wf.Spec.WorkflowTemplateRef.Name, woc.wf.Namespace, true) // not-woc-misuse - specHolder, err = woc.controller.cwftmplInformer.Lister().Get(woc.wf.Spec.WorkflowTemplateRef.Name) // not-woc-misuse + woc.controller.metrics.CountWorkflowTemplate(ctx, metrics.WorkflowNew, woc.wf.Spec.WorkflowTemplateRef.Name, woc.wf.Namespace, true) // not-woc-misuse + specHolder, err = woc.controller.cwftmplInformer.Lister().Get(woc.wf.Spec.WorkflowTemplateRef.Name) // not-woc-misuse } else { - woc.controller.metrics.CountWorkflowTemplate(ctx, "New", woc.wf.Spec.WorkflowTemplateRef.Name, woc.wf.Namespace, false) // not-woc-misuse + woc.controller.metrics.CountWorkflowTemplate(ctx, metrics.WorkflowNew, woc.wf.Spec.WorkflowTemplateRef.Name, woc.wf.Namespace, false) // not-woc-misuse specHolder, err = woc.controller.wftmplInformer.Lister().WorkflowTemplates(woc.wf.Namespace).Get(woc.wf.Spec.WorkflowTemplateRef.Name) // not-woc-misuse } if err != nil { diff --git a/workflow/metrics/counter_template.go b/workflow/metrics/counter_template.go index 8ffc630ee2d1..a3aee7eb9b67 100644 --- a/workflow/metrics/counter_template.go +++ b/workflow/metrics/counter_template.go @@ -2,12 +2,25 @@ package metrics import ( "context" + + wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1" ) const ( nameWFTemplateTriggered = `workflowtemplate_triggered_total` ) +type MetricWorkflowPhase string + +const ( + WorkflowUnknown MetricWorkflowPhase = MetricWorkflowPhase(wfv1.WorkflowUnknown) + WorkflowPending MetricWorkflowPhase = MetricWorkflowPhase(wfv1.WorkflowPending) + WorkflowSucceeded MetricWorkflowPhase = MetricWorkflowPhase(wfv1.WorkflowSucceeded) + WorkflowFailed MetricWorkflowPhase = MetricWorkflowPhase(wfv1.WorkflowFailed) + WorkflowError MetricWorkflowPhase = MetricWorkflowPhase(wfv1.WorkflowError) + WorkflowNew MetricWorkflowPhase = "New" +) + func addWorkflowTemplateCounter(_ context.Context, m *Metrics) error { return m.createInstrument(int64Counter, nameWFTemplateTriggered, @@ -25,7 +38,22 @@ func templateLabels(name, namespace string, cluster bool) instAttribs { } } -func (m *Metrics) CountWorkflowTemplate(ctx context.Context, phase, name, namespace string, cluster bool) { +func ConvertWorkflowPhase(inPhase wfv1.WorkflowPhase) MetricWorkflowPhase { + switch inPhase { + case wfv1.WorkflowPending: + return WorkflowPending + case wfv1.WorkflowSucceeded: + return WorkflowSucceeded + case wfv1.WorkflowFailed: + return WorkflowFailed + case wfv1.WorkflowError: + return WorkflowError + default: + return WorkflowError + } +} + +func (m *Metrics) CountWorkflowTemplate(ctx context.Context, phase MetricWorkflowPhase, name, namespace string, cluster bool) { labels := templateLabels(name, namespace, cluster) labels = append(labels, instAttrib{name: labelWorkflowPhase, value: phase})