@watchtowerobservability/node 0.7.9 → 0.8.1

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 (57) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/README.md +31 -9
  3. package/dist/cjs/buffer.d.ts +0 -3
  4. package/dist/cjs/diagnostics.d.ts +37 -0
  5. package/dist/cjs/diagnostics.js +92 -0
  6. package/dist/cjs/filter.d.ts +0 -1
  7. package/dist/cjs/index.d.ts +1 -4
  8. package/dist/cjs/index.js +44 -9
  9. package/dist/cjs/integrations/db.js +4 -2
  10. package/dist/cjs/integrations/next-build.d.ts +3 -1
  11. package/dist/cjs/integrations/next-build.js +5 -2
  12. package/dist/cjs/integrations/next-edge.d.ts +13 -0
  13. package/dist/cjs/integrations/next-edge.js +14 -0
  14. package/dist/cjs/integrations/next.d.ts +11 -4
  15. package/dist/cjs/integrations/next.js +12 -6
  16. package/dist/cjs/late.d.ts +7 -0
  17. package/dist/cjs/late.js +28 -0
  18. package/dist/cjs/load.d.ts +6 -1
  19. package/dist/cjs/load.js +12 -2
  20. package/dist/cjs/metrics.d.ts +1 -3
  21. package/dist/cjs/metrics.js +3 -1
  22. package/dist/cjs/next-filter.d.ts +0 -1
  23. package/dist/cjs/origin.d.ts +0 -1
  24. package/dist/cjs/redact.d.ts +0 -1
  25. package/dist/cjs/register.js +2 -0
  26. package/dist/cjs/sampling.d.ts +0 -1
  27. package/dist/cjs/scrub.d.ts +0 -1
  28. package/dist/cjs/version.d.ts +1 -1
  29. package/dist/cjs/version.js +1 -1
  30. package/dist/esm/buffer.d.ts +0 -3
  31. package/dist/esm/diagnostics.d.ts +37 -0
  32. package/dist/esm/diagnostics.js +79 -0
  33. package/dist/esm/filter.d.ts +0 -1
  34. package/dist/esm/index.d.ts +1 -4
  35. package/dist/esm/index.js +45 -10
  36. package/dist/esm/integrations/db.js +5 -3
  37. package/dist/esm/integrations/next-build.d.ts +3 -1
  38. package/dist/esm/integrations/next-build.js +5 -2
  39. package/dist/esm/integrations/next-edge.d.ts +13 -0
  40. package/dist/esm/integrations/next-edge.js +8 -0
  41. package/dist/esm/integrations/next.d.ts +11 -4
  42. package/dist/esm/integrations/next.js +12 -4
  43. package/dist/esm/late.d.ts +7 -0
  44. package/dist/esm/late.js +22 -0
  45. package/dist/esm/load.d.ts +6 -1
  46. package/dist/esm/load.js +11 -2
  47. package/dist/esm/metrics.d.ts +1 -3
  48. package/dist/esm/metrics.js +3 -1
  49. package/dist/esm/next-filter.d.ts +0 -1
  50. package/dist/esm/origin.d.ts +0 -1
  51. package/dist/esm/redact.d.ts +0 -1
  52. package/dist/esm/register.js +2 -0
  53. package/dist/esm/sampling.d.ts +0 -1
  54. package/dist/esm/scrub.d.ts +0 -1
  55. package/dist/esm/version.d.ts +1 -1
  56. package/dist/esm/version.js +1 -1
  57. package/package.json +21 -49
@@ -0,0 +1,22 @@
1
+ // "Loaded too late" detector. Import this FIRST in the entry modules (register.ts, index.ts): ES imports evaluate in order, so
2
+ // the snapshot below is taken before the SDK's own dependencies load anything. Under `node --import .../register` nothing
3
+ // but Node's bootstrap has run yet, so http/https are absent from process.moduleLoadList (checked on Node 26).
4
+ /** Which of http/https are in `list` (default: this process's loaded builtins). */
5
+ export const lateModules = (list = process.moduleLoadList ?? []) => ['http', 'https'].filter((m) => list.includes(`NativeModule ${m}`));
6
+ const early = lateModules();
7
+ const KEY = Symbol.for('watchtower.node.late-warned');
8
+ export const LATE_HINT = 'Start node with --import @watchtowerobservability/node/register';
9
+ /** Prints once per process (global flag, so the CJS and ESM copies share it). */
10
+ export function warnLate(mods, out = (m) => console.warn(m)) {
11
+ const g = globalThis;
12
+ if (!mods.length || g[KEY])
13
+ return;
14
+ g[KEY] = true;
15
+ out(`[watchtower] loaded too late: ${mods[0]} was already imported, so its requests are not traced. ${LATE_HINT}`);
16
+ }
17
+ /** The entry points call this: silent under Next (it loads http before instrumentation runs) and in test runners. */
18
+ export function checkLate() {
19
+ if (process.env.NEXT_RUNTIME || process.env.NODE_ENV === 'test' || process.env.VITEST)
20
+ return;
21
+ warnLate(early);
22
+ }
@@ -2,8 +2,13 @@
2
2
  export declare function searchPaths(): string[];
3
3
  export declare function load(id: string): unknown;
4
4
  export declare const SDK_PEERS: string[];
5
- /** Load an OpenTelemetry SDK package (an optional peer); a missing one throws an error naming what to install. */
5
+ /** Load an OpenTelemetry SDK package (a dependency, loaded lazily); a missing one throws an error naming what to install. */
6
6
  export declare function need<T>(id: string): T;
7
7
  export declare const STANDALONE_HINT = "use withWatchtower() from @watchtowerobservability/next (node SDK 0.7.9 or later) in next.config";
8
8
  /** Required OpenTelemetry peers that cannot be loaded from the SDK's location or the app root. */
9
9
  export declare function missingPeers(): string[];
10
+ /**
11
+ * Never called. Literal names so file tracers (Next's standalone nft) copy the SDK packages together with their pnpm
12
+ * links, which outputFileTracingIncludes cannot do. Safe for bundlers: these are dependencies, so always installed.
13
+ */
14
+ export declare const tracedPackages: () => Promise<unknown>[];
package/dist/esm/load.js CHANGED
@@ -8,7 +8,7 @@ import nodePath from 'node:path';
8
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
9
  export function searchPaths() {
10
10
  const entry = process.argv[1] ? `${nodePath.dirname(nodePath.resolve(process.argv[1]))}/` : '';
11
- return [typeof __filename === 'string' ? __filename : '', `${process.cwd()}/`, entry].filter(Boolean);
11
+ return [String(new URL(import.meta.url)), `${process.cwd()}/`, entry].filter(Boolean);
12
12
  }
13
13
  function resolvers() {
14
14
  return searchPaths().map((f) => createRequire(f));
@@ -32,7 +32,7 @@ export const SDK_PEERS = [
32
32
  '@opentelemetry/exporter-metrics-otlp-http', '@opentelemetry/exporter-trace-otlp-http', '@opentelemetry/otlp-transformer',
33
33
  '@opentelemetry/resources', '@opentelemetry/sdk-logs', '@opentelemetry/sdk-metrics', '@opentelemetry/sdk-trace',
34
34
  ];
35
- /** Load an OpenTelemetry SDK package (an optional peer); a missing one throws an error naming what to install. */
35
+ /** Load an OpenTelemetry SDK package (a dependency, loaded lazily); a missing one throws an error naming what to install. */
36
36
  export function need(id) {
37
37
  try {
38
38
  return load(id);
@@ -57,3 +57,12 @@ export function missingPeers() {
57
57
  }
58
58
  });
59
59
  }
60
+ /**
61
+ * Never called. Literal names so file tracers (Next's standalone nft) copy the SDK packages together with their pnpm
62
+ * links, which outputFileTracingIncludes cannot do. Safe for bundlers: these are dependencies, so always installed.
63
+ */
64
+ export const tracedPackages = () => [
65
+ import('@opentelemetry/context-async-hooks'), import('@opentelemetry/core'), import('@opentelemetry/exporter-logs-otlp-http'),
66
+ import('@opentelemetry/exporter-metrics-otlp-http'), import('@opentelemetry/exporter-trace-otlp-http'), import('@opentelemetry/otlp-transformer'),
67
+ import('@opentelemetry/resources'), import('@opentelemetry/sdk-logs'), import('@opentelemetry/sdk-metrics'), import('@opentelemetry/sdk-trace'),
68
+ ];
@@ -1,6 +1,4 @@
1
- // @ts-ignore optional peer: absent -> any (keeps skipLibCheck:false consumers compiling)
2
1
  import type { Resource } from '@opentelemetry/resources';
3
- // @ts-ignore optional peer: absent -> any (keeps skipLibCheck:false consumers compiling)
4
2
  import type { MeterProvider, PushMetricExporter } from '@opentelemetry/sdk-metrics';
5
3
  /** Explicit flag, else `WATCHTOWER_LOGS` / `WATCHTOWER_METRICS` (default on); `OTEL_*_EXPORTER=none` turns it off. */
6
4
  export declare function switchOn(name: string, otelExporterEnv: string, flag?: boolean): boolean;
@@ -8,4 +6,4 @@ export interface Metrics {
8
6
  provider: MeterProvider;
9
7
  stop(): void;
10
8
  }
11
- export declare function startMetrics(endpoint: string, headers: Record<string, string>, resource: Resource, bufferDir: string | undefined, fetch: typeof globalThis.fetch, exporter?: PushMetricExporter): Metrics;
9
+ export declare function startMetrics(endpoint: string, headers: Record<string, string>, resource: Resource, bufferDir: string | undefined, fetch: typeof globalThis.fetch, exporter?: PushMetricExporter, wrap?: (e: PushMetricExporter) => PushMetricExporter): Metrics;
@@ -14,7 +14,7 @@ export function switchOn(name, otelExporterEnv, flag) {
14
14
  return false;
15
15
  return !['off', 'false', '0', 'no'].includes((env[name] || 'on').trim().toLowerCase());
16
16
  }
17
- export function startMetrics(endpoint, headers, resource, bufferDir, fetch, exporter) {
17
+ export function startMetrics(endpoint, headers, resource, bufferDir, fetch, exporter, wrap) {
18
18
  const sdk = need('@opentelemetry/sdk-metrics');
19
19
  const url = `${endpoint}/v1/metrics`;
20
20
  let exp = exporter;
@@ -25,6 +25,8 @@ export function startMetrics(endpoint, headers, resource, bufferDir, fetch, expo
25
25
  exp = new OTLPMetricExporter({ url, headers, ...delta });
26
26
  if (bufferDir)
27
27
  exp = new MetricSpool(exp, nodePath.join(bufferDir, 'metrics'), url, headers, fetch);
28
+ if (wrap)
29
+ exp = wrap(exp);
28
30
  }
29
31
  const interval = Number(process.env.OTEL_METRIC_EXPORT_INTERVAL) || 60000;
30
32
  const reader = new sdk.PeriodicExportingMetricReader({
@@ -1,4 +1,3 @@
1
- // @ts-ignore optional peer: absent -> any (keeps skipLibCheck:false consumers compiling)
2
1
  import type { ReadableSpan } from '@opentelemetry/sdk-trace';
3
2
  /** Whether `span` is one of Next's internal spans (not a request, route handler, render, server action or fetch). */
4
3
  export declare function isNextInternal(span: ReadableSpan): boolean;
@@ -1,4 +1,3 @@
1
- // @ts-ignore optional peer: absent -> any (keeps skipLibCheck:false consumers compiling)
2
1
  import type { SpanProcessor } from '@opentelemetry/sdk-trace';
3
2
  export interface Frame {
4
3
  file: string;
@@ -1,4 +1,3 @@
1
- // @ts-ignore optional peer: absent -> any (keeps skipLibCheck:false consumers compiling)
2
1
  import type { SpanProcessor } from '@opentelemetry/sdk-trace';
3
2
  export type RedactHook = (key: string, value: unknown) => unknown;
4
3
  /** Hook keys for the non-attribute fields. Event attributes (incl. exception.message / exception.stacktrace) use their own keys. */
@@ -1,10 +1,12 @@
1
1
  // Preload entry: `node --import @watchtowerobservability/node/register app.mjs` (ESM) or `node -r @watchtowerobservability/node/register app.js` (CommonJS).
2
2
  // Starts the SDK from WT_*/OTEL_* env before the app loads, so http, fetch, pg and ioredis are patched in time.
3
3
  // Without WT_INGEST_KEY it starts nothing and says so.
4
+ import { checkLate } from './late.js'; // first: snapshots which builtins were loaded before the SDK
4
5
  import { pathToFileURL } from 'node:url';
5
6
  import { register as registerHooks } from 'node:module';
6
7
  import { init, setOffReason } from './index.js';
7
8
  import { searchPaths } from './load.js';
9
+ checkLate();
8
10
  if (process.env.WT_INGEST_KEY || process.env.OTEL_EXPORTER_OTLP_HEADERS) {
9
11
  try {
10
12
  // ESM modules (node:http imported from an ES module) are only patched through OpenTelemetry's loader hook
@@ -1,5 +1,4 @@
1
1
  import type { Attributes, Context, Link, SpanKind } from '@opentelemetry/api';
2
- // @ts-ignore optional peer: absent -> any (keeps skipLibCheck:false consumers compiling)
3
2
  import type { Sampler, SamplingResult } from '@opentelemetry/sdk-trace';
4
3
  export interface SamplingRule {
5
4
  name?: string;
@@ -1,4 +1,3 @@
1
- // @ts-ignore optional peer: absent -> any (keeps skipLibCheck:false consumers compiling)
2
1
  import type { SpanProcessor } from '@opentelemetry/sdk-trace';
3
2
  /**
4
3
  * A path segment that looks like a secret: UUID, hex run >= 16, JWT, digit run >= 16, or an opaque token (>= 16 chars of
@@ -1,2 +1,2 @@
1
1
  /** This package's version (test/distro.test.ts checks it against package.json); sent as `telemetry.distro.version`. */
2
- export declare const VERSION = "0.7.9";
2
+ export declare const VERSION = "0.8.1";
@@ -1,2 +1,2 @@
1
1
  /** This package's version (test/distro.test.ts checks it against package.json); sent as `telemetry.distro.version`. */
2
- export const VERSION = '0.7.9';
2
+ export const VERSION = '0.8.1';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@watchtowerobservability/node",
3
- "version": "0.7.9",
3
+ "version": "0.8.1",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/watchtowerobservability/watchtower-node.git"
@@ -133,6 +133,18 @@
133
133
  }
134
134
  },
135
135
  "./next": {
136
+ "edge-light": {
137
+ "types": "./dist/esm/integrations/next-edge.d.ts",
138
+ "default": "./dist/esm/integrations/next-edge.js"
139
+ },
140
+ "worker": {
141
+ "types": "./dist/esm/integrations/next-edge.d.ts",
142
+ "default": "./dist/esm/integrations/next-edge.js"
143
+ },
144
+ "browser": {
145
+ "types": "./dist/esm/integrations/next-edge.d.ts",
146
+ "default": "./dist/esm/integrations/next-edge.js"
147
+ },
136
148
  "import": {
137
149
  "types": "./dist/esm/integrations/next.d.ts",
138
150
  "default": "./dist/esm/integrations/next.js"
@@ -425,14 +437,19 @@
425
437
  "smoke": "node scripts/smoke-pack.mjs"
426
438
  },
427
439
  "dependencies": {
428
- "@opentelemetry/api": "^1.10.0"
429
- },
430
- "peerDependencies": {
440
+ "@opentelemetry/api": "^1.10.0",
431
441
  "@opentelemetry/context-async-hooks": "^3.1.0",
432
442
  "@opentelemetry/core": "^3.1.0",
433
443
  "@opentelemetry/exporter-logs-otlp-http": "^0.301.0",
434
444
  "@opentelemetry/exporter-metrics-otlp-http": "^0.301.0",
435
445
  "@opentelemetry/exporter-trace-otlp-http": "^0.301.0",
446
+ "@opentelemetry/otlp-transformer": "^0.301.0",
447
+ "@opentelemetry/resources": "^3.1.0",
448
+ "@opentelemetry/sdk-logs": "^3.1.0",
449
+ "@opentelemetry/sdk-metrics": "^3.1.0",
450
+ "@opentelemetry/sdk-trace": "^3.1.0"
451
+ },
452
+ "peerDependencies": {
436
453
  "@opentelemetry/instrumentation": ">=0.200.0",
437
454
  "@opentelemetry/instrumentation-http": ">=0.200.0",
438
455
  "@opentelemetry/instrumentation-ioredis": ">=0.50.0",
@@ -440,29 +457,9 @@
440
457
  "@opentelemetry/instrumentation-pg": ">=0.50.0",
441
458
  "@opentelemetry/instrumentation-runtime-node": ">=0.38.0",
442
459
  "@opentelemetry/instrumentation-undici": ">=0.10.0",
443
- "@opentelemetry/otlp-transformer": "^0.301.0",
444
- "@opentelemetry/resources": "^3.1.0",
445
- "@opentelemetry/sdk-logs": "^3.1.0",
446
- "@opentelemetry/sdk-metrics": "^3.1.0",
447
- "@opentelemetry/sdk-trace": "^3.1.0",
448
460
  "winston-transport": "^4.0.0"
449
461
  },
450
462
  "peerDependenciesMeta": {
451
- "@opentelemetry/context-async-hooks": {
452
- "optional": true
453
- },
454
- "@opentelemetry/core": {
455
- "optional": true
456
- },
457
- "@opentelemetry/exporter-logs-otlp-http": {
458
- "optional": true
459
- },
460
- "@opentelemetry/exporter-metrics-otlp-http": {
461
- "optional": true
462
- },
463
- "@opentelemetry/exporter-trace-otlp-http": {
464
- "optional": true
465
- },
466
463
  "@opentelemetry/instrumentation": {
467
464
  "optional": true
468
465
  },
@@ -484,36 +481,11 @@
484
481
  "@opentelemetry/instrumentation-undici": {
485
482
  "optional": true
486
483
  },
487
- "@opentelemetry/otlp-transformer": {
488
- "optional": true
489
- },
490
- "@opentelemetry/resources": {
491
- "optional": true
492
- },
493
- "@opentelemetry/sdk-logs": {
494
- "optional": true
495
- },
496
- "@opentelemetry/sdk-metrics": {
497
- "optional": true
498
- },
499
- "@opentelemetry/sdk-trace": {
500
- "optional": true
501
- },
502
484
  "winston-transport": {
503
485
  "optional": true
504
486
  }
505
487
  },
506
488
  "devDependencies": {
507
- "@opentelemetry/context-async-hooks": "^3.1.0",
508
- "@opentelemetry/core": "^3.1.0",
509
- "@opentelemetry/exporter-logs-otlp-http": "^0.301.0",
510
- "@opentelemetry/exporter-metrics-otlp-http": "^0.301.0",
511
- "@opentelemetry/exporter-trace-otlp-http": "^0.301.0",
512
- "@opentelemetry/otlp-transformer": "^0.301.0",
513
- "@opentelemetry/resources": "^3.1.0",
514
- "@opentelemetry/sdk-logs": "^3.1.0",
515
- "@opentelemetry/sdk-metrics": "^3.1.0",
516
- "@opentelemetry/sdk-trace": "^3.1.0",
517
489
  "@types/node": "22.15.0",
518
490
  "typescript": "5.7.2",
519
491
  "vitest": "5.0.5",