Skip to content

Commit a2606a1

Browse files
committed
feat(metrics) record CSV sync events
This commit introduces a change so OLM will expose metrics about CSV sync events.
1 parent a611449 commit a2606a1

File tree

3 files changed

+66
-8
lines changed

3 files changed

+66
-8
lines changed

pkg/controller/operators/olm/operator.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,8 @@ func (a *Operator) syncClusterServiceVersion(obj interface{}) (syncError error)
930930
})
931931
logger.Debug("syncing CSV")
932932

933+
metrics.EmitCSVMetric(clusterServiceVersion)
934+
933935
if a.csvNotification != nil {
934936
a.csvNotification.OnAddOrUpdate(clusterServiceVersion)
935937
}

pkg/metrics/metrics.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ import (
77

88
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned"
99
v1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/listers/operators/v1alpha1"
10+
olmv1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
11+
1012
)
1113

1214
const (
1315
NAME_LABEL = "name"
1416
INSTALLED_LABEL = "installed"
17+
VERSION_LABEL = "version"
18+
PHASE_LABEL = "phase"
19+
REASON_LABEL = "reason"
1520
)
1621

1722
// TODO(alecmerdler): Can we use this to emit Kubernetes events?
@@ -145,10 +150,19 @@ var (
145150
},
146151
[]string{NAME_LABEL, INSTALLED_LABEL},
147152
)
153+
154+
csvSyncCounter = prometheus.NewCounterVec(
155+
prometheus.CounterOpts{
156+
Name: "csv_sync_total",
157+
Help: "Monotonic count of CSV syncs",
158+
},
159+
[]string{NAME_LABEL, VERSION_LABEL, PHASE_LABEL, REASON_LABEL},
160+
)
148161
)
149162

150163
func RegisterOLM() {
151164
prometheus.MustRegister(csvCount)
165+
prometheus.MustRegister(csvSyncCounter)
152166
prometheus.MustRegister(CSVUpgradeCount)
153167
}
154168

@@ -162,3 +176,7 @@ func RegisterCatalog() {
162176
func CounterForSubscription(name, installedCSV string) prometheus.Counter {
163177
return SubscriptionSyncCount.WithLabelValues(name, installedCSV)
164178
}
179+
180+
func EmitCSVMetric(csv *olmv1alpha1.ClusterServiceVersion){
181+
csvSyncCounter.WithLabelValues(csv.Name, csv.Spec.Version.String(), string(csv.Status.Phase), string(csv.Status.Reason)).Inc()
182+
}

test/e2e/metrics_e2e_test.go

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,72 @@ import (
66
"testing"
77

88
log "github.com/sirupsen/logrus"
9+
"github.com/stretchr/testify/require"
910
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1011
"k8s.io/apimachinery/pkg/util/net"
1112

13+
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
14+
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/install"
1215
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
1316
)
1417

1518
// TestMetrics tests the metrics endpoint of the OLM pod.
1619
func TestMetricsEndpoint(t *testing.T) {
1720
c := newKubeClient(t)
21+
crc := newCRClient(t)
1822

23+
failingCSV := v1alpha1.ClusterServiceVersion{
24+
TypeMeta: metav1.TypeMeta{
25+
Kind: v1alpha1.ClusterServiceVersionKind,
26+
APIVersion: v1alpha1.ClusterServiceVersionAPIVersion,
27+
},
28+
ObjectMeta: metav1.ObjectMeta{
29+
Name: genName("failing-csv-test-"),
30+
},
31+
Spec: v1alpha1.ClusterServiceVersionSpec{
32+
InstallStrategy: v1alpha1.NamedInstallStrategy{
33+
StrategyName: install.InstallStrategyNameDeployment,
34+
StrategySpecRaw: strategyRaw,
35+
},
36+
},
37+
}
38+
39+
cleanupCSV, err := createCSV(t, c, crc, failingCSV, testNamespace, false, false)
40+
require.NoError(t, err)
41+
defer cleanupCSV()
42+
43+
_, err = fetchCSV(t, crc, failingCSV.Name, testNamespace, csvFailedChecker)
44+
require.NoError(t, err)
45+
46+
rawOutput, err := getMetricsFromPod(t, c, getOLMPodName(t, c), operatorNamespace, "8081")
47+
if err != nil {
48+
t.Fatalf("Metrics test failed: %v\n", err)
49+
}
50+
51+
// Verify metrics have been emitted for packageserver csv
52+
require.Contains(t, rawOutput, "csv_sync_total")
53+
require.Contains(t, rawOutput, "name=\""+failingCSV.Name+"\"")
54+
require.Contains(t, rawOutput, "phase=\"Failed\"")
55+
require.Contains(t, rawOutput, "reason=\"UnsupportedOperatorGroup\"")
56+
require.Contains(t, rawOutput, "version=\"0.0.0\"")
57+
log.Info(rawOutput)
58+
}
59+
60+
func getOLMPodName(t *testing.T, client operatorclient.ClientInterface) string {
1961
listOptions := metav1.ListOptions{LabelSelector: "app=olm-operator"}
20-
podList, err := c.KubernetesInterface().CoreV1().Pods(operatorNamespace).List(listOptions)
62+
podList, err := client.KubernetesInterface().CoreV1().Pods(operatorNamespace).List(listOptions)
2163
if err != nil {
2264
log.Infof("Error %v\n", err)
2365
t.Fatalf("Listing pods failed: %v\n", err)
2466
}
25-
if len(podList.Items) > 1 {
26-
t.Fatalf("Expected only 1 olm-operator pod, got %v", len(podList.Items))
67+
if len(podList.Items) != 1 {
68+
t.Fatalf("Expected 1 olm-operator pod, got %v", len(podList.Items))
2769
}
2870

2971
podName := podList.Items[0].GetName()
3072
log.Infof("Looking at pod %v in namespace %v", podName, operatorNamespace)
73+
return podName
3174

32-
rawOutput, err := getMetricsFromPod(t, c, podName, operatorNamespace, "8081")
33-
if err != nil {
34-
t.Fatalf("Metrics test failed: %v\n", err)
35-
}
36-
log.Info(rawOutput)
3775
}
3876

3977
func getMetricsFromPod(t *testing.T, client operatorclient.ClientInterface, podName string, namespace string, port string) (string, error) {

0 commit comments

Comments
 (0)