Skip to content

Commit e734678

Browse files
committed
WIP: add reconcile metrics
Signed-off-by: Alexander Greene <[email protected]>
1 parent 58c8485 commit e734678

7 files changed

+89
-1
lines changed

Makefile

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ SHELL := /bin/bash
1212
ORG := github.com/operator-framework
1313
PKG := $(ORG)/operator-lifecycle-manager
1414
MOD_FLAGS := $(shell (go version | grep -q -E "1\.1[1-9]") && echo -mod=vendor)
15+
BUILD_TAGS := "json1"
1516
CMDS := $(shell go list $(MOD_FLAGS) ./cmd/...)
1617
TCMDS := $(shell go list $(MOD_FLAGS) ./test/e2e/...)
1718
MOCKGEN := ./scripts/update_mockgen.sh
@@ -84,6 +85,7 @@ build-coverage: clean $(CMDS)
8485

8586
build-linux: build_cmd=build
8687
build-linux: arch_flags=GOOS=linux GOARCH=386
88+
build-linux: BUILD_TAGS="json1 experimental_metrics"
8789
build-linux: clean $(CMDS)
8890

8991
build-wait: clean bin/wait
@@ -101,7 +103,7 @@ bin/cpb: FORCE
101103

102104
$(CMDS): version_flags=-ldflags "-X $(PKG)/pkg/version.GitCommit=$(GIT_COMMIT) -X $(PKG)/pkg/version.OLMVersion=`cat OLM_VERSION`"
103105
$(CMDS):
104-
$(arch_flags) go $(build_cmd) $(MOD_FLAGS) $(version_flags) -tags "json1" -o bin/$(shell basename $@) $@
106+
$(arch_flags) go $(build_cmd) $(MOD_FLAGS) $(version_flags) -tags $(BUILD_TAGS) -o bin/$(shell basename $@) $@
105107

106108
build: clean $(CMDS)
107109

pkg/controller/operators/adoption_controller.go

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
operatorsv2 "github.com/operator-framework/api/pkg/operators/v2"
3131
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/decorators"
3232
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/ownerutil"
33+
"github.com/operator-framework/operator-lifecycle-manager/pkg/metrics"
3334
)
3435

3536
// AdoptionReconciler automagically associates Operator components with their respective operator resource.
@@ -114,6 +115,7 @@ func (r *AdoptionReconciler) ReconcileSubscription(ctx context.Context, req ctrl
114115
// Set up a convenient log object so we don't have to type request over and over again
115116
log := r.log.WithValues("request", req)
116117
log.V(1).Info("reconciling subscription")
118+
metrics.EmitAdoptionSubscriptionReconcile()
117119

118120
// Fetch the Subscription from the cache
119121
in := &operatorsv1alpha1.Subscription{}
@@ -177,6 +179,8 @@ func (r *AdoptionReconciler) ReconcileClusterServiceVersion(ctx context.Context,
177179
log := r.log.WithValues("request", req)
178180
log.V(1).Info("reconciling csv")
179181

182+
metrics.EmitAdoptionCSVReconcile()
183+
180184
// Fetch the CSV from the cache
181185
in := &operatorsv1alpha1.ClusterServiceVersion{}
182186
if err := r.Get(ctx, req.NamespacedName, in); err != nil {

pkg/controller/operators/operator_controller.go

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
2929
operatorsv2 "github.com/operator-framework/api/pkg/operators/v2"
3030
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/decorators"
31+
"github.com/operator-framework/operator-lifecycle-manager/pkg/metrics"
3132
)
3233

3334
var (
@@ -115,6 +116,7 @@ func NewOperatorReconciler(cli client.Client, log logr.Logger, scheme *runtime.S
115116
var _ reconcile.Reconciler = &OperatorReconciler{}
116117

117118
func (r *OperatorReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
119+
metrics.EmitOperatorReconcile()
118120
// Set up a convenient log object so we don't have to type request over and over again
119121
log := r.log.WithValues("request", req)
120122
log.V(1).Info("reconciling operator")

pkg/controller/operators/operatorcondition_controller.go

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
2323
operatorsv2 "github.com/operator-framework/api/pkg/operators/v2"
2424
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/ownerutil"
25+
"github.com/operator-framework/operator-lifecycle-manager/pkg/metrics"
2526
)
2627

2728
const (
@@ -91,6 +92,7 @@ func NewOperatorConditionReconciler(cli client.Client, log logr.Logger, scheme *
9192
var _ reconcile.Reconciler = &OperatorConditionReconciler{}
9293

9394
func (r *OperatorConditionReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
95+
metrics.EmitOperatorConditionReconcile()
9496
// Set up a convenient log object so we don't have to type request over and over again
9597
log := r.log.WithValues("request", req)
9698
log.V(2).Info("reconciling operatorcondition")

pkg/controller/operators/operatorconditiongenerator_controller.go

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
2121
operatorsv2 "github.com/operator-framework/api/pkg/operators/v2"
2222
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/ownerutil"
23+
"github.com/operator-framework/operator-lifecycle-manager/pkg/metrics"
2324
)
2425

2526
// OperatorConditionGeneratorReconciler reconciles a ClusterServiceVersion object and creates an OperatorCondition.
@@ -88,6 +89,7 @@ func NewOperatorConditionGeneratorReconciler(cli client.Client, log logr.Logger,
8889
var _ reconcile.Reconciler = &OperatorConditionGeneratorReconciler{}
8990

9091
func (r *OperatorConditionGeneratorReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
92+
metrics.EmitOperatorConditionGeneratorReconcile()
9193
// Set up a convenient log object so we don't have to type request over and over again
9294
log := r.log.WithValues("request", req)
9395

pkg/metrics/experimental.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// +build experimental_metrics
2+
3+
package metrics
4+
5+
import (
6+
"fmt"
7+
"strings"
8+
9+
"github.com/prometheus/client_golang/prometheus"
10+
)
11+
12+
func init() {
13+
// Register experimental metrics
14+
reconcileMetrics = reconcileCounters(operatorController, adoptionCSVController, adoptionSubscriptionController, operatorConditionController, operatorConditionGeneratorController)
15+
registerReconcileMetrics()
16+
fmt.Printf("metrics")
17+
}
18+
19+
func reconcileCounters(reconcilerNames ...string) map[string]prometheus.Counter {
20+
result := map[string]prometheus.Counter{}
21+
for _, s := range reconcilerNames {
22+
result[s] = reconcileCounter(s)
23+
}
24+
return result
25+
}
26+
27+
func reconcileCounter(name string) prometheus.Counter {
28+
return prometheus.NewCounter(
29+
prometheus.CounterOpts{
30+
Name: "controller_reconcile_" + name,
31+
Help: fmt.Sprintf("Count of reconcile events by the %s controller", strings.Replace(name, "_", " ", -1)),
32+
},
33+
)
34+
}
35+
36+
func registerReconcileMetrics() {
37+
for _, v := range reconcileMetrics {
38+
prometheus.MustRegister(v)
39+
}
40+
}

pkg/metrics/metrics.go

+36
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ const (
2727
APPROVAL_LABEL = "approval"
2828
WARNING_LABEL = "warning"
2929
GVK_LABEL = "gvk"
30+
31+
// Controller names
32+
operatorController = "operator"
33+
adoptionCSVController = "adoption_csv"
34+
adoptionSubscriptionController = "adoption_subscription"
35+
operatorConditionController = "operator_condition"
36+
operatorConditionGeneratorController = "operator_condition_generator"
3037
)
3138

3239
type MetricsProvider interface {
@@ -204,6 +211,9 @@ var (
204211
// Subscription objects. The key of a record is the Subscription name, while the value
205212
// is struct containing label values used in the counter
206213
subscriptionSyncCounters = make(map[string]subscriptionSyncLabelValues)
214+
215+
// Experimental Metrics
216+
reconcileMetrics = map[string]prometheus.Counter{}
207217
)
208218

209219
type subscriptionSyncLabelValues struct {
@@ -334,3 +344,29 @@ func RegisterDependencyResolutionFailure(duration time.Duration) {
334344
func EmitInstallPlanWarning() {
335345
installPlanWarningCount.Inc()
336346
}
347+
348+
func EmitOperatorReconcile() {
349+
emitReconcile(operatorConditionController)
350+
}
351+
352+
func EmitAdoptionCSVReconcile() {
353+
emitReconcile(adoptionCSVController)
354+
}
355+
356+
func EmitAdoptionSubscriptionReconcile() {
357+
emitReconcile(adoptionSubscriptionController)
358+
}
359+
360+
func EmitOperatorConditionReconcile() {
361+
emitReconcile(operatorConditionController)
362+
}
363+
364+
func EmitOperatorConditionGeneratorReconcile() {
365+
emitReconcile(operatorConditionGeneratorController)
366+
}
367+
368+
func emitReconcile(name string) {
369+
if counter, ok := reconcileMetrics[name]; ok {
370+
counter.Inc()
371+
}
372+
}

0 commit comments

Comments
 (0)