@watchtowerobservability/node 0.7.1 → 0.7.2
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 +8 -0
- package/README.md +11 -5
- package/dist/cjs/index.d.ts +9 -0
- package/dist/cjs/index.js +12 -1
- package/dist/cjs/integrations/next-build.d.ts +9 -2
- package/dist/cjs/integrations/next-build.js +81 -17
- package/dist/cjs/integrations/next-tunnel.js +2 -2
- package/dist/cjs/integrations/next.d.ts +2 -17
- package/dist/cjs/integrations/next.js +5 -23
- package/dist/cjs/next-filter.d.ts +6 -0
- package/dist/cjs/next-filter.js +22 -0
- package/dist/cjs/next-sourcemaps-cli.js +2 -1
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/esm/index.d.ts +9 -0
- package/dist/esm/index.js +12 -1
- package/dist/esm/integrations/next-build.d.ts +9 -2
- package/dist/esm/integrations/next-build.js +81 -17
- package/dist/esm/integrations/next-tunnel.js +2 -2
- package/dist/esm/integrations/next.d.ts +2 -17
- package/dist/esm/integrations/next.js +2 -20
- package/dist/esm/next-filter.d.ts +6 -0
- package/dist/esm/next-filter.js +18 -0
- package/dist/esm/next-sourcemaps-cli.js +2 -1
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/package.json +11 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
## 0.7.2
|
|
6
|
+
|
|
7
|
+
- `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.
|
|
8
|
+
- `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).
|
|
9
|
+
- Next tunnel forwards `/v1/traces`.
|
|
10
|
+
- New export `@watchtowerobservability/node/messaging` (`produce`, `consume`) and a custom-queue recipe in the README.
|
|
11
|
+
- 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`.
|
|
12
|
+
|
|
5
13
|
## 0.7.1
|
|
6
14
|
|
|
7
15
|
- 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
|
@@ -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
|
-
`
|
|
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.
|
|
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/
|
|
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,12 @@ 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
|
+
|
|
297
303
|
- **kafkajs**: upstream `@opentelemetry/instrumentation-kafkajs` already covers it.
|
|
298
304
|
|
|
299
305
|
## Profiling
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -76,6 +76,15 @@ export interface InitOptions {
|
|
|
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
|
-
|
|
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://
|
|
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;
|
|
@@ -24,8 +24,15 @@ type ConfigOrFn = NextConfig | ((...a: unknown[]) => NextConfig | Promise<NextCo
|
|
|
24
24
|
export declare function withWatchtower(config?: ConfigOrFn, opts?: {
|
|
25
25
|
sourcemaps?: boolean;
|
|
26
26
|
}): ConfigOrFn;
|
|
27
|
-
/**
|
|
27
|
+
/**
|
|
28
|
+
* Upload every `.map` under `dir` as `_next/static/<path>` for the release (8 at a time, 429/5xx retried with backoff
|
|
29
|
+
* honoring Retry-After; a failed map does not stop the others), then delete them when none failed. Returns
|
|
30
|
+
* `{uploaded, skipped, failed}`: `skipped` = the server already had those exact bytes (HTTP 200). Bodies are sent
|
|
31
|
+
* gzipped (`Content-Encoding: gzip`).
|
|
32
|
+
*/
|
|
28
33
|
export declare function uploadSourcemaps(o?: SourcemapOptions): Promise<{
|
|
29
34
|
uploaded: number;
|
|
35
|
+
skipped: number;
|
|
36
|
+
failed: number;
|
|
30
37
|
}>;
|
|
31
38
|
export {};
|
|
@@ -38,6 +38,7 @@ 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"));
|
|
41
42
|
/**
|
|
42
43
|
* `module.exports = withWatchtower(nextConfig, { sourcemaps: true })`: turns on `productionBrowserSourceMaps` so
|
|
43
44
|
* `next build` writes client maps (works with webpack and Turbopack). Then run `wt-next-sourcemaps` after the build
|
|
@@ -58,7 +59,29 @@ function* maps(dir) {
|
|
|
58
59
|
yield p;
|
|
59
60
|
}
|
|
60
61
|
}
|
|
61
|
-
|
|
62
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
63
|
+
/** The client chunk (`_next/static/...`) a map belongs to: the sibling JS's `//# sourceMappingURL=` target, else the map's own `file` field. */
|
|
64
|
+
function chunkFor(mapFile, name) {
|
|
65
|
+
try {
|
|
66
|
+
const js = mapFile.slice(0, -'.map'.length);
|
|
67
|
+
if (fs.existsSync(js)) {
|
|
68
|
+
const tail = fs.readFileSync(js, 'utf8').slice(-512);
|
|
69
|
+
if (/\/\/[#@] sourceMappingURL=/.test(tail))
|
|
70
|
+
return name.slice(0, -'.map'.length);
|
|
71
|
+
}
|
|
72
|
+
const file = JSON.parse(fs.readFileSync(mapFile, 'utf8')).file;
|
|
73
|
+
if (typeof file === 'string' && file)
|
|
74
|
+
return `${nodePath.posix.dirname(name)}/${nodePath.posix.basename(file)}`;
|
|
75
|
+
}
|
|
76
|
+
catch { /* unreadable: upload without `for` */ }
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Upload every `.map` under `dir` as `_next/static/<path>` for the release (8 at a time, 429/5xx retried with backoff
|
|
81
|
+
* honoring Retry-After; a failed map does not stop the others), then delete them when none failed. Returns
|
|
82
|
+
* `{uploaded, skipped, failed}`: `skipped` = the server already had those exact bytes (HTTP 200). Bodies are sent
|
|
83
|
+
* gzipped (`Content-Encoding: gzip`).
|
|
84
|
+
*/
|
|
62
85
|
async function uploadSourcemaps(o = {}) {
|
|
63
86
|
const env = process.env;
|
|
64
87
|
const key = o.apiKey || env.WT_API_KEY;
|
|
@@ -70,26 +93,67 @@ async function uploadSourcemaps(o = {}) {
|
|
|
70
93
|
throw new Error('WT_PROJECT (project id) is required');
|
|
71
94
|
if (!release)
|
|
72
95
|
throw new Error('WT_RELEASE is required');
|
|
96
|
+
const log = o.log || (() => { });
|
|
97
|
+
if (!o.apiUrl && !env.WT_API_URL && env.WT_API)
|
|
98
|
+
log('WT_API is deprecated, use WT_API_URL');
|
|
99
|
+
const base = (o.apiUrl || env.WT_API_URL || env.WT_API || 'https://watchtowerops.net').replace(/\/+$/, '');
|
|
73
100
|
const dir = nodePath.resolve(o.dir || '.next/static');
|
|
74
101
|
const f = o.fetch || fetch;
|
|
75
|
-
const
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
102
|
+
const root = `${base}/v1/projects/${encodeURIComponent(project)}`;
|
|
103
|
+
const send = async (path, init, retries) => {
|
|
104
|
+
for (let attempt = 0;; attempt++) {
|
|
105
|
+
let res;
|
|
106
|
+
try {
|
|
107
|
+
res = await f(root + path, { ...init, headers: { authorization: `Bearer ${key}`, ...init.headers } });
|
|
108
|
+
}
|
|
109
|
+
catch (e) {
|
|
110
|
+
const cause = e.cause;
|
|
111
|
+
throw new Error(`cannot reach ${base} (${cause?.code || cause?.message || e.message}); check WT_API_URL`);
|
|
112
|
+
}
|
|
113
|
+
if ((res.status !== 429 && res.status < 500) || attempt >= retries)
|
|
114
|
+
return res;
|
|
115
|
+
const ra = Number(res.headers.get('retry-after'));
|
|
116
|
+
await sleep(Number.isFinite(ra) && ra > 0 ? Math.min(ra, 30) * 1000 : Math.min(250 * 2 ** attempt, 8000));
|
|
117
|
+
}
|
|
81
118
|
};
|
|
82
119
|
const files = fs.existsSync(dir) ? [...maps(dir)] : [];
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
120
|
+
// release create fails fast: no retry, so a bad host or key surfaces at once
|
|
121
|
+
const rel = await send('/releases', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ version: release }) }, 0);
|
|
122
|
+
if (!rel.ok && rel.status !== 409)
|
|
123
|
+
throw new Error(`watchtower POST /releases -> HTTP ${rel.status}${rel.status === 401 || rel.status === 403 ? ' (check WT_API_KEY scope ci:write)' : ''}`);
|
|
124
|
+
let uploaded = 0, skipped = 0, failed = 0;
|
|
125
|
+
let next = 0;
|
|
126
|
+
const worker = async () => {
|
|
127
|
+
for (let file = files[next++]; file !== undefined; file = files[next++]) {
|
|
128
|
+
const name = `_next/static/${nodePath.relative(dir, file).split(nodePath.sep).join('/')}`;
|
|
129
|
+
const q = { kind: 'sourcemap', name, release };
|
|
130
|
+
const chunk = chunkFor(file, name);
|
|
131
|
+
if (chunk)
|
|
132
|
+
q.for = chunk;
|
|
133
|
+
try {
|
|
134
|
+
const res = await send(`/artifacts?${new URLSearchParams(q)}`, {
|
|
135
|
+
method: 'POST', headers: { 'content-type': 'application/octet-stream', 'content-encoding': 'gzip' },
|
|
136
|
+
body: new Uint8Array(zlib.gzipSync(fs.readFileSync(file))),
|
|
137
|
+
}, 4);
|
|
138
|
+
if (res.status === 200)
|
|
139
|
+
skipped++;
|
|
140
|
+
else if (res.ok)
|
|
141
|
+
uploaded++;
|
|
142
|
+
else {
|
|
143
|
+
failed++;
|
|
144
|
+
log(`failed ${name}: HTTP ${res.status}`);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
catch (e) {
|
|
148
|
+
failed++;
|
|
149
|
+
log(`failed ${name}: ${e.message}`);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
await Promise.all(Array.from({ length: Math.min(8, files.length) }, worker));
|
|
154
|
+
if (!o.keep && failed === 0)
|
|
91
155
|
for (const file of files)
|
|
92
156
|
fs.rmSync(file);
|
|
93
|
-
log(
|
|
94
|
-
return { uploaded
|
|
157
|
+
log(`${JSON.stringify({ uploaded, skipped, failed })} source maps for release ${release}${o.keep || failed ? '' : '; deleted the .map files'}`);
|
|
158
|
+
return { uploaded, skipped, failed };
|
|
95
159
|
}
|
|
@@ -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/
|
|
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
|
|
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
|
-
|
|
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
|
+
}
|
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
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.
|
|
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}`) }).
|
|
6
|
+
(0, next_build_js_1.uploadSourcemaps)({ dir: process.argv[2], log: (l) => console.log(`[watchtower] ${l}`) }).then((r) => { if (r.failed > 0)
|
|
7
|
+
process.exit(1); }).catch((e) => {
|
|
7
8
|
console.error(`[watchtower] source map upload failed: ${e.message}`);
|
|
8
9
|
process.exit(1);
|
|
9
10
|
});
|
package/dist/cjs/version.d.ts
CHANGED
|
@@ -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.
|
|
2
|
+
export declare const VERSION = "0.7.2";
|
package/dist/cjs/version.js
CHANGED
package/dist/esm/index.d.ts
CHANGED
|
@@ -76,6 +76,15 @@ export interface InitOptions {
|
|
|
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
|
-
|
|
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://
|
|
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;
|
|
@@ -24,8 +24,15 @@ type ConfigOrFn = NextConfig | ((...a: unknown[]) => NextConfig | Promise<NextCo
|
|
|
24
24
|
export declare function withWatchtower(config?: ConfigOrFn, opts?: {
|
|
25
25
|
sourcemaps?: boolean;
|
|
26
26
|
}): ConfigOrFn;
|
|
27
|
-
/**
|
|
27
|
+
/**
|
|
28
|
+
* Upload every `.map` under `dir` as `_next/static/<path>` for the release (8 at a time, 429/5xx retried with backoff
|
|
29
|
+
* honoring Retry-After; a failed map does not stop the others), then delete them when none failed. Returns
|
|
30
|
+
* `{uploaded, skipped, failed}`: `skipped` = the server already had those exact bytes (HTTP 200). Bodies are sent
|
|
31
|
+
* gzipped (`Content-Encoding: gzip`).
|
|
32
|
+
*/
|
|
28
33
|
export declare function uploadSourcemaps(o?: SourcemapOptions): Promise<{
|
|
29
34
|
uploaded: number;
|
|
35
|
+
skipped: number;
|
|
36
|
+
failed: number;
|
|
30
37
|
}>;
|
|
31
38
|
export {};
|
|
@@ -1,6 +1,7 @@
|
|
|
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';
|
|
4
5
|
/**
|
|
5
6
|
* `module.exports = withWatchtower(nextConfig, { sourcemaps: true })`: turns on `productionBrowserSourceMaps` so
|
|
6
7
|
* `next build` writes client maps (works with webpack and Turbopack). Then run `wt-next-sourcemaps` after the build
|
|
@@ -21,7 +22,29 @@ function* maps(dir) {
|
|
|
21
22
|
yield p;
|
|
22
23
|
}
|
|
23
24
|
}
|
|
24
|
-
|
|
25
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
26
|
+
/** The client chunk (`_next/static/...`) a map belongs to: the sibling JS's `//# sourceMappingURL=` target, else the map's own `file` field. */
|
|
27
|
+
function chunkFor(mapFile, name) {
|
|
28
|
+
try {
|
|
29
|
+
const js = mapFile.slice(0, -'.map'.length);
|
|
30
|
+
if (fs.existsSync(js)) {
|
|
31
|
+
const tail = fs.readFileSync(js, 'utf8').slice(-512);
|
|
32
|
+
if (/\/\/[#@] sourceMappingURL=/.test(tail))
|
|
33
|
+
return name.slice(0, -'.map'.length);
|
|
34
|
+
}
|
|
35
|
+
const file = JSON.parse(fs.readFileSync(mapFile, 'utf8')).file;
|
|
36
|
+
if (typeof file === 'string' && file)
|
|
37
|
+
return `${nodePath.posix.dirname(name)}/${nodePath.posix.basename(file)}`;
|
|
38
|
+
}
|
|
39
|
+
catch { /* unreadable: upload without `for` */ }
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Upload every `.map` under `dir` as `_next/static/<path>` for the release (8 at a time, 429/5xx retried with backoff
|
|
44
|
+
* honoring Retry-After; a failed map does not stop the others), then delete them when none failed. Returns
|
|
45
|
+
* `{uploaded, skipped, failed}`: `skipped` = the server already had those exact bytes (HTTP 200). Bodies are sent
|
|
46
|
+
* gzipped (`Content-Encoding: gzip`).
|
|
47
|
+
*/
|
|
25
48
|
export async function uploadSourcemaps(o = {}) {
|
|
26
49
|
const env = process.env;
|
|
27
50
|
const key = o.apiKey || env.WT_API_KEY;
|
|
@@ -33,26 +56,67 @@ export async function uploadSourcemaps(o = {}) {
|
|
|
33
56
|
throw new Error('WT_PROJECT (project id) is required');
|
|
34
57
|
if (!release)
|
|
35
58
|
throw new Error('WT_RELEASE is required');
|
|
59
|
+
const log = o.log || (() => { });
|
|
60
|
+
if (!o.apiUrl && !env.WT_API_URL && env.WT_API)
|
|
61
|
+
log('WT_API is deprecated, use WT_API_URL');
|
|
62
|
+
const base = (o.apiUrl || env.WT_API_URL || env.WT_API || 'https://watchtowerops.net').replace(/\/+$/, '');
|
|
36
63
|
const dir = nodePath.resolve(o.dir || '.next/static');
|
|
37
64
|
const f = o.fetch || fetch;
|
|
38
|
-
const
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
65
|
+
const root = `${base}/v1/projects/${encodeURIComponent(project)}`;
|
|
66
|
+
const send = async (path, init, retries) => {
|
|
67
|
+
for (let attempt = 0;; attempt++) {
|
|
68
|
+
let res;
|
|
69
|
+
try {
|
|
70
|
+
res = await f(root + path, { ...init, headers: { authorization: `Bearer ${key}`, ...init.headers } });
|
|
71
|
+
}
|
|
72
|
+
catch (e) {
|
|
73
|
+
const cause = e.cause;
|
|
74
|
+
throw new Error(`cannot reach ${base} (${cause?.code || cause?.message || e.message}); check WT_API_URL`);
|
|
75
|
+
}
|
|
76
|
+
if ((res.status !== 429 && res.status < 500) || attempt >= retries)
|
|
77
|
+
return res;
|
|
78
|
+
const ra = Number(res.headers.get('retry-after'));
|
|
79
|
+
await sleep(Number.isFinite(ra) && ra > 0 ? Math.min(ra, 30) * 1000 : Math.min(250 * 2 ** attempt, 8000));
|
|
80
|
+
}
|
|
44
81
|
};
|
|
45
82
|
const files = fs.existsSync(dir) ? [...maps(dir)] : [];
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
83
|
+
// release create fails fast: no retry, so a bad host or key surfaces at once
|
|
84
|
+
const rel = await send('/releases', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ version: release }) }, 0);
|
|
85
|
+
if (!rel.ok && rel.status !== 409)
|
|
86
|
+
throw new Error(`watchtower POST /releases -> HTTP ${rel.status}${rel.status === 401 || rel.status === 403 ? ' (check WT_API_KEY scope ci:write)' : ''}`);
|
|
87
|
+
let uploaded = 0, skipped = 0, failed = 0;
|
|
88
|
+
let next = 0;
|
|
89
|
+
const worker = async () => {
|
|
90
|
+
for (let file = files[next++]; file !== undefined; file = files[next++]) {
|
|
91
|
+
const name = `_next/static/${nodePath.relative(dir, file).split(nodePath.sep).join('/')}`;
|
|
92
|
+
const q = { kind: 'sourcemap', name, release };
|
|
93
|
+
const chunk = chunkFor(file, name);
|
|
94
|
+
if (chunk)
|
|
95
|
+
q.for = chunk;
|
|
96
|
+
try {
|
|
97
|
+
const res = await send(`/artifacts?${new URLSearchParams(q)}`, {
|
|
98
|
+
method: 'POST', headers: { 'content-type': 'application/octet-stream', 'content-encoding': 'gzip' },
|
|
99
|
+
body: new Uint8Array(zlib.gzipSync(fs.readFileSync(file))),
|
|
100
|
+
}, 4);
|
|
101
|
+
if (res.status === 200)
|
|
102
|
+
skipped++;
|
|
103
|
+
else if (res.ok)
|
|
104
|
+
uploaded++;
|
|
105
|
+
else {
|
|
106
|
+
failed++;
|
|
107
|
+
log(`failed ${name}: HTTP ${res.status}`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
catch (e) {
|
|
111
|
+
failed++;
|
|
112
|
+
log(`failed ${name}: ${e.message}`);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
await Promise.all(Array.from({ length: Math.min(8, files.length) }, worker));
|
|
117
|
+
if (!o.keep && failed === 0)
|
|
54
118
|
for (const file of files)
|
|
55
119
|
fs.rmSync(file);
|
|
56
|
-
log(
|
|
57
|
-
return { uploaded
|
|
120
|
+
log(`${JSON.stringify({ uploaded, skipped, failed })} source maps for release ${release}${o.keep || failed ? '' : '; deleted the .map files'}`);
|
|
121
|
+
return { uploaded, skipped, failed };
|
|
58
122
|
}
|
|
@@ -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/
|
|
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
|
|
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
|
-
|
|
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,8 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
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
3
|
import { uploadSourcemaps } from './integrations/next-build.js';
|
|
4
|
-
uploadSourcemaps({ dir: process.argv[2], log: (l) => console.log(`[watchtower] ${l}`) }).
|
|
4
|
+
uploadSourcemaps({ dir: process.argv[2], log: (l) => console.log(`[watchtower] ${l}`) }).then((r) => { if (r.failed > 0)
|
|
5
|
+
process.exit(1); }).catch((e) => {
|
|
5
6
|
console.error(`[watchtower] source map upload failed: ${e.message}`);
|
|
6
7
|
process.exit(1);
|
|
7
8
|
});
|
package/dist/esm/version.d.ts
CHANGED
|
@@ -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.
|
|
2
|
+
export declare const VERSION = "0.7.2";
|
package/dist/esm/version.js
CHANGED
|
@@ -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.
|
|
2
|
+
export const VERSION = '0.7.2';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@watchtowerobservability/node",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.2",
|
|
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",
|