@watchtowerobservability/node 0.7.4 → 0.7.5
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 +36 -1
- package/README.md +96 -12
- package/dist/cjs/attach.d.ts +7 -0
- package/dist/cjs/attach.js +29 -0
- package/dist/cjs/checkin.js +4 -13
- package/dist/cjs/cron/monitor.d.ts +47 -0
- package/dist/cjs/cron/monitor.js +95 -0
- package/dist/cjs/cron/schedulers.d.ts +36 -0
- package/dist/cjs/cron/schedulers.js +65 -0
- package/dist/cjs/env.d.ts +10 -0
- package/dist/cjs/env.js +98 -1
- package/dist/cjs/index.d.ts +50 -6
- package/dist/cjs/index.js +114 -37
- package/dist/cjs/instrument-for.d.ts +17 -0
- package/dist/cjs/instrument-for.js +44 -0
- package/dist/cjs/instrument.d.ts +4 -0
- package/dist/cjs/instrument.js +32 -1
- package/dist/cjs/integrations/bullmq.d.ts +8 -2
- package/dist/cjs/integrations/bullmq.js +32 -5
- package/dist/cjs/integrations/db.d.ts +7 -2
- package/dist/cjs/integrations/db.js +32 -4
- package/dist/cjs/integrations/fastify.d.ts +1 -1
- package/dist/cjs/integrations/fastify.js +3 -2
- package/dist/cjs/integrations/logs.js +16 -5
- package/dist/cjs/integrations/next-build.d.ts +25 -8
- package/dist/cjs/integrations/next-build.js +159 -34
- package/dist/cjs/integrations/next-tunnel.d.ts +6 -1
- package/dist/cjs/integrations/next-tunnel.js +13 -2
- package/dist/cjs/integrations/postgres.d.ts +2 -0
- package/dist/cjs/integrations/postgres.js +43 -1
- package/dist/cjs/integrations/temporal.d.ts +97 -0
- package/dist/cjs/integrations/temporal.js +144 -0
- package/dist/cjs/integrations/util.d.ts +17 -0
- package/dist/cjs/integrations/util.js +38 -0
- package/dist/cjs/load.d.ts +6 -0
- package/dist/cjs/load.js +28 -4
- package/dist/cjs/next-doctor-cli.d.ts +2 -0
- package/dist/cjs/next-doctor-cli.js +15 -0
- package/dist/cjs/next-doctor.d.ts +2 -0
- package/dist/cjs/next-doctor.js +50 -0
- package/dist/cjs/redact-presets.d.ts +16 -0
- package/dist/cjs/redact-presets.js +66 -0
- package/dist/cjs/register.js +19 -6
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/esm/attach.d.ts +7 -0
- package/dist/esm/attach.js +25 -0
- package/dist/esm/checkin.js +4 -13
- package/dist/esm/cron/monitor.d.ts +47 -0
- package/dist/esm/cron/monitor.js +85 -0
- package/dist/esm/cron/schedulers.d.ts +36 -0
- package/dist/esm/cron/schedulers.js +61 -0
- package/dist/esm/env.d.ts +10 -0
- package/dist/esm/env.js +62 -0
- package/dist/esm/index.d.ts +50 -6
- package/dist/esm/index.js +109 -39
- package/dist/esm/instrument-for.d.ts +17 -0
- package/dist/esm/instrument-for.js +41 -0
- package/dist/esm/instrument.d.ts +4 -0
- package/dist/esm/instrument.js +30 -1
- package/dist/esm/integrations/bullmq.d.ts +8 -2
- package/dist/esm/integrations/bullmq.js +32 -5
- package/dist/esm/integrations/db.d.ts +7 -2
- package/dist/esm/integrations/db.js +29 -4
- package/dist/esm/integrations/fastify.d.ts +1 -1
- package/dist/esm/integrations/fastify.js +4 -3
- package/dist/esm/integrations/logs.js +16 -5
- package/dist/esm/integrations/next-build.d.ts +25 -8
- package/dist/esm/integrations/next-build.js +158 -34
- package/dist/esm/integrations/next-tunnel.d.ts +6 -1
- package/dist/esm/integrations/next-tunnel.js +13 -2
- package/dist/esm/integrations/postgres.d.ts +2 -0
- package/dist/esm/integrations/postgres.js +43 -2
- package/dist/esm/integrations/temporal.d.ts +97 -0
- package/dist/esm/integrations/temporal.js +139 -0
- package/dist/esm/integrations/util.d.ts +17 -0
- package/dist/esm/integrations/util.js +35 -0
- package/dist/esm/load.d.ts +6 -0
- package/dist/esm/load.js +21 -3
- package/dist/esm/next-doctor-cli.d.ts +2 -0
- package/dist/esm/next-doctor-cli.js +13 -0
- package/dist/esm/next-doctor.d.ts +2 -0
- package/dist/esm/next-doctor.js +14 -0
- package/dist/esm/redact-presets.d.ts +16 -0
- package/dist/esm/redact-presets.js +58 -0
- package/dist/esm/register.js +20 -7
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/package.json +42 -1
|
@@ -18,17 +18,34 @@ export interface SourcemapOptions {
|
|
|
18
18
|
}
|
|
19
19
|
type NextConfig = Record<string, unknown>;
|
|
20
20
|
type ConfigOrFn = NextConfig | ((...a: unknown[]) => NextConfig | Promise<NextConfig>);
|
|
21
|
+
export interface WatchtowerNextOptions {
|
|
22
|
+
/**
|
|
23
|
+
* `true` turns on `productionBrowserSourceMaps`. `{ upload: true }` also uploads `.next/static/**\/*.map` when the
|
|
24
|
+
* webpack client build finishes (CI: needs WT_API_KEY, WT_PROJECT; the release is `release` below) and, unless
|
|
25
|
+
* `deleteAfterUpload: false`, deletes the maps. Turbopack has no webpack hooks: run `wt-next-sourcemaps` after `next build`.
|
|
26
|
+
* Other fields are `uploadSourcemaps` options (`apiUrl`, `project`, `inject`, `fetch`, ...).
|
|
27
|
+
*/
|
|
28
|
+
sourcemaps?: boolean | (Omit<SourcemapOptions, 'keep' | 'release'> & {
|
|
29
|
+
upload?: boolean;
|
|
30
|
+
deleteAfterUpload?: boolean;
|
|
31
|
+
});
|
|
32
|
+
/** Release: injected as `NEXT_PUBLIC_WT_RELEASE` (read by `<WatchtowerRUM>`). Default WT_RELEASE, then the git sha of the CI/host env. */
|
|
33
|
+
release?: string;
|
|
34
|
+
}
|
|
21
35
|
/**
|
|
22
|
-
* `module.exports = withWatchtower(nextConfig, { sourcemaps: true })
|
|
23
|
-
*
|
|
24
|
-
*
|
|
36
|
+
* `module.exports = withWatchtower(nextConfig, { sourcemaps: true })`. Makes `output: 'standalone'` work with no other
|
|
37
|
+
* config: the node SDK and its OpenTelemetry peers go into `serverExternalPackages` and (npm and pnpm layouts) into
|
|
38
|
+
* `outputFileTracingIncludes` for every route. Sets `NEXT_PUBLIC_WT_RELEASE`. With `sourcemaps` it turns on
|
|
39
|
+
* `productionBrowserSourceMaps` (webpack and Turbopack); `{ upload: true }` uploads and deletes them after the webpack
|
|
40
|
+
* client build, otherwise run `wt-next-sourcemaps` after `next build` (`"build": "next build && wt-next-sourcemaps"`).
|
|
25
41
|
*/
|
|
26
|
-
export declare function withWatchtower(config?: ConfigOrFn, opts?:
|
|
27
|
-
|
|
28
|
-
|
|
42
|
+
export declare function withWatchtower(config?: ConfigOrFn, opts?: WatchtowerNextOptions): ConfigOrFn;
|
|
43
|
+
/** A ustar archive of `[name, bytes]` (names up to 100 bytes). */
|
|
44
|
+
export declare function makeTar(files: [string, Buffer][]): Buffer;
|
|
29
45
|
/**
|
|
30
|
-
* Upload every `.map` under `dir` as `_next/static/<path>` for the release
|
|
31
|
-
*
|
|
46
|
+
* Upload every `.map` under `dir` as `_next/static/<path>` for the release: one `POST /artifacts/bundle` per ~120 MB
|
|
47
|
+
* batch (tar.gz plus manifest), falling back to single uploads (8 at a time) when the server has no bundle route.
|
|
48
|
+
* 429/5xx are retried with backoff honoring Retry-After; a failed map does not stop the others. The maps are deleted when none failed. Returns
|
|
32
49
|
* `{uploaded, skipped, failed}`: `skipped` = the server already had those exact bytes (HTTP 200). Bodies are sent
|
|
33
50
|
* gzipped (`Content-Encoding: gzip`).
|
|
34
51
|
*/
|
|
@@ -3,13 +3,62 @@ import * as fs from 'node:fs';
|
|
|
3
3
|
import * as nodePath from 'node:path';
|
|
4
4
|
import * as zlib from 'node:zlib';
|
|
5
5
|
import { injectDebugIds } from './next-debugid.js';
|
|
6
|
+
// what the node SDK loads at run time through createRequire (invisible to Next's file tracer): see src/load.ts
|
|
7
|
+
const OTEL = [
|
|
8
|
+
'@opentelemetry/api', '@opentelemetry/context-async-hooks', '@opentelemetry/core', '@opentelemetry/exporter-logs-otlp-http',
|
|
9
|
+
'@opentelemetry/exporter-metrics-otlp-http', '@opentelemetry/exporter-trace-otlp-http', '@opentelemetry/instrumentation',
|
|
10
|
+
'@opentelemetry/otlp-transformer', '@opentelemetry/resources', '@opentelemetry/sdk-logs', '@opentelemetry/sdk-metrics',
|
|
11
|
+
'@opentelemetry/sdk-trace',
|
|
12
|
+
];
|
|
13
|
+
const EXTERNAL = ['@watchtowerobservability/node', ...OTEL];
|
|
14
|
+
const SHA_ENV = ['WT_RELEASE', 'GITHUB_SHA', 'CI_COMMIT_SHA', 'VERCEL_GIT_COMMIT_SHA', 'SOURCE_VERSION', 'BUILD_SHA', 'RENDER_GIT_COMMIT', 'HEROKU_SLUG_COMMIT'];
|
|
15
|
+
const INCLUDE = ['@watchtowerobservability/node', '@opentelemetry'].flatMap((p) => [`./node_modules/${p}/**/*`, `./node_modules/.pnpm/${p.replace('/', '+')}*/**/*`]);
|
|
6
16
|
/**
|
|
7
|
-
* `module.exports = withWatchtower(nextConfig, { sourcemaps: true })
|
|
8
|
-
*
|
|
9
|
-
*
|
|
17
|
+
* `module.exports = withWatchtower(nextConfig, { sourcemaps: true })`. Makes `output: 'standalone'` work with no other
|
|
18
|
+
* config: the node SDK and its OpenTelemetry peers go into `serverExternalPackages` and (npm and pnpm layouts) into
|
|
19
|
+
* `outputFileTracingIncludes` for every route. Sets `NEXT_PUBLIC_WT_RELEASE`. With `sourcemaps` it turns on
|
|
20
|
+
* `productionBrowserSourceMaps` (webpack and Turbopack); `{ upload: true }` uploads and deletes them after the webpack
|
|
21
|
+
* client build, otherwise run `wt-next-sourcemaps` after `next build` (`"build": "next build && wt-next-sourcemaps"`).
|
|
10
22
|
*/
|
|
11
23
|
export function withWatchtower(config = {}, opts = {}) {
|
|
12
|
-
const
|
|
24
|
+
const sm = opts.sourcemaps === true ? {} : opts.sourcemaps || undefined;
|
|
25
|
+
const release = opts.release || SHA_ENV.map((k) => process.env[k]).find((v) => v && v !== 'unknown');
|
|
26
|
+
const apply = (c) => {
|
|
27
|
+
const inc = { ...c.outputFileTracingIncludes };
|
|
28
|
+
inc['/*'] = [...new Set([...(inc['/*'] ?? []), ...INCLUDE])];
|
|
29
|
+
const out = {
|
|
30
|
+
...c,
|
|
31
|
+
serverExternalPackages: [...new Set([...(c.serverExternalPackages ?? []), ...EXTERNAL])],
|
|
32
|
+
outputFileTracingIncludes: inc,
|
|
33
|
+
};
|
|
34
|
+
if (release)
|
|
35
|
+
out.env = { NEXT_PUBLIC_WT_RELEASE: release, ...c.env };
|
|
36
|
+
if (sm)
|
|
37
|
+
out.productionBrowserSourceMaps = true;
|
|
38
|
+
if (sm && 'upload' in sm && sm.upload) {
|
|
39
|
+
const { upload: _u, deleteAfterUpload, ...so } = sm;
|
|
40
|
+
const w = c.webpack;
|
|
41
|
+
out.webpack = (cfg, ctx) => {
|
|
42
|
+
const r = (w ? w(cfg, ctx) : cfg);
|
|
43
|
+
if (!ctx.dev && !ctx.isServer) {
|
|
44
|
+
(r.plugins ??= []).push({
|
|
45
|
+
apply(compiler) {
|
|
46
|
+
compiler.hooks.done.tapPromise('watchtower-sourcemaps', async () => {
|
|
47
|
+
try {
|
|
48
|
+
await uploadSourcemaps({ log: (l) => console.log(`[watchtower] ${l}`), ...so, release, keep: deleteAfterUpload === false });
|
|
49
|
+
}
|
|
50
|
+
catch (e) {
|
|
51
|
+
console.warn(`[watchtower] source map upload skipped: ${e.message}`);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
return r;
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
return out;
|
|
61
|
+
};
|
|
13
62
|
if (typeof config !== 'function')
|
|
14
63
|
return apply(config);
|
|
15
64
|
return async (...a) => apply(await config(...a));
|
|
@@ -23,6 +72,30 @@ function* maps(dir) {
|
|
|
23
72
|
yield p;
|
|
24
73
|
}
|
|
25
74
|
}
|
|
75
|
+
/** A ustar archive of `[name, bytes]` (names up to 100 bytes). */
|
|
76
|
+
export function makeTar(files) {
|
|
77
|
+
const parts = [];
|
|
78
|
+
for (const [name, data] of files) {
|
|
79
|
+
const h = Buffer.alloc(512);
|
|
80
|
+
h.write(name, 0, 100, 'utf8');
|
|
81
|
+
h.write('0000644\0', 100);
|
|
82
|
+
h.write('0000000\0', 108);
|
|
83
|
+
h.write('0000000\0', 116);
|
|
84
|
+
h.write(`${data.length.toString(8).padStart(11, '0')}\0`, 124);
|
|
85
|
+
h.write('00000000000\0', 136);
|
|
86
|
+
h.write(' ', 148);
|
|
87
|
+
h.write('0', 156);
|
|
88
|
+
h.write('ustar\0', 257);
|
|
89
|
+
h.write('00', 263);
|
|
90
|
+
let sum = 0;
|
|
91
|
+
for (const b of h)
|
|
92
|
+
sum += b;
|
|
93
|
+
h.write(`${sum.toString(8).padStart(6, '0')}\0 `, 148);
|
|
94
|
+
parts.push(h, data, Buffer.alloc((512 - (data.length % 512)) % 512));
|
|
95
|
+
}
|
|
96
|
+
parts.push(Buffer.alloc(1024));
|
|
97
|
+
return Buffer.concat(parts);
|
|
98
|
+
}
|
|
26
99
|
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
27
100
|
/** The client chunk (`_next/static/...`) a map belongs to: the sibling JS's `//# sourceMappingURL=` target, else the map's own `file` field. */
|
|
28
101
|
function chunkFor(mapFile, name) {
|
|
@@ -41,8 +114,9 @@ function chunkFor(mapFile, name) {
|
|
|
41
114
|
return undefined;
|
|
42
115
|
}
|
|
43
116
|
/**
|
|
44
|
-
* Upload every `.map` under `dir` as `_next/static/<path>` for the release
|
|
45
|
-
*
|
|
117
|
+
* Upload every `.map` under `dir` as `_next/static/<path>` for the release: one `POST /artifacts/bundle` per ~120 MB
|
|
118
|
+
* batch (tar.gz plus manifest), falling back to single uploads (8 at a time) when the server has no bundle route.
|
|
119
|
+
* 429/5xx are retried with backoff honoring Retry-After; a failed map does not stop the others. The maps are deleted when none failed. Returns
|
|
46
120
|
* `{uploaded, skipped, failed}`: `skipped` = the server already had those exact bytes (HTTP 200). Bodies are sent
|
|
47
121
|
* gzipped (`Content-Encoding: gzip`).
|
|
48
122
|
*/
|
|
@@ -89,38 +163,88 @@ export async function uploadSourcemaps(o = {}) {
|
|
|
89
163
|
if (!rel.ok && rel.status !== 409)
|
|
90
164
|
throw new Error(`watchtower POST /releases -> HTTP ${rel.status}${rel.status === 401 || rel.status === 403 ? ' (check WT_API_KEY scope ci:write)' : ''}`);
|
|
91
165
|
let uploaded = 0, skipped = 0, failed = 0;
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
else {
|
|
113
|
-
failed++;
|
|
114
|
-
log(`failed ${name}: HTTP ${res.status}`);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
catch (e) {
|
|
166
|
+
const entries = files.map((file) => {
|
|
167
|
+
const name = `_next/static/${nodePath.relative(dir, file).split(nodePath.sep).join('/')}`;
|
|
168
|
+
return { file, name, chunk: chunkFor(file, name), id: ids.get(file) };
|
|
169
|
+
});
|
|
170
|
+
const single = async (e) => {
|
|
171
|
+
const q = { kind: 'sourcemap', name: e.name, release };
|
|
172
|
+
if (e.chunk)
|
|
173
|
+
q.for = e.chunk;
|
|
174
|
+
if (e.id)
|
|
175
|
+
q.debug_id = e.id;
|
|
176
|
+
try {
|
|
177
|
+
const res = await send(`/artifacts?${new URLSearchParams(q)}`, {
|
|
178
|
+
method: 'POST', headers: { 'content-type': 'application/octet-stream', 'content-encoding': 'gzip' },
|
|
179
|
+
body: new Uint8Array(zlib.gzipSync(fs.readFileSync(e.file))),
|
|
180
|
+
}, 4);
|
|
181
|
+
if (res.status === 200)
|
|
182
|
+
skipped++;
|
|
183
|
+
else if (res.ok)
|
|
184
|
+
uploaded++;
|
|
185
|
+
else {
|
|
118
186
|
failed++;
|
|
119
|
-
log(`failed ${name}: ${
|
|
187
|
+
log(`failed ${e.name}: HTTP ${res.status}`);
|
|
120
188
|
}
|
|
121
189
|
}
|
|
190
|
+
catch (err) {
|
|
191
|
+
failed++;
|
|
192
|
+
log(`failed ${e.name}: ${err.message}`);
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
const concurrently = async (list) => {
|
|
196
|
+
let next = 0;
|
|
197
|
+
const worker = async () => {
|
|
198
|
+
for (let e = list[next++]; e !== undefined; e = list[next++])
|
|
199
|
+
await single(e);
|
|
200
|
+
};
|
|
201
|
+
await Promise.all(Array.from({ length: Math.min(8, list.length) }, worker));
|
|
122
202
|
};
|
|
123
|
-
|
|
203
|
+
// One request per batch (tar.gz + manifest). A server without the bundle route (or a request that
|
|
204
|
+
// never got through) falls back to concurrent single uploads for that batch.
|
|
205
|
+
const BATCH_BYTES = 120 * 1024 * 1024;
|
|
206
|
+
const batches = [];
|
|
207
|
+
let cur = [], curBytes = 0;
|
|
208
|
+
for (const e of entries) {
|
|
209
|
+
const size = fs.statSync(e.file).size;
|
|
210
|
+
if (cur.length && curBytes + size > BATCH_BYTES) {
|
|
211
|
+
batches.push(cur);
|
|
212
|
+
cur = [];
|
|
213
|
+
curBytes = 0;
|
|
214
|
+
}
|
|
215
|
+
cur.push(e);
|
|
216
|
+
curBytes += size;
|
|
217
|
+
}
|
|
218
|
+
if (cur.length)
|
|
219
|
+
batches.push(cur);
|
|
220
|
+
for (const batch of batches) {
|
|
221
|
+
let handled = false;
|
|
222
|
+
try {
|
|
223
|
+
const manifest = { files: batch.map((e, i) => ({ path: `m${i}.map`, name: e.name, ...(e.chunk ? { for: e.chunk } : {}), ...(e.id ? { debug_id: e.id } : {}) })) };
|
|
224
|
+
const tar = makeTar([['manifest.json', Buffer.from(JSON.stringify(manifest))], ...batch.map((e, i) => [`m${i}.map`, fs.readFileSync(e.file)])]);
|
|
225
|
+
const res = await send(`/artifacts/bundle?${new URLSearchParams({ release })}`, {
|
|
226
|
+
method: 'POST', headers: { 'content-type': 'application/octet-stream' }, body: new Uint8Array(zlib.gzipSync(tar)),
|
|
227
|
+
}, 4);
|
|
228
|
+
const r = res.ok ? (await res.json().catch(() => ({}))) : {};
|
|
229
|
+
if (typeof r.uploaded === 'number') { // anything else is not our bundle route: fall back
|
|
230
|
+
uploaded += r.uploaded;
|
|
231
|
+
skipped += r.skipped || 0;
|
|
232
|
+
failed += r.failed || 0;
|
|
233
|
+
for (const e of r.errors || [])
|
|
234
|
+
log(`failed ${e.name}: ${e.reason}`);
|
|
235
|
+
handled = true;
|
|
236
|
+
}
|
|
237
|
+
else if (res.status === 401 || res.status === 403) {
|
|
238
|
+
throw new Error(`watchtower POST /artifacts/bundle -> HTTP ${res.status} (check WT_API_KEY scope ci:write)`);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
catch (e) {
|
|
242
|
+
if (e.message.startsWith('watchtower ') || e.message.startsWith('cannot reach'))
|
|
243
|
+
throw e;
|
|
244
|
+
}
|
|
245
|
+
if (!handled)
|
|
246
|
+
await concurrently(batch);
|
|
247
|
+
}
|
|
124
248
|
if (!o.keep && failed === 0)
|
|
125
249
|
for (const file of files)
|
|
126
250
|
fs.rmSync(file);
|
|
@@ -5,9 +5,14 @@ export interface TunnelOptions {
|
|
|
5
5
|
basePath?: string;
|
|
6
6
|
/** Request body cap in bytes; default 1 MiB (over it: 413). */
|
|
7
7
|
maxBodyBytes?: number;
|
|
8
|
+
/**
|
|
9
|
+
* Only forward beacons whose publishable key is one of these (`?k=` or a `wtp_` bearer); others get 403. Stops
|
|
10
|
+
* a third party from using your tunnel as a relay for another project's key.
|
|
11
|
+
*/
|
|
12
|
+
allowKey?: string | string[];
|
|
8
13
|
fetch?: typeof fetch;
|
|
9
14
|
}
|
|
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. */
|
|
15
|
+
/** 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, plus the visitor's `Origin`/`Referer` (the edge checks the Origin against the key's allowed origins); cookies and other headers are not. */
|
|
11
16
|
export declare function tunnelHandler(o: TunnelOptions): {
|
|
12
17
|
GET: (r: Request) => Promise<Response>;
|
|
13
18
|
POST: (r: Request) => Promise<Response>;
|
|
@@ -3,12 +3,15 @@
|
|
|
3
3
|
// Browser SDK: init({ endpoint: '/api/wt', publishableKey: 'wtp_...' }).
|
|
4
4
|
// only what the browser SDK calls: /v1/rum/*, /v1/replay/<session>/chunks /v1/logs and /v1/traces
|
|
5
5
|
const ALLOWED = /^\/v1\/(rum\/[A-Za-z0-9/_-]+|replay\/[A-Za-z0-9_-]+\/chunks|logs|traces)$/;
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
// `origin` / `referer`: the edge refuses a browser key without an Origin on the key's allowed origins (403 origin_not_allowed),
|
|
7
|
+
// so the tunnel passes through the visitor's own Origin: add your site's origin to the key's allowed origins.
|
|
8
|
+
const FORWARD = ['content-type', 'content-encoding', 'user-agent', 'origin', 'referer', 'x-wt-sdk', 'x-wt-mask'];
|
|
9
|
+
/** 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, plus the visitor's `Origin`/`Referer` (the edge checks the Origin against the key's allowed origins); cookies and other headers are not. */
|
|
8
10
|
export function tunnelHandler(o) {
|
|
9
11
|
const base = (o.basePath ?? '/api/wt').replace(/\/+$/, '');
|
|
10
12
|
const max = o.maxBodyBytes ?? 1024 * 1024;
|
|
11
13
|
const edge = o.endpoint.replace(/\/+$/, '');
|
|
14
|
+
const allow = o.allowKey === undefined ? [] : [].concat(o.allowKey);
|
|
12
15
|
const send = o.fetch ?? fetch;
|
|
13
16
|
const handle = async (req) => {
|
|
14
17
|
const url = new URL(req.url);
|
|
@@ -21,10 +24,18 @@ export function tunnelHandler(o) {
|
|
|
21
24
|
if (v)
|
|
22
25
|
headers[h] = v;
|
|
23
26
|
}
|
|
27
|
+
if (!headers.origin && headers.referer) { // same-origin GETs carry no Origin: use the Referer's
|
|
28
|
+
try {
|
|
29
|
+
headers.origin = new URL(headers.referer).origin;
|
|
30
|
+
}
|
|
31
|
+
catch { /* not a URL: forward as is */ }
|
|
32
|
+
}
|
|
24
33
|
const bearer = /^Bearer (wtp_\S+)$/.exec(req.headers.get('authorization') ?? '');
|
|
25
34
|
if (bearer)
|
|
26
35
|
headers.authorization = `Bearer ${bearer[1]}`;
|
|
27
36
|
const k = url.searchParams.get('k');
|
|
37
|
+
if (allow.length && ![k, bearer?.[1]].some((x) => x && allow.includes(x)))
|
|
38
|
+
return new Response('forbidden', { status: 403 });
|
|
28
39
|
let body;
|
|
29
40
|
if (req.method === 'POST') {
|
|
30
41
|
if (Number(req.headers.get('content-length') ?? 0) > max)
|
|
@@ -9,4 +9,6 @@ export { querySummary };
|
|
|
9
9
|
* failure. Works for ESM and CJS; returns an unsubscribe function (a no-op when this Node has no
|
|
10
10
|
* tracing channels).
|
|
11
11
|
*/
|
|
12
|
+
/** Major version of the app's installed `postgres`, or undefined when absent or unreadable. */
|
|
13
|
+
export declare function postgresMajor(from?: string): number | undefined;
|
|
12
14
|
export declare function instrumentPostgres(opts?: DbOptions): () => void;
|
|
@@ -1,8 +1,26 @@
|
|
|
1
1
|
import * as dc from 'node:diagnostics_channel';
|
|
2
|
-
import {
|
|
2
|
+
import { readFileSync } from 'node:fs';
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
4
|
+
import { dirname, join } from 'node:path';
|
|
5
|
+
import { context, diag, SpanStatusCode } from '@opentelemetry/api';
|
|
6
|
+
import { load } from '../load.js';
|
|
3
7
|
import { querySummary } from '../sql.js';
|
|
4
8
|
import { startDbSpan } from './db.js';
|
|
5
9
|
export { querySummary };
|
|
10
|
+
let warned = false;
|
|
11
|
+
// Warn once, and only when postgres is installed (load is real, but cheap: cached): without tracingChannel nothing would ever be recorded.
|
|
12
|
+
function warnOnce() {
|
|
13
|
+
if (warned)
|
|
14
|
+
return;
|
|
15
|
+
warned = true;
|
|
16
|
+
try {
|
|
17
|
+
load('postgres');
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
diag.warn('watchtower: instrumentPostgres() is a no-op: this Node has no diagnostics_channel.tracingChannel; postgres@>=4 on a Node that provides it is required');
|
|
23
|
+
}
|
|
6
24
|
/**
|
|
7
25
|
* postgres.js has no OTel instrumentation; since 4.0 it publishes each query on the
|
|
8
26
|
* `postgres:query` diagnostics tracing channel. This turns those into CLIENT spans (child of the
|
|
@@ -11,9 +29,32 @@ export { querySummary };
|
|
|
11
29
|
* failure. Works for ESM and CJS; returns an unsubscribe function (a no-op when this Node has no
|
|
12
30
|
* tracing channels).
|
|
13
31
|
*/
|
|
32
|
+
/** Major version of the app's installed `postgres`, or undefined when absent or unreadable. */
|
|
33
|
+
export function postgresMajor(from = process.cwd()) {
|
|
34
|
+
try {
|
|
35
|
+
// `postgres/package.json` is not exported, so walk up from the resolved entry.
|
|
36
|
+
let dir = dirname(createRequire(join(from, 'noop.js')).resolve('postgres'));
|
|
37
|
+
for (let i = 0; i < 6; i++, dir = dirname(dir)) {
|
|
38
|
+
try {
|
|
39
|
+
const pkg = JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8'));
|
|
40
|
+
if (pkg.name === 'postgres')
|
|
41
|
+
return Number.parseInt(pkg.version ?? '', 10);
|
|
42
|
+
}
|
|
43
|
+
catch { /* keep walking */ }
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
catch { /* not installed */ }
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
14
49
|
export function instrumentPostgres(opts) {
|
|
15
|
-
if (typeof dc.tracingChannel !== 'function')
|
|
50
|
+
if (typeof dc.tracingChannel !== 'function') {
|
|
51
|
+
warnOnce();
|
|
16
52
|
return () => { };
|
|
53
|
+
}
|
|
54
|
+
const major = postgresMajor();
|
|
55
|
+
if (major !== undefined && major < 4) {
|
|
56
|
+
diag.warn(`watchtower: instrumentPostgres() records nothing with postgres@${major}: the postgres:query channel exists only in postgres@>=4`);
|
|
57
|
+
}
|
|
17
58
|
const ch = dc.tracingChannel('postgres:query');
|
|
18
59
|
const spans = new WeakMap();
|
|
19
60
|
const handlers = {
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
interface ActivityInfo {
|
|
2
|
+
activityType?: string;
|
|
3
|
+
activityId?: string;
|
|
4
|
+
attempt?: number;
|
|
5
|
+
taskQueue?: string;
|
|
6
|
+
workflowType?: string;
|
|
7
|
+
workflowExecution?: {
|
|
8
|
+
workflowId?: string;
|
|
9
|
+
runId?: string;
|
|
10
|
+
};
|
|
11
|
+
retryPolicy?: {
|
|
12
|
+
maximumAttempts?: number;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
interface ActivityCtx {
|
|
16
|
+
info?: ActivityInfo;
|
|
17
|
+
}
|
|
18
|
+
type Next = (input: unknown) => Promise<unknown>;
|
|
19
|
+
export interface TemporalInterceptorOptions {
|
|
20
|
+
/** Retry cap when the activity info does not carry the retry policy; a failure on this attempt (or a non-retryable one) is captured. */
|
|
21
|
+
maxAttempts?: number;
|
|
22
|
+
}
|
|
23
|
+
/** Worker options fragment: `Worker.create({ ..., interceptors: temporalInterceptors() })`. Spans for each activity attempt; the exception is captured on the final attempt only. */
|
|
24
|
+
export declare function temporalInterceptors(o?: TemporalInterceptorOptions): {
|
|
25
|
+
activity: ((ctx: ActivityCtx) => {
|
|
26
|
+
inbound: {
|
|
27
|
+
execute(input: unknown, next: Next): Promise<unknown>;
|
|
28
|
+
};
|
|
29
|
+
})[];
|
|
30
|
+
};
|
|
31
|
+
interface Range {
|
|
32
|
+
start?: number;
|
|
33
|
+
end?: number;
|
|
34
|
+
step?: number;
|
|
35
|
+
}
|
|
36
|
+
type Calendar = Partial<Record<'minute' | 'hour' | 'dayOfMonth' | 'month' | 'dayOfWeek', Range[]>>;
|
|
37
|
+
interface WfRef {
|
|
38
|
+
workflowId?: string;
|
|
39
|
+
firstExecutionRunId?: string;
|
|
40
|
+
runId?: string;
|
|
41
|
+
}
|
|
42
|
+
interface ActionLike {
|
|
43
|
+
takenAt?: Date | string;
|
|
44
|
+
workflow?: WfRef;
|
|
45
|
+
action?: {
|
|
46
|
+
workflow?: WfRef;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
interface Described {
|
|
50
|
+
spec?: {
|
|
51
|
+
cronExpressions?: string[];
|
|
52
|
+
calendars?: Calendar[];
|
|
53
|
+
timezoneName?: string;
|
|
54
|
+
};
|
|
55
|
+
info?: {
|
|
56
|
+
recentActions?: ActionLike[];
|
|
57
|
+
runningActions?: ActionLike[];
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
export interface TemporalClientLike {
|
|
61
|
+
options?: {
|
|
62
|
+
namespace?: string;
|
|
63
|
+
};
|
|
64
|
+
schedule: {
|
|
65
|
+
list(): AsyncIterable<{
|
|
66
|
+
scheduleId: string;
|
|
67
|
+
}>;
|
|
68
|
+
getHandle(id: string): {
|
|
69
|
+
describe(): Promise<Described>;
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
workflow: {
|
|
73
|
+
getHandle(id: string, runId?: string): {
|
|
74
|
+
describe(): Promise<{
|
|
75
|
+
status?: {
|
|
76
|
+
name?: string;
|
|
77
|
+
};
|
|
78
|
+
startTime?: Date;
|
|
79
|
+
closeTime?: Date;
|
|
80
|
+
}>;
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
export interface WatchSchedulesOptions {
|
|
85
|
+
/** Poll period; default 60_000. */
|
|
86
|
+
interval?: number;
|
|
87
|
+
/** Only watch when the client's namespace is one of these (a Temporal client is bound to one namespace). */
|
|
88
|
+
namespaces?: string[];
|
|
89
|
+
}
|
|
90
|
+
/** Crontab (5 fields) for a Temporal schedule spec, or undefined when it has none or is not expressible (intervals, several calendars). */
|
|
91
|
+
export declare function scheduleCron(spec: Described['spec']): {
|
|
92
|
+
cron: string;
|
|
93
|
+
timezone?: string;
|
|
94
|
+
} | undefined;
|
|
95
|
+
/** Poll Temporal Schedules and send a check-in per action (slug `temporal-<scheduleId>`). Returns a stop function. */
|
|
96
|
+
export declare function watchTemporalSchedules(client: TemporalClientLike, o?: WatchSchedulesOptions): () => void;
|
|
97
|
+
export {};
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
// Temporal: activity spans + final-attempt failure capture (worker interceptors), and Schedule check-ins (a poller).
|
|
2
|
+
// Duck-typed on purpose: nothing from @temporalio/* is imported, so there is no dependency and the SDK's workflow sandbox is untouched.
|
|
3
|
+
import { context, trace, SpanKind, SpanStatusCode } from '@opentelemetry/api';
|
|
4
|
+
import { captureException } from '../errors.js';
|
|
5
|
+
import { checkIn, currentTarget, normalizeCron, slugify } from '../cron/monitor.js';
|
|
6
|
+
/** Worker options fragment: `Worker.create({ ..., interceptors: temporalInterceptors() })`. Spans for each activity attempt; the exception is captured on the final attempt only. */
|
|
7
|
+
export function temporalInterceptors(o = {}) {
|
|
8
|
+
return {
|
|
9
|
+
activity: [
|
|
10
|
+
(ctx) => ({
|
|
11
|
+
inbound: {
|
|
12
|
+
async execute(input, next) {
|
|
13
|
+
const i = ctx.info ?? {};
|
|
14
|
+
const attempt = i.attempt ?? 1;
|
|
15
|
+
const span = trace.getTracer('watchtower').startSpan(`activity ${i.activityType ?? 'unknown'}`, {
|
|
16
|
+
kind: SpanKind.CONSUMER,
|
|
17
|
+
attributes: {
|
|
18
|
+
'temporal.activity.type': i.activityType ?? '', 'temporal.activity.id': i.activityId ?? '', 'temporal.attempt': attempt,
|
|
19
|
+
'temporal.task_queue': i.taskQueue ?? '', 'temporal.workflow.id': i.workflowExecution?.workflowId ?? '',
|
|
20
|
+
'temporal.workflow.type': i.workflowType ?? '',
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
try {
|
|
24
|
+
return await context.with(trace.setSpan(context.active(), span), () => next(input));
|
|
25
|
+
}
|
|
26
|
+
catch (e) {
|
|
27
|
+
const max = o.maxAttempts ?? i.retryPolicy?.maximumAttempts;
|
|
28
|
+
const final = e?.nonRetryable === true || (!!max && attempt >= max);
|
|
29
|
+
if (final)
|
|
30
|
+
captureException(e, { attributes: { 'temporal.activity.type': i.activityType, 'temporal.attempt': attempt, 'temporal.workflow.id': i.workflowExecution?.workflowId } });
|
|
31
|
+
else {
|
|
32
|
+
span.recordException(e);
|
|
33
|
+
span.setStatus({ code: SpanStatusCode.ERROR, message: e?.message });
|
|
34
|
+
}
|
|
35
|
+
throw e;
|
|
36
|
+
}
|
|
37
|
+
finally {
|
|
38
|
+
span.end();
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
}),
|
|
43
|
+
],
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
const FULL = { dayOfMonth: [1, 31], month: [1, 12], dayOfWeek: [0, 6] };
|
|
47
|
+
function field(name, rs, dflt = '*') {
|
|
48
|
+
if (!rs?.length)
|
|
49
|
+
return dflt;
|
|
50
|
+
const full = FULL[name];
|
|
51
|
+
const parts = rs.map((r) => {
|
|
52
|
+
const s = r.start ?? 0, e = r.end ?? s, st = r.step && r.step > 1 ? r.step : 1;
|
|
53
|
+
if (full && s === full[0] && e === full[1] && st === 1)
|
|
54
|
+
return '*';
|
|
55
|
+
return s === e ? `${s}` : `${s}-${e}${st > 1 ? `/${st}` : ''}`;
|
|
56
|
+
});
|
|
57
|
+
return parts.includes('*') ? '*' : parts.join(',');
|
|
58
|
+
}
|
|
59
|
+
/** Crontab (5 fields) for a Temporal schedule spec, or undefined when it has none or is not expressible (intervals, several calendars). */
|
|
60
|
+
export function scheduleCron(spec) {
|
|
61
|
+
let tz = spec?.timezoneName || undefined;
|
|
62
|
+
const raw = spec?.cronExpressions?.[0];
|
|
63
|
+
if (raw) {
|
|
64
|
+
const m = /^(?:CRON_TZ|TZ)=(\S+)\s+(.*)$/.exec(raw.trim());
|
|
65
|
+
if (m) {
|
|
66
|
+
tz = m[1];
|
|
67
|
+
}
|
|
68
|
+
return { cron: normalizeCron((m ? m[2] : raw).split(/\s+/).slice(0, 6).join(' ')), timezone: tz };
|
|
69
|
+
}
|
|
70
|
+
const cal = spec?.calendars;
|
|
71
|
+
if (cal?.length !== 1)
|
|
72
|
+
return undefined;
|
|
73
|
+
const c = cal[0];
|
|
74
|
+
return { cron: ['minute', 'hour', 'dayOfMonth', 'month', 'dayOfWeek'].map((k) => field(k, c[k], k === 'minute' || k === 'hour' ? '0' : '*')).join(' '), timezone: tz };
|
|
75
|
+
}
|
|
76
|
+
const OK = new Set(['COMPLETED']);
|
|
77
|
+
const BAD = new Set(['FAILED', 'TIMED_OUT', 'TERMINATED', 'CANCELLED']);
|
|
78
|
+
const ref = (a) => a.workflow ?? a.action?.workflow;
|
|
79
|
+
/** Poll Temporal Schedules and send a check-in per action (slug `temporal-<scheduleId>`). Returns a stop function. */
|
|
80
|
+
export function watchTemporalSchedules(client, o = {}) {
|
|
81
|
+
if (o.namespaces && !o.namespaces.includes(client.options?.namespace ?? 'default'))
|
|
82
|
+
return () => undefined;
|
|
83
|
+
const seen = new Map(); // schedule + workflow run -> last status sent
|
|
84
|
+
let busy = false;
|
|
85
|
+
const poll = async () => {
|
|
86
|
+
const target = currentTarget();
|
|
87
|
+
if (!target || busy)
|
|
88
|
+
return;
|
|
89
|
+
busy = true;
|
|
90
|
+
try {
|
|
91
|
+
for await (const s of client.schedule.list()) {
|
|
92
|
+
const slug = `temporal-${slugify(s.scheduleId) || s.scheduleId}`;
|
|
93
|
+
const d = await client.schedule.getHandle(s.scheduleId).describe();
|
|
94
|
+
const cr = scheduleCron(d.spec);
|
|
95
|
+
const monitorConfig = cr && { schedule: { type: 'crontab', value: cr.cron }, ...(cr.timezone ? { timezone: cr.timezone } : {}) };
|
|
96
|
+
const send = (st, durationMs) => checkIn(target, slug, st, { wait: true, monitorConfig, durationMs });
|
|
97
|
+
const actions = [...(d.info?.runningActions ?? []), ...(d.info?.recentActions ?? [])];
|
|
98
|
+
for (const a of actions) {
|
|
99
|
+
const w = ref(a);
|
|
100
|
+
if (!w?.workflowId)
|
|
101
|
+
continue;
|
|
102
|
+
const key = `${s.scheduleId}\0${w.workflowId}\0${w.firstExecutionRunId ?? w.runId ?? ''}`;
|
|
103
|
+
if (seen.get(key) === 'done')
|
|
104
|
+
continue;
|
|
105
|
+
let name = 'RUNNING', dur;
|
|
106
|
+
try {
|
|
107
|
+
const wd = await client.workflow.getHandle(w.workflowId, w.firstExecutionRunId ?? w.runId).describe();
|
|
108
|
+
name = String(wd.status?.name ?? 'RUNNING');
|
|
109
|
+
if (wd.startTime && wd.closeTime)
|
|
110
|
+
dur = +new Date(wd.closeTime) - +new Date(wd.startTime);
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
continue;
|
|
114
|
+
} // describe failed: retry next poll
|
|
115
|
+
if (name === 'RUNNING') {
|
|
116
|
+
if (!seen.has(key)) {
|
|
117
|
+
seen.set(key, 'running');
|
|
118
|
+
await send('in_progress');
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
else if (OK.has(name) || BAD.has(name)) {
|
|
122
|
+
if (!seen.has(key))
|
|
123
|
+
await send('in_progress'); // short run seen only after it finished
|
|
124
|
+
seen.set(key, 'done');
|
|
125
|
+
await send(OK.has(name) ? 'ok' : 'error', dur);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
catch { /* Temporal unreachable: try again next interval */ }
|
|
131
|
+
finally {
|
|
132
|
+
busy = false;
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
const t = setInterval(() => void poll(), o.interval ?? 60_000);
|
|
136
|
+
t.unref?.();
|
|
137
|
+
void poll();
|
|
138
|
+
return () => clearInterval(t);
|
|
139
|
+
}
|
|
@@ -7,3 +7,20 @@ export interface ServerSpan {
|
|
|
7
7
|
/** Start a server span; finish(status, route) names it `METHOD route` and ends it once. */
|
|
8
8
|
export declare function serverSpan(method: string, path?: string): ServerSpan;
|
|
9
9
|
export declare const statusOf: (e: unknown) => number;
|
|
10
|
+
/** `/base/:id` from an Express (or Nest-on-Express) request: `baseUrl` plus the matched `route.path`; undefined when no string route matched. */
|
|
11
|
+
export declare function expressRoute(req: {
|
|
12
|
+
baseUrl?: string;
|
|
13
|
+
route?: {
|
|
14
|
+
path?: unknown;
|
|
15
|
+
};
|
|
16
|
+
}): string | undefined;
|
|
17
|
+
/** http instrumentation `applyCustomAttributesOnSpan` hook: when the response ends, name the server span `METHOD /route/:param`. Off with `WT_ROUTE_NAMES=0`. */
|
|
18
|
+
export declare function routeNamer(span: Span, req: {
|
|
19
|
+
method?: string;
|
|
20
|
+
baseUrl?: string;
|
|
21
|
+
route?: {
|
|
22
|
+
path?: unknown;
|
|
23
|
+
};
|
|
24
|
+
}): void;
|
|
25
|
+
/** The active span when it is a recording SERVER span (made by the http instrumentation), wrapped so finish() enriches it and never ends it; else undefined. */
|
|
26
|
+
export declare function adoptServerSpan(method: string): ServerSpan | undefined;
|