diff --git a/pkg/pipelinerunmetrics/metrics.go b/pkg/pipelinerunmetrics/metrics.go index 5ff79206bf4..225c689d746 100644 --- a/pkg/pipelinerunmetrics/metrics.go +++ b/pkg/pipelinerunmetrics/metrics.go @@ -423,6 +423,7 @@ func (r *Recorder) RunningPipelineRuns(lister listers.PipelineRunLister) error { mutators := []tag.Mutator{ tag.Insert(namespaceTag, pr.Namespace), tag.Insert(pipelineTag, pipelineName), + tag.Insert(pipelinerunTag, pr.Name), } if r.cfg != nil { switch r.cfg.RunningPipelinerunLevel { diff --git a/pkg/pipelinerunmetrics/metrics_test.go b/pkg/pipelinerunmetrics/metrics_test.go index 9cefb031001..da9a98f114a 100644 --- a/pkg/pipelinerunmetrics/metrics_test.go +++ b/pkg/pipelinerunmetrics/metrics_test.go @@ -523,6 +523,59 @@ func TestRecordRunningPipelineRunsCount(t *testing.T) { metricstest.CheckLastValueData(t, "running_pipelineruns", map[string]string{}, 1) } +func TestRecordRunningPipelineRunsCountAtPipelineRunLevel(t *testing.T) { + unregisterMetrics() + + newPipelineRun := func(status corev1.ConditionStatus, pipelineRun, namespace string) *v1.PipelineRun { + return &v1.PipelineRun{ + ObjectMeta: metav1.ObjectMeta{Name: pipelineRun, Namespace: namespace}, + Status: v1.PipelineRunStatus{ + Status: duckv1.Status{ + Conditions: duckv1.Conditions{{ + Type: apis.ConditionSucceeded, + Status: status, + }}, + }, + }, + } + } + + ctx, _ := ttesting.SetupFakeContext(t) + informer := fakepipelineruninformer.Get(ctx) + // Add N randomly-named PipelineRuns with differently-succeeded statuses. + for _, pipelineRun := range []*v1.PipelineRun{ + newPipelineRun(corev1.ConditionUnknown, "testpr1", "testns1"), + newPipelineRun(corev1.ConditionUnknown, "testpr1", "testns2"), + newPipelineRun(corev1.ConditionUnknown, "testpr2", "testns2"), + newPipelineRun(corev1.ConditionUnknown, "testpr1", "testns3"), + newPipelineRun(corev1.ConditionUnknown, "testpr2", "testns3"), + newPipelineRun(corev1.ConditionUnknown, "testpr3", "testns3"), + newPipelineRun(corev1.ConditionUnknown, "testpr4", "testns3"), + } { + if err := informer.Informer().GetIndexer().Add(pipelineRun); err != nil { + t.Fatalf("Adding TaskRun to informer: %v", err) + } + } + + ctx = getConfigContextRunningPRLevel("pipelinerun") + recorder, err := NewRecorder(ctx) + if err != nil { + t.Fatalf("NewRecorder: %v", err) + } + + if err := recorder.RunningPipelineRuns(informer.Lister()); err != nil { + t.Errorf("RunningPipelineRuns: %v", err) + } + + checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns1", "pipeline": "anonymous", "pipelinerun": "testpr1"}, 1) + checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns2", "pipeline": "anonymous", "pipelinerun": "testpr1"}, 1) + checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns2", "pipeline": "anonymous", "pipelinerun": "testpr2"}, 1) + checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns3", "pipeline": "anonymous", "pipelinerun": "testpr1"}, 1) + checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns3", "pipeline": "anonymous", "pipelinerun": "testpr2"}, 1) + checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns3", "pipeline": "anonymous", "pipelinerun": "testpr3"}, 1) + checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns3", "pipeline": "anonymous", "pipelinerun": "testpr4"}, 1) +} + func TestRecordRunningPipelineRunsCountAtPipelineLevel(t *testing.T) { unregisterMetrics() @@ -572,6 +625,103 @@ func TestRecordRunningPipelineRunsCountAtPipelineLevel(t *testing.T) { checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns3", "pipeline": "anonymous"}, 4) } +func TestRecordRunningPipelineRunsCountAtNamespaceLevel(t *testing.T) { + unregisterMetrics() + + newPipelineRun := func(status corev1.ConditionStatus, namespace string) *v1.PipelineRun { + return &v1.PipelineRun{ + ObjectMeta: metav1.ObjectMeta{Name: names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pipelinerun-"), Namespace: namespace}, + Status: v1.PipelineRunStatus{ + Status: duckv1.Status{ + Conditions: duckv1.Conditions{{ + Type: apis.ConditionSucceeded, + Status: status, + }}, + }, + }, + } + } + + ctx, _ := ttesting.SetupFakeContext(t) + informer := fakepipelineruninformer.Get(ctx) + // Add N randomly-named PipelineRuns with differently-succeeded statuses. + for _, pipelineRun := range []*v1.PipelineRun{ + newPipelineRun(corev1.ConditionUnknown, "testns1"), + newPipelineRun(corev1.ConditionUnknown, "testns2"), + newPipelineRun(corev1.ConditionUnknown, "testns2"), + newPipelineRun(corev1.ConditionUnknown, "testns3"), + newPipelineRun(corev1.ConditionUnknown, "testns3"), + newPipelineRun(corev1.ConditionUnknown, "testns3"), + newPipelineRun(corev1.ConditionUnknown, "testns3"), + } { + if err := informer.Informer().GetIndexer().Add(pipelineRun); err != nil { + t.Fatalf("Adding TaskRun to informer: %v", err) + } + } + + ctx = getConfigContextRunningPRLevel("namespace") + recorder, err := NewRecorder(ctx) + if err != nil { + t.Fatalf("NewRecorder: %v", err) + } + + if err := recorder.RunningPipelineRuns(informer.Lister()); err != nil { + t.Errorf("RunningPipelineRuns: %v", err) + } + + checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns1"}, 1) + checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns2"}, 2) + checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns3"}, 4) +} + +func TestRecordRunningPipelineRunsCountAtClusterLevel(t *testing.T) { + unregisterMetrics() + + newPipelineRun := func(status corev1.ConditionStatus, namespace string) *v1.PipelineRun { + return &v1.PipelineRun{ + ObjectMeta: metav1.ObjectMeta{Name: names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pipelinerun-"), Namespace: namespace}, + Status: v1.PipelineRunStatus{ + Status: duckv1.Status{ + Conditions: duckv1.Conditions{{ + Type: apis.ConditionSucceeded, + Status: status, + }}, + }, + }, + } + } + + ctx, _ := ttesting.SetupFakeContext(t) + informer := fakepipelineruninformer.Get(ctx) + // Add N randomly-named PipelineRuns with differently-succeeded statuses. + for _, pipelineRun := range []*v1.PipelineRun{ + newPipelineRun(corev1.ConditionUnknown, "testns1"), + newPipelineRun(corev1.ConditionUnknown, "testns2"), + newPipelineRun(corev1.ConditionUnknown, "testns2"), + newPipelineRun(corev1.ConditionUnknown, "testns3"), + newPipelineRun(corev1.ConditionUnknown, "testns3"), + newPipelineRun(corev1.ConditionUnknown, "testns3"), + newPipelineRun(corev1.ConditionUnknown, "testns3"), + } { + if err := informer.Informer().GetIndexer().Add(pipelineRun); err != nil { + t.Fatalf("Adding TaskRun to informer: %v", err) + } + } + + ctx = getConfigContextRunningPRLevel("") + recorder, err := NewRecorder(ctx) + if err != nil { + t.Fatalf("NewRecorder: %v", err) + } + + if err := recorder.RunningPipelineRuns(informer.Lister()); err != nil { + t.Errorf("RunningPipelineRuns: %v", err) + } + + checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{}, 7) + +} + func TestRecordRunningPipelineRunsResolutionWaitCounts(t *testing.T) { multiplier := 3 for _, tc := range []struct {