@watchtowerobservability/node 0.7.1 → 0.7.3

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 CHANGED
@@ -2,6 +2,20 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.7.3
6
+
7
+ - URL scrubbing is narrower: only secret-looking path segments are redacted (UUIDs, hex runs >= 16, JWTs, digit runs >= 16, random-looking tokens >= 16 chars mixing letters and digits). Hosts are never touched (also scheme-relative `//host`); readable words, kebab/snake slugs and short ids stay (`/atlas/.well-known/jwks.json`, `/gallery/legal-consultation-intake`, `/config/a1b2c3d4`). Behaviour change: 10 to 15 character ids that used to be redacted now show. Same rule as the browser SDK and the gateway. `looksSecret` is now `isSecretSegment`.
8
+ - `wt-next-sourcemaps --inject` / `uploadSourcemaps({ inject: true })`: writes a debug id into every `.next/static/**/*.js` chunk that has a map (`//# debugId=<uuid>` and a registration for the browser SDK) and into its map (`debugId`), then uploads with `debug_id`. Stable across rebuilds (hash of the chunk).
9
+ - README: custom-queue recipe names `job_queue_jobs` as the depth metric the Queues page and queue alerts read.
10
+
11
+ ## 0.7.2
12
+
13
+ - `wt-next-sourcemaps` / `uploadSourcemaps()`: default API is `https://watchtowerops.net` (the old default did not resolve); one env name `WT_API_URL` (`WT_API` still works with a warning); a bad host fails fast naming it. Uploads run 8 at a time, retry 429/5xx honoring `Retry-After`, continue past failures, print `{uploaded, skipped, failed}` and exit 1 when any failed. Each map is sent with `for=<chunk path>`. Bodies are sent gzipped. `uploadSourcemaps()` now resolves `{uploaded, skipped, failed}` instead of throwing on a failed map.
14
+ - `init()` applies `nextInternals` itself when `NEXT_RUNTIME` is set (new `InitOptions.nextInternals` / `nextInternalsRate`), so the documented `init()` in `register()` filters Next's internal spans; never installed twice. Root `next.span_type` INTERNAL spans are sampled too (test with a parentless span).
15
+ - Next tunnel forwards `/v1/traces`.
16
+ - New export `@watchtowerobservability/node/messaging` (`produce`, `consume`) and a custom-queue recipe in the README.
17
+ - Start log line when datastore `requireParent` is on (`watchtower: requireParent on; queries outside a span are not recorded (WATCHTOWER_DB_REQUIRE_PARENT=0 to keep them)`); silenced by `instrumentLog: false` / `WT_INSTRUMENT_LOG=0`.
18
+
5
19
  ## 0.7.1
6
20
 
7
21
  - Fix (bundlers): optional OpenTelemetry peers are no longer named by a literal `require` / `import()`; `load()` uses `createRequire` (from the SDK's location, then the app root), so webpack and Next no longer print "Module not found" for peers you did not install or a "Critical dependency" warning. Install the peers you use as direct dependencies of the app (for a Next standalone build add them to `outputFileTracingIncludes` if the tracer misses one).
package/README.md CHANGED
@@ -39,9 +39,9 @@ wt.log('info', 'started', { port: 3000 }); // OTLP log, linked to the active
39
39
  flushes and exits with code 1 like Node does, only when no other handler is registered.
40
40
  Request bodies, headers and query arguments are never recorded. URLs are scrubbed on every span, including spans other
41
41
  libraries create (Next.js, `node:http`, `fetch`): the query string is dropped (`url.query` is removed) and path segments
42
- that look like secrets or ids (24 characters or more, or 10 or more mixing letters and digits; UUIDs included) become
43
- `:redacted`, in `http.target`, `url.full`, `url.path`, `http.url` and URLs in span names; `http.route` is kept.
44
- `/api/feed/<token>/csv?since=x` is recorded as `/api/feed/:redacted/csv`. Plain words, numbers and short ids stay readable.
42
+ that look like secrets (UUIDs, hex runs of 16 or more, JWTs, digit runs of 16 or more, or random-looking tokens of 16 or more
43
+ characters mixing letters and digits) become `:redacted`; the host never changes, in `http.target`, `url.full`, `url.path`, `http.url` and URLs in span names; `http.route` is kept.
44
+ `/api/feed/<token>/csv?since=x` is recorded as `/api/feed/:redacted/csv`. Plain words, kebab and snake slugs (`legal-consultation-intake`), short ids (`/config/a1b2c3d4`) and file names stay readable.
45
45
  Pass `urlScrubbing: false` to record URLs as the instrumentation produced them. Your `redact` hook sees scrubbed values.
46
46
 
47
47
  ## Auto-instrumentation and preload
@@ -74,23 +74,23 @@ export const register = makeRegister({ redact: (k, v) => v }); // any InitOpti
74
74
  export { onRequestError } from '@watchtowerobservability/node/next';
75
75
  ```
76
76
 
77
- `makeRegister` also takes `nextInternals: 'drop' | 'sample' | 'keep'` (default `'sample'`, `nextInternalsRate` 0.1) for Next's
77
+ `init()` applies `nextInternals` itself when `NEXT_RUNTIME` is set, so a plain `init()` in `register()` is filtered too (no double install). `nextInternals: 'drop' | 'sample' | 'keep'` (default `'sample'`, `nextInternalsRate` 0.1) for Next's
78
78
  own spans (those with a `next.span_type` attribute: load route module, prepare route module, resolve page components, start
79
- response, middleware, generateMetadata). Request, route handler, render, server action and fetch spans, all SERVER/CLIENT
79
+ response, middleware, generateMetadata). Root (parentless) internals such as `LoadComponents.loadRouteModule` are sampled like the rest. Request, route handler, render, server action and fetch spans, all SERVER/CLIENT
80
80
  spans and errored spans are always kept; children of a dropped internal span stay in the trace.
81
81
 
82
82
  **Source maps.** `next.config.js`: `module.exports = withWatchtower(nextConfig, { sourcemaps: true })` (from
83
83
  `@watchtowerobservability/node/next`) turns on `productionBrowserSourceMaps`. Build with
84
84
  `"build": "next build && wt-next-sourcemaps"`: the `wt-next-sourcemaps` bin (or `uploadSourcemaps()`) uploads every `.map` under
85
85
  `.next/static` as `_next/static/<path>` for release `$WT_RELEASE` (create it first or let the upload create it; it must equal
86
- the browser SDK's `release`), with `$WT_API_KEY` and `$WT_PROJECT` (and `$WT_API_URL`), then deletes the `.map` files so they are
87
- never served. If an upload fails the command exits 1 and leaves the files. **Key scope: `ci:write`** (it covers releases and
86
+ the browser SDK's `release`), with `$WT_API_KEY` and `$WT_PROJECT` (and `$WT_API_URL`, default `https://watchtowerops.net`; the old `$WT_API` still works with a warning), then deletes the `.map` files so they are
87
+ never served. Uploads run 8 at a time, retry 429/5xx with backoff (honoring `Retry-After`) and continue past failures; the run prints `{uploaded, skipped, failed}` (`skipped`: the server already had the same bytes, HTTP 200) and exits 1 when any failed, leaving the files. Each map is sent with `for=<chunk path>` read from the chunk's `//# sourceMappingURL=` comment or the map's `file` field. A wrong host fails at once with a message naming it. Bodies are sent gzipped. **Key scope: `ci:write`** (it covers releases and
88
88
  artifacts; `admin:write` also works). Ingest keys cannot upload. Turbopack and webpack both emit these maps; Next does not inject
89
89
  debug ids, so maps match by release and file name.
90
90
 
91
91
  **First-party tunnel** (ad blockers block third-party telemetry hosts): `app/api/wt/[...path]/route.ts`:
92
92
  `export const { GET, POST } = tunnelHandler({ endpoint: process.env.WT_EDGE_URL! });`, then `init({ endpoint: '/api/wt', publishableKey })`
93
- in the browser SDK. Only `/v1/rum/*`, `/v1/replay/<session>/chunks` and `/v1/logs` are forwarded; the only credential passed on
93
+ in the browser SDK. Only `/v1/rum/*`, `/v1/replay/<session>/chunks`, `/v1/logs` and `/v1/traces` are forwarded; the only credential passed on
94
94
  is the publishable key (`?k=` or a `wtp_` bearer), cookies and other headers are dropped, bodies over 1 MiB get 413
95
95
  (`maxBodyBytes`). The visitor's IP is the server's, so geo comes from the Next host unless the edge is told otherwise.
96
96
 
@@ -294,6 +294,14 @@ with `error.type`; the error message is not recorded.
294
294
  - **Graphile Worker**: `instrumentAddJob(utils)` (or the runner from `run`) adds `_wt` to the payload.
295
295
  `wrapTaskList` runs each task in a consumer span, strips `_wt`, and instruments `helpers.addJob` so
296
296
  follow-up jobs stay in the trace. `quickAddJob` cannot be patched under ESM; use `utils.addJob`.
297
+ - **Custom queue** (a table, Redis list, anything without an integration): `import { produce, consume } from '@watchtowerobservability/node/messaging'`.
298
+ `produce('orders send', { 'messaging.system': 'my-queue', 'messaging.destination.name': 'orders' }, (headers) => insertJob(data, headers))`
299
+ runs a PRODUCER span and hands you the `traceparent` headers to store in the job row (a `headers jsonb` column).
300
+ The worker does `consume('orders process', { ...same attrs }, [row.headers], () => handle(row))`: a CONSUMER span
301
+ linked to (and, with one carrier, a child of) the producer, so the trace runs from request to job.
302
+ For depth on the Queues page, record the gauge `job_queue_jobs{framework="my-queue", queue="orders", state="waiting"}` (states `waiting`, `active`, `dead`);
303
+ that is the metric the page and the built-in queue alerts read (`messaging.queue.depth` is read too, but the alerts ignore it).
304
+
297
305
  - **kafkajs**: upstream `@opentelemetry/instrumentation-kafkajs` already covers it.
298
306
 
299
307
  ## Profiling
@@ -69,13 +69,22 @@ export interface InitOptions {
69
69
  exporter?: unknown;
70
70
  /**
71
71
  * Scrub URLs on every span, including spans other libraries create (Next.js, node:http, fetch): the query string is dropped
72
- * (`url.query` removed) and path segments that look like secrets or ids (>= 24 chars, or >= 10 chars mixing letters and digits,
73
- * UUIDs included) become `:redacted`, in `http.target`, `url.full`, `url.path`, `http.url` and URLs in span names.
72
+ * (`url.query` removed) and path segments that look like secrets (UUIDs, hex runs, JWTs, long digit runs, random tokens of >= 16 chars;
73
+ * hosts, readable words, slugs and short ids stay) become `:redacted`, in `http.target`, `url.full`, `url.path`, `http.url` and URLs in span names.
74
74
  * `http.route` is kept. Default true; `false` records URLs as the instrumentation produced them.
75
75
  */
76
76
  urlScrubbing?: boolean;
77
77
  /** Return false to drop a finished span before export (the `/next` entry uses it for `nextInternals`). */
78
78
  spanFilter?: (span: ReadableSpan) => boolean;
79
+ /**
80
+ * Next.js's own spans (`next.span_type` set: load route module, prepare route module, resolve page components, start
81
+ * response, middleware, generateMetadata ...), root ones included. `"sample"` keeps `nextInternalsRate` of them, `"drop"`
82
+ * none, `"keep"` all. Request, route handler, render, server action and fetch spans, every SERVER/CLIENT span and every
83
+ * errored span are always kept. Default: `"sample"` when `NEXT_RUNTIME` is set (Next.js), off otherwise.
84
+ */
85
+ nextInternals?: 'drop' | 'sample' | 'keep';
86
+ /** Share of internal spans kept with `nextInternals: "sample"`; default 0.1. */
87
+ nextInternalsRate?: number;
79
88
  /**
80
89
  * Patch node:http, fetch (undici), pg and ioredis for those whose `@opentelemetry/instrumentation-*` peer is installed; defaults to on unless WT_INSTRUMENT=0.
81
90
  * `'auto'` (or WT_INSTRUMENT=auto) also instruments whichever of postgres.js, better-sqlite3 and mysql2 is installed and logs one line naming what it patched
package/dist/cjs/index.js CHANGED
@@ -49,6 +49,7 @@ const instrument_js_1 = require("./instrument.js");
49
49
  const redact_js_1 = require("./redact.js");
50
50
  const scrub_js_1 = require("./scrub.js");
51
51
  const filter_js_1 = require("./filter.js");
52
+ const next_filter_js_1 = require("./next-filter.js");
52
53
  const checkin_js_1 = require("./checkin.js");
53
54
  const shutdown_js_1 = require("./shutdown.js");
54
55
  const version_js_1 = require("./version.js");
@@ -342,6 +343,12 @@ function registerGlobals(c) {
342
343
  api_1.diag.warn('watchtower: an OpenTelemetry global (tracer provider, context manager, propagator or meter provider) was already registered and was kept; call init() before other OTel setup to own them');
343
344
  }
344
345
  function init(opts) {
346
+ const nextMode = opts?.nextInternals ?? (process.env.NEXT_RUNTIME ? 'sample' : undefined);
347
+ if (nextMode) {
348
+ const own = (0, next_filter_js_1.nextInternalsFilter)(nextMode, opts?.nextInternalsRate);
349
+ const user = opts.spanFilter;
350
+ opts = { ...opts, nextInternals: undefined, spanFilter: user ? (s) => own(s) && user(s) : own }; // cleared: a second init() never stacks the filter
351
+ }
345
352
  if (state.current && state.current.uninstall)
346
353
  state.current.uninstall();
347
354
  void state.current?.profiler?.then((p) => p?.stop());
@@ -352,9 +359,13 @@ function init(opts) {
352
359
  const mode = opts?.instrument ?? (env === 'auto' ? 'auto' : !['0', 'off', 'false', 'no'].includes(env));
353
360
  if (mode) {
354
361
  const names = (0, instrument_js_1.instrumentAll)({ tracerProvider: c.provider, meterProvider: c.meterProvider }, [c.endpoint, c.apiUrl], mode === 'auto');
355
- if (mode === 'auto' && (opts?.instrumentLog ?? !['0', 'off', 'false', 'no'].includes((process.env.WT_INSTRUMENT_LOG || '').trim().toLowerCase()))) {
362
+ const logOn = opts?.instrumentLog ?? !['0', 'off', 'false', 'no'].includes((process.env.WT_INSTRUMENT_LOG || '').trim().toLowerCase());
363
+ if (mode === 'auto' && logOn) {
356
364
  console.info(`[watchtower] instrumented: ${names.map((n) => n.replace('@opentelemetry/instrumentation-', '')).join(', ') || 'nothing (no supported package found)'}`);
357
365
  }
366
+ if (logOn && !['0', 'false', 'off', 'no'].includes((process.env.WATCHTOWER_DB_REQUIRE_PARENT || '').trim().toLowerCase())) {
367
+ console.info('watchtower: requireParent on; queries outside a span are not recorded (WATCHTOWER_DB_REQUIRE_PARENT=0 to keep them)');
368
+ }
358
369
  }
359
370
  if (process.env.WT_PROFILING === '1') {
360
371
  const attrs = c.resource.attributes;
@@ -1,7 +1,7 @@
1
1
  export interface SourcemapOptions {
2
2
  /** Directory holding the client build; default `.next/static`. */
3
3
  dir?: string;
4
- /** Default WT_API_URL, then https://api.watchtower.observer. */
4
+ /** Default WT_API_URL (old name WT_API still works, with a warning), then https://watchtowerops.net. */
5
5
  apiUrl?: string;
6
6
  /** A key with scope `ci:write` (it covers releases and artifacts; `admin:write` also works). Default WT_API_KEY. */
7
7
  apiKey?: string;
@@ -11,6 +11,8 @@ export interface SourcemapOptions {
11
11
  release?: string;
12
12
  /** Keep the .map files after a successful upload (default: delete them so they are never served). */
13
13
  keep?: boolean;
14
+ /** Before uploading, write a debug id into each chunk (`//# debugId=` plus a runtime registration) and its map (`debugId`), so errors resolve by id instead of release + file name. Default false (`--inject` on the CLI). */
15
+ inject?: boolean;
14
16
  fetch?: typeof fetch;
15
17
  log?: (line: string) => void;
16
18
  }
@@ -24,8 +26,15 @@ type ConfigOrFn = NextConfig | ((...a: unknown[]) => NextConfig | Promise<NextCo
24
26
  export declare function withWatchtower(config?: ConfigOrFn, opts?: {
25
27
  sourcemaps?: boolean;
26
28
  }): 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. */
29
+ /**
30
+ * Upload every `.map` under `dir` as `_next/static/<path>` for the release (8 at a time, 429/5xx retried with backoff
31
+ * honoring Retry-After; a failed map does not stop the others), then delete them when none failed. Returns
32
+ * `{uploaded, skipped, failed}`: `skipped` = the server already had those exact bytes (HTTP 200). Bodies are sent
33
+ * gzipped (`Content-Encoding: gzip`).
34
+ */
28
35
  export declare function uploadSourcemaps(o?: SourcemapOptions): Promise<{
29
36
  uploaded: number;
37
+ skipped: number;
38
+ failed: number;
30
39
  }>;
31
40
  export {};
@@ -38,6 +38,8 @@ exports.uploadSourcemaps = uploadSourcemaps;
38
38
  // Build-time helpers for Next.js: `withWatchtower` (next.config) and `uploadSourcemaps` (after `next build`).
39
39
  const fs = __importStar(require("node:fs"));
40
40
  const nodePath = __importStar(require("node:path"));
41
+ const zlib = __importStar(require("node:zlib"));
42
+ const next_debugid_js_1 = require("./next-debugid.js");
41
43
  /**
42
44
  * `module.exports = withWatchtower(nextConfig, { sourcemaps: true })`: turns on `productionBrowserSourceMaps` so
43
45
  * `next build` writes client maps (works with webpack and Turbopack). Then run `wt-next-sourcemaps` after the build
@@ -58,7 +60,29 @@ function* maps(dir) {
58
60
  yield p;
59
61
  }
60
62
  }
61
- /** Upload every `.map` under `dir` as `_next/static/<path>` for the release, then delete them. Throws (leaving the files) when an upload fails. */
63
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
64
+ /** The client chunk (`_next/static/...`) a map belongs to: the sibling JS's `//# sourceMappingURL=` target, else the map's own `file` field. */
65
+ function chunkFor(mapFile, name) {
66
+ try {
67
+ const js = mapFile.slice(0, -'.map'.length);
68
+ if (fs.existsSync(js)) {
69
+ const tail = fs.readFileSync(js, 'utf8').slice(-512);
70
+ if (/\/\/[#@] sourceMappingURL=/.test(tail))
71
+ return name.slice(0, -'.map'.length);
72
+ }
73
+ const file = JSON.parse(fs.readFileSync(mapFile, 'utf8')).file;
74
+ if (typeof file === 'string' && file)
75
+ return `${nodePath.posix.dirname(name)}/${nodePath.posix.basename(file)}`;
76
+ }
77
+ catch { /* unreadable: upload without `for` */ }
78
+ return undefined;
79
+ }
80
+ /**
81
+ * Upload every `.map` under `dir` as `_next/static/<path>` for the release (8 at a time, 429/5xx retried with backoff
82
+ * honoring Retry-After; a failed map does not stop the others), then delete them when none failed. Returns
83
+ * `{uploaded, skipped, failed}`: `skipped` = the server already had those exact bytes (HTTP 200). Bodies are sent
84
+ * gzipped (`Content-Encoding: gzip`).
85
+ */
62
86
  async function uploadSourcemaps(o = {}) {
63
87
  const env = process.env;
64
88
  const key = o.apiKey || env.WT_API_KEY;
@@ -70,26 +94,73 @@ async function uploadSourcemaps(o = {}) {
70
94
  throw new Error('WT_PROJECT (project id) is required');
71
95
  if (!release)
72
96
  throw new Error('WT_RELEASE is required');
97
+ const log = o.log || (() => { });
98
+ if (!o.apiUrl && !env.WT_API_URL && env.WT_API)
99
+ log('WT_API is deprecated, use WT_API_URL');
100
+ const base = (o.apiUrl || env.WT_API_URL || env.WT_API || 'https://watchtowerops.net').replace(/\/+$/, '');
73
101
  const dir = nodePath.resolve(o.dir || '.next/static');
74
102
  const f = o.fetch || fetch;
75
- const log = o.log || (() => { });
76
- const root = `${(o.apiUrl || env.WT_API_URL || 'https://api.watchtower.observer').replace(/\/+$/, '')}/v1/projects/${encodeURIComponent(project)}`;
77
- const call = async (path, init, okStatus = []) => {
78
- const res = await f(root + path, { ...init, headers: { authorization: `Bearer ${key}`, ...init.headers } });
79
- if (!res.ok && !okStatus.includes(res.status))
80
- throw new Error(`watchtower ${init.method} ${path.split('?')[0]} -> HTTP ${res.status}`);
103
+ const root = `${base}/v1/projects/${encodeURIComponent(project)}`;
104
+ const send = async (path, init, retries) => {
105
+ for (let attempt = 0;; attempt++) {
106
+ let res;
107
+ try {
108
+ res = await f(root + path, { ...init, headers: { authorization: `Bearer ${key}`, ...init.headers } });
109
+ }
110
+ catch (e) {
111
+ const cause = e.cause;
112
+ throw new Error(`cannot reach ${base} (${cause?.code || cause?.message || e.message}); check WT_API_URL`);
113
+ }
114
+ if ((res.status !== 429 && res.status < 500) || attempt >= retries)
115
+ return res;
116
+ const ra = Number(res.headers.get('retry-after'));
117
+ await sleep(Number.isFinite(ra) && ra > 0 ? Math.min(ra, 30) * 1000 : Math.min(250 * 2 ** attempt, 8000));
118
+ }
81
119
  };
120
+ const ids = o.inject && fs.existsSync(dir) ? (0, next_debugid_js_1.injectDebugIds)(dir) : new Map();
121
+ if (o.inject)
122
+ log(`debug ids written for ${ids.size} chunks`);
82
123
  const files = fs.existsSync(dir) ? [...maps(dir)] : [];
83
- await call('/releases', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ version: release }) }, [409]); // 409: exists
84
- for (const file of files) {
85
- const name = `_next/static/${nodePath.relative(dir, file).split(nodePath.sep).join('/')}`;
86
- await call(`/artifacts?${new URLSearchParams({ kind: 'sourcemap', name, release })}`, {
87
- method: 'POST', headers: { 'content-type': 'application/octet-stream' }, body: new Uint8Array(fs.readFileSync(file)),
88
- });
89
- }
90
- if (!o.keep)
124
+ // release create fails fast: no retry, so a bad host or key surfaces at once
125
+ const rel = await send('/releases', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ version: release }) }, 0);
126
+ if (!rel.ok && rel.status !== 409)
127
+ throw new Error(`watchtower POST /releases -> HTTP ${rel.status}${rel.status === 401 || rel.status === 403 ? ' (check WT_API_KEY scope ci:write)' : ''}`);
128
+ let uploaded = 0, skipped = 0, failed = 0;
129
+ let next = 0;
130
+ const worker = async () => {
131
+ for (let file = files[next++]; file !== undefined; file = files[next++]) {
132
+ const name = `_next/static/${nodePath.relative(dir, file).split(nodePath.sep).join('/')}`;
133
+ const q = { kind: 'sourcemap', name, release };
134
+ const chunk = chunkFor(file, name);
135
+ if (chunk)
136
+ q.for = chunk;
137
+ const id = ids.get(file);
138
+ if (id)
139
+ q.debug_id = id;
140
+ try {
141
+ const res = await send(`/artifacts?${new URLSearchParams(q)}`, {
142
+ method: 'POST', headers: { 'content-type': 'application/octet-stream', 'content-encoding': 'gzip' },
143
+ body: new Uint8Array(zlib.gzipSync(fs.readFileSync(file))),
144
+ }, 4);
145
+ if (res.status === 200)
146
+ skipped++;
147
+ else if (res.ok)
148
+ uploaded++;
149
+ else {
150
+ failed++;
151
+ log(`failed ${name}: HTTP ${res.status}`);
152
+ }
153
+ }
154
+ catch (e) {
155
+ failed++;
156
+ log(`failed ${name}: ${e.message}`);
157
+ }
158
+ }
159
+ };
160
+ await Promise.all(Array.from({ length: Math.min(8, files.length) }, worker));
161
+ if (!o.keep && failed === 0)
91
162
  for (const file of files)
92
163
  fs.rmSync(file);
93
- log(`uploaded ${files.length} source maps for release ${release}${o.keep ? '' : ' and deleted them'}`);
94
- return { uploaded: files.length };
164
+ log(`${JSON.stringify({ uploaded, skipped, failed })} source maps for release ${release}${o.keep || failed ? '' : '; deleted the .map files'}`);
165
+ return { uploaded, skipped, failed };
95
166
  }
@@ -0,0 +1,8 @@
1
+ /** SHA-256 of the JS bytes, first 16 as a v4-shaped UUID: the same chunk always gets the same id, so rebuilds are stable. */
2
+ export declare function debugIdFor(js: string): string;
3
+ /**
4
+ * For every `<chunk>.js` with a sibling `<chunk>.js.map` under `dir`: append the runtime registration (so the browser SDK sends the id with
5
+ * error frames) and `//# debugId=<uuid>` to the chunk, and set `"debugId"` in the map. Chunks that already carry an id keep it. Returns map path to id.
6
+ * `urlPrefix` is the served path of `dir` (default `/_next/static`).
7
+ */
8
+ export declare function injectDebugIds(dir: string, urlPrefix?: string): Map<string, string>;
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.debugIdFor = debugIdFor;
37
+ exports.injectDebugIds = injectDebugIds;
38
+ // Debug ids for Next builds: same convention as the browser SDK's bundler plugins (`//# debugId=<uuid>` in the chunk, `"debugId"` in its map).
39
+ const crypto = __importStar(require("node:crypto"));
40
+ const fs = __importStar(require("node:fs"));
41
+ const nodePath = __importStar(require("node:path"));
42
+ const COMMENT = /^\/\/# debugId=([0-9a-f-]{36})\s*$/m;
43
+ /** SHA-256 of the JS bytes, first 16 as a v4-shaped UUID: the same chunk always gets the same id, so rebuilds are stable. */
44
+ function debugIdFor(js) {
45
+ const h = crypto.createHash('sha256').update(js).digest();
46
+ h[6] = (h[6] & 0x0f) | 0x40;
47
+ h[8] = (h[8] & 0x3f) | 0x80;
48
+ const x = h.subarray(0, 16).toString('hex');
49
+ return `${x.slice(0, 8)}-${x.slice(8, 12)}-${x.slice(12, 16)}-${x.slice(16, 20)}-${x.slice(20)}`;
50
+ }
51
+ function* jsFiles(dir) {
52
+ for (const e of fs.readdirSync(dir, { withFileTypes: true })) {
53
+ const p = nodePath.join(dir, e.name);
54
+ if (e.isDirectory())
55
+ yield* jsFiles(p);
56
+ else if (e.name.endsWith('.js'))
57
+ yield p;
58
+ }
59
+ }
60
+ /**
61
+ * For every `<chunk>.js` with a sibling `<chunk>.js.map` under `dir`: append the runtime registration (so the browser SDK sends the id with
62
+ * error frames) and `//# debugId=<uuid>` to the chunk, and set `"debugId"` in the map. Chunks that already carry an id keep it. Returns map path to id.
63
+ * `urlPrefix` is the served path of `dir` (default `/_next/static`).
64
+ */
65
+ function injectDebugIds(dir, urlPrefix = '/_next/static') {
66
+ const out = new Map();
67
+ for (const js of jsFiles(dir)) {
68
+ const mapFile = `${js}.map`;
69
+ if (!fs.existsSync(mapFile))
70
+ continue;
71
+ let src = fs.readFileSync(js, 'utf8');
72
+ const old = COMMENT.exec(src);
73
+ const id = old ? old[1] : debugIdFor(src);
74
+ if (!old) {
75
+ const url = `${urlPrefix}/${nodePath.relative(dir, js).split(nodePath.sep).join('/')}`;
76
+ const snippet = `;(function(){try{var w=typeof window!=="undefined"?window:globalThis;(w._wtDebugIds=w._wtDebugIds||{})[${JSON.stringify(url)}]="${id}"}catch(e){}})();`;
77
+ const m = /\n?\/\/[#@] sourceMappingURL=[^\n]*\s*$/.exec(src);
78
+ const body = m ? src.slice(0, m.index) : src;
79
+ src = `${body}\n${snippet}${m ? `\n${m[0].trim()}` : ''}\n//# debugId=${id}\n`;
80
+ fs.writeFileSync(js, src);
81
+ }
82
+ const map = JSON.parse(fs.readFileSync(mapFile, 'utf8'));
83
+ if (map.debugId !== id)
84
+ fs.writeFileSync(mapFile, JSON.stringify({ ...map, debugId: id }));
85
+ out.set(mapFile, id);
86
+ }
87
+ return out;
88
+ }
@@ -4,8 +4,8 @@
4
4
  // Browser SDK: init({ endpoint: '/api/wt', publishableKey: 'wtp_...' }).
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.tunnelHandler = tunnelHandler;
7
- // only what the browser SDK calls: /v1/rum/*, /v1/replay/<session>/chunks and /v1/logs
8
- const ALLOWED = /^\/v1\/(rum\/[A-Za-z0-9/_-]+|replay\/[A-Za-z0-9_-]+\/chunks|logs)$/;
7
+ // only what the browser SDK calls: /v1/rum/*, /v1/replay/<session>/chunks /v1/logs and /v1/traces
8
+ const ALLOWED = /^\/v1\/(rum\/[A-Za-z0-9/_-]+|replay\/[A-Za-z0-9_-]+\/chunks|logs|traces)$/;
9
9
  const FORWARD = ['content-type', 'content-encoding', 'user-agent'];
10
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
11
  function tunnelHandler(o) {
@@ -1,25 +1,10 @@
1
- // @ts-ignore optional peer: absent -> any (keeps skipLibCheck:false consumers compiling)
2
- import type { ReadableSpan } from '@opentelemetry/sdk-trace';
3
1
  import * as wt from '../index.js';
4
2
  export { withWatchtower, uploadSourcemaps } from './next-build.js';
5
3
  export type { SourcemapOptions } from './next-build.js';
4
+ export { isNextInternal, nextInternalsFilter } from '../next-filter.js';
6
5
  export { tunnelHandler } from './next-tunnel.js';
7
6
  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;
7
+ export type NextOptions = wt.InitOptions;
23
8
  /** Build a Next.js instrumentation `register` that passes `options` to `init`; no-op off the Node.js runtime or without a key. */
24
9
  export declare function makeRegister(options?: NextOptions): () => Promise<void>;
25
10
  /** Next.js instrumentation hook: init from WT_* env on the Node.js runtime; no-op without WT_INGEST_KEY. */
@@ -33,45 +33,27 @@ var __importStar = (this && this.__importStar) || (function () {
33
33
  };
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.register = exports.tunnelHandler = exports.uploadSourcemaps = exports.withWatchtower = void 0;
37
- exports.isNextInternal = isNextInternal;
38
- exports.nextInternalsFilter = nextInternalsFilter;
36
+ exports.register = exports.tunnelHandler = exports.nextInternalsFilter = exports.isNextInternal = exports.uploadSourcemaps = exports.withWatchtower = void 0;
39
37
  exports.makeRegister = makeRegister;
40
38
  exports.onRequestError = onRequestError;
41
39
  // instrumentation.js: module.exports = require('@watchtowerobservability/node/next');
42
40
  // or, with options: export const register = makeRegister({ redact }); export { onRequestError } from '@watchtowerobservability/node/next';
43
- const api_1 = require("@opentelemetry/api");
44
41
  const wt = __importStar(require("../index.js"));
45
42
  var next_build_js_1 = require("./next-build.js");
46
43
  Object.defineProperty(exports, "withWatchtower", { enumerable: true, get: function () { return next_build_js_1.withWatchtower; } });
47
44
  Object.defineProperty(exports, "uploadSourcemaps", { enumerable: true, get: function () { return next_build_js_1.uploadSourcemaps; } });
45
+ var next_filter_js_1 = require("../next-filter.js");
46
+ Object.defineProperty(exports, "isNextInternal", { enumerable: true, get: function () { return next_filter_js_1.isNextInternal; } });
47
+ Object.defineProperty(exports, "nextInternalsFilter", { enumerable: true, get: function () { return next_filter_js_1.nextInternalsFilter; } });
48
48
  var next_tunnel_js_1 = require("./next-tunnel.js");
49
49
  Object.defineProperty(exports, "tunnelHandler", { enumerable: true, get: function () { return next_tunnel_js_1.tunnelHandler; } });
50
- const KEEP_TYPES = new Set([
51
- 'BaseServer.handleRequest', 'AppRouteRouteHandlers.runHandler', 'AppRender.fetch', 'AppRender.getBodyResult',
52
- 'Render.getServerSideProps', 'Render.getStaticProps', 'NextNodeServer.runApi',
53
- ]);
54
- const KEEP_NAMES = /^(render route|executing api route|fetch |.*server action)/i;
55
- /** Whether `span` is one of Next's internal spans (not a request, route handler, render, server action or fetch). */
56
- function isNextInternal(span) {
57
- const type = span.attributes['next.span_type'];
58
- if (typeof type !== 'string')
59
- return false;
60
- return !KEEP_TYPES.has(type) && !KEEP_NAMES.test(span.name) && span.kind !== 1 && span.kind !== 2 && span.status.code !== api_1.SpanStatusCode.ERROR;
61
- }
62
- /** The `spanFilter` for a `nextInternals` mode; `rand` is injectable for tests. */
63
- function nextInternalsFilter(mode = 'sample', rate = 0.1, rand = Math.random) {
64
- return (s) => mode === 'keep' || !isNextInternal(s) || (mode === 'sample' && rand() < rate);
65
- }
66
50
  /** Build a Next.js instrumentation `register` that passes `options` to `init`; no-op off the Node.js runtime or without a key. */
67
51
  function makeRegister(options = {}) {
68
52
  return async function register() {
69
53
  const rt = process.env.NEXT_RUNTIME;
70
54
  if ((rt && rt !== 'nodejs') || !(options.apiKey || process.env.WT_INGEST_KEY))
71
55
  return;
72
- const { nextInternals, nextInternalsRate, ...init } = options;
73
- const own = nextInternalsFilter(nextInternals, nextInternalsRate);
74
- wt.init({ ...init, spanFilter: init.spanFilter ? (s) => own(s) && init.spanFilter(s) : own });
56
+ wt.init({ ...options, nextInternals: options.nextInternals ?? 'sample' }); // init() applies the filter (once)
75
57
  };
76
58
  }
77
59
  /** Next.js instrumentation hook: init from WT_* env on the Node.js runtime; no-op without WT_INGEST_KEY. */
@@ -0,0 +1,6 @@
1
+ // @ts-ignore optional peer: absent -> any (keeps skipLibCheck:false consumers compiling)
2
+ import type { ReadableSpan } from '@opentelemetry/sdk-trace';
3
+ /** Whether `span` is one of Next's internal spans (not a request, route handler, render, server action or fetch). */
4
+ export declare function isNextInternal(span: ReadableSpan): boolean;
5
+ /** The `spanFilter` for a `nextInternals` mode; `rand` is injectable for tests. */
6
+ export declare function nextInternalsFilter(mode?: 'drop' | 'sample' | 'keep', rate?: number, rand?: () => number): (s: ReadableSpan) => boolean;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isNextInternal = isNextInternal;
4
+ exports.nextInternalsFilter = nextInternalsFilter;
5
+ // Filter for Next.js's own internal spans (`nextInternals` option of init / makeRegister).
6
+ const api_1 = require("@opentelemetry/api");
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
+ 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 !== api_1.SpanStatusCode.ERROR;
18
+ }
19
+ /** The `spanFilter` for a `nextInternals` mode; `rand` is injectable for tests. */
20
+ function nextInternalsFilter(mode = 'sample', rate = 0.1, rand = Math.random) {
21
+ return (s) => mode === 'keep' || !isNextInternal(s) || (mode === 'sample' && rand() < rate);
22
+ }
@@ -1,9 +1,11 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- // `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.
4
+ // `wt-next-sourcemaps [--inject] [dir]`: upload .next/static source maps after `next build` (env: WT_API_KEY, WT_PROJECT, WT_RELEASE, WT_API_URL), then delete them. `--inject` first writes a debug id into every chunk and map.
5
5
  const next_build_js_1 = require("./integrations/next-build.js");
6
- (0, next_build_js_1.uploadSourcemaps)({ dir: process.argv[2], log: (l) => console.log(`[watchtower] ${l}`) }).catch((e) => {
6
+ const args = process.argv.slice(2);
7
+ (0, next_build_js_1.uploadSourcemaps)({ dir: args.find((a) => !a.startsWith('--')), inject: args.includes('--inject'), log: (l) => console.log(`[watchtower] ${l}`) }).then((r) => { if (r.failed > 0)
8
+ process.exit(1); }).catch((e) => {
7
9
  console.error(`[watchtower] source map upload failed: ${e.message}`);
8
10
  process.exit(1);
9
11
  });
@@ -1,8 +1,12 @@
1
1
  // @ts-ignore optional peer: absent -> any (keeps skipLibCheck:false consumers compiling)
2
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`. */
3
+ /**
4
+ * A path segment that looks like a secret: UUID, hex run >= 16, JWT, digit run >= 16, or an opaque token (>= 16 chars of
5
+ * [A-Za-z0-9_+=-] with a letter and a digit that is not a readable kebab/snake slug). Words, slugs, short ids and file names stay.
6
+ * Same rule in the browser SDK and the gateway (`redact.ScrubURL`).
7
+ */
8
+ export declare function isSecretSegment(s: string): boolean;
9
+ /** Drop the query string and fragment, strip `user:pass@`, and replace secret-looking path segments with `:redacted`. The scheme and host are never touched. */
6
10
  export declare function scrubUrl(value: string): string;
7
11
  /** 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
12
  export declare function urlScrubber(): SpanProcessor;
package/dist/cjs/scrub.js CHANGED
@@ -1,19 +1,30 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.looksSecret = looksSecret;
3
+ exports.isSecretSegment = isSecretSegment;
4
4
  exports.scrubUrl = scrubUrl;
5
5
  exports.urlScrubber = urlScrubber;
6
6
  const URL_KEYS = ['http.target', 'url.full', 'url.path', 'http.url', 'http.request.url'];
7
7
  const QUERY_KEYS = ['url.query'];
8
- /** 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). */
9
- function looksSecret(seg) {
10
- return seg.length >= 24 || (seg.length >= 10 && /[A-Za-z]/.test(seg) && /\d/.test(seg));
8
+ const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
9
+ const HEX = /^[0-9a-f]{16,}$/i;
10
+ const JWT = /^eyJ[\w-]*\.[\w-]+\.[\w-]*$/;
11
+ const DIGITS = /^\d{16,}$/;
12
+ const TOKEN = /^[A-Za-z0-9_+=-]{16,}$/;
13
+ // readable word-ish part of a kebab/snake slug: `legal`, `Intake`, `q1`, `2024`
14
+ const PART = /^([A-Za-z][a-z]*\d{0,2}|\d{1,4})$/;
15
+ /**
16
+ * A path segment that looks like a secret: UUID, hex run >= 16, JWT, digit run >= 16, or an opaque token (>= 16 chars of
17
+ * [A-Za-z0-9_+=-] with a letter and a digit that is not a readable kebab/snake slug). Words, slugs, short ids and file names stay.
18
+ * Same rule in the browser SDK and the gateway (`redact.ScrubURL`).
19
+ */
20
+ function isSecretSegment(s) {
21
+ return UUID.test(s) || HEX.test(s) || JWT.test(s) || DIGITS.test(s) || (TOKEN.test(s) && /\d/.test(s) && /[a-z]/i.test(s) && !s.split(/[-_]/).every((p) => PART.test(p)));
11
22
  }
12
- /** Drop the query string and fragment, strip `user:pass@`, and replace secret-looking path segments with `:redacted`. */
23
+ /** Drop the query string and fragment, strip `user:pass@`, and replace secret-looking path segments with `:redacted`. The scheme and host are never touched. */
13
24
  function scrubUrl(value) {
14
- const m = /^(?:([a-z][a-z0-9+.-]*:\/\/)([^/?#]*))?([^?#]*)/i.exec(value);
15
- const origin = m[1] ? m[1] + m[2].replace(/^[^@]*@/, '') : '';
16
- return origin + m[3].split('/').map((s) => (looksSecret(s) ? ':redacted' : s)).join('/');
25
+ const m = /^(?:([a-z][a-z0-9+.-]*:)?\/\/([^/?#]*))?([^?#]*)/i.exec(value);
26
+ const origin = m[2] === undefined ? '' : `${m[1] ?? ''}//${m[2].replace(/^[^@]*@/, '')}`;
27
+ return origin + m[3].split('/').map((s) => (isSecretSegment(s) ? ':redacted' : s)).join('/');
17
28
  }
18
29
  function scrubName(name) {
19
30
  return name.split(' ').map((t) => (/^(https?:\/\/|\/)/i.test(t) ? scrubUrl(t) : t)).join(' ');
@@ -1,2 +1,2 @@
1
1
  /** This package's version (test/distro.test.ts checks it against package.json); sent as `telemetry.distro.version`. */
2
- export declare const VERSION = "0.7.1";
2
+ export declare const VERSION = "0.7.3";
@@ -2,4 +2,4 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.VERSION = void 0;
4
4
  /** This package's version (test/distro.test.ts checks it against package.json); sent as `telemetry.distro.version`. */
5
- exports.VERSION = '0.7.1';
5
+ exports.VERSION = '0.7.3';
@@ -69,13 +69,22 @@ export interface InitOptions {
69
69
  exporter?: unknown;
70
70
  /**
71
71
  * Scrub URLs on every span, including spans other libraries create (Next.js, node:http, fetch): the query string is dropped
72
- * (`url.query` removed) and path segments that look like secrets or ids (>= 24 chars, or >= 10 chars mixing letters and digits,
73
- * UUIDs included) become `:redacted`, in `http.target`, `url.full`, `url.path`, `http.url` and URLs in span names.
72
+ * (`url.query` removed) and path segments that look like secrets (UUIDs, hex runs, JWTs, long digit runs, random tokens of >= 16 chars;
73
+ * hosts, readable words, slugs and short ids stay) become `:redacted`, in `http.target`, `url.full`, `url.path`, `http.url` and URLs in span names.
74
74
  * `http.route` is kept. Default true; `false` records URLs as the instrumentation produced them.
75
75
  */
76
76
  urlScrubbing?: boolean;
77
77
  /** Return false to drop a finished span before export (the `/next` entry uses it for `nextInternals`). */
78
78
  spanFilter?: (span: ReadableSpan) => boolean;
79
+ /**
80
+ * Next.js's own spans (`next.span_type` set: load route module, prepare route module, resolve page components, start
81
+ * response, middleware, generateMetadata ...), root ones included. `"sample"` keeps `nextInternalsRate` of them, `"drop"`
82
+ * none, `"keep"` all. Request, route handler, render, server action and fetch spans, every SERVER/CLIENT span and every
83
+ * errored span are always kept. Default: `"sample"` when `NEXT_RUNTIME` is set (Next.js), off otherwise.
84
+ */
85
+ nextInternals?: 'drop' | 'sample' | 'keep';
86
+ /** Share of internal spans kept with `nextInternals: "sample"`; default 0.1. */
87
+ nextInternalsRate?: number;
79
88
  /**
80
89
  * Patch node:http, fetch (undici), pg and ioredis for those whose `@opentelemetry/instrumentation-*` peer is installed; defaults to on unless WT_INSTRUMENT=0.
81
90
  * `'auto'` (or WT_INSTRUMENT=auto) also instruments whichever of postgres.js, better-sqlite3 and mysql2 is installed and logs one line naming what it patched
package/dist/esm/index.js CHANGED
@@ -8,6 +8,7 @@ import { instrumentAll } from './instrument.js';
8
8
  import { LOG_BODY_KEY, redactor } from './redact.js';
9
9
  import { urlScrubber } from './scrub.js';
10
10
  import { filterExporter } from './filter.js';
11
+ import { nextInternalsFilter } from './next-filter.js';
11
12
  import { checkin as runCheckin } from './checkin.js';
12
13
  import { installSignalFlush } from './shutdown.js';
13
14
  import { VERSION } from './version.js';
@@ -298,6 +299,12 @@ function registerGlobals(c) {
298
299
  diag.warn('watchtower: an OpenTelemetry global (tracer provider, context manager, propagator or meter provider) was already registered and was kept; call init() before other OTel setup to own them');
299
300
  }
300
301
  export function init(opts) {
302
+ const nextMode = opts?.nextInternals ?? (process.env.NEXT_RUNTIME ? 'sample' : undefined);
303
+ if (nextMode) {
304
+ const own = nextInternalsFilter(nextMode, opts?.nextInternalsRate);
305
+ const user = opts.spanFilter;
306
+ opts = { ...opts, nextInternals: undefined, spanFilter: user ? (s) => own(s) && user(s) : own }; // cleared: a second init() never stacks the filter
307
+ }
301
308
  if (state.current && state.current.uninstall)
302
309
  state.current.uninstall();
303
310
  void state.current?.profiler?.then((p) => p?.stop());
@@ -308,9 +315,13 @@ export function init(opts) {
308
315
  const mode = opts?.instrument ?? (env === 'auto' ? 'auto' : !['0', 'off', 'false', 'no'].includes(env));
309
316
  if (mode) {
310
317
  const names = instrumentAll({ tracerProvider: c.provider, meterProvider: c.meterProvider }, [c.endpoint, c.apiUrl], mode === 'auto');
311
- if (mode === 'auto' && (opts?.instrumentLog ?? !['0', 'off', 'false', 'no'].includes((process.env.WT_INSTRUMENT_LOG || '').trim().toLowerCase()))) {
318
+ const logOn = opts?.instrumentLog ?? !['0', 'off', 'false', 'no'].includes((process.env.WT_INSTRUMENT_LOG || '').trim().toLowerCase());
319
+ if (mode === 'auto' && logOn) {
312
320
  console.info(`[watchtower] instrumented: ${names.map((n) => n.replace('@opentelemetry/instrumentation-', '')).join(', ') || 'nothing (no supported package found)'}`);
313
321
  }
322
+ if (logOn && !['0', 'false', 'off', 'no'].includes((process.env.WATCHTOWER_DB_REQUIRE_PARENT || '').trim().toLowerCase())) {
323
+ console.info('watchtower: requireParent on; queries outside a span are not recorded (WATCHTOWER_DB_REQUIRE_PARENT=0 to keep them)');
324
+ }
314
325
  }
315
326
  if (process.env.WT_PROFILING === '1') {
316
327
  const attrs = c.resource.attributes;
@@ -1,7 +1,7 @@
1
1
  export interface SourcemapOptions {
2
2
  /** Directory holding the client build; default `.next/static`. */
3
3
  dir?: string;
4
- /** Default WT_API_URL, then https://api.watchtower.observer. */
4
+ /** Default WT_API_URL (old name WT_API still works, with a warning), then https://watchtowerops.net. */
5
5
  apiUrl?: string;
6
6
  /** A key with scope `ci:write` (it covers releases and artifacts; `admin:write` also works). Default WT_API_KEY. */
7
7
  apiKey?: string;
@@ -11,6 +11,8 @@ export interface SourcemapOptions {
11
11
  release?: string;
12
12
  /** Keep the .map files after a successful upload (default: delete them so they are never served). */
13
13
  keep?: boolean;
14
+ /** Before uploading, write a debug id into each chunk (`//# debugId=` plus a runtime registration) and its map (`debugId`), so errors resolve by id instead of release + file name. Default false (`--inject` on the CLI). */
15
+ inject?: boolean;
14
16
  fetch?: typeof fetch;
15
17
  log?: (line: string) => void;
16
18
  }
@@ -24,8 +26,15 @@ type ConfigOrFn = NextConfig | ((...a: unknown[]) => NextConfig | Promise<NextCo
24
26
  export declare function withWatchtower(config?: ConfigOrFn, opts?: {
25
27
  sourcemaps?: boolean;
26
28
  }): 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. */
29
+ /**
30
+ * Upload every `.map` under `dir` as `_next/static/<path>` for the release (8 at a time, 429/5xx retried with backoff
31
+ * honoring Retry-After; a failed map does not stop the others), then delete them when none failed. Returns
32
+ * `{uploaded, skipped, failed}`: `skipped` = the server already had those exact bytes (HTTP 200). Bodies are sent
33
+ * gzipped (`Content-Encoding: gzip`).
34
+ */
28
35
  export declare function uploadSourcemaps(o?: SourcemapOptions): Promise<{
29
36
  uploaded: number;
37
+ skipped: number;
38
+ failed: number;
30
39
  }>;
31
40
  export {};
@@ -1,6 +1,8 @@
1
1
  // Build-time helpers for Next.js: `withWatchtower` (next.config) and `uploadSourcemaps` (after `next build`).
2
2
  import * as fs from 'node:fs';
3
3
  import * as nodePath from 'node:path';
4
+ import * as zlib from 'node:zlib';
5
+ import { injectDebugIds } from './next-debugid.js';
4
6
  /**
5
7
  * `module.exports = withWatchtower(nextConfig, { sourcemaps: true })`: turns on `productionBrowserSourceMaps` so
6
8
  * `next build` writes client maps (works with webpack and Turbopack). Then run `wt-next-sourcemaps` after the build
@@ -21,7 +23,29 @@ function* maps(dir) {
21
23
  yield p;
22
24
  }
23
25
  }
24
- /** Upload every `.map` under `dir` as `_next/static/<path>` for the release, then delete them. Throws (leaving the files) when an upload fails. */
26
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
27
+ /** The client chunk (`_next/static/...`) a map belongs to: the sibling JS's `//# sourceMappingURL=` target, else the map's own `file` field. */
28
+ function chunkFor(mapFile, name) {
29
+ try {
30
+ const js = mapFile.slice(0, -'.map'.length);
31
+ if (fs.existsSync(js)) {
32
+ const tail = fs.readFileSync(js, 'utf8').slice(-512);
33
+ if (/\/\/[#@] sourceMappingURL=/.test(tail))
34
+ return name.slice(0, -'.map'.length);
35
+ }
36
+ const file = JSON.parse(fs.readFileSync(mapFile, 'utf8')).file;
37
+ if (typeof file === 'string' && file)
38
+ return `${nodePath.posix.dirname(name)}/${nodePath.posix.basename(file)}`;
39
+ }
40
+ catch { /* unreadable: upload without `for` */ }
41
+ return undefined;
42
+ }
43
+ /**
44
+ * Upload every `.map` under `dir` as `_next/static/<path>` for the release (8 at a time, 429/5xx retried with backoff
45
+ * honoring Retry-After; a failed map does not stop the others), then delete them when none failed. Returns
46
+ * `{uploaded, skipped, failed}`: `skipped` = the server already had those exact bytes (HTTP 200). Bodies are sent
47
+ * gzipped (`Content-Encoding: gzip`).
48
+ */
25
49
  export async function uploadSourcemaps(o = {}) {
26
50
  const env = process.env;
27
51
  const key = o.apiKey || env.WT_API_KEY;
@@ -33,26 +57,73 @@ export async function uploadSourcemaps(o = {}) {
33
57
  throw new Error('WT_PROJECT (project id) is required');
34
58
  if (!release)
35
59
  throw new Error('WT_RELEASE is required');
60
+ const log = o.log || (() => { });
61
+ if (!o.apiUrl && !env.WT_API_URL && env.WT_API)
62
+ log('WT_API is deprecated, use WT_API_URL');
63
+ const base = (o.apiUrl || env.WT_API_URL || env.WT_API || 'https://watchtowerops.net').replace(/\/+$/, '');
36
64
  const dir = nodePath.resolve(o.dir || '.next/static');
37
65
  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}`);
66
+ const root = `${base}/v1/projects/${encodeURIComponent(project)}`;
67
+ const send = async (path, init, retries) => {
68
+ for (let attempt = 0;; attempt++) {
69
+ let res;
70
+ try {
71
+ res = await f(root + path, { ...init, headers: { authorization: `Bearer ${key}`, ...init.headers } });
72
+ }
73
+ catch (e) {
74
+ const cause = e.cause;
75
+ throw new Error(`cannot reach ${base} (${cause?.code || cause?.message || e.message}); check WT_API_URL`);
76
+ }
77
+ if ((res.status !== 429 && res.status < 500) || attempt >= retries)
78
+ return res;
79
+ const ra = Number(res.headers.get('retry-after'));
80
+ await sleep(Number.isFinite(ra) && ra > 0 ? Math.min(ra, 30) * 1000 : Math.min(250 * 2 ** attempt, 8000));
81
+ }
44
82
  };
83
+ const ids = o.inject && fs.existsSync(dir) ? injectDebugIds(dir) : new Map();
84
+ if (o.inject)
85
+ log(`debug ids written for ${ids.size} chunks`);
45
86
  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)
87
+ // release create fails fast: no retry, so a bad host or key surfaces at once
88
+ const rel = await send('/releases', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ version: release }) }, 0);
89
+ if (!rel.ok && rel.status !== 409)
90
+ throw new Error(`watchtower POST /releases -> HTTP ${rel.status}${rel.status === 401 || rel.status === 403 ? ' (check WT_API_KEY scope ci:write)' : ''}`);
91
+ let uploaded = 0, skipped = 0, failed = 0;
92
+ let next = 0;
93
+ const worker = async () => {
94
+ for (let file = files[next++]; file !== undefined; file = files[next++]) {
95
+ const name = `_next/static/${nodePath.relative(dir, file).split(nodePath.sep).join('/')}`;
96
+ const q = { kind: 'sourcemap', name, release };
97
+ const chunk = chunkFor(file, name);
98
+ if (chunk)
99
+ q.for = chunk;
100
+ const id = ids.get(file);
101
+ if (id)
102
+ q.debug_id = id;
103
+ try {
104
+ const res = await send(`/artifacts?${new URLSearchParams(q)}`, {
105
+ method: 'POST', headers: { 'content-type': 'application/octet-stream', 'content-encoding': 'gzip' },
106
+ body: new Uint8Array(zlib.gzipSync(fs.readFileSync(file))),
107
+ }, 4);
108
+ if (res.status === 200)
109
+ skipped++;
110
+ else if (res.ok)
111
+ uploaded++;
112
+ else {
113
+ failed++;
114
+ log(`failed ${name}: HTTP ${res.status}`);
115
+ }
116
+ }
117
+ catch (e) {
118
+ failed++;
119
+ log(`failed ${name}: ${e.message}`);
120
+ }
121
+ }
122
+ };
123
+ await Promise.all(Array.from({ length: Math.min(8, files.length) }, worker));
124
+ if (!o.keep && failed === 0)
54
125
  for (const file of files)
55
126
  fs.rmSync(file);
56
- log(`uploaded ${files.length} source maps for release ${release}${o.keep ? '' : ' and deleted them'}`);
57
- return { uploaded: files.length };
127
+ log(`${JSON.stringify({ uploaded, skipped, failed })} source maps for release ${release}${o.keep || failed ? '' : '; deleted the .map files'}`);
128
+ return { uploaded, skipped, failed };
58
129
  }
@@ -0,0 +1,8 @@
1
+ /** SHA-256 of the JS bytes, first 16 as a v4-shaped UUID: the same chunk always gets the same id, so rebuilds are stable. */
2
+ export declare function debugIdFor(js: string): string;
3
+ /**
4
+ * For every `<chunk>.js` with a sibling `<chunk>.js.map` under `dir`: append the runtime registration (so the browser SDK sends the id with
5
+ * error frames) and `//# debugId=<uuid>` to the chunk, and set `"debugId"` in the map. Chunks that already carry an id keep it. Returns map path to id.
6
+ * `urlPrefix` is the served path of `dir` (default `/_next/static`).
7
+ */
8
+ export declare function injectDebugIds(dir: string, urlPrefix?: string): Map<string, string>;
@@ -0,0 +1,51 @@
1
+ // Debug ids for Next builds: same convention as the browser SDK's bundler plugins (`//# debugId=<uuid>` in the chunk, `"debugId"` in its map).
2
+ import * as crypto from 'node:crypto';
3
+ import * as fs from 'node:fs';
4
+ import * as nodePath from 'node:path';
5
+ const COMMENT = /^\/\/# debugId=([0-9a-f-]{36})\s*$/m;
6
+ /** SHA-256 of the JS bytes, first 16 as a v4-shaped UUID: the same chunk always gets the same id, so rebuilds are stable. */
7
+ export function debugIdFor(js) {
8
+ const h = crypto.createHash('sha256').update(js).digest();
9
+ h[6] = (h[6] & 0x0f) | 0x40;
10
+ h[8] = (h[8] & 0x3f) | 0x80;
11
+ const x = h.subarray(0, 16).toString('hex');
12
+ return `${x.slice(0, 8)}-${x.slice(8, 12)}-${x.slice(12, 16)}-${x.slice(16, 20)}-${x.slice(20)}`;
13
+ }
14
+ function* jsFiles(dir) {
15
+ for (const e of fs.readdirSync(dir, { withFileTypes: true })) {
16
+ const p = nodePath.join(dir, e.name);
17
+ if (e.isDirectory())
18
+ yield* jsFiles(p);
19
+ else if (e.name.endsWith('.js'))
20
+ yield p;
21
+ }
22
+ }
23
+ /**
24
+ * For every `<chunk>.js` with a sibling `<chunk>.js.map` under `dir`: append the runtime registration (so the browser SDK sends the id with
25
+ * error frames) and `//# debugId=<uuid>` to the chunk, and set `"debugId"` in the map. Chunks that already carry an id keep it. Returns map path to id.
26
+ * `urlPrefix` is the served path of `dir` (default `/_next/static`).
27
+ */
28
+ export function injectDebugIds(dir, urlPrefix = '/_next/static') {
29
+ const out = new Map();
30
+ for (const js of jsFiles(dir)) {
31
+ const mapFile = `${js}.map`;
32
+ if (!fs.existsSync(mapFile))
33
+ continue;
34
+ let src = fs.readFileSync(js, 'utf8');
35
+ const old = COMMENT.exec(src);
36
+ const id = old ? old[1] : debugIdFor(src);
37
+ if (!old) {
38
+ const url = `${urlPrefix}/${nodePath.relative(dir, js).split(nodePath.sep).join('/')}`;
39
+ const snippet = `;(function(){try{var w=typeof window!=="undefined"?window:globalThis;(w._wtDebugIds=w._wtDebugIds||{})[${JSON.stringify(url)}]="${id}"}catch(e){}})();`;
40
+ const m = /\n?\/\/[#@] sourceMappingURL=[^\n]*\s*$/.exec(src);
41
+ const body = m ? src.slice(0, m.index) : src;
42
+ src = `${body}\n${snippet}${m ? `\n${m[0].trim()}` : ''}\n//# debugId=${id}\n`;
43
+ fs.writeFileSync(js, src);
44
+ }
45
+ const map = JSON.parse(fs.readFileSync(mapFile, 'utf8'));
46
+ if (map.debugId !== id)
47
+ fs.writeFileSync(mapFile, JSON.stringify({ ...map, debugId: id }));
48
+ out.set(mapFile, id);
49
+ }
50
+ return out;
51
+ }
@@ -1,8 +1,8 @@
1
1
  // First-party tunnel for browser telemetry (ad blockers block third-party telemetry hosts).
2
2
  // app/api/wt/[...path]/route.ts: export const { GET, POST } = tunnelHandler({ endpoint: process.env.WT_EDGE_URL! });
3
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)$/;
4
+ // only what the browser SDK calls: /v1/rum/*, /v1/replay/<session>/chunks /v1/logs and /v1/traces
5
+ const ALLOWED = /^\/v1\/(rum\/[A-Za-z0-9/_-]+|replay\/[A-Za-z0-9_-]+\/chunks|logs|traces)$/;
6
6
  const FORWARD = ['content-type', 'content-encoding', 'user-agent'];
7
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
8
  export function tunnelHandler(o) {
@@ -1,25 +1,10 @@
1
- // @ts-ignore optional peer: absent -> any (keeps skipLibCheck:false consumers compiling)
2
- import type { ReadableSpan } from '@opentelemetry/sdk-trace';
3
1
  import * as wt from '../index.js';
4
2
  export { withWatchtower, uploadSourcemaps } from './next-build.js';
5
3
  export type { SourcemapOptions } from './next-build.js';
4
+ export { isNextInternal, nextInternalsFilter } from '../next-filter.js';
6
5
  export { tunnelHandler } from './next-tunnel.js';
7
6
  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;
7
+ export type NextOptions = wt.InitOptions;
23
8
  /** Build a Next.js instrumentation `register` that passes `options` to `init`; no-op off the Node.js runtime or without a key. */
24
9
  export declare function makeRegister(options?: NextOptions): () => Promise<void>;
25
10
  /** Next.js instrumentation hook: init from WT_* env on the Node.js runtime; no-op without WT_INGEST_KEY. */
@@ -1,34 +1,16 @@
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';
4
3
  import * as wt from '../index.js';
5
4
  export { withWatchtower, uploadSourcemaps } from './next-build.js';
5
+ export { isNextInternal, nextInternalsFilter } from '../next-filter.js';
6
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
- }
23
7
  /** Build a Next.js instrumentation `register` that passes `options` to `init`; no-op off the Node.js runtime or without a key. */
24
8
  export function makeRegister(options = {}) {
25
9
  return async function register() {
26
10
  const rt = process.env.NEXT_RUNTIME;
27
11
  if ((rt && rt !== 'nodejs') || !(options.apiKey || process.env.WT_INGEST_KEY))
28
12
  return;
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 });
13
+ wt.init({ ...options, nextInternals: options.nextInternals ?? 'sample' }); // init() applies the filter (once)
32
14
  };
33
15
  }
34
16
  /** Next.js instrumentation hook: init from WT_* env on the Node.js runtime; no-op without WT_INGEST_KEY. */
@@ -0,0 +1,6 @@
1
+ // @ts-ignore optional peer: absent -> any (keeps skipLibCheck:false consumers compiling)
2
+ import type { ReadableSpan } from '@opentelemetry/sdk-trace';
3
+ /** Whether `span` is one of Next's internal spans (not a request, route handler, render, server action or fetch). */
4
+ export declare function isNextInternal(span: ReadableSpan): boolean;
5
+ /** The `spanFilter` for a `nextInternals` mode; `rand` is injectable for tests. */
6
+ export declare function nextInternalsFilter(mode?: 'drop' | 'sample' | 'keep', rate?: number, rand?: () => number): (s: ReadableSpan) => boolean;
@@ -0,0 +1,18 @@
1
+ // Filter for Next.js's own internal spans (`nextInternals` option of init / makeRegister).
2
+ import { SpanStatusCode } from '@opentelemetry/api';
3
+ const KEEP_TYPES = new Set([
4
+ 'BaseServer.handleRequest', 'AppRouteRouteHandlers.runHandler', 'AppRender.fetch', 'AppRender.getBodyResult',
5
+ 'Render.getServerSideProps', 'Render.getStaticProps', 'NextNodeServer.runApi',
6
+ ]);
7
+ const KEEP_NAMES = /^(render route|executing api route|fetch |.*server action)/i;
8
+ /** Whether `span` is one of Next's internal spans (not a request, route handler, render, server action or fetch). */
9
+ export function isNextInternal(span) {
10
+ const type = span.attributes['next.span_type'];
11
+ if (typeof type !== 'string')
12
+ return false;
13
+ return !KEEP_TYPES.has(type) && !KEEP_NAMES.test(span.name) && span.kind !== 1 && span.kind !== 2 && span.status.code !== SpanStatusCode.ERROR;
14
+ }
15
+ /** The `spanFilter` for a `nextInternals` mode; `rand` is injectable for tests. */
16
+ export function nextInternalsFilter(mode = 'sample', rate = 0.1, rand = Math.random) {
17
+ return (s) => mode === 'keep' || !isNextInternal(s) || (mode === 'sample' && rand() < rate);
18
+ }
@@ -1,7 +1,9 @@
1
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.
2
+ // `wt-next-sourcemaps [--inject] [dir]`: upload .next/static source maps after `next build` (env: WT_API_KEY, WT_PROJECT, WT_RELEASE, WT_API_URL), then delete them. `--inject` first writes a debug id into every chunk and map.
3
3
  import { uploadSourcemaps } from './integrations/next-build.js';
4
- uploadSourcemaps({ dir: process.argv[2], log: (l) => console.log(`[watchtower] ${l}`) }).catch((e) => {
4
+ const args = process.argv.slice(2);
5
+ uploadSourcemaps({ dir: args.find((a) => !a.startsWith('--')), inject: args.includes('--inject'), log: (l) => console.log(`[watchtower] ${l}`) }).then((r) => { if (r.failed > 0)
6
+ process.exit(1); }).catch((e) => {
5
7
  console.error(`[watchtower] source map upload failed: ${e.message}`);
6
8
  process.exit(1);
7
9
  });
@@ -1,8 +1,12 @@
1
1
  // @ts-ignore optional peer: absent -> any (keeps skipLibCheck:false consumers compiling)
2
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`. */
3
+ /**
4
+ * A path segment that looks like a secret: UUID, hex run >= 16, JWT, digit run >= 16, or an opaque token (>= 16 chars of
5
+ * [A-Za-z0-9_+=-] with a letter and a digit that is not a readable kebab/snake slug). Words, slugs, short ids and file names stay.
6
+ * Same rule in the browser SDK and the gateway (`redact.ScrubURL`).
7
+ */
8
+ export declare function isSecretSegment(s: string): boolean;
9
+ /** Drop the query string and fragment, strip `user:pass@`, and replace secret-looking path segments with `:redacted`. The scheme and host are never touched. */
6
10
  export declare function scrubUrl(value: string): string;
7
11
  /** 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
12
  export declare function urlScrubber(): SpanProcessor;
package/dist/esm/scrub.js CHANGED
@@ -1,14 +1,25 @@
1
1
  const URL_KEYS = ['http.target', 'url.full', 'url.path', 'http.url', 'http.request.url'];
2
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));
3
+ const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
4
+ const HEX = /^[0-9a-f]{16,}$/i;
5
+ const JWT = /^eyJ[\w-]*\.[\w-]+\.[\w-]*$/;
6
+ const DIGITS = /^\d{16,}$/;
7
+ const TOKEN = /^[A-Za-z0-9_+=-]{16,}$/;
8
+ // readable word-ish part of a kebab/snake slug: `legal`, `Intake`, `q1`, `2024`
9
+ const PART = /^([A-Za-z][a-z]*\d{0,2}|\d{1,4})$/;
10
+ /**
11
+ * A path segment that looks like a secret: UUID, hex run >= 16, JWT, digit run >= 16, or an opaque token (>= 16 chars of
12
+ * [A-Za-z0-9_+=-] with a letter and a digit that is not a readable kebab/snake slug). Words, slugs, short ids and file names stay.
13
+ * Same rule in the browser SDK and the gateway (`redact.ScrubURL`).
14
+ */
15
+ export function isSecretSegment(s) {
16
+ return UUID.test(s) || HEX.test(s) || JWT.test(s) || DIGITS.test(s) || (TOKEN.test(s) && /\d/.test(s) && /[a-z]/i.test(s) && !s.split(/[-_]/).every((p) => PART.test(p)));
6
17
  }
7
- /** Drop the query string and fragment, strip `user:pass@`, and replace secret-looking path segments with `:redacted`. */
18
+ /** Drop the query string and fragment, strip `user:pass@`, and replace secret-looking path segments with `:redacted`. The scheme and host are never touched. */
8
19
  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('/');
20
+ const m = /^(?:([a-z][a-z0-9+.-]*:)?\/\/([^/?#]*))?([^?#]*)/i.exec(value);
21
+ const origin = m[2] === undefined ? '' : `${m[1] ?? ''}//${m[2].replace(/^[^@]*@/, '')}`;
22
+ return origin + m[3].split('/').map((s) => (isSecretSegment(s) ? ':redacted' : s)).join('/');
12
23
  }
13
24
  function scrubName(name) {
14
25
  return name.split(' ').map((t) => (/^(https?:\/\/|\/)/i.test(t) ? scrubUrl(t) : t)).join(' ');
@@ -1,2 +1,2 @@
1
1
  /** This package's version (test/distro.test.ts checks it against package.json); sent as `telemetry.distro.version`. */
2
- export declare const VERSION = "0.7.1";
2
+ export declare const VERSION = "0.7.3";
@@ -1,2 +1,2 @@
1
1
  /** This package's version (test/distro.test.ts checks it against package.json); sent as `telemetry.distro.version`. */
2
- export const VERSION = '0.7.1';
2
+ export const VERSION = '0.7.3';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@watchtowerobservability/node",
3
- "version": "0.7.1",
3
+ "version": "0.7.3",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/watchtowerobservability/watchtower-node.git"
@@ -251,6 +251,16 @@
251
251
  "default": "./dist/cjs/integrations/orm.js"
252
252
  }
253
253
  },
254
+ "./messaging": {
255
+ "import": {
256
+ "types": "./dist/esm/integrations/messaging.d.ts",
257
+ "default": "./dist/esm/integrations/messaging.js"
258
+ },
259
+ "require": {
260
+ "types": "./dist/cjs/integrations/messaging.d.ts",
261
+ "default": "./dist/cjs/integrations/messaging.js"
262
+ }
263
+ },
254
264
  "./amqp": {
255
265
  "import": {
256
266
  "types": "./dist/esm/integrations/amqp.d.ts",