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 metric to measure handler panics #11

Merged
merged 1 commit into from
Nov 14, 2023
Merged
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
23 changes: 19 additions & 4 deletions worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ type worker struct {

func newWorker(obs Observability, id, capacity, maxTries, delayQueueSize int, delayResolution time.Duration, maxReconcileTime time.Duration, handler Reconciler) (*worker, error) {
obs.Logger = obs.Logger.With("worker-id", id)

handler, err := newPanicReconciler(obs, newReconcilerWithTimeout(handler, maxReconcileTime))
if err != nil {
return nil, err
}

w := &worker{
id: id,
Observability: obs,
Expand All @@ -50,9 +56,10 @@ func newWorker(obs Observability, id, capacity, maxTries, delayQueueSize int, de
metrics: &metrics{},
delayQueue: newQueue(delayQueueSize, delayResolution),
objectLocks: newObjectLocks(capacity),
handler: newPanicReconciler(obs, newReconcilerWithTimeout(handler, maxReconcileTime)),
handler: handler,
}
err := decorateMeter(w, obs.Meter)

err = decorateMeter(w, obs.Meter)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -131,12 +138,20 @@ func decorateMeter(w *worker, meter metric.Meter) error {
return nil
}

func newPanicReconciler(obs Observability, delegate Reconciler) Reconciler {
func newPanicReconciler(obs Observability, delegate Reconciler) (Reconciler, error) {
panicCounter, err := obs.Meter.Int64Counter("kreconciler_handler_panic",
metric.WithDescription("The number of times the reconciler's handler function panicked"),
)
if err != nil {
return nil, err
}

return ReconcilerFunc(func(ctx context.Context, id string) (r Result) {
defer func() {
if err := recover(); err != nil {
l := obs.LoggerWithCtx(ctx)
l.Error("Panicked inside an reconciler", "error", err, "stack", string(debug.Stack()))
panicCounter.Add(ctx, 1)
span := trace.SpanFromContext(ctx)
span.AddEvent("panic")
if e, ok := err.(error); ok {
Expand All @@ -148,7 +163,7 @@ func newPanicReconciler(obs Observability, delegate Reconciler) Reconciler {
}()
r = delegate.Apply(ctx, id)
return
})
}), nil
}

func newReconcilerWithTimeout(delegate Reconciler, timeout time.Duration) Reconciler {
Expand Down
Loading