@heystack/otel 0.4.2 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/workers.d.ts CHANGED
@@ -23,6 +23,11 @@ interface OtlpKeyValue {
23
23
  key: string;
24
24
  value: OtlpAnyValue;
25
25
  }
26
+ interface OtlpSpanEvent {
27
+ timeUnixNano: string;
28
+ name: string;
29
+ attributes: OtlpKeyValue[];
30
+ }
26
31
  interface OtlpSpan {
27
32
  traceId: string;
28
33
  spanId: string;
@@ -32,6 +37,7 @@ interface OtlpSpan {
32
37
  startTimeUnixNano: string;
33
38
  endTimeUnixNano: string;
34
39
  attributes: OtlpKeyValue[];
40
+ events: OtlpSpanEvent[];
35
41
  status: {
36
42
  code: number;
37
43
  message?: string;
@@ -54,6 +60,14 @@ interface OtlpTracesPayload {
54
60
  * hence the same resource, so we emit a single resourceSpans entry.
55
61
  */
56
62
  export declare function serializeSpans(spans: ReadableSpan[]): OtlpTracesPayload;
63
+ /** Parse a W3C `traceparent`. Returns null for malformed or all-zero ids. */
64
+ export declare function parseTraceparent(header: string | null): {
65
+ traceId: string;
66
+ spanId: string;
67
+ traceFlags: number;
68
+ } | null;
69
+ /** Add `value` to Access-Control-Expose-Headers without duplicating it. */
70
+ export declare function appendExposeHeader(headers: Headers, value: string): void;
57
71
  /**
58
72
  * Test-only helper: run the self-span attribute check directly against a plain
59
73
  * attribute bag + ingest hostname, without constructing a ReadableSpan. The
@@ -61,6 +75,12 @@ export declare function serializeSpans(spans: ReadableSpan[]): OtlpTracesPayload
61
75
  * the exporter derives via `safeHostname(cfg.url)`.
62
76
  */
63
77
  export declare function isSelfSpanForTest(attrs: Record<string, unknown>, ingestHost: string): boolean;
78
+ /**
79
+ * Reset the outbound-fetch instrumentation: restore the captured platform fetch
80
+ * (only when our wrapper is still the installed global) and clear the guard.
81
+ * Internal/testing helper.
82
+ */
83
+ export declare function __resetFetchInstrumentation(): void;
64
84
  /**
65
85
  * A WinterCG-compatible OTLP/JSON span exporter. POSTs ended spans to the
66
86
  * Heystack ingest using the platform `fetch` — no Node built-ins.
@@ -68,7 +88,7 @@ export declare function isSelfSpanForTest(attrs: Record<string, unknown>, ingest
68
88
  export declare class HeystackSpanExporter implements SpanExporter {
69
89
  private readonly url;
70
90
  /** Hostname (no port) of the ingest endpoint, used to drop self-trace spans. */
71
- private readonly ingestHost;
91
+ readonly ingestHost: string;
72
92
  private readonly headers;
73
93
  private shutdownState;
74
94
  /**
@@ -122,6 +142,23 @@ export interface WorkersConfig {
122
142
  * automatically borrows the request context's `ctx.waitUntil`.
123
143
  */
124
144
  waitUntil?: (p: Promise<unknown>) => void;
145
+ /**
146
+ * Optional hook called on each incoming request to supply identity context.
147
+ * Return `{ id }` to tag the SERVER span with `enduser.id`, `{ session }` for
148
+ * `session.id`, or `{ requestId }` to override `http.request.id` (otherwise
149
+ * the `cf-ray` request header is used as a fallback). Any field may be omitted.
150
+ */
151
+ getUser?: (req: Request) => {
152
+ id?: string;
153
+ session?: string;
154
+ requestId?: string;
155
+ } | undefined;
156
+ /**
157
+ * Declare which bindings to instrument with tracing (Task 5). Pass `true` to
158
+ * trace all bindings, or an array of binding names to trace selectively.
159
+ * Defaults to `false` (no binding tracing). Consumed by a later task.
160
+ */
161
+ instrumentBindings?: boolean | string[];
125
162
  }
126
163
  /**
127
164
  * A `BasicTracerProvider` with the underlying `HeystackSpanExporter` attached so
@@ -143,12 +180,31 @@ export type HeystackTracerProvider = BasicTracerProvider & {
143
180
  */
144
181
  export declare function createTracerProvider(config: HeystackOptions): HeystackTracerProvider;
145
182
  /**
146
- * A minimal SYNCHRONOUS, stack-based ContextManager the registered manager for
147
- * the /workers entry (no `node:async_hooks`, so it works on any WinterCG runtime).
148
- * It makes `context.with()` propagate synchronously, which is enough for the
149
- * exporter's `suppressTracing` to take effect and for the belt-and-suspenders
150
- * self-span filter but it does NOT carry context across `await` boundaries (so
151
- * cross-`await` parent linking and per-request isolation are best-effort).
183
+ * An AsyncLocalStorage-backed ContextManager for the /workers entry. When the
184
+ * runtime exposes `globalThis.AsyncLocalStorage` (workerd and Node do), this
185
+ * manager is preferred over `SyncStackContextManager` because it propagates
186
+ * context across `await` boundaries child spans created after an `await`
187
+ * inside a handler are correctly parented to the request's root span.
188
+ *
189
+ * WinterCG-safe: it does NOT import `node:async_hooks`; instead it uses the
190
+ * ALS global that the runtime exposes. Falls back to `SyncStackContextManager`
191
+ * when the global is absent (Deno, Bun without globals, etc.).
192
+ */
193
+ export declare class AlsContextManager implements ContextManager {
194
+ private _als;
195
+ constructor();
196
+ active(): Context;
197
+ with<A extends unknown[], F extends (...args: A) => ReturnType<F>>(ctx: Context, fn: F, thisArg?: ThisParameterType<F>, ...args: A): ReturnType<F>;
198
+ bind<T>(_ctx: Context, target: T): T;
199
+ enable(): this;
200
+ disable(): this;
201
+ }
202
+ /**
203
+ * A minimal SYNCHRONOUS, stack-based ContextManager — the fallback for the
204
+ * /workers entry when `globalThis.AsyncLocalStorage` is absent (any WinterCG
205
+ * runtime without the global). It makes `context.with()` propagate
206
+ * synchronously, which is enough for the exporter's `suppressTracing` to take
207
+ * effect — but it does NOT carry context across `await` boundaries.
152
208
  */
153
209
  export declare class SyncStackContextManager implements ContextManager {
154
210
  private _stack;
@@ -269,4 +325,22 @@ type EnvOf<H> = H extends {
269
325
  scheduled: (controller: ScheduledController, env: infer E, ctx: ExecutionContext) => unknown;
270
326
  } ? E : unknown;
271
327
  export declare function instrument<H extends WorkerHandler<any>>(handler: H, config: WorkersConfig): Instrumented<EnvOf<H>, H>;
328
+ /**
329
+ * Run `fn` inside a new child span named `name`. The span is automatically
330
+ * parented to the currently-active span (via `context.active()`), started
331
+ * before `fn` and ended in `finally` — so the caller never needs to call
332
+ * `span.end()`. If `fn` throws, the exception is recorded on the span and
333
+ * the status is set to ERROR before re-throwing.
334
+ *
335
+ * Two call signatures are supported:
336
+ * withSpan("name", async (span) => { ... })
337
+ * withSpan("name", { attr: "value" }, async (span) => { ... })
338
+ */
339
+ export declare function withSpan<T>(name: string, fn: (span: Span) => T | Promise<T>): Promise<T>;
340
+ export declare function withSpan<T>(name: string, attrs: Record<string, string | number | boolean>, fn: (span: Span) => T | Promise<T>): Promise<T>;
341
+ /**
342
+ * Add a named event (with optional attributes) to the currently-active span.
343
+ * No-op when no span is active.
344
+ */
345
+ export declare function addEvent(name: string, attrs?: Record<string, string | number | boolean>): void;
272
346
  export type { Span };