generated from HugoRCD/nuxt-module-starter
-
-
Notifications
You must be signed in to change notification settings - Fork 10
feat: add shared drain pipeline for batching and retry #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HugoRCD
wants to merge
7
commits into
main
Choose a base branch
from
feat/drain-pipeline
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cdcd6eb
feat: add shared drain pipeline for batching and retry
HugoRCD 749936a
chore: apply automated lint fixes
autofix-ci[bot] e37564a
feat: add shared drain pipeline for batching and retry
HugoRCD 6fb0aca
Merge remote-tracking branch 'origin/main' into feat/drain-pipeline
HugoRCD 66d5d31
fix lint
HugoRCD 5c245a5
Merge remote-tracking branch 'origin/feat/drain-pipeline' into feat/d…
HugoRCD 11cb24d
fix from code review
HugoRCD File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| --- | ||
| title: Drain Pipeline | ||
| description: Batch events, retry on failure, and protect against buffer overflow with the shared drain pipeline. | ||
| navigation: | ||
| title: Pipeline | ||
| icon: i-lucide-workflow | ||
| links: | ||
| - label: Adapters Overview | ||
| icon: i-custom-plug | ||
| to: /adapters/overview | ||
| color: neutral | ||
| variant: subtle | ||
| - label: Custom Adapters | ||
| icon: i-lucide-code | ||
| to: /adapters/custom | ||
| color: neutral | ||
| variant: subtle | ||
| --- | ||
|
|
||
| In production, sending one HTTP request per log event is wasteful. The drain pipeline buffers events and sends them in batches, retries on transient failures, and drops the oldest events when the buffer overflows. | ||
|
|
||
| ## Quick Start | ||
|
|
||
| ```typescript [server/plugins/evlog-drain.ts] | ||
| import type { DrainContext } from 'evlog' | ||
| import { createDrainPipeline } from 'evlog/pipeline' | ||
| import { createAxiomDrain } from 'evlog/axiom' | ||
|
|
||
| export default defineNitroPlugin((nitroApp) => { | ||
| const pipeline = createDrainPipeline<DrainContext>() | ||
| const drain = pipeline(createAxiomDrain()) | ||
|
|
||
| nitroApp.hooks.hook('evlog:drain', drain) | ||
| nitroApp.hooks.hook('close', () => drain.flush()) | ||
| }) | ||
| ``` | ||
|
|
||
| ::callout{icon="i-lucide-alert-triangle" color="warning"} | ||
| Always call `drain.flush()` on server shutdown to ensure buffered events are sent before the process exits. | ||
| :: | ||
|
|
||
| ## How It Works | ||
|
|
||
| 1. Events are buffered in memory as they arrive via the `evlog:drain` hook | ||
| 2. A batch is flushed when either the **batch size** is reached or the **interval** expires (whichever comes first) | ||
| 3. If the drain function fails, the batch is retried with the configured **backoff strategy** | ||
| 4. If all retries are exhausted, `onDropped` is called with the lost events | ||
| 5. If the buffer exceeds `maxBufferSize`, the oldest events are dropped to prevent memory leaks | ||
|
|
||
| ## Configuration | ||
|
|
||
| ```typescript [server/plugins/evlog-drain.ts] | ||
| import type { DrainContext } from 'evlog' | ||
| import { createDrainPipeline } from 'evlog/pipeline' | ||
| import { createAxiomDrain } from 'evlog/axiom' | ||
|
|
||
| export default defineNitroPlugin((nitroApp) => { | ||
| const pipeline = createDrainPipeline<DrainContext>({ | ||
| batch: { | ||
| size: 50, // Flush every 50 events | ||
| intervalMs: 5000, // Or every 5 seconds, whichever comes first | ||
| }, | ||
| retry: { | ||
| maxAttempts: 3, | ||
| backoff: 'exponential', | ||
| initialDelayMs: 1000, | ||
| maxDelayMs: 30000, | ||
| }, | ||
| maxBufferSize: 1000, | ||
| onDropped: (events, error) => { | ||
| console.error(`[evlog] Dropped ${events.length} events:`, error?.message) | ||
| }, | ||
| }) | ||
|
|
||
| const drain = pipeline(createAxiomDrain()) | ||
|
|
||
| nitroApp.hooks.hook('evlog:drain', drain) | ||
| nitroApp.hooks.hook('close', () => drain.flush()) | ||
| }) | ||
| ``` | ||
|
|
||
| ### Options Reference | ||
|
|
||
| | Option | Default | Description | | ||
| |--------|---------|-------------| | ||
| | `batch.size` | `50` | Maximum events per batch | | ||
| | `batch.intervalMs` | `5000` | Max time (ms) before flushing a partial batch | | ||
| | `retry.maxAttempts` | `3` | Total attempts including the initial one | | ||
| | `retry.backoff` | `'exponential'` | `'exponential'` \| `'linear'` \| `'fixed'` | | ||
| | `retry.initialDelayMs` | `1000` | Base delay for the first retry | | ||
| | `retry.maxDelayMs` | `30000` | Upper bound for any retry delay | | ||
| | `maxBufferSize` | `1000` | Max buffered events before dropping oldest | | ||
| | `onDropped` | — | Callback when events are dropped (overflow or retry exhaustion) | | ||
|
|
||
| ## Backoff Strategies | ||
|
|
||
| | Strategy | Delay Pattern | Use Case | | ||
| |----------|--------------|----------| | ||
| | `exponential` | 1s, 2s, 4s, 8s... | Default. Best for transient failures that may need time to recover | | ||
| | `linear` | 1s, 2s, 3s, 4s... | Predictable delay growth | | ||
| | `fixed` | 1s, 1s, 1s, 1s... | Same delay every time. Useful for rate-limited APIs | | ||
|
|
||
| ## Returned Drain Function | ||
|
|
||
| The function returned by `pipeline(drain)` is hook-compatible and exposes: | ||
|
|
||
| | Property | Type | Description | | ||
| |----------|------|-------------| | ||
| | `drain(ctx)` | `(ctx: T) => void` | Push a single event into the buffer | | ||
| | `drain.flush()` | `() => Promise<void>` | Force-flush all buffered events | | ||
| | `drain.pending` | `number` | Number of events currently buffered | | ||
|
|
||
| ## Multiple Destinations | ||
|
|
||
| Wrap multiple adapters with a single pipeline: | ||
|
|
||
| ```typescript [server/plugins/evlog-drain.ts] | ||
| import type { DrainContext } from 'evlog' | ||
| import { createDrainPipeline } from 'evlog/pipeline' | ||
| import { createAxiomDrain } from 'evlog/axiom' | ||
| import { createOTLPDrain } from 'evlog/otlp' | ||
|
|
||
| export default defineNitroPlugin((nitroApp) => { | ||
| const axiom = createAxiomDrain() | ||
| const otlp = createOTLPDrain() | ||
|
|
||
| const pipeline = createDrainPipeline<DrainContext>() | ||
| const drain = pipeline(async (batch) => { | ||
| await Promise.allSettled([axiom(batch), otlp(batch)]) | ||
| }) | ||
|
|
||
| nitroApp.hooks.hook('evlog:drain', drain) | ||
| nitroApp.hooks.hook('close', () => drain.flush()) | ||
| }) | ||
| ``` | ||
|
|
||
| ## Custom Drain Function | ||
|
|
||
| You don't need an adapter — pass any async function that accepts a batch: | ||
|
|
||
| ```typescript [server/plugins/evlog-drain.ts] | ||
| import type { DrainContext } from 'evlog' | ||
| import { createDrainPipeline } from 'evlog/pipeline' | ||
|
|
||
| export default defineNitroPlugin((nitroApp) => { | ||
| const pipeline = createDrainPipeline<DrainContext>({ | ||
| batch: { size: 100 }, | ||
| }) | ||
|
|
||
| const drain = pipeline(async (batch) => { | ||
| await fetch('https://your-service.com/logs', { | ||
| method: 'POST', | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| body: JSON.stringify(batch.map(ctx => ctx.event)), | ||
| }) | ||
| }) | ||
|
|
||
| nitroApp.hooks.hook('evlog:drain', drain) | ||
| nitroApp.hooks.hook('close', () => drain.flush()) | ||
| }) | ||
| ``` | ||
|
|
||
| ## Next Steps | ||
|
|
||
| - [Adapters Overview](/adapters/overview) - Available built-in adapters | ||
| - [Custom Adapters](/adapters/custom) - Build your own drain function | ||
| - [Best Practices](/core-concepts/best-practices) - Security and production tips |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.