@hellsoft/vaka-node 0.0.0-stage → 0.3.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.
@@ -0,0 +1,196 @@
1
+ import type { Instrumentation } from '@opentelemetry/instrumentation';
2
+ import { parseStack, type Frame } from './stack';
3
+ import { currentScope, globalScope, withScope, type Breadcrumb, type Level, type User, type Scope } from './scope';
4
+ import { type Item } from './transport';
5
+ import { vakaLogRecordProcessor, vakaSpanExporter, VakaScopeSpanProcessor } from './otel';
6
+ export type { Frame, Breadcrumb, Level, User, Scope, Item };
7
+ export type { ReqLike } from "./integrations";
8
+ export { parseStack, withScope, currentScope, vakaSpanExporter, vakaLogRecordProcessor, VakaScopeSpanProcessor };
9
+ export type Props = Record<string, string | number | boolean | null | undefined>;
10
+ export type FlagValue = boolean | string;
11
+ export interface Options {
12
+ /** Project ingest key (vk_...). Default: `VAKA_KEY` or `VAKA_INGEST_KEY`. */
13
+ key?: string;
14
+ /** Vaka server URL, e.g. https://vaka.example.com. Default: `VAKA_ENDPOINT`. */
15
+ endpoint?: string;
16
+ /** App version. Default: `VAKA_RELEASE`, else `npm_package_version`. */
17
+ release?: string;
18
+ /** Default: `VAKA_ENVIRONMENT`, else `NODE_ENV`, else `production`. */
19
+ environment?: string;
20
+ /** OpenTelemetry `service.name`. Default: `OTEL_SERVICE_NAME`, else `npm_package_name`. */
21
+ serviceName?: string;
22
+ /**
23
+ * Start the OpenTelemetry Node SDK and export traces and logs to Vaka (default true). Set false
24
+ * when you run your own NodeSDK; add `vakaSpanExporter()` and `vakaLogRecordProcessor()` to it.
25
+ */
26
+ otel?: boolean;
27
+ /** Fraction of new traces to record, 0..1 (default 1). Incoming `traceparent` decisions win. */
28
+ tracesSampleRate?: number;
29
+ /** Export OpenTelemetry log records to Vaka (default true). */
30
+ logs?: boolean;
31
+ /**
32
+ * OpenTelemetry instrumentations (default: HTTP server and client only). Pass
33
+ * `getNodeAutoInstrumentations()` from `@opentelemetry/auto-instrumentations-node` for Express,
34
+ * Fastify, databases, queues and more.
35
+ */
36
+ instrumentations?: (Instrumentation | Instrumentation[])[];
37
+ /** Extra OpenTelemetry resource attributes. */
38
+ resourceAttributes?: Record<string, string | number | boolean>;
39
+ /** Report uncaught exceptions and unhandled rejections (default true). */
40
+ captureUnhandled?: boolean;
41
+ /**
42
+ * After an unhandled rejection: `strict` exits with code 1 like Node does by default, `warn`
43
+ * prints a warning and keeps running, `none` only reports. Default: follows
44
+ * `--unhandled-rejections`, else `strict`.
45
+ */
46
+ unhandledRejection?: 'strict' | 'warn' | 'none';
47
+ /** Exit with code 1 after reporting an uncaught exception, as Node does (default true). */
48
+ exitOnUncaught?: boolean;
49
+ /** How long to wait for delivery before exiting on a crash (default 2000 ms). */
50
+ shutdownTimeoutMs?: number;
51
+ /** Record console.warn/error and outgoing HTTP requests as breadcrumbs (default true). */
52
+ breadcrumbs?: boolean;
53
+ /** Maximum breadcrumbs kept per scope (default 100). */
54
+ maxBreadcrumbs?: number;
55
+ /** Read source lines around in-app frames from disk (default true). */
56
+ sourceContext?: boolean;
57
+ /** Fraction of errors to send, 0..1 (default 1). */
58
+ errorSampleRate?: number;
59
+ /** Errors whose message matches are dropped. */
60
+ ignoreErrors?: (string | RegExp)[];
61
+ /** Capture errors with these HTTP statuses from the middleware (default: status >= 500). */
62
+ shouldCaptureStatus?: (status: number) => boolean;
63
+ /** Cache flag evaluations per identity for this long (default 30000 ms; 0 disables). */
64
+ flagsCacheTtlMs?: number;
65
+ /** Modify or drop (return null) an item before it is sent. */
66
+ beforeSend?: (item: Item) => Item | null;
67
+ /** How often queued items are sent (default 2000 ms). Errors go out at once. */
68
+ flushIntervalMs?: number;
69
+ /** Log SDK activity to stderr. */
70
+ debug?: boolean;
71
+ }
72
+ export interface CaptureContext {
73
+ level?: Level;
74
+ tags?: Record<string, string>;
75
+ extra?: Record<string, unknown>;
76
+ handled?: boolean;
77
+ fingerprint?: string[];
78
+ user?: User;
79
+ }
80
+ export interface FlagContext {
81
+ /** Stable id for bucketing; defaults to the scope's user id. */
82
+ distinctId?: string;
83
+ userId?: string;
84
+ /** Properties targeting rules can match, e.g. `{ plan: 'pro' }`. */
85
+ properties?: Props;
86
+ }
87
+ export declare const SDK: {
88
+ name: string;
89
+ version: string;
90
+ };
91
+ /** Initialize the SDK. Call once, as early as possible (before importing your HTTP framework). */
92
+ export declare function init(options?: Options): void;
93
+ /** True after a successful init(). */
94
+ export declare function isInitialized(): boolean;
95
+ /** Set the user for the current scope (request inside middleware, else global). Opt-in. */
96
+ export declare function setUser(user: User | string | null): void;
97
+ export declare function setTag(key: string, value: string): void;
98
+ export declare function setTags(tags: Record<string, string>): void;
99
+ /** Attach structured context (e.g. `setContext('order', { id: 42 })`) to errors in this scope. */
100
+ export declare function setContext(name: string, value: Record<string, unknown> | null): void;
101
+ export declare function addBreadcrumb(b: Breadcrumb): void;
102
+ /** The active OpenTelemetry trace id, if any. */
103
+ export declare function traceId(): string | undefined;
104
+ /**
105
+ * Report an error. Returns the event id, or undefined when it was dropped. `fatal` marks a crash
106
+ * (the process is about to exit).
107
+ */
108
+ export declare function captureException(error: unknown, ctx?: CaptureContext, fatal?: boolean): string | undefined;
109
+ /** Alias of captureException, matching the browser SDK. */
110
+ export declare const captureError: typeof captureException;
111
+ /** Report a message as an error-like event. */
112
+ export declare function captureMessage(message: string, level?: Level, ctx?: CaptureContext): string | undefined;
113
+ /** A file attached to feedback: PNG, JPEG or WebP, at most 5 MB. */
114
+ export interface FeedbackAttachment {
115
+ filename: string;
116
+ data: Uint8Array | ArrayBuffer;
117
+ /** Default: guessed from the file name. */
118
+ contentType?: string;
119
+ }
120
+ export interface Feedback {
121
+ /** What the user wrote (required). Longer than 5000 characters is cut. */
122
+ message: string;
123
+ /** Contact email, only when the user gave it. */
124
+ email?: string;
125
+ name?: string;
126
+ /** The error event this feedback is about (the id captureException returned). */
127
+ eventId?: string;
128
+ /** The issue this feedback is about: id or short id like `API-12`. */
129
+ issue?: string;
130
+ /** Default: the current scope's user. */
131
+ userId?: string;
132
+ /** The end user's session id, when your app has one. */
133
+ sessionId?: string;
134
+ /** Page or screen the feedback is about. Default: the current request's URL. */
135
+ url?: string;
136
+ /**
137
+ * The end user's IP address (e.g. `req.ip`). Feedback is rate limited per address, so without it
138
+ * all feedback from this server shares one limit.
139
+ */
140
+ ip?: string;
141
+ tags?: Record<string, string>;
142
+ /** Up to 3 images. */
143
+ attachments?: FeedbackAttachment[];
144
+ }
145
+ export interface FeedbackResult {
146
+ id: string;
147
+ /** e.g. `API-F12` */
148
+ shortId: string;
149
+ }
150
+ /** Thrown by captureFeedback when the server rejects the feedback or cannot be reached. */
151
+ export declare class FeedbackError extends Error {
152
+ /** HTTP status, or 0 when nothing was sent. 429 means a rate limit was hit. */
153
+ status: number;
154
+ constructor(message: string, status?: number);
155
+ }
156
+ /**
157
+ * Forward feedback your users wrote (a support form, in-app chat) to Vaka, with the current
158
+ * scope's user, release, environment, tags and flags. Sent right away, not batched.
159
+ * Rejects with a FeedbackError when the server refuses it (e.g. 429 for a rate limit).
160
+ */
161
+ export declare function captureFeedback(f: Feedback): Promise<FeedbackResult>;
162
+ /** Record a custom server-side event (e.g. `track('order_paid', { amount: 49 })`). */
163
+ export declare function track(name: string, props?: Props, o?: {
164
+ userId?: string;
165
+ url?: string;
166
+ }): void;
167
+ /**
168
+ * Evaluate all flags for one person. The values are also attached to the current scope, so
169
+ * errors, events and spans in this request carry them as exposures.
170
+ */
171
+ export declare function getFlags(ctx?: FlagContext): Promise<Record<string, FlagValue>>;
172
+ /** One flag's value: `true`/`false`, or the variant key. Returns `fallback` when unknown or on failure. */
173
+ export declare function getFlag(key: string, fallback?: FlagValue, ctx?: FlagContext): Promise<FlagValue>;
174
+ /** True when a flag is on (any variant). */
175
+ export declare function isEnabled(key: string, ctx?: FlagContext): Promise<boolean>;
176
+ /** Express/Connect: isolates breadcrumbs, user and tags per request. `app.use(Vaka.requestHandler())` first. */
177
+ export declare function requestHandler(): (req: import("./integrations").ReqLike, _res: unknown, next: (err?: unknown) => void) => void;
178
+ /** Express: reports errors passed to `next(err)`. `app.use(Vaka.errorHandler())` after your routes. */
179
+ export declare function errorHandler(): (err: unknown, req: import("./integrations").ReqLike, res: {
180
+ statusCode?: number;
181
+ }, next: (err?: unknown) => void) => void;
182
+ /** Express: add both middlewares' error half to an existing app (call after routes). */
183
+ export declare function setupExpressErrorHandler(app: {
184
+ use: (...a: unknown[]) => unknown;
185
+ }): void;
186
+ /** Fastify: `await app.register(Vaka.fastifyPlugin)`. */
187
+ export declare const fastifyPlugin: any;
188
+ /** Wrap any `(req, res)` handler (node:http, serverless) so it gets a scope and errors are reported. */
189
+ export declare function wrapHandler<A extends unknown[], R>(fn: (...args: A) => R): (...args: A) => R;
190
+ /** Send queued items. Resolves true when everything was delivered within `timeoutMs`. */
191
+ export declare function flush(timeoutMs?: number): Promise<boolean>;
192
+ /** Flush errors and events, then shut down OpenTelemetry (flushing spans and logs). */
193
+ export declare function close(timeoutMs?: number): Promise<boolean>;
194
+ /** For tests: reset all state and remove hooks. */
195
+ export declare function _reset(): Promise<void>;
196
+ export { globalScope as _globalScope };