Skip to content

Commit

Permalink
reconciler: Add ability to provide default metrics
Browse files Browse the repository at this point in the history
Noticed that we need the ability to provide the default metrics
implementation in cilium/cilium as most reconcilers would not
want their own specific implementation. It would be unnecessary
boilerplate to have to depend on metrics and pass it in the
configuration, so add it as an optional to Params[T].

Signed-off-by: Jussi Maki <[email protected]>
  • Loading branch information
joamaki committed Apr 23, 2024
1 parent 533275b commit 03ed0ce
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
28 changes: 17 additions & 11 deletions reconciler/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ func New[Obj comparable](p Params[Obj]) (Reconciler[Obj], error) {
return nil, err
}

if p.Config.Metrics == nil {
p.Config.Metrics = NewUnpublishedExpVarMetrics()
metrics := p.Config.Metrics
if metrics == nil {
if p.DefaultMetrics == nil {
metrics = NewUnpublishedExpVarMetrics()
} else {
metrics = p.DefaultMetrics
}
}

idx := p.Table.PrimaryIndexer()
Expand All @@ -40,7 +45,7 @@ func New[Obj comparable](p Params[Obj]) (Reconciler[Obj], error) {
}
r := &reconciler[Obj]{
Params: p,
metrics: p.Config.Metrics,
metrics: metrics,
retries: newRetries(p.Config.RetryBackoffMinDuration, p.Config.RetryBackoffMaxDuration, objectToKey),
externalFullTrigger: make(chan struct{}, 1),
primaryIndexer: idx,
Expand All @@ -57,14 +62,15 @@ func New[Obj comparable](p Params[Obj]) (Reconciler[Obj], error) {
type Params[Obj comparable] struct {
cell.In

Config Config[Obj]
Lifecycle cell.Lifecycle
Log *slog.Logger
DB *statedb.DB
Table statedb.RWTable[Obj]
Jobs job.Registry
ModuleID cell.FullModuleID
Health cell.Health
Config Config[Obj]
Lifecycle cell.Lifecycle
Log *slog.Logger
DB *statedb.DB
Table statedb.RWTable[Obj]
Jobs job.Registry
ModuleID cell.FullModuleID
Health cell.Health
DefaultMetrics Metrics `optional:"true"`
}

type reconciler[Obj comparable] struct {
Expand Down
6 changes: 4 additions & 2 deletions reconciler/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,16 @@ func testReconciler(t *testing.T, batchOps bool) {
"test",
"Test",

cell.Provide(func() reconciler.Metrics {
return expVarMetrics
}),

cell.Provide(func(db_ *statedb.DB) (statedb.RWTable[*testObject], error) {
db = db_
return testObjects, db.RegisterTable(testObjects)
}),
cell.Provide(func() reconciler.Config[*testObject] {
cfg := reconciler.Config[*testObject]{
Metrics: expVarMetrics,

// Don't run the full reconciliation via timer, but rather explicitly so that the full
// reconciliation operations don't mix with incremental when not expected.
FullReconcilationInterval: time.Hour,
Expand Down
8 changes: 8 additions & 0 deletions reconciler/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ type Reconciler[Obj any] interface {
}

type Config[Obj any] struct {
// Metrics to use with this reconciler. The metrics capture the duration
// of operations during incremental and full reconcilation and the errors
// that occur during either.
//
// If nil, then the default metrics are used via Params.
// A simple implementation of metrics based on the expvar package come
// with the reconciler and a custom one can be used by implementing the
// Metrics interface.
Metrics Metrics

// FullReconcilationInterval is the amount of time to wait between full
Expand Down

0 comments on commit 03ed0ce

Please sign in to comment.