Wide Events for Next.js
Automatic wide events for Next.js apps.
Overview
@vestig/next provides seamless wide event integration:
- Middleware — Automatic HTTP context and correlation IDs
- Server Actions — Wrapped actions with wide event tracking
- Helpers — Access the current wide event anywhere in your request
Installation
Middleware Setup
The wide event middleware automatically:
- Creates a wide event for each request
- Populates HTTP context (method, path, headers)
- Generates and propagates correlation IDs
- Emits the event when the request completes
- Applies tail sampling if configured
Quick Start
For basic setup, export the pre-configured middleware:
Accessing the Wide Event
From any server-side code within a request, access the current wide event:
Helper Functions
@vestig/next/wide-events provides convenient helpers:
getWideEvent()
Get the current wide event (returns undefined outside of context):
requireWideEvent()
Get the wide event or throw if not in context:
setWideEventUser()
Set user information and enable VIP detection:
setWideEventPerformance()
Add timing metrics:
setWideEventField() / mergeWideEventFields()
Add custom fields:
setWideEventFeatureFlags()
Track feature flags:
setWideEventError() / event.setError()
Record error information. You can use either the standalone helper or the method on the event builder:
timeWideEventOperation()
Time sub-operations:
getWideEventElapsed()
Get elapsed time since request started:
Server Actions
Important: Server actions called from client components run as separate HTTP requests and do not automatically inherit the middleware's wide event context. You must wrap your server actions with withWideEvent() to enable tracking:
Wrap server actions with wide event tracking:
Action Factory
Create a factory with shared options:
Context Integration
If a server action is called from within a middleware-created context, it enriches the existing wide event instead of creating a new one:
Complete Example
Result: ONE comprehensive event per request:
Middleware Options
Related
- Wide Events — Core wide events documentation
- Tail Sampling — Outcome-based sampling
- Next.js Integration — Full Next.js setup guide