Skip to content

Commit

Permalink
feat: new cron workflow trigger counter metric
Browse files Browse the repository at this point in the history
From #12589.

A new metric which counts how many times each cron workflow has
triggered. A simple enough counter which can be checked against
expectations for the cron.

Note to reviewers: this is part of a stack of reviews for metrics
changes. Please don't merge until the rest of the stack is also ready.

Signed-off-by: Alan Clucas <[email protected]>
  • Loading branch information
Joibel committed Jul 1, 2024
1 parent fe4d153 commit dc7479a
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 0 deletions.
9 changes: 9 additions & 0 deletions docs/metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,15 @@ The build information for this workflow controller
| `treestate` | Whether the git tree was `dirty` or `clean` when this was built |
| `tag` | The tag on the git commit or `untagged` if it was not tagged |

#### `cronworkflows_triggered_total`

A counter of the number of times a CronWorkflow has been

| attribute | explanation |
|-------------|-------------------------------------------|
| `name` | ⚠️ The name of the CronWorkflow. |
| `namespace` | The namespace in which the pod is running |

#### `gauge`

A gauge of the number of workflows currently in the cluster in each phase. The `Running` count does not mean that a workflows pods are running, just that the controller has scheduled them. A workflow can be stuck in `Running` with pending pods for a long time.
Expand Down
1 change: 1 addition & 0 deletions docs/upgrading.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ These notes explain the differences in using the Prometheus `/metrics` endpoint
The following are new metrics:

* `controller_build_info`
* `cronworkflows_triggered_total`
* `k8s_request_duration`
* `leader`
* `pods_total_count`
Expand Down
16 changes: 16 additions & 0 deletions test/e2e/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,22 @@ func (s *MetricsSuite) TestPodPendingMetric() {
WaitForWorkflowDeletion()
}

func (s *MetricsSuite) TestCronTriggeredCounter() {
s.Given().
CronWorkflow(`@testdata/cronworkflow-metrics.yaml`).
When().
CreateCronWorkflow().
Wait(1 * time.Minute). // This pattern is used in cron_test.go too
Then().
ExpectCron(func(t *testing.T, cronWf *wfv1.CronWorkflow) {
s.e(s.T()).GET("").
Expect().
Status(200).
Body().
Contains(`cronworkflows_triggered_total{name="test-cron-metric",namespace="argo"} 1`)
})
}

func TestMetricsSuite(t *testing.T) {
suite.Run(t, new(MetricsSuite))
}
19 changes: 19 additions & 0 deletions test/e2e/testdata/cronworkflow-metrics.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: argoproj.io/v1alpha1
kind: CronWorkflow
metadata:
name: test-cron-metric
spec:
schedule: "* * * * *"
concurrencyPolicy: "Allow"
startingDeadlineSeconds: 0
workflowSpec:
metadata:
labels:
workflows.argoproj.io/test: "true"
podGC:
strategy: OnPodCompletion
entrypoint: whalesay
templates:
- name: whalesay
container:
image: argoproj/argosay:v2
1 change: 1 addition & 0 deletions workflow/cron/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func (woc *cronWfOperationCtx) run(ctx context.Context, scheduledRuntime time.Ti
defer woc.persistUpdate(ctx)

woc.log.Infof("Running %s", woc.name)
woc.metrics.CronWfTrigger(ctx, woc.name, woc.cronWf.ObjectMeta.Namespace)

// If the cron workflow has a schedule that was just updated, update its annotation
if woc.cronWf.IsUsingNewSchedule() {
Expand Down
25 changes: 25 additions & 0 deletions workflow/metrics/counter_cronworkflow_trigger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package metrics

import (
"context"
)

const (
nameCronTriggered = `cronworkflows_triggered_total`
)

func addCronWfTriggerCounter(_ context.Context, m *Metrics) error {
return m.createInstrument(int64Counter,
nameCronTriggered,
"Total number of cron workflows triggered",
"{cronworkflow}",
withAsBuiltIn(),
)
}

func (m *Metrics) CronWfTrigger(ctx context.Context, name, namespace string) {
m.addInt(ctx, nameCronTriggered, 1, instAttribs{
{name: labelCronWFName, value: name},
{name: labelWorkflowNamespace, value: namespace},
})
}
2 changes: 2 additions & 0 deletions workflow/metrics/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const (
labelBuildGitTreeState string = `treestate`
labelBuildGitTag string = `tag`

labelCronWFName string = `name`

labelErrorCause string = "cause"

labelLogLevel string = `level`
Expand Down
1 change: 1 addition & 0 deletions workflow/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func New(ctx context.Context, serviceName string, config *Config, callbacks Call
addPodMissingCounter,
addPodPendingCounter,
addWorkflowPhaseGauge,
addCronWfTriggerCounter,
addOperationDurationHistogram,
addErrorCounter,
addLogCounter,
Expand Down

0 comments on commit dc7479a

Please sign in to comment.