audit

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 1, 2026 License: MIT Imports: 8 Imported by: 3 modules, 5 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

audit — async SQLite audit logging

audit records endpoint calls to an audit_log table with async buffering. A kit middleware captures duration, parameters, result, and error automatically.

Quick start

logger := audit.NewSQLiteLogger(db,
    audit.WithIDGenerator(idgen.Prefixed("aud_", idgen.Default)),
)
logger.Init()
defer logger.Close()

// Wrap any kit.Endpoint — captures timing, params, result, user, transport.
endpoint = kit.Chain(
    audit.Middleware(logger, "create_dossier"),
)(endpoint)

How it works

  1. The middleware calls the next endpoint, measures wall-clock time, and builds an Entry from the context (kit.GetUserID, kit.GetTransport, etc.).
  2. The entry is sent to LogAsync, which pushes it to a 256-capacity channel.
  3. A background goroutine collects entries in batches of 32 or flushes every 500 ms, inserting them into SQLite.
  4. If the channel is full, the entry is dropped with a warning log.

Schema

CREATE TABLE audit_log (
    entry_id      TEXT PRIMARY KEY,
    timestamp     INTEGER NOT NULL,
    action        TEXT NOT NULL,
    transport     TEXT NOT NULL DEFAULT 'http',
    user_id       TEXT,
    request_id    TEXT,
    parameters    TEXT,
    result        TEXT,
    error_message TEXT,
    duration_ms   INTEGER,
    status        TEXT NOT NULL DEFAULT 'success'
);

Indexes on timestamp, action, user_id.

Exported API

Symbol Description
SQLiteLogger Async audit writer (256-entry buffer, 32-batch, 500 ms flush)
NewSQLiteLogger(db, opts) Create logger and start flush goroutine
Entry Audit trail record
Logger Interface: Log, LogAsync, Close
Middleware(logger, action) Kit middleware capturing endpoint metadata
WithIDGenerator(gen) Option to override ID generation

Documentation

Index

Constants

View Source
const Schema = `` /* 522-byte string literal not displayed */

Variables

This section is empty.

Functions

func Middleware

func Middleware(logger Logger, actionName string) kit.Middleware

Middleware wraps an Endpoint: measures duration, captures params/result/error, and logs asynchronously via the Logger.

Types

type Entry

type Entry struct {
	EntryID    string `json:"entry_id"`
	Timestamp  int64  `json:"timestamp"`
	Action     string `json:"action"`
	Transport  string `json:"transport"` // "http" or "mcp_quic"
	UserID     string `json:"user_id"`
	RequestID  string `json:"request_id"`
	Parameters string `json:"parameters"`
	Result     string `json:"result"`
	Error      string `json:"error_message"`
	DurationMs int64  `json:"duration_ms"`
	Status     string `json:"status"` // "success" or "error"
}

Entry records a single action for the audit trail.

type Logger

type Logger interface {
	Log(ctx context.Context, entry *Entry) error
	LogAsync(entry *Entry)
	Close() error
}

Logger writes audit entries to storage.

type Option

type Option func(*SQLiteLogger)

Option configures a SQLiteLogger.

func WithIDGenerator

func WithIDGenerator(gen idgen.Generator) Option

WithIDGenerator sets a custom ID generator for audit entry IDs.

type SQLiteLogger

type SQLiteLogger struct {
	// contains filtered or unexported fields
}

SQLiteLogger writes audit entries to the audit_log table asynchronously.

func NewSQLiteLogger

func NewSQLiteLogger(sqlDB *sql.DB, opts ...Option) *SQLiteLogger

func (*SQLiteLogger) Close

func (l *SQLiteLogger) Close() error

func (*SQLiteLogger) Init

func (l *SQLiteLogger) Init() error

func (*SQLiteLogger) Log

func (l *SQLiteLogger) Log(_ context.Context, entry *Entry) error

func (*SQLiteLogger) LogAsync

func (l *SQLiteLogger) LogAsync(entry *Entry)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL