@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
@@ -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;
@@ -30,3 +30,38 @@ export const statusOf = (e) => {
30
30
  const x = e;
31
31
  return (x && (x.status || x.statusCode)) || 500;
32
32
  };
33
+ /** `/base/:id` from an Express (or Nest-on-Express) request: `baseUrl` plus the matched `route.path`; undefined when no string route matched. */
34
+ export function expressRoute(req) {
35
+ const p = req.route?.path;
36
+ return typeof p === 'string' ? `${req.baseUrl || ''}${p}` : undefined;
37
+ }
38
+ /** http instrumentation `applyCustomAttributesOnSpan` hook: when the response ends, name the server span `METHOD /route/:param`. Off with `WT_ROUTE_NAMES=0`. */
39
+ export function routeNamer(span, req) {
40
+ if (['0', 'off', 'false', 'no'].includes((process.env.WT_ROUTE_NAMES || '').trim().toLowerCase()))
41
+ return;
42
+ const route = expressRoute(req);
43
+ if (!route)
44
+ return;
45
+ span.updateName(`${req.method || 'GET'} ${route}`);
46
+ span.setAttribute('http.route', route);
47
+ }
48
+ /** 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. */
49
+ export function adoptServerSpan(method) {
50
+ const span = trace.getActiveSpan();
51
+ if (!span || !span.isRecording() || span.kind !== SpanKind.SERVER)
52
+ return undefined;
53
+ return {
54
+ span,
55
+ ctx: context.active(),
56
+ finish(status, route) {
57
+ if (route) {
58
+ span.updateName(`${method} ${route}`);
59
+ span.setAttribute('http.route', route);
60
+ }
61
+ if (status)
62
+ span.setAttribute('http.response.status_code', status);
63
+ if (status !== undefined && status >= 500)
64
+ span.setStatus({ code: SpanStatusCode.ERROR });
65
+ },
66
+ };
67
+ }
@@ -1,3 +1,9 @@
1
+ /** Where peers are searched, in order: the SDK's own location, the cwd, the entry script's directory (pnpm monorepo apps run from the repo root). */
2
+ export declare function searchPaths(): string[];
1
3
  export declare function load(id: string): unknown;
4
+ export declare const SDK_PEERS: string[];
2
5
  /** Load an OpenTelemetry SDK package (an optional peer); a missing one throws an error naming what to install. */
3
6
  export declare function need<T>(id: string): T;
7
+ export declare const STANDALONE_HINT = "use withWatchtower() from @watchtowerobservability/next, or add them to serverExternalPackages and outputFileTracingIncludes in next.config";
8
+ /** Required OpenTelemetry peers that cannot be loaded from the SDK's location or the app root. */
9
+ export declare function missingPeers(): string[];
package/dist/esm/load.js CHANGED
@@ -1,12 +1,17 @@
1
1
  // Optional-peer loader (same source for the CommonJS and ESM builds).
2
2
  import { createRequire } from 'node:module';
3
+ import nodePath from 'node:path';
3
4
  // Bundlers must NOT follow this: an optional peer the app did not install would make webpack warn "Module not found",
4
5
  // and a bare require call on a variable id is a "Critical dependency" warning. createRequire() is opaque to webpack/esbuild/Next's tracer,
5
6
  // so peers resolve at run time from the SDK's own location, then from the app root (cwd). Install the peers in the app
6
7
  // (pnpm: as direct dependencies); for a Next standalone build add them to outputFileTracingIncludes.
8
+ /** Where peers are searched, in order: the SDK's own location, the cwd, the entry script's directory (pnpm monorepo apps run from the repo root). */
9
+ export function searchPaths() {
10
+ const entry = process.argv[1] ? `${nodePath.dirname(nodePath.resolve(process.argv[1]))}/` : '';
11
+ return [typeof __filename === 'string' ? __filename : '', `${process.cwd()}/`, entry].filter(Boolean);
12
+ }
7
13
  function resolvers() {
8
- const from = [typeof __filename === 'string' ? __filename : '', `${process.cwd()}/`].filter(Boolean);
9
- return from.map((f) => createRequire(f));
14
+ return searchPaths().map((f) => createRequire(f));
10
15
  }
11
16
  export function load(id) {
12
17
  let err;
@@ -22,7 +27,7 @@ export function load(id) {
22
27
  }
23
28
  throw err;
24
29
  }
25
- const SDK_PEERS = [
30
+ export const SDK_PEERS = [
26
31
  '@opentelemetry/context-async-hooks', '@opentelemetry/core', '@opentelemetry/exporter-logs-otlp-http',
27
32
  '@opentelemetry/exporter-metrics-otlp-http', '@opentelemetry/exporter-trace-otlp-http', '@opentelemetry/otlp-transformer',
28
33
  '@opentelemetry/resources', '@opentelemetry/sdk-logs', '@opentelemetry/sdk-metrics', '@opentelemetry/sdk-trace',
@@ -39,3 +44,16 @@ export function need(id) {
39
44
  throw e;
40
45
  }
41
46
  }
47
+ export const STANDALONE_HINT = 'use withWatchtower() from @watchtowerobservability/next, or add them to serverExternalPackages and outputFileTracingIncludes in next.config';
48
+ /** Required OpenTelemetry peers that cannot be loaded from the SDK's location or the app root. */
49
+ export function missingPeers() {
50
+ return SDK_PEERS.filter((id) => {
51
+ try {
52
+ load(id);
53
+ return false;
54
+ }
55
+ catch {
56
+ return true;
57
+ }
58
+ });
59
+ }
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env node
2
+ // `wt-next-doctor [dir]`: exit 1 when .next/standalone lacks the SDK or an OpenTelemetry peer (see next-doctor.ts).
3
+ import { standaloneMissing } from './next-doctor.js';
4
+ import { STANDALONE_HINT } from './load.js';
5
+ const missing = standaloneMissing(process.argv[2]);
6
+ if (!missing)
7
+ console.log('[watchtower] no .next/standalone found; nothing to check');
8
+ else if (missing.length) {
9
+ console.error(`[watchtower] missing from .next/standalone/node_modules: ${missing.join(', ')}\n[watchtower] fix: ${STANDALONE_HINT}`);
10
+ process.exit(1);
11
+ }
12
+ else
13
+ console.log('[watchtower] standalone build includes the SDK and its OpenTelemetry peers');
@@ -0,0 +1,2 @@
1
+ /** Packages missing from `<dir>/.next/standalone/node_modules`; undefined when there is no standalone build. */
2
+ export declare function standaloneMissing(dir?: string): string[] | undefined;
@@ -0,0 +1,14 @@
1
+ // After `next build` with output 'standalone', check that the SDK and its OpenTelemetry peers were traced into
2
+ // .next/standalone/node_modules (npm layout, or pnpm's .pnpm store). Exits 1 when any is missing.
3
+ import * as fs from 'node:fs';
4
+ import * as nodePath from 'node:path';
5
+ import { SDK_PEERS } from './load.js';
6
+ /** Packages missing from `<dir>/.next/standalone/node_modules`; undefined when there is no standalone build. */
7
+ export function standaloneMissing(dir = '.') {
8
+ const nm = nodePath.join(dir, '.next', 'standalone', 'node_modules');
9
+ if (!fs.existsSync(nodePath.join(dir, '.next', 'standalone')))
10
+ return undefined;
11
+ const pnpm = nodePath.join(nm, '.pnpm');
12
+ const store = fs.existsSync(pnpm) ? fs.readdirSync(pnpm) : [];
13
+ return ['@watchtowerobservability/node', ...SDK_PEERS].filter((id) => !fs.existsSync(nodePath.join(nm, id)) && !store.some((e) => e.startsWith(`${id.replace('/', '+')}@`)));
14
+ }
@@ -4,3 +4,5 @@
4
4
  * sample labels. Frame names keep their `(file:line)` suffix; one function per distinct frame.
5
5
  */
6
6
  export declare function foldedToPprof(counts: Map<string, number>, periodNs: number, startMs: number, durationMs: number): Uint8Array;
7
+ /** Live heap stacks (`a;b;c` root first) with `[objects, bytes]` to a pprof with inuse_objects/count and inuse_space/bytes. */
8
+ export declare function heapToPprof(stacks: Map<string, [number, number]>, periodBytes: number, startMs: number, durationMs: number): Uint8Array;
package/dist/esm/pprof.js CHANGED
@@ -41,6 +41,16 @@ class Pb {
41
41
  * sample labels. Frame names keep their `(file:line)` suffix; one function per distinct frame.
42
42
  */
43
43
  export function foldedToPprof(counts, periodNs, startMs, durationMs) {
44
+ const rows = new Map();
45
+ for (const [k, n] of counts)
46
+ rows.set(k, [n, n * periodNs]);
47
+ return encodePprof([['samples', 'count'], ['cpu', 'nanoseconds']], rows, ['cpu', 'nanoseconds'], periodNs, startMs, durationMs);
48
+ }
49
+ /** Live heap stacks (`a;b;c` root first) with `[objects, bytes]` to a pprof with inuse_objects/count and inuse_space/bytes. */
50
+ export function heapToPprof(stacks, periodBytes, startMs, durationMs) {
51
+ return encodePprof([['inuse_objects', 'count'], ['inuse_space', 'bytes']], stacks, ['space', 'bytes'], periodBytes, startMs, durationMs);
52
+ }
53
+ function encodePprof(types, counts, periodType, period, startMs, durationMs) {
44
54
  const strings = [''];
45
55
  const index = new Map([['', 0]]);
46
56
  const s = (x) => {
@@ -52,10 +62,12 @@ export function foldedToPprof(counts, periodNs, startMs, durationMs) {
52
62
  }
53
63
  return i;
54
64
  };
55
- const vt = (type, unit) => new Pb().int(1, s(type)).int(2, s(unit));
65
+ const vt = ([type, unit]) => new Pb().int(1, s(type)).int(2, s(unit));
56
66
  const frames = new Map();
57
- const out = new Pb().msg(1, vt('samples', 'count')).msg(1, vt('cpu', 'nanoseconds'));
58
- for (const [key, n] of counts) {
67
+ const out = new Pb();
68
+ for (const t of types)
69
+ out.msg(1, vt(t));
70
+ for (const [key, values] of counts) {
59
71
  const tab = key.indexOf('\t');
60
72
  const labels = tab >= 0 ? key.slice(0, tab) : '';
61
73
  // pprof location ids are leaf first
@@ -65,7 +77,7 @@ export function foldedToPprof(counts, periodNs, startMs, durationMs) {
65
77
  frames.set(f, (id = frames.size + 1));
66
78
  return id;
67
79
  });
68
- const sample = new Pb().packed(1, ids).packed(2, [n, n * periodNs]);
80
+ const sample = new Pb().packed(1, ids).packed(2, values);
69
81
  for (const kv of labels ? labels.split(',') : []) {
70
82
  const eq = kv.indexOf('=');
71
83
  if (eq > 0)
@@ -78,7 +90,7 @@ export function foldedToPprof(counts, periodNs, startMs, durationMs) {
78
90
  out.msg(5, new Pb().int(1, id).int(2, s(f))); // Function{id, name}
79
91
  }
80
92
  out.int(9, BigInt(Math.round(startMs)) * 1000000n).int(10, BigInt(Math.round(durationMs)) * 1000000n);
81
- out.msg(11, vt('cpu', 'nanoseconds')).int(12, periodNs);
93
+ out.msg(11, vt(periodType)).int(12, period);
82
94
  for (const str of strings)
83
95
  out.bytes(6, Buffer.from(str));
84
96
  return new Uint8Array(gzipSync(Uint8Array.from(out.b)));
@@ -38,7 +38,29 @@ export declare function withSpan<T>(fn: () => T, ids?: {
38
38
  }): T;
39
39
  /** Takes the intervals a profile up to `until` can need; open ones stay for the next profile. */
40
40
  export declare function takeIntervals(until: number): SpanInterval[];
41
+ /** V8's sampling heap profile: a tree of live (not yet collected) sampled allocations. */
42
+ export interface HeapProfileNode {
43
+ callFrame: CallFrame;
44
+ selfSize: number;
45
+ id: number;
46
+ children?: HeapProfileNode[];
47
+ }
48
+ export interface HeapProfile {
49
+ head: HeapProfileNode;
50
+ samples?: {
51
+ size: number;
52
+ nodeId: number;
53
+ ordinal: number;
54
+ }[];
55
+ }
56
+ export type ProfileType = 'cpu' | 'heap';
57
+ /** `cpu,heap` (WT_PROFILING_TYPES) to types; unknown names are ignored, nothing valid means cpu only. */
58
+ export declare function parseProfilingTypes(v?: string): ProfileType[];
41
59
  export interface ProfilerOptions {
60
+ /** Profile types (default: WT_PROFILING_TYPES, else `['cpu']`). `heap` adds V8's sampling heap profiler. */
61
+ types?: ProfileType[];
62
+ /** Heap sampling interval in bytes (default 524 288). */
63
+ heapIntervalBytes?: number;
42
64
  /** Ingest key; defaults to WT_INGEST_KEY. */
43
65
  apiKey?: string;
44
66
  /** Gateway base URL; defaults to WT_OTLP_ENDPOINT or http://localhost:4318. */
@@ -55,6 +77,11 @@ export interface ProfilerOptions {
55
77
  }
56
78
  /** Root-first folded stacks with their sample counts; V8's synthetic `(root)` is left out. */
57
79
  export declare function foldProfile(p: CpuProfile, spans?: SpanInterval[]): Map<string, number>;
80
+ /**
81
+ * Root-first stacks of live heap with `[objects, bytes]`: bytes are the node's self size, objects the
82
+ * number of sampled allocations that still live in it (at least 1 for a node with bytes).
83
+ */
84
+ export declare function foldHeap(p: HeapProfile): Map<string, [number, number]>;
58
85
  export declare function foldedBody(counts: Map<string, number>): string;
59
86
  /** `/v1/profiles?format=pprof&service=…&version=…` on the gateway. */
60
87
  export declare function profilesUrl(endpoint: string, service: string, version?: string): string;
@@ -69,6 +96,8 @@ export declare class Profiler {
69
96
  private readonly tags;
70
97
  private readonly intervalUs;
71
98
  private readonly periodMs;
99
+ private readonly types;
100
+ private readonly heapIntervalBytes;
72
101
  private session?;
73
102
  private timer?;
74
103
  private from;
@@ -77,7 +106,9 @@ export declare class Profiler {
77
106
  start(): Promise<this>;
78
107
  /** Stop the current profile, upload it and (when `restart`) start the next one. */
79
108
  flush(restart: boolean): Promise<void>;
109
+ uploadHeap(stacks: Map<string, [number, number]>, fromS: number, untilS: number): Promise<void>;
80
110
  upload(counts: Map<string, number>, fromS: number, untilS: number): Promise<void>;
111
+ private send;
81
112
  stop(): Promise<void>;
82
113
  }
83
114
  /** Start a [[Profiler]] with the SDK's environment defaults. */
@@ -1,4 +1,4 @@
1
- // Continuous CPU profiler on V8's own sampler (`node:inspector`, no native addon): every
1
+ // Continuous CPU (and opt-in heap) profiler on V8's own samplers (`node:inspector`, no native addon): every
2
2
  // `periodMs` the CPU profile is folded into stacks, encoded as pprof and posted to the gateway's
3
3
  // `/v1/profiles?format=pprof` with `service` and `version`. Best effort: a failed upload is
4
4
  // counted and dropped. `withSpan` labels samples taken while its function runs, joined
@@ -7,7 +7,7 @@ import { AsyncLocalStorage } from 'node:async_hooks';
7
7
  import { Session } from 'node:inspector';
8
8
  import { promiseHooks } from 'node:v8';
9
9
  import { trace } from '@opentelemetry/api';
10
- import { foldedToPprof } from './pprof.js';
10
+ import { foldedToPprof, heapToPprof } from './pprof.js';
11
11
  const intervals = [];
12
12
  const MAX_INTERVALS = 100_000;
13
13
  function nowUs() {
@@ -97,6 +97,11 @@ function prefixAt(sorted, t) {
97
97
  return sorted[i].prefix;
98
98
  return '';
99
99
  }
100
+ /** `cpu,heap` (WT_PROFILING_TYPES) to types; unknown names are ignored, nothing valid means cpu only. */
101
+ export function parseProfilingTypes(v) {
102
+ const t = [...new Set((v ?? '').split(',').map((x) => x.trim().toLowerCase()))].filter((x) => x === 'cpu' || x === 'heap');
103
+ return t.length ? t : ['cpu'];
104
+ }
100
105
  const MAX_DEPTH = 128;
101
106
  function frameName(f) {
102
107
  const file = f.url ? f.url.split('/').pop() : '';
@@ -135,6 +140,28 @@ export function foldProfile(p, spans = []) {
135
140
  }
136
141
  return counts;
137
142
  }
143
+ /**
144
+ * Root-first stacks of live heap with `[objects, bytes]`: bytes are the node's self size, objects the
145
+ * number of sampled allocations that still live in it (at least 1 for a node with bytes).
146
+ */
147
+ export function foldHeap(p) {
148
+ const objs = new Map();
149
+ for (const x of p.samples ?? [])
150
+ objs.set(x.nodeId, (objs.get(x.nodeId) ?? 0) + 1);
151
+ const out = new Map();
152
+ const walk = (n, path) => {
153
+ const names = n.callFrame.functionName === '(root)' ? path : [...path, frameName(n.callFrame)];
154
+ if (n.selfSize > 0 && names.length && names.length <= MAX_DEPTH) {
155
+ const key = names.join(';');
156
+ const cur = out.get(key) ?? [0, 0];
157
+ out.set(key, [cur[0] + Math.max(objs.get(n.id) ?? 0, 1), cur[1] + n.selfSize]);
158
+ }
159
+ for (const c of n.children ?? [])
160
+ walk(c, names);
161
+ };
162
+ walk(p.head, []);
163
+ return out;
164
+ }
138
165
  export function foldedBody(counts) {
139
166
  let out = '';
140
167
  for (const [k, n] of counts)
@@ -167,6 +194,8 @@ export class Profiler {
167
194
  tags;
168
195
  intervalUs;
169
196
  periodMs;
197
+ types;
198
+ heapIntervalBytes;
170
199
  session;
171
200
  timer;
172
201
  from = 0;
@@ -180,6 +209,8 @@ export class Profiler {
180
209
  this.tags = { env: process.env.WT_ENVIRONMENT, version: process.env.WT_RELEASE, ...o.tags };
181
210
  this.intervalUs = Math.min(Math.max(o.intervalUs ?? 10_000, 1_000), 1_000_000);
182
211
  this.periodMs = Math.max(o.periodMs ?? 60_000, 1_000);
212
+ this.types = o.types?.length ? o.types : parseProfilingTypes(process.env.WT_PROFILING_TYPES);
213
+ this.heapIntervalBytes = Math.max(o.heapIntervalBytes ?? 524_288, 1024);
183
214
  if (o.asyncSpans)
184
215
  enableAsyncSpanLabels();
185
216
  }
@@ -197,9 +228,14 @@ export class Profiler {
197
228
  return this;
198
229
  this.session = new Session();
199
230
  this.session.connect();
200
- await this.post('Profiler.enable');
201
- await this.post('Profiler.setSamplingInterval', { interval: this.intervalUs });
202
- await this.post('Profiler.start');
231
+ if (this.types.includes('cpu')) {
232
+ await this.post('Profiler.enable');
233
+ await this.post('Profiler.setSamplingInterval', { interval: this.intervalUs });
234
+ await this.post('Profiler.start');
235
+ }
236
+ if (this.types.includes('heap')) {
237
+ await this.post('HeapProfiler.startSampling', { samplingInterval: this.heapIntervalBytes });
238
+ }
203
239
  this.from = Date.now() / 1000;
204
240
  this.timer = setInterval(() => void this.flush(true), this.periodMs);
205
241
  this.timer.unref();
@@ -210,19 +246,35 @@ export class Profiler {
210
246
  if (!this.session)
211
247
  return;
212
248
  const until = Date.now() / 1000;
213
- const { profile } = await this.post('Profiler.stop');
214
249
  const from = this.from;
215
- if (restart) {
216
- await this.post('Profiler.start');
217
- this.from = until;
250
+ let heap;
251
+ if (this.types.includes('heap')) {
252
+ // live-heap snapshot; sampling keeps running
253
+ const { profile } = await this.post('HeapProfiler.getSamplingProfile');
254
+ heap = foldHeap(profile);
218
255
  }
219
- const counts = foldProfile(profile, takeIntervals(nowUs()));
220
- if (counts.size)
256
+ let counts;
257
+ if (this.types.includes('cpu')) {
258
+ const { profile } = await this.post('Profiler.stop');
259
+ if (restart)
260
+ await this.post('Profiler.start');
261
+ counts = foldProfile(profile, takeIntervals(nowUs()));
262
+ }
263
+ if (restart)
264
+ this.from = until;
265
+ if (counts?.size)
221
266
  await this.upload(counts, from, until);
267
+ if (heap?.size)
268
+ await this.uploadHeap(heap, from, until);
269
+ }
270
+ async uploadHeap(stacks, fromS, untilS) {
271
+ await this.send(heapToPprof(stacks, this.heapIntervalBytes, fromS * 1000, (untilS - fromS) * 1000));
222
272
  }
223
273
  async upload(counts, fromS, untilS) {
274
+ await this.send(foldedToPprof(counts, this.intervalUs * 1000, fromS * 1000, (untilS - fromS) * 1000));
275
+ }
276
+ async send(body) {
224
277
  try {
225
- const body = foldedToPprof(counts, this.intervalUs * 1000, fromS * 1000, (untilS - fromS) * 1000);
226
278
  const r = await fetch(profilesUrl(this.endpoint, this.service, this.tags.version), {
227
279
  method: 'POST', body,
228
280
  headers: { authorization: `Bearer ${this.key}`, 'content-type': 'application/octet-stream' },
@@ -241,6 +293,8 @@ export class Profiler {
241
293
  if (this.timer)
242
294
  clearInterval(this.timer);
243
295
  await this.flush(false).catch(() => undefined);
296
+ if (this.types.includes('heap'))
297
+ await this.post('HeapProfiler.stopSampling').catch(() => undefined);
244
298
  this.session?.disconnect();
245
299
  this.session = undefined;
246
300
  }