Skip to content

Commit

Permalink
chore: add new enum for metrics workflow phases
Browse files Browse the repository at this point in the history
Signed-off-by: Alan Clucas <[email protected]>
  • Loading branch information
Joibel committed Aug 19, 2024
1 parent d8f6580 commit 15fabdb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
9 changes: 5 additions & 4 deletions workflow/controller/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -2350,7 +2351,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)
Expand Down Expand Up @@ -3875,10 +3876,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 {
Expand Down
30 changes: 29 additions & 1 deletion workflow/metrics/counter_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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})

Expand Down

0 comments on commit 15fabdb

Please sign in to comment.