Documentation
¶
Overview ¶
Package auditlog records a versioned audit trail of model changes and answers history and diff queries over it.
Each change is stored as a snapshot keyed by (ModelType, ModelID); snapshots for the same model form an ordered history with a monotonically increasing Version. Create is idempotent against no-op writes: if the new snapshot is identical to the latest one (ignoring updated_at/updated_by) no new version is stored. The Core depends only on the Store interface declared here; the Postgres implementation lives in the auditpg subpackage, an in-memory one for tests in audittest.
Recording changes ¶
Audit at the domain layer, not the transport: emit a change where it happens, so every path (REST, gRPC, workers, consumers) is covered by one wiring. Two equivalent entry points, both converging on Core.Create:
// Direct (atomic with the business write on a Core over its transaction,
// auditlog.NewCore(log, auditpg.NewStore(log, tx)), or best-effort):
_, err := core.Create(ctx, auditlog.NewAuditLog{
ModelType: "widget", ModelID: id, CreatedBy: actor, Payload: snapshot,
})
// Decoupled, over the in-process eventbus (producer doesn't import auditlog):
auditbus.Register(bus, core) // once at startup
auditbus.Publish(ctx, bus, auditbus.Event{ModelType: "widget", ModelID: id, ...})
Recorders ¶
Transports/producers depend on the Recorder interface (best-effort Record), so recording can be synchronous or asynchronous without changing call sites:
- *Core — synchronous; the only option that can be atomic (a Core over the business transaction).
- AsyncRecorder — in-process workers, sharded by model (per-model order), non-blocking; lost on crash.
- auditqueue.Recorder — a task of a skit queue, written by its workers; survives crashes, smooths spikes.
Querying ¶
last, err := core.QueryLastByModelID(ctx, "widget", id) hist, err := core.QueryHistoryByModelID(ctx, "widget", id) diff, err := core.QueryDiffByModelID(ctx, "widget", id, filter) // between two versions recs, err := core.QueryDiffAllVersionByModelID(ctx, "widget", id)
The auditrest subpackage exposes a ready-made read-side handler group (history / diff / changes) mountable on any skit router in one call:
auditrest.NewHandlers(core).Routes(r.HandleApp)
Compaction ¶
Compact thins a model's history while always keeping the first and last versions. Two API entry points; scheduling is left to the caller's worker.
- Opportunistic, inline on write: WithAutoCompact makes Create compact the model it just wrote, once every N versions (best-effort, gated so most writes pay nothing) — often enough to keep history bounded on its own.
- On demand, in portions: CompactBatch compacts up to Limit models over a version threshold. Call it from an API handler, a CLI command, or a worker; loop until CompactResult.Models is 0 to drain a backlog gently.
In code:
core := auditlog.NewCore(log, store, auditlog.WithAutoCompact(100, opts)) // inline
res, err := core.CompactBatch(ctx, auditlog.CompactBatchOptions{Threshold: 100, Limit: 50, Compact: opts})
Scheduled compaction is wired with the worker package — auditlog ships no built-in loop:
tick := func(ctx context.Context) error {
_, err := core.CompactBatch(ctx, auditlog.CompactBatchOptions{Threshold: 100, Limit: 50, Compact: opts})
return err
}
group.Add(worker.NewLoop(log, worker.LoopConfig{Name: "auditlog-compaction", Interval: time.Hour}, tick))
Concurrency ¶
Create is a read-modify-write (read latest version, insert version+1). The auditpg schema declares UNIQUE (model_type, model_id, version): a racing insert at the same version fails with a duplicate-key error, and Create re-reads and retries (bounded) so concurrent distinct changes are not lost.
Example ¶
Create stores the snapshot of the model as its next version, unless it equals the latest one: then nothing is stored and the version is 0. The history lists the versions in order, the changes the fields each version changed.
package main
import (
"bytes"
"context"
"fmt"
"github.com/assanoff/skit/auditlog"
"github.com/assanoff/skit/auditlog/audittest"
)
// widget is the model the examples audit.
type widget struct {
Name string `json:"name"`
Price int `json:"price"`
}
func main() {
ctx := context.Background()
core := auditlog.NewCore(nil, audittest.NewStore())
for _, w := range []widget{{"gear", 10}, {"gear", 10}, {"gear", 12}, {"big gear", 12}} {
al, err := core.Create(ctx, auditlog.NewAuditLog{
ModelType: "widget", ModelID: "w-1", CreatedBy: "ann", Payload: w,
})
fmt.Println("create:", al.Version, err)
}
hist, err := core.QueryHistoryByModelID(ctx, "widget", "w-1")
if err != nil {
fmt.Println(err)
return
}
for _, al := range hist {
fmt.Println(al.Version, al.CreatedBy, string(bytes.TrimSpace(al.Payload)))
}
recs, err := core.QueryDiffAllVersionByModelID(ctx, "widget", "w-1")
if err != nil {
fmt.Println(err)
return
}
for _, rec := range recs {
for _, f := range rec.ChangedFields {
fmt.Printf("v%d %s: %v -> %v\n", rec.Version, f.Key, f.OldValue, f.NewValue)
}
}
}
Output: create: 1 <nil> create: 0 <nil> create: 2 <nil> create: 3 <nil> 1 ann {"name":"gear","price":10} 2 ann {"name":"gear","price":12} 3 ann {"name":"big gear","price":12} v1 price: <nil> -> 10 v2 price: 10 -> 12 v3 name: gear -> big gear
Index ¶
- Variables
- type AsyncConfig
- type AsyncRecorder
- type AuditLog
- type CompactBatchOptions
- type CompactOptions
- type CompactResult
- type Core
- func (c *Core) Compact(ctx context.Context, modelType, modelID string, opts CompactOptions) (int, error)
- func (c *Core) CompactBatch(ctx context.Context, opts CompactBatchOptions) (CompactResult, error)
- func (c *Core) Create(ctx context.Context, in NewAuditLog) (AuditLog, error)
- func (c *Core) NormalizeDiff(diff string) string
- func (c *Core) QueryDiffAllVersionByModelID(ctx context.Context, modelType, modelID string) ([]DiffRecord, error)
- func (c *Core) QueryDiffByModelID(ctx context.Context, modelType, modelID string, filter QueryFilter) (string, error)
- func (c *Core) QueryHistoryByModelID(ctx context.Context, modelType, modelID string) ([]AuditLog, error)
- func (c *Core) QueryLastByModelID(ctx context.Context, modelType, modelID string) (AuditLog, error)
- func (c *Core) Record(ctx context.Context, entry NewAuditLog)
- type DiffRecord
- type Field
- type ModelRef
- type NewAuditLog
- type Option
- type QueryFilter
- type Recorder
- type Store
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidFilter = errors.New("invalid audit log query filter")
ErrInvalidFilter is returned when a query filter is missing required fields (e.g. a diff request without both version numbers).
var ErrNotFound = errors.New("audit log not found")
ErrNotFound is returned when no audit log entry matches the query.
Functions ¶
This section is empty.
Types ¶
type AsyncConfig ¶
type AsyncConfig struct {
// Workers is the number of background workers (and shards). Entries for the
// same model always go to the same shard, so per-model order is preserved.
// Defaults to 4.
Workers int
// Buffer is the capacity of each shard's queue. Defaults to 256.
Buffer int
// BlockOnFull makes Record block until there is room instead of dropping.
// Default (false) drops the entry and calls OnDrop, so the request path is
// never slowed by a saturated audit pipeline. On shutdown Record drops even
// when BlockOnFull is set, so a saturated shard cannot hang a caller forever.
BlockOnFull bool
// OnDrop is invoked with an entry dropped because its shard was full (when
// BlockOnFull is false). Use it for a metric/log; may be nil.
OnDrop func(NewAuditLog)
}
AsyncConfig configures an AsyncRecorder.
type AsyncRecorder ¶
type AsyncRecorder struct {
// contains filtered or unexported fields
}
AsyncRecorder records audit entries on background workers so the request path is not slowed by the database write. It implements Recorder (non-blocking Record) and worker.Runnable (Start/Stop/Name): supervise it in a worker.Group.
Entries are sharded by (ModelType, ModelID) onto a fixed worker, so writes for the same model stay ordered — important because Core.Create derives the next version from the latest stored one. On shutdown, Start drains the buffered entries before returning.
func NewAsyncRecorder ¶
func NewAsyncRecorder(core *Core, cfg AsyncConfig) *AsyncRecorder
NewAsyncRecorder builds an AsyncRecorder over core. It does not write anything until Start is called: Record before Start buffers the entry, and Record once Start has returned drops it.
Example ¶
The AsyncRecorder writes on its workers, off the path of the request: Record only enqueues, and the entries of one model keep their order. In a worker.Group, Start writes what is buffered before it returns.
package main
import (
"bytes"
"context"
"fmt"
"log/slog"
"github.com/assanoff/skit/auditlog"
"github.com/assanoff/skit/auditlog/audittest"
"github.com/assanoff/skit/worker"
)
// widget is the model the examples audit.
type widget struct {
Name string `json:"name"`
Price int `json:"price"`
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
core := auditlog.NewCore(nil, audittest.NewStore())
rec := auditlog.NewAsyncRecorder(core, auditlog.AsyncConfig{Workers: 2})
g := worker.NewGroup(slog.New(slog.DiscardHandler), worker.GroupConfig{})
g.Add(rec)
done := make(chan error, 1)
go func() { done <- g.Run(ctx) }()
for _, name := range []string{"gear", "big gear", "huge gear"} {
rec.Record(ctx, auditlog.NewAuditLog{
ModelType: "widget", ModelID: "w-1", CreatedBy: "ann",
Payload: widget{name, 10},
})
}
cancel() // shutdown: the recorder writes its buffer and stops
fmt.Println("group:", <-done)
hist, err := core.QueryHistoryByModelID(context.Background(), "widget", "w-1")
if err != nil {
fmt.Println(err)
return
}
for _, al := range hist {
fmt.Println(al.Version, string(bytes.TrimSpace(al.Payload)))
}
}
Output: group: <nil> 1 {"name":"gear","price":10} 2 {"name":"big gear","price":10} 3 {"name":"huge gear","price":10}
func (*AsyncRecorder) Name ¶
func (a *AsyncRecorder) Name() string
Name identifies the recorder in the supervisor and logs.
func (*AsyncRecorder) Record ¶
func (a *AsyncRecorder) Record(ctx context.Context, entry NewAuditLog)
Record enqueues entry onto its model's shard and returns immediately. The request context is detached (WithoutCancel) so the background write is not canceled when the request ends, while trace/values are preserved for logging. When the shard is full it blocks or drops per AsyncConfig.
type AuditLog ¶
type AuditLog struct {
ID int
Version int
ModelType string
ModelID string
Method string
Path string
Payload []byte
CreatedAt time.Time
CreatedBy string
}
AuditLog is a single, versioned snapshot of a model recorded in the audit trail. Entries for the same (ModelType, ModelID) form an ordered history; Version increments by one per stored change. Payload is the JSON-encoded snapshot of the model at that version. CreatedBy is the actor (e.g. the JWT subject) that made the change.
type CompactBatchOptions ¶
type CompactBatchOptions struct {
// Threshold: only models with more than this many stored versions are
// candidates.
Threshold int
// Limit caps how many models this call processes — the "portion". Default 100.
Limit int
// Compact options applied to each model.
Compact CompactOptions
}
CompactBatchOptions configures one batch of compaction across many models.
type CompactOptions ¶
type CompactOptions struct {
// Factor keeps roughly every Factor-th middle version (Factor >= 2). When
// MaxVersions is also set, the cap takes precedence. Factor < 2 disables
// factor thinning (only MaxVersions, if set, thins).
Factor int
// KeepRecent always retains this many most-recent versions untouched.
KeepRecent int
// MaxVersions, when > 0, caps the total kept versions: the middle is
// downsampled evenly so no more than MaxVersions remain.
MaxVersions int
// MinVersions skips compaction entirely when a model has this many versions or
// fewer. Defaults to 2 (nothing below first+last is ever thinned).
MinVersions int
}
CompactOptions controls how a model's version history is thinned. The first (baseline) and last (current) versions are always kept, so diffs and the current snapshot remain available.
type CompactResult ¶
type CompactResult struct {
Models int // models that had versions removed
Deleted int // total versions removed
}
CompactResult reports what a CompactBatch did.
type Core ¶
type Core struct {
// contains filtered or unexported fields
}
Core is the audit-log business logic: it records versioned model snapshots and answers history/diff queries. It depends only on the Store interface declared here; the Postgres implementation lives in the auditpg subpackage.
func (*Core) Compact ¶
func (c *Core) Compact(ctx context.Context, modelType, modelID string, opts CompactOptions) (int, error)
Compact thins the stored version history of one model per opts and returns how many versions were deleted. The first and last versions are never removed.
func (*Core) CompactBatch ¶
func (c *Core) CompactBatch(ctx context.Context, opts CompactBatchOptions) (CompactResult, error)
CompactBatch compacts one portion of the models whose version count exceeds the threshold (most-versioned first), up to Limit models. It is the single reusable entry point for scheduled (worker.Loop), command-line, or API-triggered runs — call it repeatedly until Result.Models is 0 to drain a large backlog gently.
Example ¶
CompactBatch thins the history of the models over a threshold of versions and keeps the first and the last version of each. Run it from a worker.Loop, a command or an admin endpoint; loop until Models is 0 to drain a backlog.
package main
import (
"context"
"fmt"
"github.com/assanoff/skit/auditlog"
"github.com/assanoff/skit/auditlog/audittest"
)
// widget is the model the examples audit.
type widget struct {
Name string `json:"name"`
Price int `json:"price"`
}
func main() {
ctx := context.Background()
core := auditlog.NewCore(nil, audittest.NewStore())
for price := range 10 {
core.Record(ctx, auditlog.NewAuditLog{
ModelType: "widget", ModelID: "w-1", Payload: widget{"gear", 10 + price},
})
}
res, err := core.CompactBatch(ctx, auditlog.CompactBatchOptions{
Threshold: 5, // the models with more than 5 versions
Limit: 50,
Compact: auditlog.CompactOptions{Factor: 3, KeepRecent: 2},
})
fmt.Println(res.Models, res.Deleted, err)
hist, err := core.QueryHistoryByModelID(ctx, "widget", "w-1")
if err != nil {
fmt.Println(err)
return
}
for _, al := range hist {
fmt.Print(al.Version, " ")
}
fmt.Println()
}
Output: 1 5 <nil> 1 4 7 9 10
func (*Core) Create ¶
Create records a new version of a model. When the new snapshot is identical to the latest stored one (ignoring the normalized-away updated_at/updated_by fields) no new version is written and a zero AuditLog is returned. The returned AuditLog has Version == 0 exactly when nothing was stored.
Create is a read-modify-write (read latest version, insert version+1). If a concurrent writer inserts the same version first, the UNIQUE constraint rejects this insert; Create then re-reads the latest version and retries, so concurrent distinct changes are not lost (up to maxCreateAttempts).
func (*Core) NormalizeDiff ¶
NormalizeDiff keeps only the lines of diff that start with + or -: the fields that changed.
func (*Core) QueryDiffAllVersionByModelID ¶
func (c *Core) QueryDiffAllVersionByModelID(ctx context.Context, modelType, modelID string) ([]DiffRecord, error)
QueryDiffAllVersionByModelID finds the diffs between all versions of the model.
func (*Core) QueryDiffByModelID ¶
func (c *Core) QueryDiffByModelID(ctx context.Context, modelType, modelID string, filter QueryFilter) (string, error)
QueryDiffByModelID returns a textual diff between two versions of a model. Both filter.CurrentVersion and filter.TargetVersion are required.
func (*Core) QueryHistoryByModelID ¶
func (c *Core) QueryHistoryByModelID(ctx context.Context, modelType, modelID string) ([]AuditLog, error)
QueryHistoryByModelID returns every version of a model in ascending order.
func (*Core) QueryLastByModelID ¶
QueryLastByModelID returns the most recent entry for a model, or ErrNotFound.
type DiffRecord ¶
type DiffRecord struct {
ID int
ModelID string
ModelType string
Method string
Path string
Version int
UpdatedBy *string
UpdatedAt *time.Time
ChangedFields []Field
}
DiffRecord is one version in a model's history together with the fields that changed relative to the previous version. It is returned by Core.QueryDiffAllVersionByModelID.
type NewAuditLog ¶
type NewAuditLog struct {
ModelID string
ModelType string
Method string
Path string
Payload any
CreatedBy string
}
NewAuditLog is the input to Core.Create. Payload is any value that JSON-encodes to the model snapshot; the version and CreatedAt are assigned by Create (last version + 1, or skipped when unchanged). Method/Path label the transport action (HTTP verb + path, or the gRPC full method).
type Option ¶
type Option func(*Core)
Option customizes a Core.
func WithAutoCompact ¶
func WithAutoCompact(every int, opts CompactOptions) Option
WithAutoCompact enables opportunistic compaction: after Create stores a new version, once every `every` versions it compacts that model with opts (best-effort — a failure is logged, never returned). Set every to roughly the threshold at which history should be thinned. The separate Compact/CompactBatch remain available for on-demand or scheduled runs.
Example ¶
WithAutoCompact compacts the model Create has just written once every N versions, so its history stays bounded without a scheduled run.
package main
import (
"context"
"fmt"
"github.com/assanoff/skit/auditlog"
"github.com/assanoff/skit/auditlog/audittest"
)
// widget is the model the examples audit.
type widget struct {
Name string `json:"name"`
Price int `json:"price"`
}
func main() {
ctx := context.Background()
core := auditlog.NewCore(nil, audittest.NewStore(),
auditlog.WithAutoCompact(5, auditlog.CompactOptions{Factor: 2}))
for price := range 10 {
core.Record(ctx, auditlog.NewAuditLog{
ModelType: "widget", ModelID: "w-1", Payload: widget{"gear", 10 + price},
})
}
hist, err := core.QueryHistoryByModelID(ctx, "widget", "w-1")
if err != nil {
fmt.Println(err)
return
}
for _, al := range hist {
fmt.Print(al.Version, " ")
}
fmt.Println()
}
Output: 1 5 7 9 10
type QueryFilter ¶
type QueryFilter struct {
CurrentVersion *int `validate:"omitempty,numeric,min=1"`
TargetVersion *int `validate:"omitempty,numeric,min=1"`
}
QueryFilter selects which versions a diff query compares. Both versions are required for QueryDiffByModelID.
func (*QueryFilter) Validate ¶
func (qf *QueryFilter) Validate() error
Validate reports whether the filter is well-formed, returning an errs.InvalidArgument error with per-field detail when not.
func (*QueryFilter) WithCurrentVersion ¶
func (qf *QueryFilter) WithCurrentVersion(ver int)
WithCurrentVersion sets the current-version field.
func (*QueryFilter) WithTargetVersion ¶
func (qf *QueryFilter) WithTargetVersion(ver int)
WithTargetVersion sets the target-version field.
type Recorder ¶
type Recorder interface {
Record(ctx context.Context, entry NewAuditLog)
}
Recorder records an audit entry best-effort (errors are handled internally, not returned). Producers (auditbus.Register) depend on this interface so the recording can be synchronous (*Core) or asynchronous (AsyncRecorder, or the queue-backed auditqueue.Recorder) without changing the call sites.
type Store ¶
type Store interface {
Save(ctx context.Context, al AuditLog) error
QueryLastByModelID(ctx context.Context, modelType, modelID string) (AuditLog, error)
QueryHistoryByModelID(ctx context.Context, modelType, modelID string) ([]AuditLog, error)
QueryModelByVersion(ctx context.Context, modelType, modelID string, ver int) (AuditLog, error)
// Versions returns the stored version numbers for a model, ascending. Used by
// Compact to decide what to thin.
Versions(ctx context.Context, modelType, modelID string) ([]int, error)
// DeleteVersions removes the given versions of a model and returns the count
// deleted. Used by Compact.
DeleteVersions(ctx context.Context, modelType, modelID string, versions []int) (int, error)
// OverThreshold lists models whose stored-version count exceeds threshold, up
// to limit. Used by CompactBatch to find compaction candidates.
OverThreshold(ctx context.Context, threshold, limit int) ([]ModelRef, error)
}
Store is the persistence contract for audit log entries. A caller that needs the audit write to commit atomically with its business change runs Create on a Core over a store built on that transaction (auditpg.NewStore(log, tx)). Save fails with dbx.ErrDBDuplicatedEntry when the model already has the version, so Create retries at the next one; a model or a version that is not stored is ErrNotFound.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package auditbus records audit entries from events on the in-process eventbus, so a domain package asks for an audit write without importing auditlog: the producer publishes an Event on the bus, and the recorder that Register subscribes to it writes the entry.
|
Package auditbus records audit entries from events on the in-process eventbus, so a domain package asks for an audit write without importing auditlog: the producer publishes an Event on the bus, and the recorder that Register subscribes to it writes the entry. |
|
Package auditpg is the Postgres auditlog.Store: the versions of the models in the audit_log table, one row per version, the snapshot of the model in a JSONB column.
|
Package auditpg is the Postgres auditlog.Store: the versions of the models in the audit_log table, one row per version, the snapshot of the model in a JSONB column. |
|
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.
|
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. |
|
Package auditrest is the read side of the audit log over REST: the history of a model, the diff between two of its versions and the fields each version changed, mounted on any skit router in one call.
|
Package auditrest is the read side of the audit log over REST: the history of a model, the diff between two of its versions and the fields each version changed, mounted on any skit router in one call. |
|
Package audittest provides an in-memory auditlog.Store for tests and examples, so an audit Core runs without Postgres.
|
Package audittest provides an in-memory auditlog.Store for tests and examples, so an audit Core runs without Postgres. |
|
internal
|
|