Skip to content

Commit a17cb22

Browse files
committed
feat(metrics): record sync count for CSVs, labeled with name and phase
1 parent af9b90d commit a17cb22

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

pkg/controller/operators/olm/operator.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
"strings"
88
"time"
99

10-
log "github.com/sirupsen/logrus"
1110
v1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1"
1211
"github.com/sirupsen/logrus"
12+
log "github.com/sirupsen/logrus"
1313
corev1 "k8s.io/api/core/v1"
1414
extinf "k8s.io/apiextensions-apiserver/pkg/client/informers/externalversions"
1515
k8serrors "k8s.io/apimachinery/pkg/api/errors"
@@ -678,6 +678,8 @@ func (a *Operator) syncClusterServiceVersion(obj interface{}) (syncError error)
678678
return
679679
}
680680

681+
a.recordMetrics(logger, clusterServiceVersion)
682+
681683
outCSV, syncError := a.transitionCSVState(*clusterServiceVersion)
682684

683685
if outCSV == nil {
@@ -777,6 +779,18 @@ func (a *Operator) syncGcCsv(obj interface{}) (syncError error) {
777779
return
778780
}
779781

782+
func (a *Operator) recordMetrics(logger *logrus.Entry, csv *v1alpha1.ClusterServiceVersion) {
783+
if csv.IsCopied() {
784+
// don't record for copied CSVs
785+
return
786+
}
787+
counter, err := metrics.CounterForCSV(csv.GetName(), string(csv.Status.Phase))
788+
if err != nil {
789+
logger.WithError(err).Warn("could not record metrics")
790+
}
791+
counter.Inc()
792+
}
793+
780794
// operatorGroupFromAnnotations returns the OperatorGroup for the CSV only if the CSV is active one in the group
781795
func (a *Operator) operatorGroupFromAnnotations(logger *logrus.Entry, csv *v1alpha1.ClusterServiceVersion) *v1.OperatorGroup {
782796
annotations := csv.GetAnnotations()
@@ -1426,7 +1440,7 @@ func (a *Operator) apiServiceOwnerConflicts(csv *v1alpha1.ClusterServiceVersion)
14261440
continue
14271441
}
14281442

1429-
adoptable, err := a.isAPIServiceAdoptable(csv, apiService)
1443+
adoptable, err := a.isAPIServiceAdoptable(csv, apiService)
14301444
if err != nil {
14311445
a.logger.WithFields(log.Fields{"obj": "apiService", "labels": apiService.GetLabels()}).Errorf("adoption check failed - %v", err)
14321446
}

pkg/metrics/metrics.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import (
99
v1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/listers/operators/v1alpha1"
1010
)
1111

12+
const (
13+
PHASE_LABEL = "phase"
14+
NAME_LABEL = "name"
15+
)
16+
1217
// TODO(alecmerdler): Can we use this to emit Kubernetes events?
1318
type MetricsProvider interface {
1419
HandleMetrics() error
@@ -132,15 +137,31 @@ var (
132137
Help: "Monotonic count of CSV upgrades",
133138
},
134139
)
140+
141+
CSVSyncCount = prometheus.NewCounterVec(
142+
prometheus.CounterOpts{
143+
Name: "csv_sync_total",
144+
Help: "Monotonic count of CSV syncs",
145+
},
146+
[]string{PHASE_LABEL, NAME_LABEL},
147+
)
135148
)
136149

137150
func RegisterOLM() {
138151
prometheus.MustRegister(csvCount)
139152
prometheus.MustRegister(CSVUpgradeCount)
153+
prometheus.MustRegister(CSVSyncCount)
140154
}
141155

142156
func RegisterCatalog() {
143157
prometheus.MustRegister(installPlanCount)
144158
prometheus.MustRegister(subscriptionCount)
145159
prometheus.MustRegister(catalogSourceCount)
146160
}
161+
162+
func CounterForCSV(name, phase string) (prometheus.Counter, error) {
163+
return CSVSyncCount.GetMetricWith(map[string]string{
164+
PHASE_LABEL: phase,
165+
NAME_LABEL: name,
166+
})
167+
}

test/e2e/metrics_e2e_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ package e2e
55
import (
66
"testing"
77

8-
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
98
log "github.com/sirupsen/logrus"
9+
"github.com/stretchr/testify/require"
1010
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1111
"k8s.io/apimachinery/pkg/util/net"
12+
13+
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
1214
)
1315

1416
// TestMetrics tests the metrics endpoint of the OLM pod.
@@ -33,7 +35,10 @@ func TestMetricsEndpoint(t *testing.T) {
3335
t.Fatalf("Metrics test failed: %v\n", err)
3436
}
3537

36-
log.Debugf("Metrics:\n%v", rawOutput)
38+
// Verify metrics have been emitted for packageserver csv
39+
require.Contains(t, rawOutput, "csv_sync_total counter")
40+
require.Contains(t, rawOutput, "phase=\"Succeeded\"")
41+
require.Contains(t, rawOutput, "packageserver")
3742
}
3843

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

0 commit comments

Comments
 (0)