Skip to content

Commit 8e9c523

Browse files
committed
feat(metrics): record sync count for subscriptions, labeled with name
and most recently installed CSV version
1 parent a17cb22 commit 8e9c523

File tree

5 files changed

+19
-34
lines changed

5 files changed

+19
-34
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ build-linux: build_cmd=build
5656
build-linux: arch_flags=GOOS=linux GOARCH=386
5757
build-linux: clean $(CMDS)
5858

59-
$(CMDS): version_flags=-ldflags "-w -X $(PKG)/pkg/version.GitCommit=`git rev-parse --short HEAD` -X $(PKG)/pkg/version.OLMVersion=`cat OLM_VERSION`"
59+
$(CMDS): version_flags=-ldflags "-X $(PKG)/pkg/version.GitCommit=`git rev-parse --short HEAD` -X $(PKG)/pkg/version.OLMVersion=`cat OLM_VERSION`"
6060
$(CMDS):
6161
CGO_ENABLED=0 $(arch_flags) go $(build_cmd) $(MOD_FLAGS) $(version_flags) -o bin/$(shell basename $@) $@
6262

@@ -87,7 +87,7 @@ setup-bare: clean e2e.namespace
8787
. ./scripts/install_bare.sh $(shell cat ./e2e.namespace) test/e2e/resources
8888

8989
e2e:
90-
go test -v -failfast -timeout 70m ./test/e2e/... -namespace=openshift-operators -kubeconfig=${KUBECONFIG} -olmNamespace=openshift-operator-lifecycle-manager
90+
go test -v $(MOD_FLAGS) -failfast -timeout 70m ./test/e2e/... -namespace=openshift-operators -kubeconfig=${KUBECONFIG} -olmNamespace=openshift-operator-lifecycle-manager
9191

9292
e2e-local: build-linux
9393
. ./scripts/build_local.sh

pkg/controller/operators/catalog/subscription/syncer.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
listers "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/listers/operators/v1alpha1"
1616
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/kubestate"
1717
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/ownerutil"
18+
"github.com/operator-framework/operator-lifecycle-manager/pkg/metrics"
1819
)
1920

2021
var scheme = runtime.NewScheme()
@@ -49,6 +50,8 @@ func (s *subscriptionSyncer) Sync(ctx context.Context, event kubestate.ResourceE
4950
return err
5051
}
5152

53+
s.recordMetrics(res)
54+
5255
logger := s.logger.WithFields(logrus.Fields{
5356
"reconciling": fmt.Sprintf("%T", res),
5457
"selflink": res.GetSelfLink(),
@@ -82,6 +85,10 @@ func (s *subscriptionSyncer) Sync(ctx context.Context, event kubestate.ResourceE
8285
return nil
8386
}
8487

88+
func (o *subscriptionSyncer) recordMetrics(sub *v1alpha1.Subscription) {
89+
metrics.CounterForSubscription(sub.GetName(), sub.Status.InstalledCSV).Inc()
90+
}
91+
8592
func (s *subscriptionSyncer) Notify(event kubestate.ResourceEvent) {
8693
s.notify(event)
8794
}

pkg/controller/operators/olm/operator.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -678,8 +678,6 @@ func (a *Operator) syncClusterServiceVersion(obj interface{}) (syncError error)
678678
return
679679
}
680680

681-
a.recordMetrics(logger, clusterServiceVersion)
682-
683681
outCSV, syncError := a.transitionCSVState(*clusterServiceVersion)
684682

685683
if outCSV == nil {
@@ -779,18 +777,6 @@ func (a *Operator) syncGcCsv(obj interface{}) (syncError error) {
779777
return
780778
}
781779

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-
794780
// operatorGroupFromAnnotations returns the OperatorGroup for the CSV only if the CSV is active one in the group
795781
func (a *Operator) operatorGroupFromAnnotations(logger *logrus.Entry, csv *v1alpha1.ClusterServiceVersion) *v1.OperatorGroup {
796782
annotations := csv.GetAnnotations()

pkg/metrics/metrics.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
)
1111

1212
const (
13-
PHASE_LABEL = "phase"
14-
NAME_LABEL = "name"
13+
NAME_LABEL = "name"
14+
INSTALLED_LABEL = "installed"
1515
)
1616

1717
// TODO(alecmerdler): Can we use this to emit Kubernetes events?
@@ -138,30 +138,27 @@ var (
138138
},
139139
)
140140

141-
CSVSyncCount = prometheus.NewCounterVec(
141+
SubscriptionSyncCount = prometheus.NewCounterVec(
142142
prometheus.CounterOpts{
143-
Name: "csv_sync_total",
144-
Help: "Monotonic count of CSV syncs",
143+
Name: "subscription_sync_total",
144+
Help: "Monotonic count of subscription syncs",
145145
},
146-
[]string{PHASE_LABEL, NAME_LABEL},
146+
[]string{NAME_LABEL, INSTALLED_LABEL},
147147
)
148148
)
149149

150150
func RegisterOLM() {
151151
prometheus.MustRegister(csvCount)
152152
prometheus.MustRegister(CSVUpgradeCount)
153-
prometheus.MustRegister(CSVSyncCount)
154153
}
155154

156155
func RegisterCatalog() {
157156
prometheus.MustRegister(installPlanCount)
158157
prometheus.MustRegister(subscriptionCount)
159158
prometheus.MustRegister(catalogSourceCount)
159+
prometheus.MustRegister(SubscriptionSyncCount)
160160
}
161161

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-
})
162+
func CounterForSubscription(name, installedCSV string) prometheus.Counter {
163+
return SubscriptionSyncCount.WithLabelValues(name, installedCSV)
167164
}

test/e2e/metrics_e2e_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"testing"
77

88
log "github.com/sirupsen/logrus"
9-
"github.com/stretchr/testify/require"
109
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1110
"k8s.io/apimachinery/pkg/util/net"
1211

@@ -34,11 +33,7 @@ func TestMetricsEndpoint(t *testing.T) {
3433
if err != nil {
3534
t.Fatalf("Metrics test failed: %v\n", err)
3635
}
37-
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")
36+
log.Info(rawOutput)
4237
}
4338

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

0 commit comments

Comments
 (0)