Skip to content

Observability

Give tasks an ID and a kind so logs and metrics can tell them apart:

pool.SubmitTask(ctx, adaptivepool.Task{
ID: "order-8812", // unique: logs and OnTaskDone only
Kind: "send_email", // bounded: metric label
Run: sendEmail,
})

Keep the set of kinds small: each one becomes a metric label value.

The core has no metrics dependency. Plug in a recorder with WithRecorder.

Prometheus

Terminal window
go get github.com/iyashjayesh/go-adaptive-pool/promrecorder
reg := prometheus.NewRegistry()
pool, _ := adaptivepool.New(
adaptivepool.WithName("orders"),
adaptivepool.WithRecorder(promrecorder.New(reg)),
)
http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))

OpenTelemetry

Terminal window
go get github.com/iyashjayesh/go-adaptive-pool/otelrecorder
pool, _ := adaptivepool.New(
adaptivepool.WithName("orders"),
adaptivepool.WithRecorder(otelrecorder.New(meterProvider)), // nil uses otel.GetMeterProvider()
)

Both report, per pool and task kind: active and busy workers, queue depth and wait, throughput, execution time, failures by status, rejections by reason, retries, and jobs dropped by a forced shutdown. Any other backend needs a five-method Recorder.

The examples/observability directory has a ready-made Grafana dashboard.

The pool logs through log/slog (default: slog.Default()). Failures are logged with pool, task_id, kind, status, attempts, duration and err:

adaptivepool.WithLogger(slog.New(slog.NewJSONHandler(os.Stdout, nil)))

Submit returns as soon as a task is queued. WithOnTaskDone tells you how it ended, and is called exactly once per executed task:

pool, _ := adaptivepool.New(
adaptivepool.WithOnTaskDone(func(r adaptivepool.TaskResult) {
if r.Err != nil {
deadLetters.Publish(r.ID, r.Kind, r.Status, r.Err) // hand off, don't block
}
}),
)

TaskResult contains ID, Kind, QueueWait, Duration, Attempts, Status and Err. The hook runs on the worker goroutine, so keep it fast. A panic inside it is recovered and logged.

A job dropped from the queue by a forced shutdown is also reported, with status shutdown and Attempts 0.

Every finished task has exactly one status, used in the status metric label, the log attribute and TaskResult.Status:

StatusMeaning
successThe job returned nil (possibly after retries)
errorThe job returned an error and retries are disabled
panicThe job panicked (never retried)
retries_exhaustedAll allowed attempts failed
budget_exceededMaxElapsedTime would have been exceeded
permanentThe job returned a Permanent error
canceledThe job failed after its context was done
shutdownA forced shutdown aborted retrying, or dropped the task from the queue before it ran

For metric names, PromQL queries and custom recorders, see the Retries and Observability guide.