Skip to main content

Hooks Overview

Hooks are the middleware layer for event-spec's event pipeline. They execute at four lifecycle stages around provider dispatch, enabling cross-cutting concerns like validation, sampling, consent, PII stripping, and observability without touching provider code.

Execution order

Before runs once for all providers. A non-nil error return from any Before hook cancels the entire dispatch and marks the event as Dropped.

After, Error, and Finally run once per provider result in reverse registration order.

Base type

Use UnimplementedHook as a base to get no-op implementations of all four stages, then override only the ones you need:

type MyHook struct {
hooks.UnimplementedHook
}

Registering hooks

// Client-level hooks
client := analytics.NewClient(
analytics.WithHooks(
validation.New(lookup),
sampling.New(cfg),
),
)

// API-level (applies to all clients)
analytics.AddGlobalHook(myCustomHook)

Built-in hooks

HookPackageStatus
Samplinghooks/sampling✅ Available
Validationhooks/validation✅ Available
Logginghooks/logging❌ Planned
OpenTelemetryhooks/otel❌ Planned

Custom hooks

See Custom Hooks for a full walkthrough of writing and registering your own hook.