Files
WRNexusJSDoc/app/pages/packages/tracking.wrn
T

206 lines
14 KiB
Plaintext

page wrnexustracking {
seo {
title = "@wrnexus/tracking"
description = "Error/event capture, middleware, filtering, and sinks."
}
view {
<div class="docs-shell">
<a href="#main" class="skip-link">Skip to content</a>
<header class="topbar">
<a class="brand" href="/"><span>W</span> WRNexusJS</a>
<nav aria-label="Primary"><a href="/getting-started">Get started</a><a href="/packages">Packages</a><a href="https://component.wrnexusjs.dev/">Components</a><a href="/language">Language</a><a href="/architecture">Architecture</a></nav>
<div class="topbar-actions"><a class="preview-pill" href="/access">Private preview · v0.8.5</a><button data-wire-theme-toggle class="theme-button" aria-label="Toggle color theme" title="Toggle color theme">◐</button></div>
</header>
<div class="mobile-doc-nav"><details><summary>Browse documentation</summary><nav><a href="/getting-started">Get started</a><a href="/packages">Packages</a><a href="https://component.wrnexusjs.dev/">Components</a><a href="/language">Language</a><a href="/architecture">Architecture</a><a href="/tutorial">Tutorial</a><a href="/guides/project-structure">Guides</a><a href="/examples">Examples</a><a href="/search">Search</a></nav></details></div>
<main class="portal-main docs-layout">
<article id="main" class="documentation prose standalone package-document"><nav class="breadcrumbs" aria-label="Breadcrumb"><a href="/">Home</a><span>/</span><a href="/packages">Packages</a><span>/</span><span aria-current="page">@wrnexus/tracking</span></nav><section class="doc-intro"><span class="eyebrow">Runtime · Package reference</span><h1>@wrnexus/tracking</h1><p>Error/event capture, middleware, filtering, and sinks.</p><div class="doc-meta"><span>v0.8.5</span><span>Private registry</span><span>Runtime</span></div><section id="access" class="access-callout"><h2>Install the package</h2><p>After WorkRoot approves private registry access, install the release-aligned package:</p><pre><code>bun add @wrnexus/tracking@0.8.5</code><button type="button" class="copy-button" aria-label="Copy installation command">Copy</button></pre><p><a href="/access">Request preview access</a>. Never put registry tokens in source control.</p></section></section><section id="guide"><blockquote>Error tracking for WRNexusJS apps: capture exceptions manually or via middleware and fan them out to pluggable sinks.</blockquote>
<p>Part of the <strong>WRNexusJS</strong> framework — an SSR-first, Bun-native full-stack web framework.</p>
<h3 id="overview">Overview</h3>
<p><code>@wrnexus/tracking</code> is a small, server-side error-capture layer. You create a tracker with one or more <strong>sinks</strong>, then feed it errors — either manually with <code>tracker.capture(err, context)</code> or automatically by mounting <code>tracker.middleware()</code> in your request pipeline. A <code>consoleSink</code> is included; forwarding to Sentry, Datadog, or any other backend is just a matter of writing a tiny sink. Reach for it when you want a single, sink-agnostic place to route application errors. Sinks run best-effort — a throwing sink never breaks the request.</p>
<pre data-language="bash"><code>bun add @wrnexus/tracking</code></pre>
<blockquote>Private package — the machine must be authenticated to the <code>wrnexus</code> npm org</blockquote>
<blockquote>(a read token in <code>~/.npmrc</code>). Requires <strong>Bun</strong> (Node is not supported).</blockquote>
<h3 id="api">API</h3>
<h4 id="createtracker-options-tracker"><code>createTracker(options?): Tracker</code></h4>
<p>Creates a tracker. <code>TrackerOptions</code>:</p>
<div class="table-wrap"><table>
<thead><tr><th>Option</th><th>Type</th><th>Description</th></tr></thead>
<tbody><tr><td><code>sinks</code></td><td><code>ErrorSink[]</code></td><td>Initial sinks to fan events out to. Defaults to <code>[]</code>.</td></tr><tr><td><code>now</code></td><td><code>() =&gt; number</code></td><td>Clock used for <code>event.timestamp</code> (epoch ms). Defaults to <code>Date.now</code>.</td></tr><tr><td><code>beforeSend</code></td><td>`(event: ErrorEvent) =&gt; ErrorEvent \</td><td>null`</td><td>Scrub/enrich an event before it reaches any sink. Return <code>null</code> to drop it.</td></tr></tbody></table></div>
<p>The returned <code>Tracker</code>:</p>
<div class="table-wrap"><table>
<thead><tr><th>Member</th><th>Signature</th><th>Description</th></tr></thead>
<tbody><tr><td><code>capture</code></td><td><code>(error: unknown, context?: Record&lt;string, unknown&gt;) =&gt; Promise&lt;void&gt;</code></td><td>Normalizes any thrown value into an <code>Error</code>, builds an <code>ErrorEvent</code>, runs <code>beforeSend</code>, then dispatches to all sinks. Non-<code>Error</code> values are wrapped in an <code>Error</code> named <code>NonError</code>.</td></tr><tr><td><code>addSink</code></td><td><code>(sink: ErrorSink) =&gt; void</code></td><td>Registers an additional sink at runtime.</td></tr><tr><td><code>middleware</code></td><td><code>() =&gt; Middleware</code></td><td>Returns a WRNexusJS <code>Middleware</code> that captures any error thrown downstream, then re-throws it so the framework's error handler still produces the response.</td></tr></tbody></table></div>
<p>The middleware attaches this context to captured events:</p>
<pre data-language="ts"><code>&#123; method: ctx.req.method, path: ctx.url.pathname, requestId: ctx.locals.requestId &#125;</code></pre>
<h4 id="consolesink-errorsink"><code>consoleSink: ErrorSink</code></h4>
<p>A built-in sink that logs a compact one-line message via <code>console.error</code>, e.g. <code>[error] TypeError: cannot read x &#123;&quot;userId&quot;:42&#125;</code>.</p>
<h4 id="types">Types</h4>
<pre data-language="ts"><code>interface ErrorEvent &#123;
error: Error;
context: Record&lt;string, unknown&gt;; // request info, user id, tags…
timestamp: number; // epoch ms
&#125;
interface ErrorSink &#123;
name?: string;
capture(event: ErrorEvent): void | Promise&lt;void&gt;;
&#125;</code></pre>
<h3 id="usage">Usage</h3>
<p>Manual capture:</p>
<pre data-language="ts"><code>import &#123; createTracker, consoleSink &#125; from &quot;@wrnexus/tracking&quot;;
const tracker = createTracker(&#123; sinks: [consoleSink] &#125;);
try &#123;
await doWork();
&#125; catch (err) &#123;
await tracker.capture(err, &#123; userId: 42, op: &quot;doWork&quot; &#125;);
throw err;
&#125;</code></pre>
<p>As request middleware:</p>
<pre data-language="ts"><code>import &#123; createTracker, consoleSink &#125; from &quot;@wrnexus/tracking&quot;;
const tracker = createTracker(&#123; sinks: [consoleSink] &#125;);
app.use(tracker.middleware()); // captures + re-throws downstream errors</code></pre>
<p>A custom sink with <code>beforeSend</code> scrubbing:</p>
<pre data-language="ts"><code>import &#123; createTracker, type ErrorSink &#125; from &quot;@wrnexus/tracking&quot;;
const sentrySink: ErrorSink = &#123;
name: &quot;sentry&quot;,
async capture(event) &#123;
await Sentry.captureException(event.error, &#123; extra: event.context &#125;);
&#125;,
&#125;;
const tracker = createTracker(&#123;
sinks: [sentrySink],
beforeSend(event) &#123;
delete event.context.password; // scrub secrets
return event; // return null to drop the event entirely
&#125;,
&#125;);
tracker.addSink(anotherSink); // add more sinks later</code></pre>
<h3 id="requirements-notes">Requirements / Notes</h3>
<ul>
<li>Runs on <strong>Bun</strong> only (Node is not supported).</li>
<li>Peer package: [<code>@wrnexus/core</code>](../core) — the <code>Context</code> and <code>Middleware</code> types</li>
<p>used by <code>tracker.middleware()</code> come from there.</p>
<li>Sink dispatch is fire-and-forget-safe: all sinks run via <code>Promise.all</code>, and a</li>
<p>sink that throws is swallowed so it can never break the app.</p>
</ul></section><section id="api" class="api"><h2>Complete TypeScript API</h2><p>Generated from the exact installed package declarations.</p><pre data-language="typescript"><code>import &#123; Middleware &#125; from '@wrnexus/core';
type TelemetryKind = &quot;error&quot; | &quot;event&quot; | &quot;metric&quot; | &quot;span&quot;;
interface TelemetryEnvelope &#123;
id: string;
kind: TelemetryKind;
name: string;
timestamp: number;
traceId?: string;
userId?: string;
tenantId?: string;
attributes: Record&lt;string, unknown&gt;;
payload?: unknown;
&#125;
interface TelemetrySink &#123;
name?: string;
send(events: TelemetryEnvelope[]): void | Promise&lt;void&gt;;
&#125;
interface TelemetryPipeline &#123;
emit(input: Omit&lt;TelemetryEnvelope, &quot;id&quot; | &quot;timestamp&quot;&gt; &amp; Partial&lt;Pick&lt;TelemetryEnvelope, &quot;id&quot; | &quot;timestamp&quot;&gt;&gt;): Promise&lt;void&gt;;
flush(): Promise&lt;void&gt;;
close(): Promise&lt;void&gt;;
size(): number;
&#125;
interface TelemetryPipelineOptions &#123;
sinks: TelemetrySink[];
batchSize?: number;
flushIntervalMs?: number;
maxQueueSize?: number;
sampleRate?: number;
beforeSend?: (event: TelemetryEnvelope) =&gt; TelemetryEnvelope | null;
onSinkError?: (error: unknown, sink: TelemetrySink, events: readonly TelemetryEnvelope[]) =&gt; void | Promise&lt;void&gt;;
now?: () =&gt; number;
random?: () =&gt; number;
&#125;
declare function createTelemetryPipeline(options: TelemetryPipelineOptions): TelemetryPipeline;
declare const telemetryConsoleSink: TelemetrySink;
/**
* @wrnexus/tracking — error tracking with pluggable sinks. Capture exceptions
* manually or via middleware, and fan them out to any sink (console by default;
* write a small sink to forward to Sentry/Datadog/etc.).
*
* const tracker = createTracker(&#123; sinks: [consoleSink] &#125;);
* app-middleware: tracker.middleware() // captures + re-throws request errors
* tracker.capture(err, &#123; userId &#125;); // manual
*/
interface ErrorEvent &#123;
error: Error;
/** Arbitrary structured context (request info, user id, tags…). */
context: Record&lt;string, unknown&gt;;
/** Epoch ms. */
timestamp: number;
&#125;
interface ErrorSink &#123;
name?: string;
capture(event: ErrorEvent): void | Promise&lt;void&gt;;
&#125;
interface Tracker &#123;
capture(error: unknown, context?: Record&lt;string, unknown&gt;): Promise&lt;void&gt;;
addSink(sink: ErrorSink): void;
/** Middleware that captures errors thrown downstream, then re-throws them. */
middleware(): Middleware;
&#125;
interface TrackerOptions &#123;
sinks?: ErrorSink[];
now?: () =&gt; number;
/** Scrub/enrich an event before it hits sinks (return null to drop it). */
beforeSend?: (event: ErrorEvent) =&gt; ErrorEvent | null;
&#125;
/** A sink that logs a compact one-line error to the console. */
declare const consoleSink: ErrorSink;
declare function createTracker(options?: TrackerOptions): Tracker;
export &#123; type ErrorEvent, type ErrorSink, type TelemetryEnvelope, type TelemetryKind, type TelemetryPipeline, type TelemetryPipelineOptions, type TelemetrySink, type Tracker, type TrackerOptions, consoleSink, createTelemetryPipeline, createTracker, telemetryConsoleSink &#125;;
</code></pre></section><section id="examples" class="examples"><h2>Examples</h2><p>Copy-ready examples from the installed package documentation.</p><div class="example-grid"><article class="example-card"><h3>Manual capture</h3><pre data-language="ts"><code>import &#123; createTracker, consoleSink &#125; from &quot;@wrnexus/tracking&quot;;
const tracker = createTracker(&#123; sinks: [consoleSink] &#125;);
try &#123;
await doWork();
&#125; catch (err) &#123;
await tracker.capture(err, &#123; userId: 42, op: &quot;doWork&quot; &#125;);
throw err;
&#125;</code></pre></article><article class="example-card"><h3>As request middleware</h3><pre data-language="ts"><code>import &#123; createTracker, consoleSink &#125; from &quot;@wrnexus/tracking&quot;;
const tracker = createTracker(&#123; sinks: [consoleSink] &#125;);
app.use(tracker.middleware()); // captures + re-throws downstream errors</code></pre></article><article class="example-card"><h3>A custom sink with beforeSend scrubbing</h3><pre data-language="ts"><code>import &#123; createTracker, type ErrorSink &#125; from &quot;@wrnexus/tracking&quot;;
const sentrySink: ErrorSink = &#123;
name: &quot;sentry&quot;,
async capture(event) &#123;
await Sentry.captureException(event.error, &#123; extra: event.context &#125;);
&#125;,
&#125;;
const tracker = createTracker(&#123;
sinks: [sentrySink],
beforeSend(event) &#123;
delete event.context.password; // scrub secrets
return event; // return null to drop the event entirely
&#125;,
&#125;);
tracker.addSink(anotherSink); // add more sinks later</code></pre></article></div></section></article>
<aside class="on-this-page"><h2>On this page</h2><nav><a class="toc-level-2" href="#guide">Guide</a><a class="toc-level-3" href="#overview">Overview</a><a class="toc-level-3" href="#api">API</a><a class="toc-level-4" href="#createtracker-options-tracker">createTracker(options?): Tracker</a><a class="toc-level-4" href="#consolesink-errorsink">consoleSink: ErrorSink</a><a class="toc-level-4" href="#types">Types</a><a class="toc-level-3" href="#usage">Usage</a><a class="toc-level-3" href="#requirements-notes">Requirements / Notes</a><a class="toc-level-2" href="#api">Complete API</a><a class="toc-level-2" href="#examples">Examples</a></nav></aside>
</main>
<footer><div class="footer-brand"><span class="footer-mark" aria-hidden="true">W</span><p><strong>WRNexusJS 0.8.5</strong><span>Complete API documentation generated from installed package declarations.</span></p></div><nav aria-label="Footer"><a href="/packages">All packages</a><a href="/getting-started">Get started</a><a href="/security">Security</a><a href="/support">Support</a><a href="/llms.txt">AI guide</a></nav><p class="footer-meta">Private Developer Preview · Bun-native</p></footer>
</div>
}
}