@watchtowerobservability/node 0.7.3 → 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.
Files changed (97) hide show
  1. package/CHANGELOG.md +40 -1
  2. package/README.md +96 -12
  3. package/dist/cjs/attach.d.ts +7 -0
  4. package/dist/cjs/attach.js +29 -0
  5. package/dist/cjs/checkin.js +4 -13
  6. package/dist/cjs/cron/monitor.d.ts +47 -0
  7. package/dist/cjs/cron/monitor.js +95 -0
  8. package/dist/cjs/cron/schedulers.d.ts +36 -0
  9. package/dist/cjs/cron/schedulers.js +65 -0
  10. package/dist/cjs/env.d.ts +10 -0
  11. package/dist/cjs/env.js +98 -1
  12. package/dist/cjs/index.d.ts +50 -6
  13. package/dist/cjs/index.js +114 -37
  14. package/dist/cjs/instrument-for.d.ts +17 -0
  15. package/dist/cjs/instrument-for.js +44 -0
  16. package/dist/cjs/instrument.d.ts +4 -0
  17. package/dist/cjs/instrument.js +32 -1
  18. package/dist/cjs/integrations/bullmq.d.ts +8 -2
  19. package/dist/cjs/integrations/bullmq.js +32 -5
  20. package/dist/cjs/integrations/db.d.ts +7 -2
  21. package/dist/cjs/integrations/db.js +32 -4
  22. package/dist/cjs/integrations/fastify.d.ts +1 -1
  23. package/dist/cjs/integrations/fastify.js +3 -2
  24. package/dist/cjs/integrations/logs.js +16 -5
  25. package/dist/cjs/integrations/next-build.d.ts +25 -8
  26. package/dist/cjs/integrations/next-build.js +159 -34
  27. package/dist/cjs/integrations/next-tunnel.d.ts +6 -1
  28. package/dist/cjs/integrations/next-tunnel.js +13 -2
  29. package/dist/cjs/integrations/postgres.d.ts +2 -0
  30. package/dist/cjs/integrations/postgres.js +43 -1
  31. package/dist/cjs/integrations/temporal.d.ts +97 -0
  32. package/dist/cjs/integrations/temporal.js +144 -0
  33. package/dist/cjs/integrations/util.d.ts +17 -0
  34. package/dist/cjs/integrations/util.js +38 -0
  35. package/dist/cjs/load.d.ts +6 -0
  36. package/dist/cjs/load.js +28 -4
  37. package/dist/cjs/next-doctor-cli.d.ts +2 -0
  38. package/dist/cjs/next-doctor-cli.js +15 -0
  39. package/dist/cjs/next-doctor.d.ts +2 -0
  40. package/dist/cjs/next-doctor.js +50 -0
  41. package/dist/cjs/pprof.d.ts +2 -0
  42. package/dist/cjs/pprof.js +18 -5
  43. package/dist/cjs/profiling.d.ts +31 -0
  44. package/dist/cjs/profiling.js +67 -11
  45. package/dist/cjs/redact-presets.d.ts +16 -0
  46. package/dist/cjs/redact-presets.js +66 -0
  47. package/dist/cjs/register.js +19 -6
  48. package/dist/cjs/version.d.ts +1 -1
  49. package/dist/cjs/version.js +1 -1
  50. package/dist/esm/attach.d.ts +7 -0
  51. package/dist/esm/attach.js +25 -0
  52. package/dist/esm/checkin.js +4 -13
  53. package/dist/esm/cron/monitor.d.ts +47 -0
  54. package/dist/esm/cron/monitor.js +85 -0
  55. package/dist/esm/cron/schedulers.d.ts +36 -0
  56. package/dist/esm/cron/schedulers.js +61 -0
  57. package/dist/esm/env.d.ts +10 -0
  58. package/dist/esm/env.js +62 -0
  59. package/dist/esm/index.d.ts +50 -6
  60. package/dist/esm/index.js +109 -39
  61. package/dist/esm/instrument-for.d.ts +17 -0
  62. package/dist/esm/instrument-for.js +41 -0
  63. package/dist/esm/instrument.d.ts +4 -0
  64. package/dist/esm/instrument.js +30 -1
  65. package/dist/esm/integrations/bullmq.d.ts +8 -2
  66. package/dist/esm/integrations/bullmq.js +32 -5
  67. package/dist/esm/integrations/db.d.ts +7 -2
  68. package/dist/esm/integrations/db.js +29 -4
  69. package/dist/esm/integrations/fastify.d.ts +1 -1
  70. package/dist/esm/integrations/fastify.js +4 -3
  71. package/dist/esm/integrations/logs.js +16 -5
  72. package/dist/esm/integrations/next-build.d.ts +25 -8
  73. package/dist/esm/integrations/next-build.js +158 -34
  74. package/dist/esm/integrations/next-tunnel.d.ts +6 -1
  75. package/dist/esm/integrations/next-tunnel.js +13 -2
  76. package/dist/esm/integrations/postgres.d.ts +2 -0
  77. package/dist/esm/integrations/postgres.js +43 -2
  78. package/dist/esm/integrations/temporal.d.ts +97 -0
  79. package/dist/esm/integrations/temporal.js +139 -0
  80. package/dist/esm/integrations/util.d.ts +17 -0
  81. package/dist/esm/integrations/util.js +35 -0
  82. package/dist/esm/load.d.ts +6 -0
  83. package/dist/esm/load.js +21 -3
  84. package/dist/esm/next-doctor-cli.d.ts +2 -0
  85. package/dist/esm/next-doctor-cli.js +13 -0
  86. package/dist/esm/next-doctor.d.ts +2 -0
  87. package/dist/esm/next-doctor.js +14 -0
  88. package/dist/esm/pprof.d.ts +2 -0
  89. package/dist/esm/pprof.js +17 -5
  90. package/dist/esm/profiling.d.ts +31 -0
  91. package/dist/esm/profiling.js +66 -12
  92. package/dist/esm/redact-presets.d.ts +16 -0
  93. package/dist/esm/redact-presets.js +58 -0
  94. package/dist/esm/register.js +20 -7
  95. package/dist/esm/version.d.ts +1 -1
  96. package/dist/esm/version.js +1 -1
  97. package/package.json +42 -1
@@ -1,5 +1,7 @@
1
1
  import { context, propagation, trace, SpanKind, SpanStatusCode } from '@opentelemetry/api';
2
2
  import { CONTEXT_KEY, injectData as inject, isPlainObject } from './messaging.js';
3
+ import { captureException } from '../errors.js';
4
+ import { monitorJob, slugify } from '../cron/monitor.js';
3
5
  export { CONTEXT_KEY };
4
6
  const PATCHED = Symbol.for('watchtower.bullmq');
5
7
  const tracer = () => trace.getTracer('watchtower');
@@ -9,12 +11,13 @@ const base = (queue, op, name) => ({
9
11
  'messaging.operation.type': op,
10
12
  'messaging.operation.name': name,
11
13
  });
12
- async function run(span, fn) {
14
+ async function run(span, fn, recorded) {
13
15
  try {
14
16
  return await fn();
15
17
  }
16
18
  catch (e) {
17
- span.recordException(e);
19
+ if (!recorded?.())
20
+ span.recordException(e);
18
21
  span.setStatus({ code: SpanStatusCode.ERROR, message: e?.message });
19
22
  throw e;
20
23
  }
@@ -27,10 +30,10 @@ async function run(span, fn) {
27
30
  * `Queue.add`/`addBulk`, CONSUMER spans around each job a `Worker` processes, parented by the
28
31
  * producer's context carried in job data under `_wt` (stripped before your processor runs; object
29
32
  * data only). Call it once, before creating queues and workers. Returns false when this bullmq
30
- * has no `Worker.callProcessJob` (consumer spans off; producer spans still on).
33
+ * has no `Worker.callProcessJob` (consumer spans off; producer spans still on). Options: see `BullMQOptions`.
31
34
  * shortcut: sandboxed (file path) processors run in a child process and get no consumer span.
32
35
  */
33
- export function instrumentBullMQ(bullmq) {
36
+ export function instrumentBullMQ(bullmq, options = {}) {
34
37
  const q = bullmq.Queue.prototype;
35
38
  const w = bullmq.Worker.prototype;
36
39
  if (q[PATCHED])
@@ -73,7 +76,31 @@ export function instrumentBullMQ(bullmq) {
73
76
  attrs['messaging.message.id'] = job.id;
74
77
  if (job.opts?.delay)
75
78
  attrs['messaging.bullmq.job.delay_ms'] = job.opts.delay;
76
- return tracer().startActiveSpan(`process ${this.name}`, { kind: SpanKind.CONSUMER, attributes: attrs }, parent, (span) => run(span, () => call.call(this, job, ...rest)));
79
+ const queue = this.name;
80
+ const rep = job.opts?.repeat;
81
+ const cron = options.cronMonitors ? rep?.pattern ?? rep?.cron : undefined;
82
+ return tracer().startActiveSpan(`process ${queue}`, { kind: SpanKind.CONSUMER, attributes: attrs }, parent, (span) => {
83
+ let recorded = false;
84
+ const exec = async () => {
85
+ try {
86
+ return await call.call(this, job, ...rest);
87
+ }
88
+ catch (e) {
89
+ const attempts = job.opts?.attempts ?? 1;
90
+ const made = (job.attemptsMade ?? 0) + 1;
91
+ if (options.captureFailures && (made >= attempts || e?.name === 'UnrecoverableError')) {
92
+ captureException(e, { attributes: {
93
+ 'bullmq.queue': queue, 'bullmq.job.name': job.name, 'bullmq.job.id': job.id, 'bullmq.job.attempt': made,
94
+ 'bullmq.job.attempts_made': made, 'bullmq.job.attempts': attempts,
95
+ } });
96
+ recorded = true;
97
+ }
98
+ throw e;
99
+ }
100
+ };
101
+ const slug = cron ? slugify(`${queue}-${job.name}`) : '';
102
+ return run(span, () => (slug ? monitorJob(slug, exec, { schedule: cron, timezone: rep?.tz }) : exec()), () => recorded);
103
+ });
77
104
  };
78
105
  return true;
79
106
  }
@@ -5,8 +5,7 @@ export interface DbOptions {
5
5
  /** Flag a statement run more than this many times under one parent span. Default:
6
6
  * `WATCHTOWER_DB_N_PLUS_ONE` or 5; 0 turns it off. */
7
7
  nPlusOne?: number;
8
- /** Record a query only when a span is active (a request, job or `observe` span). Default true (`WATCHTOWER_DB_REQUIRE_PARENT=0`
9
- * turns it off): startup DDL, migrations and polling loops run outside any span and would otherwise each become a root trace. */
8
+ /** Record a query only when a span is active (a request, job or `observe` span). Default: true when HTTP server instrumentation is active, else false (`WATCHTOWER_DB_REQUIRE_PARENT=0/1` overrides): startup DDL, migrations and polling loops run outside any span and would otherwise each become a root trace. */
10
9
  requireParent?: boolean;
11
10
  }
12
11
  export interface DbOp {
@@ -22,6 +21,12 @@ export interface DbOp {
22
21
  export declare const inside: (ctx?: Context) => boolean;
23
22
  /** `parent` with `span` active; `nested` marks it as inside an instrumented call (no inner spans). */
24
23
  export declare const enter: (parent: Context, span: Span, nested?: boolean) => Context;
24
+ /** Set by `init()`: whether node:http server instrumentation is on (requests then give queries a parent span). */
25
+ export declare const setHttpServerActive: (v: boolean) => void;
26
+ /** Explicit option, then `WATCHTOWER_DB_REQUIRE_PARENT`, else only when HTTP server instrumentation is active (a queue worker has no request spans, so its queries become root spans). */
27
+ export declare function dbRequireParent(opt: boolean | undefined): boolean;
28
+ /** Datastore spans dropped so far because `requireParent` found no active parent span. */
29
+ export declare const droppedDbSpans: () => number;
25
30
  /** A CLIENT span for `op` (not made active): summary, hash, N+1 hint. Prefer `traced`. */
26
31
  export declare function startDbSpan(op: DbOp, opts: DbOptions | undefined, parent: Context, startTime?: TimeInput): Span;
27
32
  /** A finished CLIENT span for a query a library reports after the fact (ORM loggers): it ends now. */
@@ -2,7 +2,7 @@
2
2
  // statement hash and N+1 hint, connection-pool metrics and embedded-store gauges.
3
3
  import * as fs from 'node:fs';
4
4
  import * as nodePath from 'node:path';
5
- import { context, createContextKey, INVALID_SPAN_CONTEXT, metrics, trace, SpanKind, SpanStatusCode } from '@opentelemetry/api';
5
+ import { context, createContextKey, diag, INVALID_SPAN_CONTEXT, metrics, trace, SpanKind, SpanStatusCode } from '@opentelemetry/api';
6
6
  import { isPgDialect, normalizeAndHash, querySummary } from '../sql.js';
7
7
  /** Set inside an instrumented call so a wrapper the library calls internally adds no second span. */
8
8
  const INSIDE = createContextKey('watchtower.db.inside');
@@ -25,12 +25,37 @@ function repeats(parent, key) {
25
25
  m.set(key, n);
26
26
  return n;
27
27
  }
28
+ let httpServerActive = false;
29
+ /** Set by `init()`: whether node:http server instrumentation is on (requests then give queries a parent span). */
30
+ export const setHttpServerActive = (v) => { httpServerActive = v; };
31
+ /** Explicit option, then `WATCHTOWER_DB_REQUIRE_PARENT`, else only when HTTP server instrumentation is active (a queue worker has no request spans, so its queries become root spans). */
32
+ export function dbRequireParent(opt) {
33
+ if (opt !== undefined)
34
+ return opt;
35
+ const e = (process.env.WATCHTOWER_DB_REQUIRE_PARENT || '').trim().toLowerCase();
36
+ if (['0', 'false', 'off', 'no'].includes(e))
37
+ return false;
38
+ if (['1', 'true', 'on', 'yes'].includes(e))
39
+ return true;
40
+ return httpServerActive;
41
+ }
42
+ let droppedSpans = 0;
43
+ let droppedCounter;
44
+ /** Datastore spans dropped so far because `requireParent` found no active parent span. */
45
+ export const droppedDbSpans = () => droppedSpans;
46
+ /** Count a dropped span: the `wt.sdk.db.dropped_spans` counter (exported with the SDK's metrics) and one diag warning. */
47
+ function dropped() {
48
+ if (droppedSpans++ === 0)
49
+ diag.warn('watchtower: a datastore span was dropped because it has no parent span (requireParent); counted in wt.sdk.db.dropped_spans. Set WATCHTOWER_DB_REQUIRE_PARENT=0 to keep such queries as root spans');
50
+ droppedCounter ??= metrics.getMeter('watchtower').createCounter('wt.sdk.db.dropped_spans', { description: 'Datastore spans dropped for lack of a parent span (requireParent)' });
51
+ droppedCounter.add(1);
52
+ return trace.wrapSpanContext(INVALID_SPAN_CONTEXT);
53
+ }
28
54
  /** A CLIENT span for `op` (not made active): summary, hash, N+1 hint. Prefer `traced`. */
29
55
  export function startDbSpan(op, opts, parent, startTime) {
30
56
  const p = trace.getSpan(parent)?.spanContext();
31
- const requireParent = opts?.requireParent ?? !['0', 'false', 'off', 'no'].includes((process.env.WATCHTOWER_DB_REQUIRE_PARENT || '').trim().toLowerCase());
32
- if (requireParent && !(p && trace.isSpanContextValid(p)))
33
- return trace.wrapSpanContext(INVALID_SPAN_CONTEXT); // non-recording: nothing is exported
57
+ if (dbRequireParent(opts?.requireParent) && !(p && trace.isSpanContextValid(p)))
58
+ return dropped(); // non-recording: nothing is exported
34
59
  const attrs = { 'db.system.name': op.system, ...op.attributes };
35
60
  let { operation, collection } = op;
36
61
  let name = [operation, collection].filter(Boolean).join(' ') || op.system;
@@ -2,5 +2,5 @@
2
2
  export interface FastifyLike {
3
3
  addHook(name: string, fn: (...args: any[]) => void): unknown;
4
4
  }
5
- /** await app.register(plugin): server span per request plus 5xx error capture. */
5
+ /** await app.register(plugin): server span per request (or the http instrumentation's own server span, enriched) plus 5xx error capture. */
6
6
  export declare function plugin(fastify: FastifyLike, _opts: unknown, done: (err?: Error) => void): void;
@@ -1,13 +1,14 @@
1
1
  import { context } from '@opentelemetry/api';
2
2
  import { captureException } from '../errors.js';
3
3
  import { capturePayload, captureActive } from '../payload.js';
4
- import { serverSpan, statusOf } from './util.js';
4
+ import { adoptServerSpan, serverSpan, statusOf } from './util.js';
5
5
  const KEY = Symbol('wt.span');
6
6
  const SENT = Symbol('wt.sent');
7
- /** await app.register(plugin): server span per request plus 5xx error capture. */
7
+ /** await app.register(plugin): server span per request (or the http instrumentation's own server span, enriched) plus 5xx error capture. */
8
8
  export function plugin(fastify, _opts, done) {
9
9
  fastify.addHook('onRequest', (req, _reply, next) => {
10
- const s = serverSpan(req.method, req.url);
10
+ // with the http instrumentation active its server span is enriched (route, status) instead of starting a second one
11
+ const s = adoptServerSpan(req.method) ?? serverSpan(req.method, req.url);
11
12
  req[KEY] = s;
12
13
  context.with(s.ctx, next);
13
14
  });
@@ -4,11 +4,22 @@ import { load } from '../load.js';
4
4
  const defaultSink = { log: (level, body, attrs) => wt.log(level, body, attrs) };
5
5
  const PINO = { 10: 'trace', 20: 'debug', 30: 'info', 40: 'warn', 50: 'error', 60: 'fatal' };
6
6
  const CORE = new Set(['level', 'time', 'msg', 'pid', 'hostname', 'v']);
7
- const flat = (o, skip) => {
8
- const attrs = {};
9
- for (const [k, v] of Object.entries(o))
10
- if (!skip || !skip.has(k))
11
- attrs[k] = typeof v === 'object' ? JSON.stringify(v) : v;
7
+ const MAX_DEPTH = 4;
8
+ const plain = (v) => !!v && typeof v === 'object' && Object.getPrototypeOf(v) === Object.prototype;
9
+ const prim = (v) => v == null || ['string', 'number', 'boolean'].includes(typeof v);
10
+ /** Nested plain objects become dotted attributes (`req.url`) up to depth 4 so `redact` and the presets see each leaf; arrays of primitives stay arrays, anything else is a JSON string. */
11
+ const flat = (o, skip, prefix = '', depth = 1, attrs = {}) => {
12
+ for (const [k, v] of Object.entries(o)) {
13
+ if (skip && skip.has(k))
14
+ continue;
15
+ const key = prefix + k;
16
+ if (plain(v) && depth < MAX_DEPTH)
17
+ flat(v, undefined, `${key}.`, depth + 1, attrs);
18
+ else if (Array.isArray(v) && v.every(prim))
19
+ attrs[key] = v;
20
+ else
21
+ attrs[key] = typeof v === 'object' && v !== null ? JSON.stringify(v) : v;
22
+ }
12
23
  return attrs;
13
24
  };
14
25
  /** pino(opts, pinoStream()) or pino.multistream([{ stream: pinoStream() }, { stream: process.stdout }]). */
@@ -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 })`: turns on `productionBrowserSourceMaps` so
23
- * `next build` writes client maps (works with webpack and Turbopack). Then run `wt-next-sourcemaps` after the build
24
- * (`"build": "next build && wt-next-sourcemaps"`) to upload them and delete the .map files from `.next/static`.
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
- sourcemaps?: boolean;
28
- }): ConfigOrFn;
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 (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
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 })`: turns on `productionBrowserSourceMaps` so
8
- * `next build` writes client maps (works with webpack and Turbopack). Then run `wt-next-sourcemaps` after the build
9
- * (`"build": "next build && wt-next-sourcemaps"`) to upload them and delete the .map files from `.next/static`.
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 apply = (c) => (opts.sourcemaps ? { ...c, productionBrowserSourceMaps: true } : c);
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 (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
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
- 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) {
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}: ${e.message}`);
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
- await Promise.all(Array.from({ length: Math.min(8, files.length) }, worker));
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
- const FORWARD = ['content-type', 'content-encoding', 'user-agent'];
7
- /** Next route handlers `{ GET, POST }` forwarding allow-listed RUM paths to the edge. Only the publishable key (`?k=` or a `wtp_` bearer) is forwarded as auth; cookies and other headers are not. */
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 { context, SpanStatusCode } from '@opentelemetry/api';
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 = {