Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add autoupdate controller metrics #50807

Open
wants to merge 1 commit into
base: hugo/diagnostics-service-use-local-metrics-registry
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions lib/autoupdate/rollout/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/gravitational/trace"
"github.com/jonboulle/clockwork"
"github.com/prometheus/client_golang/prometheus"

"github.com/gravitational/teleport"
"github.com/gravitational/teleport/api/utils/retryutils"
Expand All @@ -45,13 +46,14 @@ type Controller struct {
clock clockwork.Clock
log *slog.Logger
period time.Duration
metrics *metrics
}

// NewController creates a new Controller for the autoupdate_agent_rollout kind.
// The period can be specified to control the sync frequency. This is mainly
// used to speed up tests or for demo purposes. When empty, the controller picks
// a sane default value.
func NewController(client Client, log *slog.Logger, clock clockwork.Clock, period time.Duration) (*Controller, error) {
func NewController(client Client, log *slog.Logger, clock clockwork.Clock, period time.Duration, reg prometheus.Registerer) (*Controller, error) {
if client == nil {
return nil, trace.BadParameter("missing client")
}
Expand All @@ -61,6 +63,9 @@ func NewController(client Client, log *slog.Logger, clock clockwork.Clock, perio
if clock == nil {
return nil, trace.BadParameter("missing clock")
}
if reg == nil {
return nil, trace.BadParameter("missing prometheus.Registerer")
}

if period <= 0 {
period = defaultReconcilerPeriod
Expand All @@ -77,13 +82,17 @@ func NewController(client Client, log *slog.Logger, clock clockwork.Clock, perio
return nil, trace.Wrap(err, "failed to initialize time-based strategy")
}

m := newMetrics(reg)

return &Controller{
clock: clock,
log: log,
metrics: m,
clock: clock,
log: log,
reconciler: reconciler{
clt: client,
log: log,
clock: clock,
clt: client,
log: log,
clock: clock,
metrics: m,
rolloutStrategies: []rolloutStrategy{
timeBased,
haltOnError,
Expand Down Expand Up @@ -122,13 +131,22 @@ func (c *Controller) Run(ctx context.Context) error {
// tryAndCatch tries to run the controller reconciliation logic and recovers from potential panic by converting them
// into errors. This ensures that a critical bug in the reconciler cannot bring down the whole Teleport cluster.
func (c *Controller) tryAndCatch(ctx context.Context) (err error) {
startTime := c.clock.Now()
// If something terribly bad happens during the reconciliation, we recover and return an error
defer func() {
if r := recover(); r != nil {
c.log.ErrorContext(ctx, "Recovered from panic in the autoupdate_agent_rollout controller", "panic", r)
err = trace.NewAggregate(err, trace.Errorf("Panic recovered during reconciliation: %v", r))
c.metrics.observeReconciliation(metricsReconciliationResultLabelValuePanic, c.clock.Now().Sub(startTime))
}
}()

err = trace.Wrap(c.reconciler.reconcile(ctx))
endTime := c.clock.Now()
result := metricsReconciliationResultLabelValueSuccess
if err != nil {
result = metricsReconciliationResultLabelValueFail
}
c.metrics.observeReconciliation(result, endTime.Sub(startTime))
return
}
Loading
Loading