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

fix: Metric emission with retryStrategy #3470

Merged
merged 4 commits into from
Jul 14, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 17 additions & 7 deletions workflow/controller/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1485,6 +1485,16 @@ func (woc *wfOperationCtx) executeTemplate(nodeName string, orgTmpl wfv1.Templat
retryParentNode = processedRetryParentNode
// The retry node might have completed by now.
if retryParentNode.Fulfilled() {
if resolvedTmpl.Metrics != nil {
// In this check, a completed node may or may not have existed prior to this execution. If it did exist, ensure that it wasn't
// completed before this execution. If it did not exist prior, then we can infer that it was completed during this execution.
// The statement "(!ok || !prevNodeStatus.Fulfilled())" checks for this behavior and represents the material conditional
// "ok -> !prevNodeStatus.Fulfilled()" (https://en.wikipedia.org/wiki/Material_conditional)
if prevNodeStatus, ok := woc.preExecutionNodePhases[retryParentNode.ID]; (!ok || !prevNodeStatus.Fulfilled()) && retryParentNode.Fulfilled() {
localScope, realTimeScope := woc.prepareMetricScope(node)
woc.computeMetrics(resolvedTmpl.Metrics.Prometheus, localScope, realTimeScope, false)
}
}
return retryParentNode, nil
}
lastChildNode := getChildNodeIndex(retryParentNode, woc.wf.Status.Nodes, -1)
Expand Down Expand Up @@ -1561,9 +1571,9 @@ func (woc *wfOperationCtx) executeTemplate(nodeName string, orgTmpl wfv1.Templat
//
// In this check, a completed node may or may not have existed prior to this execution. If it did exist, ensure that it wasn't
// completed before this execution. If it did not exist prior, then we can infer that it was completed during this execution.
// The statement "(!ok || prevNodeStatus.Completed())" checks for this behavior and represents the material conditional
// "ok -> prevNodeStatus.Completed()" (https://en.wikipedia.org/wiki/Material_conditional)
if prevNodeStatus, ok := woc.preExecutionNodePhases[node.ID]; (!ok || prevNodeStatus.Fulfilled()) && node.Fulfilled() {
// The statement "(!ok || !prevNodeStatus.Fulfilled())" checks for this behavior and represents the material conditional
// "ok -> !prevNodeStatus.Fulfilled()" (https://en.wikipedia.org/wiki/Material_conditional)
if prevNodeStatus, ok := woc.preExecutionNodePhases[node.ID]; (!ok || !prevNodeStatus.Fulfilled()) && node.Fulfilled() {
localScope, realTimeScope := woc.prepareMetricScope(node)
woc.computeMetrics(resolvedTmpl.Metrics.Prometheus, localScope, realTimeScope, false)
}
Expand Down Expand Up @@ -1897,7 +1907,7 @@ func (woc *wfOperationCtx) executeContainer(nodeName string, templateScope strin
})

if err != nil {
return woc.checkForbiddenErrorAndResbmitAllowed(err, node.Name, tmpl)
return woc.checkForbiddenErrorAndResubmitAllowed(err, node.Name, tmpl)
}

return node, err
Expand Down Expand Up @@ -2036,12 +2046,12 @@ func (woc *wfOperationCtx) executeScript(nodeName string, templateScope string,
executionDeadline: opts.executionDeadline,
})
if err != nil {
return woc.checkForbiddenErrorAndResbmitAllowed(err, node.Name, tmpl)
return woc.checkForbiddenErrorAndResubmitAllowed(err, node.Name, tmpl)
}
return node, err
}

func (woc *wfOperationCtx) checkForbiddenErrorAndResbmitAllowed(err error, nodeName string, tmpl *wfv1.Template) (*wfv1.NodeStatus, error) {
func (woc *wfOperationCtx) checkForbiddenErrorAndResubmitAllowed(err error, nodeName string, tmpl *wfv1.Template) (*wfv1.NodeStatus, error) {
if apierr.IsForbidden(err) && isResubmitAllowed(tmpl) {
// Our error was most likely caused by a lack of resources. If pod resubmission is allowed, keep the node pending
woc.requeue(0)
Expand Down Expand Up @@ -2312,7 +2322,7 @@ func (woc *wfOperationCtx) executeResource(nodeName string, templateScope string
mainCtr.Command = []string{"argoexec", "resource", tmpl.Resource.Action}
_, err = woc.createWorkflowPod(nodeName, *mainCtr, tmpl, &createWorkflowPodOpts{onExitPod: opts.onExitTemplate, executionDeadline: opts.executionDeadline})
if err != nil {
return woc.checkForbiddenErrorAndResbmitAllowed(err, node.Name, tmpl)
return woc.checkForbiddenErrorAndResubmitAllowed(err, node.Name, tmpl)
}

return node, err
Expand Down
96 changes: 96 additions & 0 deletions workflow/controller/operator_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"testing"

"github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"

"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -215,3 +217,97 @@ func TestMetricEmissionSameOperationCreationAndFailure(t *testing.T) {
assert.NoError(t, err)
assert.Contains(t, metricErrorCounterString, `counter:<value:1 > `)
}

var testRetryStrategyMetric = `
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
name: workflow-template-whalesay-9pk8f
spec:
arguments: {}
entrypoint: whalesay
templates:
- arguments: {}
inputs: {}
metadata: {}
metrics:
prometheus:
- counter:
value: "1"
help: number of times the outer workflow was invoked
name: workflow_counter
name: whalesay
outputs: {}
steps:
- - arguments:
parameters:
- name: message
value: hello world
name: call-whalesay-template
template: whalesay-template
- arguments: {}
container:
args:
- '{{inputs.parameters.message}}'
command:
- cowsay
image: docker/whalesay
name: ""
resources: {}
inputs:
parameters:
- name: message
metadata: {}
metrics:
prometheus:
- counter:
value: "1"
help: number of times the template was executed
name: template_counter
name: whalesay-template
outputs: {}
retryStrategy:
limit: 2
status:
phase: Running
startedAt: "2020-07-14T16:26:21Z"
`

func TestRetryStrategyMetric(t *testing.T) {
cancel, controller := newController()
defer cancel()
wfcset := controller.wfclientset.ArgoprojV1alpha1().Workflows("")
wf := unmarshalWF(testRetryStrategyMetric)
_, err := wfcset.Create(wf)
assert.NoError(t, err)
woc := newWorkflowOperationCtx(wf, controller)

woc.operate()

// Ensure no metrics have been emitted yet
metricErrorDesc := wf.Spec.Templates[0].Metrics.Prometheus[0].GetDesc()
assert.Nil(t, controller.metrics.GetCustomMetric(metricErrorDesc))
metricErrorDesc = wf.Spec.Templates[1].Metrics.Prometheus[0].GetDesc()
assert.Nil(t, controller.metrics.GetCustomMetric(metricErrorDesc))

// Simulate pod succeeded
podNode := woc.wf.Status.Nodes["workflow-template-whalesay-9pk8f-1966833540"]
podNode.Phase = v1alpha1.NodeSucceeded
woc.wf.Status.Nodes["workflow-template-whalesay-9pk8f-1966833540"] = podNode
woc.operate()

metricErrorDesc = wf.Spec.Templates[0].Metrics.Prometheus[0].GetDesc()
assert.NotNil(t, controller.metrics.GetCustomMetric(metricErrorDesc))
metricErrorCounter := controller.metrics.GetCustomMetric(metricErrorDesc).(prometheus.Counter)
metricErrorCounterString, err := getMetricStringValue(metricErrorCounter)
assert.NoError(t, err)
assert.Contains(t, metricErrorCounterString, `counter:<value:1 > `)

metricErrorDesc = wf.Spec.Templates[1].Metrics.Prometheus[0].GetDesc()
assert.NotNil(t, controller.metrics.GetCustomMetric(metricErrorDesc))
metricErrorCounter = controller.metrics.GetCustomMetric(metricErrorDesc).(prometheus.Counter)
metricErrorCounterString, err = getMetricStringValue(metricErrorCounter)
assert.NoError(t, err)
assert.Contains(t, metricErrorCounterString, `counter:<value:1 > `)

}
4 changes: 2 additions & 2 deletions workflow/controller/operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3965,13 +3965,13 @@ func TestCheckForbiddenErrorAndResbmitAllowed(t *testing.T) {
forbiddenErr := apierr.NewForbidden(schema.GroupResource{Group: "test", Resource: "test1"}, "test", nil)
nonForbiddenErr := apierr.NewBadRequest("badrequest")
t.Run("ForbiddenError", func(t *testing.T) {
node, err := woc.checkForbiddenErrorAndResbmitAllowed(forbiddenErr, "resubmit-pending-wf", &wf.Spec.Templates[0])
node, err := woc.checkForbiddenErrorAndResubmitAllowed(forbiddenErr, "resubmit-pending-wf", &wf.Spec.Templates[0])
assert.NotNil(t, node)
assert.NoError(t, err)
assert.Equal(t, wfv1.NodePending, node.Phase)
})
t.Run("NonForbiddenError", func(t *testing.T) {
node, err := woc.checkForbiddenErrorAndResbmitAllowed(nonForbiddenErr, "resubmit-pending-wf", &wf.Spec.Templates[0])
node, err := woc.checkForbiddenErrorAndResubmitAllowed(nonForbiddenErr, "resubmit-pending-wf", &wf.Spec.Templates[0])
assert.Error(t, err)
assert.Nil(t, node)
})
Expand Down