@watchtowerobservability/node 0.6.0 → 0.7.1
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/CHANGELOG.md +62 -0
- package/README.md +53 -5
- package/dist/cjs/buffer.d.ts +3 -0
- package/dist/cjs/checkin.d.ts +7 -0
- package/dist/cjs/checkin.js +18 -0
- package/dist/cjs/filter.d.ts +3 -0
- package/dist/cjs/filter.js +16 -0
- package/dist/cjs/index.d.ts +25 -3
- package/dist/cjs/index.js +34 -6
- package/dist/cjs/instrument.d.ts +5 -2
- package/dist/cjs/instrument.js +26 -3
- package/dist/cjs/integrations/db.d.ts +3 -0
- package/dist/cjs/integrations/db.js +4 -1
- package/dist/cjs/integrations/next-build.d.ts +31 -0
- package/dist/cjs/integrations/next-build.js +95 -0
- package/dist/cjs/integrations/next-tunnel.d.ts +14 -0
- package/dist/cjs/integrations/next-tunnel.js +52 -0
- package/dist/cjs/integrations/next.d.ts +22 -1
- package/dist/cjs/integrations/next.js +28 -2
- package/dist/cjs/load.js +20 -35
- package/dist/cjs/metrics.d.ts +2 -0
- package/dist/cjs/next-sourcemaps-cli.d.ts +2 -0
- package/dist/cjs/next-sourcemaps-cli.js +9 -0
- package/dist/cjs/origin.d.ts +1 -0
- package/dist/cjs/redact.d.ts +1 -0
- package/dist/cjs/register.js +11 -0
- package/dist/cjs/sampling.d.ts +1 -0
- package/dist/cjs/scrub.d.ts +8 -0
- package/dist/cjs/scrub.js +39 -0
- package/dist/cjs/shutdown.d.ts +14 -0
- package/dist/cjs/shutdown.js +26 -0
- package/dist/cjs/sql.js +11 -1
- package/dist/cjs/version.d.ts +2 -0
- package/dist/cjs/version.js +5 -0
- package/dist/esm/buffer.d.ts +3 -0
- package/dist/esm/checkin.d.ts +7 -0
- package/dist/esm/checkin.js +15 -0
- package/dist/esm/filter.d.ts +3 -0
- package/dist/esm/filter.js +13 -0
- package/dist/esm/index.d.ts +25 -3
- package/dist/esm/index.js +32 -5
- package/dist/esm/instrument.d.ts +5 -2
- package/dist/esm/instrument.js +26 -3
- package/dist/esm/integrations/db.d.ts +3 -0
- package/dist/esm/integrations/db.js +5 -2
- package/dist/esm/integrations/next-build.d.ts +31 -0
- package/dist/esm/integrations/next-build.js +58 -0
- package/dist/esm/integrations/next-tunnel.d.ts +14 -0
- package/dist/esm/integrations/next-tunnel.js +49 -0
- package/dist/esm/integrations/next.d.ts +22 -1
- package/dist/esm/integrations/next.js +22 -1
- package/dist/esm/load.js +20 -60
- package/dist/esm/metrics.d.ts +2 -0
- package/dist/esm/next-sourcemaps-cli.d.ts +2 -0
- package/dist/esm/next-sourcemaps-cli.js +7 -0
- package/dist/esm/origin.d.ts +1 -0
- package/dist/esm/redact.d.ts +1 -0
- package/dist/esm/register.js +11 -0
- package/dist/esm/sampling.d.ts +1 -0
- package/dist/esm/scrub.d.ts +8 -0
- package/dist/esm/scrub.js +34 -0
- package/dist/esm/shutdown.d.ts +14 -0
- package/dist/esm/shutdown.js +23 -0
- package/dist/esm/sql.js +11 -1
- package/dist/esm/version.d.ts +2 -0
- package/dist/esm/version.js +2 -0
- package/package.json +10 -2
package/dist/esm/instrument.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// Auto-instrumentation: node:http, fetch (undici), pg and ioredis, each only when its optional peer package is installed.
|
|
2
2
|
import { load } from './load.js';
|
|
3
|
+
import { instrumentBetterSqlite3 } from './integrations/sqlite.js';
|
|
4
|
+
import { instrumentPostgres } from './integrations/postgres.js';
|
|
3
5
|
const ENTRIES = [
|
|
4
6
|
{
|
|
5
7
|
pkg: '@opentelemetry/instrumentation-http', ctor: 'HttpInstrumentation',
|
|
@@ -12,15 +14,24 @@ const ENTRIES = [
|
|
|
12
14
|
},
|
|
13
15
|
{ pkg: '@opentelemetry/instrumentation-pg', ctor: 'PgInstrumentation', opts: () => ({}) },
|
|
14
16
|
{ pkg: '@opentelemetry/instrumentation-ioredis', ctor: 'IORedisInstrumentation', opts: () => ({}) },
|
|
17
|
+
{ pkg: '@opentelemetry/instrumentation-mysql2', ctor: 'MySQL2Instrumentation', opts: () => ({}), auto: true },
|
|
15
18
|
];
|
|
16
|
-
/**
|
|
17
|
-
|
|
19
|
+
/** Drivers patched in place (no OTel package): `instrument: 'auto'` only. Each loads the app's installed copy or is skipped. */
|
|
20
|
+
const DRIVERS = [
|
|
21
|
+
{ name: 'postgres.js', run: () => { load('postgres'); instrumentPostgres(); } }, // only when `postgres` is installed
|
|
22
|
+
{ name: 'better-sqlite3', run: () => instrumentBetterSqlite3(load('better-sqlite3')) },
|
|
23
|
+
];
|
|
24
|
+
/**
|
|
25
|
+
* Register whichever of the OTel instrumentations is installed (http, undici, pg, ioredis; with `auto` also mysql2) and,
|
|
26
|
+
* with `auto`, patch postgres.js and better-sqlite3 when installed. Returns the names that were enabled.
|
|
27
|
+
*/
|
|
28
|
+
export function instrumentAll(p, selfUrls, auto = false) {
|
|
18
29
|
let registerInstrumentations;
|
|
19
30
|
try {
|
|
20
31
|
({ registerInstrumentations } = load('@opentelemetry/instrumentation'));
|
|
21
32
|
}
|
|
22
33
|
catch {
|
|
23
|
-
return []; // optional peer not installed
|
|
34
|
+
return auto ? patchDrivers([]) : []; // optional peer not installed
|
|
24
35
|
}
|
|
25
36
|
const self = selfUrls.flatMap((u) => { try {
|
|
26
37
|
return [new URL(u)];
|
|
@@ -31,6 +42,8 @@ export function instrumentAll(p, selfUrls) {
|
|
|
31
42
|
const found = [];
|
|
32
43
|
const names = [];
|
|
33
44
|
for (const e of ENTRIES) {
|
|
45
|
+
if (e.auto && !auto)
|
|
46
|
+
continue;
|
|
34
47
|
try {
|
|
35
48
|
const Ctor = load(e.pkg)[e.ctor];
|
|
36
49
|
if (Ctor) {
|
|
@@ -44,5 +57,15 @@ export function instrumentAll(p, selfUrls) {
|
|
|
44
57
|
}
|
|
45
58
|
if (found.length)
|
|
46
59
|
registerInstrumentations({ instrumentations: found, tracerProvider: p.tracerProvider, meterProvider: p.meterProvider });
|
|
60
|
+
return auto ? patchDrivers(names) : names;
|
|
61
|
+
}
|
|
62
|
+
function patchDrivers(names) {
|
|
63
|
+
for (const d of DRIVERS) {
|
|
64
|
+
try {
|
|
65
|
+
d.run();
|
|
66
|
+
names.push(d.name);
|
|
67
|
+
}
|
|
68
|
+
catch { /* driver not installed */ }
|
|
69
|
+
}
|
|
47
70
|
return names;
|
|
48
71
|
}
|
|
@@ -5,6 +5,9 @@ export interface DbOptions {
|
|
|
5
5
|
/** Flag a statement run more than this many times under one parent span. Default:
|
|
6
6
|
* `WATCHTOWER_DB_N_PLUS_ONE` or 5; 0 turns it off. */
|
|
7
7
|
nPlusOne?: number;
|
|
8
|
+
/** Record a query only when a span is active (a request, job or `observe` span). Default true (`WATCHTOWER_DB_REQUIRE_PARENT=0`
|
|
9
|
+
* turns it off): startup DDL, migrations and polling loops run outside any span and would otherwise each become a root trace. */
|
|
10
|
+
requireParent?: boolean;
|
|
8
11
|
}
|
|
9
12
|
export interface DbOp {
|
|
10
13
|
/** `db.system.name`, e.g. `sqlite`. */
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// statement hash and N+1 hint, connection-pool metrics and embedded-store gauges.
|
|
3
3
|
import * as fs from 'node:fs';
|
|
4
4
|
import * as nodePath from 'node:path';
|
|
5
|
-
import { context, createContextKey, metrics, trace, SpanKind, SpanStatusCode } from '@opentelemetry/api';
|
|
5
|
+
import { context, createContextKey, INVALID_SPAN_CONTEXT, metrics, trace, SpanKind, SpanStatusCode } from '@opentelemetry/api';
|
|
6
6
|
import { isPgDialect, normalizeAndHash, querySummary } from '../sql.js';
|
|
7
7
|
/** Set inside an instrumented call so a wrapper the library calls internally adds no second span. */
|
|
8
8
|
const INSIDE = createContextKey('watchtower.db.inside');
|
|
@@ -27,6 +27,10 @@ function repeats(parent, key) {
|
|
|
27
27
|
}
|
|
28
28
|
/** A CLIENT span for `op` (not made active): summary, hash, N+1 hint. Prefer `traced`. */
|
|
29
29
|
export function startDbSpan(op, opts, parent, startTime) {
|
|
30
|
+
const p = trace.getSpan(parent)?.spanContext();
|
|
31
|
+
const requireParent = opts?.requireParent ?? !['0', 'false', 'off', 'no'].includes((process.env.WATCHTOWER_DB_REQUIRE_PARENT || '').trim().toLowerCase());
|
|
32
|
+
if (requireParent && !(p && trace.isSpanContextValid(p)))
|
|
33
|
+
return trace.wrapSpanContext(INVALID_SPAN_CONTEXT); // non-recording: nothing is exported
|
|
30
34
|
const attrs = { 'db.system.name': op.system, ...op.attributes };
|
|
31
35
|
let { operation, collection } = op;
|
|
32
36
|
let name = [operation, collection].filter(Boolean).join(' ') || op.system;
|
|
@@ -49,7 +53,6 @@ export function startDbSpan(op, opts, parent, startTime) {
|
|
|
49
53
|
attrs['db.collection.name'] = collection;
|
|
50
54
|
if (op.namespace)
|
|
51
55
|
attrs['db.namespace'] = op.namespace;
|
|
52
|
-
const p = trace.getSpan(parent)?.spanContext();
|
|
53
56
|
const max = threshold(opts);
|
|
54
57
|
if (p && max > 0) {
|
|
55
58
|
const n = repeats(p.spanId, key);
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export interface SourcemapOptions {
|
|
2
|
+
/** Directory holding the client build; default `.next/static`. */
|
|
3
|
+
dir?: string;
|
|
4
|
+
/** Default WT_API_URL, then https://api.watchtower.observer. */
|
|
5
|
+
apiUrl?: string;
|
|
6
|
+
/** A key with scope `ci:write` (it covers releases and artifacts; `admin:write` also works). Default WT_API_KEY. */
|
|
7
|
+
apiKey?: string;
|
|
8
|
+
/** Project id or slug the maps belong to. Default WT_PROJECT. */
|
|
9
|
+
project?: string;
|
|
10
|
+
/** Release version: must equal the `release` the browser SDK reports. Default WT_RELEASE. */
|
|
11
|
+
release?: string;
|
|
12
|
+
/** Keep the .map files after a successful upload (default: delete them so they are never served). */
|
|
13
|
+
keep?: boolean;
|
|
14
|
+
fetch?: typeof fetch;
|
|
15
|
+
log?: (line: string) => void;
|
|
16
|
+
}
|
|
17
|
+
type NextConfig = Record<string, unknown>;
|
|
18
|
+
type ConfigOrFn = NextConfig | ((...a: unknown[]) => NextConfig | Promise<NextConfig>);
|
|
19
|
+
/**
|
|
20
|
+
* `module.exports = withWatchtower(nextConfig, { sourcemaps: true })`: turns on `productionBrowserSourceMaps` so
|
|
21
|
+
* `next build` writes client maps (works with webpack and Turbopack). Then run `wt-next-sourcemaps` after the build
|
|
22
|
+
* (`"build": "next build && wt-next-sourcemaps"`) to upload them and delete the .map files from `.next/static`.
|
|
23
|
+
*/
|
|
24
|
+
export declare function withWatchtower(config?: ConfigOrFn, opts?: {
|
|
25
|
+
sourcemaps?: boolean;
|
|
26
|
+
}): ConfigOrFn;
|
|
27
|
+
/** Upload every `.map` under `dir` as `_next/static/<path>` for the release, then delete them. Throws (leaving the files) when an upload fails. */
|
|
28
|
+
export declare function uploadSourcemaps(o?: SourcemapOptions): Promise<{
|
|
29
|
+
uploaded: number;
|
|
30
|
+
}>;
|
|
31
|
+
export {};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// Build-time helpers for Next.js: `withWatchtower` (next.config) and `uploadSourcemaps` (after `next build`).
|
|
2
|
+
import * as fs from 'node:fs';
|
|
3
|
+
import * as nodePath from 'node:path';
|
|
4
|
+
/**
|
|
5
|
+
* `module.exports = withWatchtower(nextConfig, { sourcemaps: true })`: turns on `productionBrowserSourceMaps` so
|
|
6
|
+
* `next build` writes client maps (works with webpack and Turbopack). Then run `wt-next-sourcemaps` after the build
|
|
7
|
+
* (`"build": "next build && wt-next-sourcemaps"`) to upload them and delete the .map files from `.next/static`.
|
|
8
|
+
*/
|
|
9
|
+
export function withWatchtower(config = {}, opts = {}) {
|
|
10
|
+
const apply = (c) => (opts.sourcemaps ? { ...c, productionBrowserSourceMaps: true } : c);
|
|
11
|
+
if (typeof config !== 'function')
|
|
12
|
+
return apply(config);
|
|
13
|
+
return async (...a) => apply(await config(...a));
|
|
14
|
+
}
|
|
15
|
+
function* maps(dir) {
|
|
16
|
+
for (const e of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
17
|
+
const p = nodePath.join(dir, e.name);
|
|
18
|
+
if (e.isDirectory())
|
|
19
|
+
yield* maps(p);
|
|
20
|
+
else if (e.name.endsWith('.map'))
|
|
21
|
+
yield p;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/** Upload every `.map` under `dir` as `_next/static/<path>` for the release, then delete them. Throws (leaving the files) when an upload fails. */
|
|
25
|
+
export async function uploadSourcemaps(o = {}) {
|
|
26
|
+
const env = process.env;
|
|
27
|
+
const key = o.apiKey || env.WT_API_KEY;
|
|
28
|
+
const project = o.project || env.WT_PROJECT;
|
|
29
|
+
const release = o.release || env.WT_RELEASE;
|
|
30
|
+
if (!key)
|
|
31
|
+
throw new Error('WT_API_KEY is required (a key with scope ci:write)');
|
|
32
|
+
if (!project)
|
|
33
|
+
throw new Error('WT_PROJECT (project id) is required');
|
|
34
|
+
if (!release)
|
|
35
|
+
throw new Error('WT_RELEASE is required');
|
|
36
|
+
const dir = nodePath.resolve(o.dir || '.next/static');
|
|
37
|
+
const f = o.fetch || fetch;
|
|
38
|
+
const log = o.log || (() => { });
|
|
39
|
+
const root = `${(o.apiUrl || env.WT_API_URL || 'https://api.watchtower.observer').replace(/\/+$/, '')}/v1/projects/${encodeURIComponent(project)}`;
|
|
40
|
+
const call = async (path, init, okStatus = []) => {
|
|
41
|
+
const res = await f(root + path, { ...init, headers: { authorization: `Bearer ${key}`, ...init.headers } });
|
|
42
|
+
if (!res.ok && !okStatus.includes(res.status))
|
|
43
|
+
throw new Error(`watchtower ${init.method} ${path.split('?')[0]} -> HTTP ${res.status}`);
|
|
44
|
+
};
|
|
45
|
+
const files = fs.existsSync(dir) ? [...maps(dir)] : [];
|
|
46
|
+
await call('/releases', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ version: release }) }, [409]); // 409: exists
|
|
47
|
+
for (const file of files) {
|
|
48
|
+
const name = `_next/static/${nodePath.relative(dir, file).split(nodePath.sep).join('/')}`;
|
|
49
|
+
await call(`/artifacts?${new URLSearchParams({ kind: 'sourcemap', name, release })}`, {
|
|
50
|
+
method: 'POST', headers: { 'content-type': 'application/octet-stream' }, body: new Uint8Array(fs.readFileSync(file)),
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
if (!o.keep)
|
|
54
|
+
for (const file of files)
|
|
55
|
+
fs.rmSync(file);
|
|
56
|
+
log(`uploaded ${files.length} source maps for release ${release}${o.keep ? '' : ' and deleted them'}`);
|
|
57
|
+
return { uploaded: files.length };
|
|
58
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface TunnelOptions {
|
|
2
|
+
/** Edge base URL the requests are forwarded to. */
|
|
3
|
+
endpoint: string;
|
|
4
|
+
/** Route prefix to strip; default `/api/wt`. */
|
|
5
|
+
basePath?: string;
|
|
6
|
+
/** Request body cap in bytes; default 1 MiB (over it: 413). */
|
|
7
|
+
maxBodyBytes?: number;
|
|
8
|
+
fetch?: typeof fetch;
|
|
9
|
+
}
|
|
10
|
+
/** Next route handlers `{ GET, POST }` forwarding allow-listed RUM paths to the edge. Only the publishable key (`?k=` or a `wtp_` bearer) is forwarded as auth; cookies and other headers are not. */
|
|
11
|
+
export declare function tunnelHandler(o: TunnelOptions): {
|
|
12
|
+
GET: (r: Request) => Promise<Response>;
|
|
13
|
+
POST: (r: Request) => Promise<Response>;
|
|
14
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// First-party tunnel for browser telemetry (ad blockers block third-party telemetry hosts).
|
|
2
|
+
// app/api/wt/[...path]/route.ts: export const { GET, POST } = tunnelHandler({ endpoint: process.env.WT_EDGE_URL! });
|
|
3
|
+
// Browser SDK: init({ endpoint: '/api/wt', publishableKey: 'wtp_...' }).
|
|
4
|
+
// only what the browser SDK calls: /v1/rum/*, /v1/replay/<session>/chunks and /v1/logs
|
|
5
|
+
const ALLOWED = /^\/v1\/(rum\/[A-Za-z0-9/_-]+|replay\/[A-Za-z0-9_-]+\/chunks|logs)$/;
|
|
6
|
+
const FORWARD = ['content-type', 'content-encoding', 'user-agent'];
|
|
7
|
+
/** Next route handlers `{ GET, POST }` forwarding allow-listed RUM paths to the edge. Only the publishable key (`?k=` or a `wtp_` bearer) is forwarded as auth; cookies and other headers are not. */
|
|
8
|
+
export function tunnelHandler(o) {
|
|
9
|
+
const base = (o.basePath ?? '/api/wt').replace(/\/+$/, '');
|
|
10
|
+
const max = o.maxBodyBytes ?? 1024 * 1024;
|
|
11
|
+
const edge = o.endpoint.replace(/\/+$/, '');
|
|
12
|
+
const send = o.fetch ?? fetch;
|
|
13
|
+
const handle = async (req) => {
|
|
14
|
+
const url = new URL(req.url);
|
|
15
|
+
const path = url.pathname.startsWith(base) ? url.pathname.slice(base.length) : url.pathname;
|
|
16
|
+
if (!ALLOWED.test(path) || path.includes('..'))
|
|
17
|
+
return new Response('not found', { status: 404 });
|
|
18
|
+
const headers = {};
|
|
19
|
+
for (const h of FORWARD) {
|
|
20
|
+
const v = req.headers.get(h);
|
|
21
|
+
if (v)
|
|
22
|
+
headers[h] = v;
|
|
23
|
+
}
|
|
24
|
+
const bearer = /^Bearer (wtp_\S+)$/.exec(req.headers.get('authorization') ?? '');
|
|
25
|
+
if (bearer)
|
|
26
|
+
headers.authorization = `Bearer ${bearer[1]}`;
|
|
27
|
+
const k = url.searchParams.get('k');
|
|
28
|
+
let body;
|
|
29
|
+
if (req.method === 'POST') {
|
|
30
|
+
if (Number(req.headers.get('content-length') ?? 0) > max)
|
|
31
|
+
return new Response('payload too large', { status: 413 });
|
|
32
|
+
body = await req.arrayBuffer();
|
|
33
|
+
if (body.byteLength > max)
|
|
34
|
+
return new Response('payload too large', { status: 413 });
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
const res = await send(`${edge}${path}${k ? `?k=${encodeURIComponent(k)}` : ''}`, { method: req.method, headers, body });
|
|
38
|
+
const out = new Headers();
|
|
39
|
+
const ct = res.headers.get('content-type');
|
|
40
|
+
if (ct)
|
|
41
|
+
out.set('content-type', ct);
|
|
42
|
+
return new Response(res.status === 204 ? null : await res.arrayBuffer(), { status: res.status, headers: out });
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return new Response('bad gateway', { status: 502 });
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
return { GET: handle, POST: handle };
|
|
49
|
+
}
|
|
@@ -1,6 +1,27 @@
|
|
|
1
|
+
// @ts-ignore optional peer: absent -> any (keeps skipLibCheck:false consumers compiling)
|
|
2
|
+
import type { ReadableSpan } from '@opentelemetry/sdk-trace';
|
|
1
3
|
import * as wt from '../index.js';
|
|
4
|
+
export { withWatchtower, uploadSourcemaps } from './next-build.js';
|
|
5
|
+
export type { SourcemapOptions } from './next-build.js';
|
|
6
|
+
export { tunnelHandler } from './next-tunnel.js';
|
|
7
|
+
export type { TunnelOptions } from './next-tunnel.js';
|
|
8
|
+
export interface NextOptions extends wt.InitOptions {
|
|
9
|
+
/**
|
|
10
|
+
* Next.js's own spans (`next.span_type` set: load route module, prepare route module, resolve page components, start
|
|
11
|
+
* response, middleware, generateMetadata ...). `"sample"` (default) keeps `nextInternalsRate` of them, `"drop"` none,
|
|
12
|
+
* `"keep"` all. Request (`BaseServer.handleRequest`), route handler, render, server action and fetch spans, every
|
|
13
|
+
* SERVER/CLIENT span and every errored span are always kept. A dropped span's children stay in the trace.
|
|
14
|
+
*/
|
|
15
|
+
nextInternals?: 'drop' | 'sample' | 'keep';
|
|
16
|
+
/** Share of internal spans kept with `nextInternals: "sample"`; default 0.1. */
|
|
17
|
+
nextInternalsRate?: number;
|
|
18
|
+
}
|
|
19
|
+
/** Whether `span` is one of Next's internal spans (not a request, route handler, render, server action or fetch). */
|
|
20
|
+
export declare function isNextInternal(span: ReadableSpan): boolean;
|
|
21
|
+
/** The `spanFilter` for a `nextInternals` mode; `rand` is injectable for tests. */
|
|
22
|
+
export declare function nextInternalsFilter(mode?: 'drop' | 'sample' | 'keep', rate?: number, rand?: () => number): (s: ReadableSpan) => boolean;
|
|
2
23
|
/** Build a Next.js instrumentation `register` that passes `options` to `init`; no-op off the Node.js runtime or without a key. */
|
|
3
|
-
export declare function makeRegister(options?:
|
|
24
|
+
export declare function makeRegister(options?: NextOptions): () => Promise<void>;
|
|
4
25
|
/** Next.js instrumentation hook: init from WT_* env on the Node.js runtime; no-op without WT_INGEST_KEY. */
|
|
5
26
|
export declare const register: () => Promise<void>;
|
|
6
27
|
/** Next.js onRequestError: server render, route handler and server action errors. */
|
|
@@ -1,13 +1,34 @@
|
|
|
1
1
|
// instrumentation.js: module.exports = require('@watchtowerobservability/node/next');
|
|
2
2
|
// or, with options: export const register = makeRegister({ redact }); export { onRequestError } from '@watchtowerobservability/node/next';
|
|
3
|
+
import { SpanStatusCode } from '@opentelemetry/api';
|
|
3
4
|
import * as wt from '../index.js';
|
|
5
|
+
export { withWatchtower, uploadSourcemaps } from './next-build.js';
|
|
6
|
+
export { tunnelHandler } from './next-tunnel.js';
|
|
7
|
+
const KEEP_TYPES = new Set([
|
|
8
|
+
'BaseServer.handleRequest', 'AppRouteRouteHandlers.runHandler', 'AppRender.fetch', 'AppRender.getBodyResult',
|
|
9
|
+
'Render.getServerSideProps', 'Render.getStaticProps', 'NextNodeServer.runApi',
|
|
10
|
+
]);
|
|
11
|
+
const KEEP_NAMES = /^(render route|executing api route|fetch |.*server action)/i;
|
|
12
|
+
/** Whether `span` is one of Next's internal spans (not a request, route handler, render, server action or fetch). */
|
|
13
|
+
export function isNextInternal(span) {
|
|
14
|
+
const type = span.attributes['next.span_type'];
|
|
15
|
+
if (typeof type !== 'string')
|
|
16
|
+
return false;
|
|
17
|
+
return !KEEP_TYPES.has(type) && !KEEP_NAMES.test(span.name) && span.kind !== 1 && span.kind !== 2 && span.status.code !== SpanStatusCode.ERROR;
|
|
18
|
+
}
|
|
19
|
+
/** The `spanFilter` for a `nextInternals` mode; `rand` is injectable for tests. */
|
|
20
|
+
export function nextInternalsFilter(mode = 'sample', rate = 0.1, rand = Math.random) {
|
|
21
|
+
return (s) => mode === 'keep' || !isNextInternal(s) || (mode === 'sample' && rand() < rate);
|
|
22
|
+
}
|
|
4
23
|
/** Build a Next.js instrumentation `register` that passes `options` to `init`; no-op off the Node.js runtime or without a key. */
|
|
5
24
|
export function makeRegister(options = {}) {
|
|
6
25
|
return async function register() {
|
|
7
26
|
const rt = process.env.NEXT_RUNTIME;
|
|
8
27
|
if ((rt && rt !== 'nodejs') || !(options.apiKey || process.env.WT_INGEST_KEY))
|
|
9
28
|
return;
|
|
10
|
-
|
|
29
|
+
const { nextInternals, nextInternalsRate, ...init } = options;
|
|
30
|
+
const own = nextInternalsFilter(nextInternals, nextInternalsRate);
|
|
31
|
+
wt.init({ ...init, spanFilter: init.spanFilter ? (s) => own(s) && init.spanFilter(s) : own });
|
|
11
32
|
};
|
|
12
33
|
}
|
|
13
34
|
/** Next.js instrumentation hook: init from WT_* env on the Node.js runtime; no-op without WT_INGEST_KEY. */
|
package/dist/esm/load.js
CHANGED
|
@@ -1,66 +1,26 @@
|
|
|
1
|
-
// Optional-peer loader
|
|
2
|
-
// file tracer) can follow it; a computed `require(name)` is invisible to them. This is the CommonJS form;
|
|
3
|
-
// scripts/build.mjs prepends `const require = createRequire(import.meta.url)` for dist/esm (webpack follows that too).
|
|
4
|
-
// The cwd fallback only matters when `require` does not exist (test runners running the TS source as ESM).
|
|
1
|
+
// Optional-peer loader (same source for the CommonJS and ESM builds).
|
|
5
2
|
import { createRequire } from 'node:module';
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
try { pre["@opentelemetry/resources"] = await import("@opentelemetry/resources"); } catch (e) { pre["@opentelemetry/resources"] = e; }
|
|
14
|
-
try { pre["@opentelemetry/sdk-logs"] = await import("@opentelemetry/sdk-logs"); } catch (e) { pre["@opentelemetry/sdk-logs"] = e; }
|
|
15
|
-
try { pre["@opentelemetry/sdk-metrics"] = await import("@opentelemetry/sdk-metrics"); } catch (e) { pre["@opentelemetry/sdk-metrics"] = e; }
|
|
16
|
-
try { pre["@opentelemetry/sdk-trace"] = await import("@opentelemetry/sdk-trace"); } catch (e) { pre["@opentelemetry/sdk-trace"] = e; }
|
|
17
|
-
try { pre["@opentelemetry/instrumentation-runtime-node"] = await import("@opentelemetry/instrumentation-runtime-node"); } catch (e) { pre["@opentelemetry/instrumentation-runtime-node"] = e; }
|
|
18
|
-
try { pre["@opentelemetry/instrumentation"] = await import("@opentelemetry/instrumentation"); } catch (e) { pre["@opentelemetry/instrumentation"] = e; }
|
|
19
|
-
try { pre["@opentelemetry/instrumentation-http"] = await import("@opentelemetry/instrumentation-http"); } catch (e) { pre["@opentelemetry/instrumentation-http"] = e; }
|
|
20
|
-
try { pre["@opentelemetry/instrumentation-undici"] = await import("@opentelemetry/instrumentation-undici"); } catch (e) { pre["@opentelemetry/instrumentation-undici"] = e; }
|
|
21
|
-
try { pre["@opentelemetry/instrumentation-pg"] = await import("@opentelemetry/instrumentation-pg"); } catch (e) { pre["@opentelemetry/instrumentation-pg"] = e; }
|
|
22
|
-
try { pre["@opentelemetry/instrumentation-ioredis"] = await import("@opentelemetry/instrumentation-ioredis"); } catch (e) { pre["@opentelemetry/instrumentation-ioredis"] = e; }
|
|
23
|
-
try { pre["winston-transport"] = await import("winston-transport"); } catch (e) { pre["winston-transport"] = e; }
|
|
24
|
-
const nodeRequire = createRequire(import.meta.url);
|
|
25
|
-
const require = (id) => {
|
|
26
|
-
const m = pre[id];
|
|
27
|
-
if (m === undefined) return nodeRequire(id);
|
|
28
|
-
if (m instanceof Error) throw Object.assign(new Error(m.message), { code: m.code === 'ERR_MODULE_NOT_FOUND' ? 'MODULE_NOT_FOUND' : m.code });
|
|
29
|
-
return m.default && typeof m.default === 'object' ? { ...m.default, ...m } : m;
|
|
30
|
-
};
|
|
31
|
-
const NONE = Symbol('not a known peer');
|
|
32
|
-
function known(id) {
|
|
33
|
-
try { // bundlers only tolerate an optional require that is lexically inside try
|
|
34
|
-
switch (id) {
|
|
35
|
-
case '@opentelemetry/context-async-hooks': return require('@opentelemetry/context-async-hooks');
|
|
36
|
-
case '@opentelemetry/core': return require('@opentelemetry/core');
|
|
37
|
-
case '@opentelemetry/exporter-logs-otlp-http': return require('@opentelemetry/exporter-logs-otlp-http');
|
|
38
|
-
case '@opentelemetry/exporter-metrics-otlp-http': return require('@opentelemetry/exporter-metrics-otlp-http');
|
|
39
|
-
case '@opentelemetry/exporter-trace-otlp-http': return require('@opentelemetry/exporter-trace-otlp-http');
|
|
40
|
-
case '@opentelemetry/otlp-transformer': return require('@opentelemetry/otlp-transformer');
|
|
41
|
-
case '@opentelemetry/resources': return require('@opentelemetry/resources');
|
|
42
|
-
case '@opentelemetry/sdk-logs': return require('@opentelemetry/sdk-logs');
|
|
43
|
-
case '@opentelemetry/sdk-metrics': return require('@opentelemetry/sdk-metrics');
|
|
44
|
-
case '@opentelemetry/sdk-trace': return require('@opentelemetry/sdk-trace');
|
|
45
|
-
case '@opentelemetry/instrumentation-runtime-node': return require('@opentelemetry/instrumentation-runtime-node');
|
|
46
|
-
case '@opentelemetry/instrumentation': return require('@opentelemetry/instrumentation');
|
|
47
|
-
case '@opentelemetry/instrumentation-http': return require('@opentelemetry/instrumentation-http');
|
|
48
|
-
case '@opentelemetry/instrumentation-undici': return require('@opentelemetry/instrumentation-undici');
|
|
49
|
-
case '@opentelemetry/instrumentation-pg': return require('@opentelemetry/instrumentation-pg');
|
|
50
|
-
case '@opentelemetry/instrumentation-ioredis': return require('@opentelemetry/instrumentation-ioredis');
|
|
51
|
-
case 'winston-transport': return require('winston-transport');
|
|
52
|
-
default: return NONE;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
catch (e) {
|
|
56
|
-
throw e;
|
|
57
|
-
}
|
|
3
|
+
// Bundlers must NOT follow this: an optional peer the app did not install would make webpack warn "Module not found",
|
|
4
|
+
// and a bare require call on a variable id is a "Critical dependency" warning. createRequire() is opaque to webpack/esbuild/Next's tracer,
|
|
5
|
+
// so peers resolve at run time from the SDK's own location, then from the app root (cwd). Install the peers in the app
|
|
6
|
+
// (pnpm: as direct dependencies); for a Next standalone build add them to outputFileTracingIncludes.
|
|
7
|
+
function resolvers() {
|
|
8
|
+
const from = [typeof __filename === 'string' ? __filename : '', `${process.cwd()}/`].filter(Boolean);
|
|
9
|
+
return from.map((f) => createRequire(f));
|
|
58
10
|
}
|
|
59
11
|
export function load(id) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
12
|
+
let err;
|
|
13
|
+
for (const r of resolvers()) {
|
|
14
|
+
try {
|
|
15
|
+
return r(id);
|
|
16
|
+
}
|
|
17
|
+
catch (e) {
|
|
18
|
+
if (e?.code !== 'MODULE_NOT_FOUND')
|
|
19
|
+
throw e;
|
|
20
|
+
err ??= e;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
throw err;
|
|
64
24
|
}
|
|
65
25
|
const SDK_PEERS = [
|
|
66
26
|
'@opentelemetry/context-async-hooks', '@opentelemetry/core', '@opentelemetry/exporter-logs-otlp-http',
|
package/dist/esm/metrics.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
// @ts-ignore optional peer: absent -> any (keeps skipLibCheck:false consumers compiling)
|
|
1
2
|
import type { Resource } from '@opentelemetry/resources';
|
|
3
|
+
// @ts-ignore optional peer: absent -> any (keeps skipLibCheck:false consumers compiling)
|
|
2
4
|
import type { MeterProvider, PushMetricExporter } from '@opentelemetry/sdk-metrics';
|
|
3
5
|
/** Explicit flag, else `WATCHTOWER_LOGS` / `WATCHTOWER_METRICS` (default on); `OTEL_*_EXPORTER=none` turns it off. */
|
|
4
6
|
export declare function switchOn(name: string, otelExporterEnv: string, flag?: boolean): boolean;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// `wt-next-sourcemaps [dir]`: upload .next/static source maps after `next build` (env: WT_API_KEY, WT_PROJECT, WT_RELEASE, WT_API_URL), then delete them.
|
|
3
|
+
import { uploadSourcemaps } from './integrations/next-build.js';
|
|
4
|
+
uploadSourcemaps({ dir: process.argv[2], log: (l) => console.log(`[watchtower] ${l}`) }).catch((e) => {
|
|
5
|
+
console.error(`[watchtower] source map upload failed: ${e.message}`);
|
|
6
|
+
process.exit(1);
|
|
7
|
+
});
|
package/dist/esm/origin.d.ts
CHANGED
package/dist/esm/redact.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// @ts-ignore optional peer: absent -> any (keeps skipLibCheck:false consumers compiling)
|
|
1
2
|
import type { SpanProcessor } from '@opentelemetry/sdk-trace';
|
|
2
3
|
export type RedactHook = (key: string, value: unknown) => unknown;
|
|
3
4
|
/** Hook keys for the non-attribute fields. Event attributes (incl. exception.message / exception.stacktrace) use their own keys. */
|
package/dist/esm/register.js
CHANGED
|
@@ -12,12 +12,23 @@ if (process.env.WT_INGEST_KEY || process.env.OTEL_EXPORTER_OTLP_HEADERS) {
|
|
|
12
12
|
const key = Symbol.for('watchtower.node.esm-hook');
|
|
13
13
|
if (!g[key]) {
|
|
14
14
|
g[key] = true;
|
|
15
|
+
// Node 26 prints DEP0205 for module.register(); import-in-the-middle's hook has no sync (registerHooks) form yet,
|
|
16
|
+
// so mute only that one warning for this call.
|
|
17
|
+
const emit = process.emitWarning;
|
|
18
|
+
process.emitWarning = ((w, ...a) => {
|
|
19
|
+
if (JSON.stringify(a).includes('DEP0205'))
|
|
20
|
+
return;
|
|
21
|
+
return emit.call(process, w, ...a);
|
|
22
|
+
});
|
|
15
23
|
try {
|
|
16
24
|
registerHooks('@opentelemetry/instrumentation/hook.mjs', pathToFileURL(`${process.cwd()}/`));
|
|
17
25
|
}
|
|
18
26
|
catch {
|
|
19
27
|
// @opentelemetry/instrumentation not installed: CommonJS apps are still instrumented
|
|
20
28
|
}
|
|
29
|
+
finally {
|
|
30
|
+
process.emitWarning = emit;
|
|
31
|
+
}
|
|
21
32
|
}
|
|
22
33
|
init({});
|
|
23
34
|
}
|
package/dist/esm/sampling.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Attributes, Context, Link, SpanKind } from '@opentelemetry/api';
|
|
2
|
+
// @ts-ignore optional peer: absent -> any (keeps skipLibCheck:false consumers compiling)
|
|
2
3
|
import type { Sampler, SamplingResult } from '@opentelemetry/sdk-trace';
|
|
3
4
|
export interface SamplingRule {
|
|
4
5
|
name?: string;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// @ts-ignore optional peer: absent -> any (keeps skipLibCheck:false consumers compiling)
|
|
2
|
+
import type { SpanProcessor } from '@opentelemetry/sdk-trace';
|
|
3
|
+
/** A path segment that reads like a secret or an opaque id: >= 24 chars, or >= 10 chars mixing a letter and a digit (UUIDs included). */
|
|
4
|
+
export declare function looksSecret(seg: string): boolean;
|
|
5
|
+
/** Drop the query string and fragment, strip `user:pass@`, and replace secret-looking path segments with `:redacted`. */
|
|
6
|
+
export declare function scrubUrl(value: string): string;
|
|
7
|
+
/** Span processor: scrubs URL attributes (query dropped, `url.query` removed, secret path segments redacted) and URLs inside the span name. `http.route` is kept. */
|
|
8
|
+
export declare function urlScrubber(): SpanProcessor;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
const URL_KEYS = ['http.target', 'url.full', 'url.path', 'http.url', 'http.request.url'];
|
|
2
|
+
const QUERY_KEYS = ['url.query'];
|
|
3
|
+
/** A path segment that reads like a secret or an opaque id: >= 24 chars, or >= 10 chars mixing a letter and a digit (UUIDs included). */
|
|
4
|
+
export function looksSecret(seg) {
|
|
5
|
+
return seg.length >= 24 || (seg.length >= 10 && /[A-Za-z]/.test(seg) && /\d/.test(seg));
|
|
6
|
+
}
|
|
7
|
+
/** Drop the query string and fragment, strip `user:pass@`, and replace secret-looking path segments with `:redacted`. */
|
|
8
|
+
export function scrubUrl(value) {
|
|
9
|
+
const m = /^(?:([a-z][a-z0-9+.-]*:\/\/)([^/?#]*))?([^?#]*)/i.exec(value);
|
|
10
|
+
const origin = m[1] ? m[1] + m[2].replace(/^[^@]*@/, '') : '';
|
|
11
|
+
return origin + m[3].split('/').map((s) => (looksSecret(s) ? ':redacted' : s)).join('/');
|
|
12
|
+
}
|
|
13
|
+
function scrubName(name) {
|
|
14
|
+
return name.split(' ').map((t) => (/^(https?:\/\/|\/)/i.test(t) ? scrubUrl(t) : t)).join(' ');
|
|
15
|
+
}
|
|
16
|
+
/** Span processor: scrubs URL attributes (query dropped, `url.query` removed, secret path segments redacted) and URLs inside the span name. `http.route` is kept. */
|
|
17
|
+
export function urlScrubber() {
|
|
18
|
+
return {
|
|
19
|
+
onStart() { },
|
|
20
|
+
onEnd(span) {
|
|
21
|
+
const a = span.attributes;
|
|
22
|
+
for (const k of URL_KEYS)
|
|
23
|
+
if (typeof a[k] === 'string')
|
|
24
|
+
a[k] = scrubUrl(a[k]);
|
|
25
|
+
for (const k of QUERY_KEYS)
|
|
26
|
+
delete a[k];
|
|
27
|
+
const n = scrubName(span.name);
|
|
28
|
+
if (n !== span.name)
|
|
29
|
+
span.name = n;
|
|
30
|
+
},
|
|
31
|
+
shutdown: () => Promise.resolve(),
|
|
32
|
+
forceFlush: () => Promise.resolve(),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface SignalProc {
|
|
2
|
+
on(event: string, l: () => void): unknown;
|
|
3
|
+
off(event: string, l: () => void): unknown;
|
|
4
|
+
listenerCount(event: string): number;
|
|
5
|
+
kill(pid: number, signal: string): unknown;
|
|
6
|
+
readonly pid: number;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* On SIGTERM and SIGINT: flush (at most `timeoutMs`). Adding a signal listener turns off Node's default exit, so when
|
|
10
|
+
* ours is the only listener it removes itself and re-sends the signal after the flush; when the app or a framework
|
|
11
|
+
* (`next start`) has its own handler, that handler decides how the process ends and ours only starts the flush.
|
|
12
|
+
* Returns the uninstall function.
|
|
13
|
+
*/
|
|
14
|
+
export declare function installSignalFlush(flush: () => Promise<unknown>, timeoutMs?: number, proc?: SignalProc): () => void;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Flush on SIGTERM / SIGINT without changing how the process ends.
|
|
2
|
+
/**
|
|
3
|
+
* On SIGTERM and SIGINT: flush (at most `timeoutMs`). Adding a signal listener turns off Node's default exit, so when
|
|
4
|
+
* ours is the only listener it removes itself and re-sends the signal after the flush; when the app or a framework
|
|
5
|
+
* (`next start`) has its own handler, that handler decides how the process ends and ours only starts the flush.
|
|
6
|
+
* Returns the uninstall function.
|
|
7
|
+
*/
|
|
8
|
+
export function installSignalFlush(flush, timeoutMs = 2000, proc = process) {
|
|
9
|
+
const handlers = new Map();
|
|
10
|
+
for (const sig of ['SIGTERM', 'SIGINT']) {
|
|
11
|
+
const h = () => {
|
|
12
|
+
const alone = proc.listenerCount(sig) <= 1;
|
|
13
|
+
const done = Promise.race([flush().catch(() => undefined), new Promise((r) => setTimeout(r, timeoutMs).unref())]);
|
|
14
|
+
if (!alone)
|
|
15
|
+
return;
|
|
16
|
+
void done.finally(() => { proc.off(sig, h); proc.kill(proc.pid, sig); });
|
|
17
|
+
};
|
|
18
|
+
handlers.set(sig, h);
|
|
19
|
+
proc.on(sig, h);
|
|
20
|
+
}
|
|
21
|
+
return () => { for (const [sig, h] of handlers)
|
|
22
|
+
proc.off(sig, h); };
|
|
23
|
+
}
|
package/dist/esm/sql.js
CHANGED
|
@@ -204,9 +204,19 @@ export function normalizeAndHash(stmt, pg = false) {
|
|
|
204
204
|
/** Systems whose dialect keeps `"identifier"` quotes and the `#` operator (as the worker decides). */
|
|
205
205
|
export const isPgDialect = (system) => system === 'postgresql' || system === 'cockroachdb' || system === 'redshift';
|
|
206
206
|
const OP = /^\s*(?:with\b[\s\S]*?\)\s*)?([a-z]+)/i;
|
|
207
|
-
const
|
|
207
|
+
const NAME = '(?:"[^"]+"|[\\w$]+)(?:\\.(?:"[^"]+"|[\\w$]+))?';
|
|
208
|
+
const TARGET = new RegExp(`\\b(?:from|into|update|join|table)\\s+(?:if\\s+(?:not\\s+)?exists\\s+)?(${NAME})`, 'i');
|
|
209
|
+
// DDL: `CREATE [TEMP|VIRTUAL|UNIQUE...] TABLE|INDEX|VIEW [IF NOT EXISTS] name [ON table]`; an index is named after its table
|
|
210
|
+
const CREATE = new RegExp(`^\\s*create\\s+(?:(?:temp|temporary|virtual|unique|fulltext|spatial|clustered|nonclustered|materialized|or\\s+replace)\\s+)*(table|index|view)\\s+(?:if\\s+not\\s+exists\\s+)?(?:concurrently\\s+)?(${NAME})(?:\\s+on\\s+(?:only\\s+)?(${NAME}))?`, 'i');
|
|
208
211
|
/** `SELECT users`-style summary: the operation and first table, never values or the statement text. */
|
|
209
212
|
export function querySummary(text, fallback = 'postgres') {
|
|
213
|
+
const ddl = text?.match(CREATE);
|
|
214
|
+
if (ddl) {
|
|
215
|
+
const kind = ddl[1].toLowerCase();
|
|
216
|
+
const operation = `CREATE ${kind.toUpperCase()}`;
|
|
217
|
+
const target = (kind === 'index' ? ddl[3] ?? ddl[2] : ddl[2]).replace(/"/g, '');
|
|
218
|
+
return { operation, target, summary: `${operation} ${target}` };
|
|
219
|
+
}
|
|
210
220
|
const operation = text?.match(OP)?.[1]?.toUpperCase();
|
|
211
221
|
const target = text?.match(TARGET)?.[1]?.replace(/"/g, '');
|
|
212
222
|
return { operation, target, summary: [operation ?? fallback, target].filter(Boolean).join(' ') };
|