Documentation
¶
Overview ¶
Package auditqueue is the durable auditlog.Recorder: Record schedules the audit entry as a task of a skit queue — one INSERT on the request path — and the workers of the queue write it through the audit core. Use it when an entry must survive a crash or the writes come in spikes; for the lowest latency and the order of each model use auditlog.AsyncRecorder.
Wiring ¶
The recorder schedules on the queue, the workers drain it into the core; supervise them in a worker.Group:
q := queue.NewPG(log, db)
if err := q.EnsureSchema(ctx); err != nil {
return err
}
rec := auditqueue.NewRecorder(q, log) // an auditlog.Recorder
auditbus.Register(bus, rec)
workers, err := auditqueue.NewWorkers(q, core, log, auditqueue.WorkerConfig{
Metrics: reg,
})
if err != nil {
return err
}
for _, w := range workers {
group.Add(w)
}
The tasks go to the queue Queue under the kind Kind. Record is best-effort: a failed schedule is logged, not returned. A task whose payload does not decode goes to the dead letters of the queue; a failed write is retried with backoff.
WorkerConfig ¶
- Interval: the wait after a poll that found no entry (default 1s).
- Batch: the entries claimed at once (default 100).
- Metrics: the registry of the skit_queue_* and skit_worker_* metrics of the queue; nil turns them off.
Order ¶
The queue keeps no order within a model: the workers of every replica claim its tasks. Core.Create retries on a version collision, so no entry is lost, but two changes of one model handled out of order are stored in that order. Run the workers on one replica, or use AsyncRecorder, when the order matters.
Index ¶
Constants ¶
const ( // Queue is the queue the audit entries go through. Queue = "auditlog" // Kind is the queue task kind for audit entries. Kind = "auditlog.record" )
Variables ¶
This section is empty.
Functions ¶
func NewWorkers ¶ added in v0.14.0
func NewWorkers(q *queue.PG, core *auditlog.Core, log *slog.Logger, cfg WorkerConfig) ([]worker.Runnable, error)
NewWorkers returns the workers of Queue (queue.Workers), which record the audit entries through core. A payload that does not decode goes to the dead letters; a failed record is retried with backoff. Supervise the workers in a worker.Group. log may be nil.
It returns an error only if wiring the queue fails — which cannot happen here, as the single registration uses a constant Kind and a non-nil handler.
Types ¶
type Enqueuer ¶ added in v0.14.0
type Enqueuer interface {
Schedule(ctx context.Context, p queue.Params) (inserted bool, err error)
}
Enqueuer schedules a task on a queue; *queue.PG implements it.
type Recorder ¶
type Recorder struct {
// contains filtered or unexported fields
}
Recorder enqueues audit entries onto a queue. It implements auditlog.Recorder.
func NewRecorder ¶
NewRecorder builds a queue-backed recorder.
type WorkerConfig ¶
type WorkerConfig struct {
// Interval is the wait after a poll that found no entry (default 1s).
Interval time.Duration
// Batch caps the entries claimed at once (default 100).
Batch int
// Metrics is the registry for the skit_queue_* metrics of the audit queue
// and the skit_worker_* metrics of its loops; nil — metrics off.
Metrics prometheus.Registerer
}
WorkerConfig configures the workers of the audit queue.