go-auditGo Audit
API Reference

API Reference

Public methods on the Auditor type. All methods accept a context.Context and return errors using fmt.Errorf wrapping for cause inspection.

Query(ctx, opts QueryOptions)
ReadSync

Query audit logs by entity, action, or date range. Supports cursor-based pagination and returns entries ordered by occurredAt descending.

ParameterTypeDescription
ctxcontext.ContextRequest-scoped context, propagated to the underlying SQL driver.
optsQueryOptionsFilter, ordering, and pagination knobs. See QueryOptions.
Returns([]Entry, error)
query.go
entries, err := auditor.Query(ctx, audit.QueryOptions{
    Entity: "users",
    Action: "update",
    Limit:  50,
})
API().Record(ctx, call APICall)
WriteAsync

Log third-party API calls with automatic header and body redaction. Headers like Authorization and Cookie are masked, and JSON paths can be configured for body redaction.

ParameterTypeDescription
ctxcontext.ContextRequest context. Cancellation aborts the write.
callAPICallOutbound HTTP call snapshot — endpoint, status, latency, headers, body.
Returnserror
api_record.go
err := auditor.API().Record(ctx, audit.APICall{
    Endpoint: "https://api.stripe.com/v1/charges",
    Method:   "POST",
    Status:   200,
    Latency:  180 * time.Millisecond,
})
Snapshot(ctx, entityID string, asOf time.Time)
ReadSync

Retrieve entity state at a specific point in time by replaying audit log entries up to the cutoff. Read-only — does not modify any data.

ParameterTypeDescription
ctxcontext.ContextCancelable context for the read.
entityIDstringStable identifier of the entity to reconstruct.
asOftime.TimeCutoff timestamp; UTC recommended.
Returns(Snapshot, error)
snapshot.go
snap, err := auditor.Snapshot(ctx, "user-42", cutoff)
if err != nil { return err }
// snap.State is the projected entity at `cutoff`.
Restore(ctx, entityID string, asOf time.Time)
MutatingSync

Revert an entity to its state at a previous timestamp. The restore itself is recorded as a new audit entry, preserving the full chain of changes.

ParameterTypeDescription
ctxcontext.ContextCancelable context for the write transaction.
entityIDstringIdentifier of the entity to roll back.
asOftime.TimeTarget timestamp to restore to.
Returnserror
restore.go
if err := auditor.Restore(ctx, "user-42", t); err != nil {
    log.Fatal(err)
}
Purge(ctx, before time.Time)
MutatingAsync

Permanently delete audit log entries older than the cutoff. Returns the number of rows deleted. Use with caution — purged data cannot be recovered.

ParameterTypeDescription
ctxcontext.ContextCancelable context. Cancellation rolls back uncommitted batches.
beforetime.TimeAll entries strictly older than this timestamp are removed.
Returns(int64, error)
purge.go
n, err := auditor.Purge(ctx, time.Now().AddDate(-1, 0, 0))
// n is the number of rows removed.

On this page