@watchtowerobservability/node 0.4.0 → 0.6.0

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 (62) hide show
  1. package/README.md +44 -9
  2. package/dist/cjs/buffer.d.ts +2 -3
  3. package/dist/cjs/buffer.js +6 -6
  4. package/dist/cjs/env.d.ts +12 -0
  5. package/dist/cjs/env.js +37 -0
  6. package/dist/cjs/errors.d.ts +8 -0
  7. package/dist/cjs/errors.js +23 -0
  8. package/dist/cjs/index.d.ts +34 -12
  9. package/dist/cjs/index.js +106 -52
  10. package/dist/cjs/instrument.d.ts +7 -0
  11. package/dist/cjs/instrument.js +51 -0
  12. package/dist/cjs/integrations/genai-agents.d.ts +4 -1
  13. package/dist/cjs/integrations/genai-agents.js +27 -24
  14. package/dist/cjs/integrations/genai-frameworks.d.ts +25 -0
  15. package/dist/cjs/integrations/genai-frameworks.js +211 -0
  16. package/dist/cjs/integrations/genai.d.ts +1 -0
  17. package/dist/cjs/integrations/genai.js +7 -2
  18. package/dist/cjs/integrations/next.d.ts +4 -1
  19. package/dist/cjs/integrations/next.js +13 -7
  20. package/dist/cjs/load.d.ts +2 -0
  21. package/dist/cjs/load.js +54 -5
  22. package/dist/cjs/metrics.d.ts +1 -2
  23. package/dist/cjs/metrics.js +6 -6
  24. package/dist/cjs/origin.d.ts +9 -0
  25. package/dist/cjs/origin.js +43 -0
  26. package/dist/cjs/redact.d.ts +8 -0
  27. package/dist/cjs/redact.js +43 -0
  28. package/dist/cjs/register.d.ts +1 -0
  29. package/dist/cjs/register.js +29 -0
  30. package/dist/cjs/sampling.d.ts +53 -0
  31. package/dist/cjs/sampling.js +98 -0
  32. package/dist/esm/buffer.d.ts +2 -3
  33. package/dist/esm/buffer.js +6 -6
  34. package/dist/esm/env.d.ts +12 -0
  35. package/dist/esm/env.js +33 -0
  36. package/dist/esm/errors.d.ts +8 -0
  37. package/dist/esm/errors.js +22 -0
  38. package/dist/esm/index.d.ts +34 -12
  39. package/dist/esm/index.js +104 -51
  40. package/dist/esm/instrument.d.ts +7 -0
  41. package/dist/esm/instrument.js +48 -0
  42. package/dist/esm/integrations/genai-agents.d.ts +4 -1
  43. package/dist/esm/integrations/genai-agents.js +4 -4
  44. package/dist/esm/integrations/genai-frameworks.d.ts +25 -0
  45. package/dist/esm/integrations/genai-frameworks.js +206 -0
  46. package/dist/esm/integrations/genai.d.ts +1 -0
  47. package/dist/esm/integrations/genai.js +3 -1
  48. package/dist/esm/integrations/next.d.ts +4 -1
  49. package/dist/esm/integrations/next.js +11 -6
  50. package/dist/esm/load.d.ts +2 -0
  51. package/dist/esm/load.js +78 -2
  52. package/dist/esm/metrics.d.ts +1 -2
  53. package/dist/esm/metrics.js +5 -5
  54. package/dist/esm/origin.d.ts +9 -0
  55. package/dist/esm/origin.js +39 -0
  56. package/dist/esm/redact.d.ts +8 -0
  57. package/dist/esm/redact.js +39 -0
  58. package/dist/esm/register.d.ts +1 -0
  59. package/dist/esm/register.js +27 -0
  60. package/dist/esm/sampling.d.ts +53 -0
  61. package/dist/esm/sampling.js +91 -0
  62. package/package.json +86 -14
package/README.md CHANGED
@@ -4,14 +4,18 @@
4
4
 
5
5
  ## Install
6
6
 
7
- GitHub Packages needs a token with `read:packages` even for public packages:
7
+ Published on npm:
8
8
 
9
9
  ```sh
10
- npm install @watchtowerobservability/node
10
+ npm install @watchtowerobservability/node @opentelemetry/context-async-hooks @opentelemetry/core \
11
+ @opentelemetry/exporter-logs-otlp-http @opentelemetry/exporter-metrics-otlp-http @opentelemetry/exporter-trace-otlp-http \
12
+ @opentelemetry/otlp-transformer @opentelemetry/resources @opentelemetry/sdk-logs @opentelemetry/sdk-metrics @opentelemetry/sdk-trace
11
13
  ```
12
14
 
13
- Or install the attested tarball from a release without a token:
14
- `npm install https://github.com/watchtowerobservability/watchtower-node/releases/download/v0.2.0/watchtowerobservability-node-0.2.0.tgz`.
15
+ `@opentelemetry/api` is the only hard dependency; the OpenTelemetry SDK packages above are optional peers (so your app controls their versions) and are loaded by `init`, which throws an error naming the missing ones.
16
+
17
+ Optional auto-instrumentation peers (installed ones are used, see below):
18
+ `npm install @opentelemetry/instrumentation @opentelemetry/instrumentation-http @opentelemetry/instrumentation-undici @opentelemetry/instrumentation-pg @opentelemetry/instrumentation-ioredis`.
15
19
 
16
20
  Written in TypeScript and shipped as both ESM and CommonJS (`import` and `require` both work); the `.d.ts`
17
21
  typings are generated from the source. A thin layer over the OpenTelemetry Node SDK (ruling L1): `Authorization: Bearer` header, endpoint default (`WT_OTLP_ENDPOINT`), disk offline buffer,
@@ -35,6 +39,32 @@ wt.log('info', 'started', { port: 3000 }); // OTLP log, linked to the active
35
39
  flushes and exits with code 1 like Node does, only when no other handler is registered.
36
40
  Request bodies, headers, query strings and query arguments are never recorded.
37
41
 
42
+ ## Auto-instrumentation and preload
43
+
44
+ `init` patches `node:http`, `fetch` (undici), `pg` and `ioredis` for each `@opentelemetry/instrumentation-*` package you have installed (the list above); pass `instrument: false` or set `WT_INSTRUMENT=0` to skip. Patching only reaches modules loaded after it runs, so load the SDK first:
45
+
46
+ ```sh
47
+ node --require @watchtowerobservability/node/register app.js # CommonJS app (also: node -r ...)
48
+ node --import @watchtowerobservability/node/register app.mjs # ESM app
49
+ ```
50
+
51
+ `register` calls `init({})` when `WT_INGEST_KEY` is set. For an ESM app it also registers OpenTelemetry's ESM loader hook (`@opentelemetry/instrumentation/hook.mjs`, resolved from your project, so install `@opentelemetry/instrumentation`) so `node:http` and other ES-module imports get patched; no extra flag is needed. The CommonJS and ESM copies of this package share one client (kept on `globalThis`), so `flush()` and `shutdown()` work from either. Start a CommonJS app with `--require` and an ES-module app with `--import`.
52
+
53
+ `init` registers the global tracer provider, context manager, propagator and meter provider only when none is registered yet (OpenTelemetry allows each once): call `init` before other OTel setup. It no longer disables globals set earlier; a warning (via the OTel `diag` logger) says when one was kept. A second `init` replaces only what the first one installed.
54
+
55
+ ## Next.js
56
+
57
+ ```ts
58
+ // instrumentation.ts
59
+ import { makeRegister } from '@watchtowerobservability/node/next';
60
+ export const register = makeRegister({ redact: (k, v) => v }); // any InitOptions; plain `register` takes env only
61
+ export { onRequestError } from '@watchtowerobservability/node/next';
62
+ ```
63
+
64
+ Install the OpenTelemetry peers above in the app itself (with pnpm, as direct dependencies, so Next's standalone tracer copies them). The SDK requires each peer by its literal name, so webpack and Next's file tracer can follow them; if the SDK arrives through a transpiled workspace package, add it to `transpilePackages`. Add `outputFileTracingIncludes` for the peers only if the standalone build still reports a missing `@opentelemetry/*` package.
65
+
66
+ Bundling with esbuild or webpack (ESM or CommonJS output) works: both the CommonJS and ESM builds name every optional peer with a literal `require`/`import()` inside try, so the bundler follows the peers you have installed and skips the rest.
67
+
38
68
  ## Integrations
39
69
 
40
70
  Each lives at its own subpath, so nothing is loaded that you do not import.
@@ -82,7 +112,10 @@ Each lives at its own subpath, so nothing is loaded that you do not import.
82
112
  | Mistral, Ollama | `.../genai` | `instrumentMistral(new Mistral())`, `instrumentOllama(ollama)` |
83
113
  | LlamaIndex.TS | `.../genai` | `instrumentLlamaIndex(Settings)` |
84
114
  | OpenAI Agents SDK | `.../genai` | `instrumentOpenAIAgents(addTraceProcessor)` |
85
- | Vercel AI SDK | `.../genai` | `generateText({ experimental_telemetry: vercelAiTelemetry() })` |
115
+ | LangChain.js / LangGraph.js | `.../genai` | `graph.invoke(x, { callbacks: [langchainCallbackHandler()] })` |
116
+ | Vercel AI SDK agent loop | `.../genai` | `const { generateText } = instrumentVercelAI({ generateText })` |
117
+ | Mastra | `.../genai` | `instrumentMastra(agent)` |
118
+ | Vercel AI SDK (telemetry only) | `.../genai` | `generateText({ experimental_telemetry: vercelAiTelemetry() })` |
86
119
 
87
120
  GenAI spans follow the OTel GenAI semantic conventions (model, token usage, finish reasons); prompt and
88
121
  completion text is not recorded. Streaming calls end their span at creation, without token usage.
@@ -265,14 +298,16 @@ See `docs/product/security/app-api-protection.md`.
265
298
  | Option | Env var | Default |
266
299
  | --- | --- | --- |
267
300
  | `apiKey` | `WT_INGEST_KEY` | required, sent as `Authorization: Bearer`, masked in output |
268
- | `endpoint` | `WT_OTLP_ENDPOINT` | `http://localhost:4318` |
269
- | `serviceName` | `WT_SERVICE` | `app` |
301
+ | `endpoint` | `WT_OTLP_ENDPOINT`, then `OTEL_EXPORTER_OTLP_ENDPOINT` | `http://localhost:4318` |
302
+ | `serviceName` | `WT_SERVICE`, then `OTEL_SERVICE_NAME` | `app` |
270
303
  | `environment` | `WT_ENVIRONMENT` | unset |
271
- | `release` | `WT_RELEASE` | unset |
304
+ | `release` | `WT_RELEASE`, then `service.version` in `OTEL_RESOURCE_ATTRIBUTES` | unset; sent as `service.version`, and an issue's first release is linked when a release with that version exists in the project |
305
+ | `resource` | `OTEL_RESOURCE_ATTRIBUTES` | none; `OTEL_EXPORTER_OTLP_HEADERS` adds extra headers, and its `Authorization=Bearer <key>` is used when `WT_INGEST_KEY` is unset |
306
+ | `instrument` | `WT_INSTRUMENT` | `on` for installed http / undici / pg / ioredis instrumentations |
272
307
  | `bufferDir` | | no offline buffer (spans, logs and metrics spool to `traces/`, `logs/` and `metrics/`, 500 batches each) |
273
308
  | `logs` | `WATCHTOWER_LOGS` (`OTEL_LOGS_EXPORTER=none` also turns it off) | `on`: OTLP logs (`log()`, pino, winston) |
274
309
  | `metrics` | `WATCHTOWER_METRICS` (`OTEL_METRICS_EXPORTER=none` also turns it off) | `on`: OTLP metrics, see below |
275
- | `redact` | | none; called for every span and log attribute |
310
+ | `redact` | | none; called for every span and log attribute, every span event's attributes (`exception.message`, `exception.stacktrace`), the span name (key `span.name`), the status message (`status.message`) and log bodies (`log.body`) |
276
311
 
277
312
  Metrics: `init` sets the global meter provider, so `metrics.getMeter('app')` (re-exported from
278
313
  `@opentelemetry/api`) records to Watchtower. A periodic reader exports to `/v1/metrics` every
@@ -1,7 +1,6 @@
1
- import type { SpanExporter, ReadableSpan } from '@opentelemetry/sdk-trace-base';
1
+ import type { SpanExporter, ReadableSpan } from '@opentelemetry/sdk-trace';
2
2
  import type { LogRecordExporter, ReadableLogRecord } from '@opentelemetry/sdk-logs';
3
- import { AggregationTemporality } from '@opentelemetry/sdk-metrics';
4
- import type { InstrumentType, PushMetricExporter, ResourceMetrics } from '@opentelemetry/sdk-metrics';
3
+ import type { AggregationTemporality, InstrumentType, PushMetricExporter, ResourceMetrics } from '@opentelemetry/sdk-metrics';
5
4
  type Done = Parameters<SpanExporter['export']>[1];
6
5
  /** What the spool wraps: a span, log record or metric exporter; `B` is one export batch. */
7
6
  export interface BatchExporter<B> {
@@ -40,14 +40,14 @@ exports.MetricSpool = exports.spoolLogs = exports.SpoolExporter = exports.encode
40
40
  const fs = __importStar(require("node:fs"));
41
41
  const path = __importStar(require("node:path"));
42
42
  const crypto = __importStar(require("node:crypto"));
43
- const sdk_metrics_1 = require("@opentelemetry/sdk-metrics");
44
- const otlp_transformer_1 = require("@opentelemetry/otlp-transformer");
43
+ const load_js_1 = require("./load.js");
44
+ const ser = () => (0, load_js_1.need)('@opentelemetry/otlp-transformer');
45
45
  /** OTLP/JSON encoders for the three signals. */
46
- const encodeSpans = (spans) => Buffer.from(otlp_transformer_1.JsonTraceSerializer.serializeRequest(spans));
46
+ const encodeSpans = (spans) => Buffer.from(ser().JsonTraceSerializer.serializeRequest(spans));
47
47
  exports.encodeSpans = encodeSpans;
48
- const encodeLogs = (logs) => Buffer.from(otlp_transformer_1.JsonLogsSerializer.serializeRequest(logs));
48
+ const encodeLogs = (logs) => Buffer.from(ser().JsonLogsSerializer.serializeRequest(logs));
49
49
  exports.encodeLogs = encodeLogs;
50
- const encodeMetrics = (rm) => Buffer.from(otlp_transformer_1.JsonMetricsSerializer.serializeRequest(rm));
50
+ const encodeMetrics = (rm) => Buffer.from(ser().JsonMetricsSerializer.serializeRequest(rm));
51
51
  exports.encodeMetrics = encodeMetrics;
52
52
  class SpoolExporter {
53
53
  inner;
@@ -125,7 +125,7 @@ class MetricSpool extends SpoolExporter {
125
125
  }
126
126
  selectAggregationTemporality(t) {
127
127
  const pick = this.metricInner.selectAggregationTemporality;
128
- return pick ? pick.call(this.metricInner, t) : sdk_metrics_1.AggregationTemporality.CUMULATIVE; // the SDK default
128
+ return pick ? pick.call(this.metricInner, t) : 1; // CUMULATIVE, the SDK default (enum value, so sdk-metrics is not imported eagerly)
129
129
  }
130
130
  forceFlush() {
131
131
  return this.metricInner.forceFlush();
@@ -0,0 +1,12 @@
1
+ /** Parse `k=v,k2=v2` (OTEL_RESOURCE_ATTRIBUTES / OTEL_EXPORTER_OTLP_HEADERS); values are percent-decoded. */
2
+ export declare function parseKv(s: string | undefined): Record<string, string>;
3
+ export interface OtelEnv {
4
+ endpoint?: string;
5
+ /** Header names are lower-cased by nobody: they are kept as written, except `Authorization` is matched case-insensitively. */
6
+ headers: Record<string, string>;
7
+ /** `Authorization: Bearer <key>` from OTEL_EXPORTER_OTLP_HEADERS, the key only. */
8
+ bearer?: string;
9
+ serviceName?: string;
10
+ resource: Record<string, string>;
11
+ }
12
+ export declare function otelEnv(env?: NodeJS.ProcessEnv): OtelEnv;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ // Standard OTEL_* environment variables, used as fallbacks after the WT_* ones.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.parseKv = parseKv;
5
+ exports.otelEnv = otelEnv;
6
+ /** Parse `k=v,k2=v2` (OTEL_RESOURCE_ATTRIBUTES / OTEL_EXPORTER_OTLP_HEADERS); values are percent-decoded. */
7
+ function parseKv(s) {
8
+ const out = {};
9
+ for (const part of (s || '').split(',')) {
10
+ const i = part.indexOf('=');
11
+ if (i <= 0)
12
+ continue;
13
+ const dec = (x) => { try {
14
+ return decodeURIComponent(x.trim());
15
+ }
16
+ catch {
17
+ return x.trim();
18
+ } };
19
+ out[dec(part.slice(0, i))] = dec(part.slice(i + 1));
20
+ }
21
+ return out;
22
+ }
23
+ function otelEnv(env = process.env) {
24
+ const headers = parseKv(env.OTEL_EXPORTER_OTLP_HEADERS);
25
+ const authKey = Object.keys(headers).find((k) => k.toLowerCase() === 'authorization');
26
+ const auth = authKey ? headers[authKey] : undefined;
27
+ if (authKey)
28
+ delete headers[authKey];
29
+ const resource = parseKv(env.OTEL_RESOURCE_ATTRIBUTES);
30
+ return {
31
+ endpoint: env.OTEL_EXPORTER_OTLP_ENDPOINT || undefined,
32
+ headers,
33
+ bearer: auth && /^bearer\s+/i.test(auth) ? auth.replace(/^bearer\s+/i, '') : undefined,
34
+ serviceName: env.OTEL_SERVICE_NAME || resource['service.name'] || undefined,
35
+ resource,
36
+ };
37
+ }
@@ -2,7 +2,15 @@ export interface CaptureContext {
2
2
  /** false for crashes (uncaught); default true. Keys starting with `wt.` are dropped. */
3
3
  handled?: boolean;
4
4
  attributes?: Record<string, string | number | boolean | undefined>;
5
+ /**
6
+ * Exception replay: variables to show on the throwing frame. Node cannot read another frame's locals, so
7
+ * the caller names them. Secret-looking names are masked, values are shortened; the project must also turn
8
+ * on "exception replay" in its issue settings or the server drops them.
9
+ */
10
+ locals?: Record<string, unknown>;
5
11
  }
12
+ /** `exception.locals` for the innermost stack frame of `e`: the function and line come from its stack, the values from `locals`. */
13
+ export declare function localsAttribute(e: Error, locals: Record<string, unknown>): string | undefined;
6
14
  /** The slice of `process` the hooks need; tests pass an EventEmitter. */
7
15
  export interface ProcessLike {
8
16
  on(event: string, listener: (arg: unknown) => void): unknown;
@@ -1,11 +1,31 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.localsAttribute = localsAttribute;
3
4
  exports.dropReserved = dropReserved;
4
5
  exports.captureException = captureException;
5
6
  exports.installProcessHandlers = installProcessHandlers;
6
7
  // Error capture: an exception becomes an OTel span event (exception.*) with trace and span ids.
7
8
  const api_1 = require("@opentelemetry/api");
8
9
  const captured = new WeakMap(); // error object -> traceId, so framework + process hooks never double-report
10
+ const SENSITIVE = /pass|secret|token|api[-_]?key|auth|cookie|session|credit|card|ssn|private|bearer/i;
11
+ /** `exception.locals` for the innermost stack frame of `e`: the function and line come from its stack, the values from `locals`. */
12
+ function localsAttribute(e, locals) {
13
+ const m = /^\s+at (?:(\S+) \()?(.*?):(\d+):\d+\)?$/m.exec(e.stack || '');
14
+ const vars = {};
15
+ for (const [k, v] of Object.entries(locals).slice(0, 20)) {
16
+ let text;
17
+ try {
18
+ text = typeof v === 'string' ? v : JSON.stringify(v) ?? String(v);
19
+ }
20
+ catch {
21
+ text = '<unrepresentable>';
22
+ }
23
+ vars[k] = SENSITIVE.test(k) ? '[Filtered]' : text.slice(0, 120);
24
+ }
25
+ if (!Object.keys(vars).length)
26
+ return undefined;
27
+ return JSON.stringify([{ function: m?.[1] ?? '', filename: m?.[2] ?? '', lineno: m ? Number(m[3]) : undefined, vars }]);
28
+ }
9
29
  function dropReserved(attrs) {
10
30
  return Object.fromEntries(Object.entries(attrs || {}).filter(([k]) => !k.startsWith('wt.')));
11
31
  }
@@ -20,7 +40,10 @@ function captureException(err, ctx) {
20
40
  const active = api_1.trace.getActiveSpan();
21
41
  const span = active && active.isRecording() ? active : api_1.trace.getTracer('watchtower').startSpan('exception');
22
42
  span.setAttributes(attrs);
43
+ const replay = o.locals ? localsAttribute(e, o.locals) : undefined;
23
44
  span.recordException(e);
45
+ if (replay)
46
+ span.setAttribute('exception.locals', replay); // the OTel JS exception event takes no attributes
24
47
  span.setStatus({ code: api_1.SpanStatusCode.ERROR, message: e.message });
25
48
  const traceId = span.spanContext().traceId;
26
49
  if (span !== active)
@@ -1,11 +1,13 @@
1
1
  import { inspect } from 'node:util';
2
2
  import { metrics, trace } from '@opentelemetry/api';
3
3
  import type { Tracer } from '@opentelemetry/api';
4
- import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
5
- import { LoggerProvider } from '@opentelemetry/sdk-logs';
4
+ import type { Resource } from '@opentelemetry/resources';
5
+ import type { TracerProvider } from '@opentelemetry/sdk-trace';
6
+ import type { LoggerProvider } from '@opentelemetry/sdk-logs';
6
7
  import type { MeterProvider } from '@opentelemetry/sdk-metrics';
7
8
  import { captureException } from './errors.js';
8
9
  import type { CaptureContext } from './errors.js';
10
+ import { RemoteSampler } from './sampling.js';
9
11
  import type { Profiler } from './profiling.js';
10
12
  export { captureException, metrics, trace };
11
13
  export { setCaptureRules, capturePayload } from './payload.js';
@@ -13,40 +15,56 @@ export type { CaptureRule } from './payload.js';
13
15
  export type { CaptureContext };
14
16
  export declare const DEFAULT_ENDPOINT = "http://localhost:4318";
15
17
  export type LogLevel = 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal';
18
+ /** code.filepath/code.lineno of the frame that called `log()` (stack line 0 is "Error", 1 is `log`, 2 the caller). */
19
+ export declare function callerOf(stack: string | undefined): Record<string, string | number>;
16
20
  export interface InitOptions {
17
- /** Ingest key; defaults to WT_INGEST_KEY. */
21
+ /** Ingest key; defaults to WT_INGEST_KEY, then a `Bearer` Authorization in OTEL_EXPORTER_OTLP_HEADERS. */
18
22
  apiKey?: string;
19
23
  /** Defaults to WT_ENVIRONMENT. */
20
24
  environment?: string;
21
- /** Defaults to WT_RELEASE. */
25
+ /** Sent as `service.version`; the server links an issue's first release by matching it to an existing release's version. Defaults to WT_RELEASE, then `service.version` in OTEL_RESOURCE_ATTRIBUTES. */
22
26
  release?: string;
23
27
  /** Hook uncaughtException / unhandledRejection (default true). */
24
28
  captureProcessErrors?: boolean;
25
- /** Replace the log exporter (tests). */
29
+ /** Replace the log exporter (tests); setting it turns logs on unless `logs` is false. Exported through a batch processor. */
26
30
  logExporter?: unknown;
27
31
  /** Export OTLP logs; defaults to WATCHTOWER_LOGS (on unless `off`; OTEL_LOGS_EXPORTER=none also turns it off). */
28
32
  logs?: boolean;
33
+ /** Stamp code.filepath/code.lineno of the `log()` caller (reads the stack); defaults to WATCHTOWER_LOG_CALLER=1. */
34
+ logCaller?: boolean;
29
35
  /** Export OTLP metrics; defaults to WATCHTOWER_METRICS (on unless `off`; OTEL_METRICS_EXPORTER=none also turns it off). */
30
36
  metrics?: boolean;
31
- /** Replace the metric exporter (tests). */
37
+ /** Replace the metric exporter (tests); setting it turns metrics on unless `metrics` is false. */
32
38
  metricExporter?: unknown;
33
- /** OTLP/HTTP base URL; defaults to WT_OTLP_ENDPOINT or http://localhost:4318. */
39
+ /** OTLP/HTTP base URL; defaults to WT_OTLP_ENDPOINT, then OTEL_EXPORTER_OTLP_ENDPOINT, then http://localhost:4318. */
34
40
  endpoint?: string;
35
41
  /** REST API base URL for getPrompt/score; defaults to WT_API_URL or endpoint. */
36
42
  apiUrl?: string;
37
43
  project?: string;
38
- /** Defaults to WT_SERVICE. */
44
+ /** Defaults to WT_SERVICE, then OTEL_SERVICE_NAME, then `service.name` in OTEL_RESOURCE_ATTRIBUTES, then `app`. */
39
45
  serviceName?: string;
40
- /** Resource attributes; keys starting with `wt.` are dropped. */
46
+ /** Resource attributes (on top of OTEL_RESOURCE_ATTRIBUTES); keys starting with `wt.` are dropped. */
41
47
  resource?: Record<string, string | number | boolean>;
42
- /** Called for every span attribute before export; return the value to keep. */
48
+ /**
49
+ * Called before export for every span attribute, every span event's and link's attributes (so `exception.message`
50
+ * and `exception.stacktrace`), the span name (key `span.name`), the status message (`status.message`) and each log
51
+ * body (`log.body`) and log attribute; return the value to keep.
52
+ */
43
53
  redact?: (key: string, value: unknown) => unknown;
44
54
  /** Directory for the offline buffer. */
45
55
  bufferDir?: string;
56
+ /** Apply the project's head sampling rules in the SDK, polled from /v1/sampling; defaults to WT_REMOTE_SAMPLING=1. Spans dropped here never reach the gateway, so its exact request counts see only what is kept. */
57
+ remoteSampling?: boolean;
58
+ /** Seconds between sampling polls; defaults to WT_SAMPLING_INTERVAL_S or 30. */
59
+ samplingIntervalS?: number;
60
+ /** Stamp code.filepath / code.lineno / code.function on spans (the code that started the span); defaults to WT_CODE_ORIGIN=1. */
61
+ codeOrigin?: boolean;
46
62
  /** Fetch the service's payload capture rules (APM-016) and record bodies/GraphQL where a rule matches; defaults to WT_CAPTURE_RULES=1. */
47
63
  captureRules?: boolean;
48
64
  /** Replace the span exporter (tests). */
49
65
  exporter?: unknown;
66
+ /** Patch node:http, fetch (undici), pg and ioredis for those whose `@opentelemetry/instrumentation-*` peer is installed; defaults to on unless WT_INSTRUMENT=0. `init` only: use the `/register` preload so it runs before the app imports them. */
67
+ instrument?: boolean;
50
68
  fetch?: typeof fetch;
51
69
  }
52
70
  export interface EvalRow {
@@ -76,10 +94,12 @@ export declare class Client {
76
94
  readonly project: string | undefined;
77
95
  readonly headers: {
78
96
  Authorization: string;
79
- };
97
+ } & Record<string, string>;
80
98
  readonly fetch: typeof fetch;
81
99
  readonly redact: InitOptions['redact'];
82
- readonly provider: NodeTracerProvider;
100
+ private readonly logCaller;
101
+ readonly provider: TracerProvider;
102
+ readonly resource: Resource;
83
103
  readonly tracer: Tracer;
84
104
  readonly loggerProvider: LoggerProvider;
85
105
  readonly logger: ReturnType<LoggerProvider['getLogger']>;
@@ -87,6 +107,8 @@ export declare class Client {
87
107
  readonly meterProvider?: MeterProvider;
88
108
  private readonly stopRuntime;
89
109
  uninstall?: () => void;
110
+ /** Set when remote sampling is on. */
111
+ remoteSampler?: RemoteSampler;
90
112
  /** Set when WT_PROFILING=1 started the continuous profiler. */
91
113
  profiler?: Promise<Profiler | undefined>;
92
114
  constructor(opts: InitOptions);
package/dist/cjs/index.js CHANGED
@@ -34,6 +34,7 @@ var __importStar = (this && this.__importStar) || (function () {
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.shutdown = exports.observe = exports.evaluateResume = exports.evaluate = exports.score = exports.getPrompt = exports.log = exports.flush = exports.Client = exports.DEFAULT_ENDPOINT = exports.capturePayload = exports.setCaptureRules = exports.trace = exports.metrics = exports.captureException = void 0;
37
+ exports.callerOf = callerOf;
37
38
  exports.safeResource = safeResource;
38
39
  exports.init = init;
39
40
  // Thin Watchtower layer over the OpenTelemetry Node SDK: key header, endpoint default,
@@ -42,12 +43,10 @@ const node_util_1 = require("node:util");
42
43
  const api_1 = require("@opentelemetry/api");
43
44
  Object.defineProperty(exports, "metrics", { enumerable: true, get: function () { return api_1.metrics; } });
44
45
  Object.defineProperty(exports, "trace", { enumerable: true, get: function () { return api_1.trace; } });
45
- const resources_1 = require("@opentelemetry/resources");
46
- const sdk_trace_base_1 = require("@opentelemetry/sdk-trace-base");
47
- const sdk_trace_node_1 = require("@opentelemetry/sdk-trace-node");
48
- const sdk_logs_1 = require("@opentelemetry/sdk-logs");
49
- const exporter_trace_otlp_http_1 = require("@opentelemetry/exporter-trace-otlp-http");
50
- const exporter_logs_otlp_http_1 = require("@opentelemetry/exporter-logs-otlp-http");
46
+ const load_js_1 = require("./load.js");
47
+ const env_js_1 = require("./env.js");
48
+ const instrument_js_1 = require("./instrument.js");
49
+ const redact_js_1 = require("./redact.js");
51
50
  const buffer_js_1 = require("./buffer.js");
52
51
  const metrics_js_1 = require("./metrics.js");
53
52
  const nodePath = __importStar(require("node:path"));
@@ -55,10 +54,17 @@ const errors_js_1 = require("./errors.js");
55
54
  Object.defineProperty(exports, "captureException", { enumerable: true, get: function () { return errors_js_1.captureException; } });
56
55
  const payload_js_1 = require("./payload.js");
57
56
  const profiling_js_1 = require("./profiling.js");
57
+ const sampling_js_1 = require("./sampling.js");
58
+ const origin_js_1 = require("./origin.js");
58
59
  var payload_js_2 = require("./payload.js");
59
60
  Object.defineProperty(exports, "setCaptureRules", { enumerable: true, get: function () { return payload_js_2.setCaptureRules; } });
60
61
  Object.defineProperty(exports, "capturePayload", { enumerable: true, get: function () { return payload_js_2.capturePayload; } });
61
62
  exports.DEFAULT_ENDPOINT = 'http://localhost:4318';
63
+ /** code.filepath/code.lineno of the frame that called `log()` (stack line 0 is "Error", 1 is `log`, 2 the caller). */
64
+ function callerOf(stack) {
65
+ const m = /\(?((?:file:\/\/)?[^()\s]+):(\d+):\d+\)?$/.exec((stack ?? '').split('\n')[2] ?? '');
66
+ return m ? { 'code.filepath': m[1].replace(/^file:\/\//, ''), 'code.lineno': Number(m[2]) } : {};
67
+ }
62
68
  const SEVERITY = { trace: 1, debug: 5, info: 9, warn: 13, error: 17, fatal: 21 };
63
69
  /** Drop caller-supplied wt.* keys: tenant identity comes only from the key. */
64
70
  function safeResource(attrs) {
@@ -72,7 +78,9 @@ class Client {
72
78
  headers;
73
79
  fetch;
74
80
  redact;
81
+ logCaller;
75
82
  provider;
83
+ resource;
76
84
  tracer;
77
85
  loggerProvider;
78
86
  logger;
@@ -80,56 +88,83 @@ class Client {
80
88
  meterProvider;
81
89
  stopRuntime = () => { };
82
90
  uninstall;
91
+ /** Set when remote sampling is on. */
92
+ remoteSampler;
83
93
  /** Set when WT_PROFILING=1 started the continuous profiler. */
84
94
  profiler;
85
95
  constructor(opts) {
86
96
  const o = opts || {};
87
97
  const env = process.env;
88
- const apiKey = o.apiKey || env.WT_INGEST_KEY;
98
+ const oe = (0, env_js_1.otelEnv)(env);
99
+ const apiKey = o.apiKey || env.WT_INGEST_KEY || oe.bearer;
89
100
  if (!apiKey)
90
101
  throw new Error('apiKey is required (or set WT_INGEST_KEY)');
91
- this.endpoint = (o.endpoint || env.WT_OTLP_ENDPOINT || exports.DEFAULT_ENDPOINT).replace(/\/+$/, '');
102
+ this.endpoint = (o.endpoint || env.WT_OTLP_ENDPOINT || oe.endpoint || exports.DEFAULT_ENDPOINT).replace(/\/+$/, '');
92
103
  this.apiUrl = (o.apiUrl || env.WT_API_URL || this.endpoint).replace(/\/+$/, '');
93
104
  this.project = o.project || env.WT_PROJECT_ID;
94
- this.headers = { Authorization: `Bearer ${apiKey}` };
105
+ this.headers = { ...oe.headers, Authorization: `Bearer ${apiKey}` };
95
106
  this.fetch = o.fetch || globalThis.fetch;
96
- const environment = o.environment || env.WT_ENVIRONMENT;
97
- const release = o.release || env.WT_RELEASE;
98
- const resource = new resources_1.Resource({
99
- 'service.name': o.serviceName || env.WT_SERVICE || 'app',
107
+ const environment = o.environment || env.WT_ENVIRONMENT || oe.resource['deployment.environment.name'];
108
+ const release = o.release || env.WT_RELEASE || oe.resource['service.version'];
109
+ // the OTel SDK packages are optional peers, loaded here (not at import time) with a clear error when missing
110
+ const { resourceFromAttributes } = (0, load_js_1.need)('@opentelemetry/resources');
111
+ const resource = resourceFromAttributes({
112
+ ...safeResource(oe.resource),
113
+ 'service.name': o.serviceName || env.WT_SERVICE || oe.serviceName || 'app',
100
114
  ...(environment ? { 'deployment.environment.name': environment } : {}),
101
115
  ...(release ? { 'service.version': release } : {}),
102
116
  ...safeResource(o.resource),
103
117
  });
118
+ this.resource = resource;
104
119
  this.redact = o.redact;
105
- this.provider = new sdk_trace_node_1.NodeTracerProvider({ resource });
106
- if (o.redact)
107
- this.provider.addSpanProcessor(redactor(o.redact));
120
+ this.logCaller = o.logCaller ?? env.WATCHTOWER_LOG_CALLER === '1';
108
121
  let exporter = o.exporter;
109
122
  if (!exporter) {
110
123
  const url = `${this.endpoint}/v1/traces`;
111
- exporter = new exporter_trace_otlp_http_1.OTLPTraceExporter({ url, headers: this.headers });
124
+ const { OTLPTraceExporter } = (0, load_js_1.need)('@opentelemetry/exporter-trace-otlp-http');
125
+ exporter = new OTLPTraceExporter({ url, headers: this.headers });
112
126
  // spans and log records spool to their own subdirectories
113
127
  if (o.bufferDir)
114
128
  exporter = new buffer_js_1.SpoolExporter(exporter, nodePath.join(o.bufferDir, 'traces'), url, this.headers, this.fetch);
115
129
  }
116
- this.provider.addSpanProcessor(new sdk_trace_base_1.BatchSpanProcessor(exporter));
130
+ // the redactor runs first, so the batch processor exports redacted attributes
131
+ const { BatchSpanProcessor, TracerProvider: TP } = (0, load_js_1.need)('@opentelemetry/sdk-trace');
132
+ let sampler;
133
+ if (o.remoteSampling ?? env.WT_REMOTE_SAMPLING === '1') {
134
+ const { ParentBasedSampler } = (0, load_js_1.need)('@opentelemetry/sdk-trace');
135
+ this.remoteSampler = new sampling_js_1.RemoteSampler({
136
+ endpoint: this.endpoint, headers: this.headers, service: String(resource.attributes['service.name']), env: environment,
137
+ intervalMs: (o.samplingIntervalS ?? (Number(env.WT_SAMPLING_INTERVAL_S) || 30)) * 1000, fetch: this.fetch,
138
+ });
139
+ sampler = new ParentBasedSampler({ root: this.remoteSampler });
140
+ }
141
+ this.provider = new TP({
142
+ resource,
143
+ ...(sampler ? { sampler: sampler } : {}),
144
+ spanProcessors: [
145
+ ...(o.codeOrigin ?? env.WT_CODE_ORIGIN === '1' ? [(0, origin_js_1.codeOrigin)()] : []),
146
+ ...(o.redact ? [(0, redact_js_1.redactor)(o.redact)] : []),
147
+ new BatchSpanProcessor({ exporter }),
148
+ ],
149
+ });
117
150
  this.tracer = this.provider.getTracer('watchtower');
118
- this.loggerProvider = new sdk_logs_1.LoggerProvider({ resource });
119
- const logsOn = (0, metrics_js_1.switchOn)('WATCHTOWER_LOGS', 'OTEL_LOGS_EXPORTER', o.logs);
151
+ // an explicit exporter never switches the other signals off: only `logs` / `metrics` / the env switches do
152
+ const logsOn = (0, metrics_js_1.switchOn)('WATCHTOWER_LOGS', 'OTEL_LOGS_EXPORTER', o.logs ?? (o.logExporter ? true : undefined));
120
153
  let logExporter = logsOn ? o.logExporter : undefined;
121
- if (logsOn && !logExporter && !o.exporter) {
154
+ if (logsOn && !logExporter) {
122
155
  const url = `${this.endpoint}/v1/logs`;
123
- logExporter = new exporter_logs_otlp_http_1.OTLPLogExporter({ url, headers: this.headers });
156
+ const { OTLPLogExporter } = (0, load_js_1.need)('@opentelemetry/exporter-logs-otlp-http');
157
+ logExporter = new OTLPLogExporter({ url, headers: this.headers });
124
158
  if (o.bufferDir)
125
159
  logExporter = (0, buffer_js_1.spoolLogs)(logExporter, nodePath.join(o.bufferDir, 'logs'), url, this.headers, this.fetch);
126
160
  }
127
- if (logExporter) {
128
- this.loggerProvider.addLogRecordProcessor(o.logExporter ? new sdk_logs_1.SimpleLogRecordProcessor(logExporter) : new sdk_logs_1.BatchLogRecordProcessor(logExporter));
129
- }
161
+ const { LoggerProvider: LP, BatchLogRecordProcessor } = (0, load_js_1.need)('@opentelemetry/sdk-logs');
162
+ this.loggerProvider = new LP({
163
+ resource,
164
+ processors: !logExporter ? [] : [new BatchLogRecordProcessor({ exporter: logExporter })],
165
+ });
130
166
  this.logger = this.loggerProvider.getLogger('watchtower');
131
- // with a test span exporter only an explicit metric exporter is used, as for logs
132
- if ((0, metrics_js_1.switchOn)('WATCHTOWER_METRICS', 'OTEL_METRICS_EXPORTER', o.metrics) && (o.metricExporter || !o.exporter)) {
167
+ if ((0, metrics_js_1.switchOn)('WATCHTOWER_METRICS', 'OTEL_METRICS_EXPORTER', o.metrics ?? (o.metricExporter ? true : undefined))) {
133
168
  const m = (0, metrics_js_1.startMetrics)(this.endpoint, this.headers, resource, o.bufferDir, this.fetch, o.metricExporter);
134
169
  this.meterProvider = m.provider;
135
170
  this.stopRuntime = m.stop;
@@ -142,8 +177,12 @@ class Client {
142
177
  log(level, body, attributes) {
143
178
  let attrs = (0, errors_js_1.dropReserved)(attributes);
144
179
  const redact = this.redact;
145
- if (redact)
180
+ if (redact) {
146
181
  attrs = Object.fromEntries(Object.entries(attrs).map(([k, v]) => [k, redact(k, v)]));
182
+ body = String(redact(redact_js_1.LOG_BODY_KEY, body) ?? '');
183
+ }
184
+ if (this.logCaller)
185
+ attrs = { ...attrs, ...callerOf(new Error().stack) };
147
186
  this.logger.emit({
148
187
  severityNumber: SEVERITY[level] || SEVERITY.info,
149
188
  severityText: String(level).toUpperCase(),
@@ -251,35 +290,50 @@ class Client {
251
290
  }
252
291
  shutdown() {
253
292
  this.stopRuntime();
293
+ this.remoteSampler?.stop();
254
294
  return Promise.all([this.profiler?.then((p) => p?.stop()), this.provider.shutdown(), this.loggerProvider.shutdown(), this.meterProvider?.shutdown()]).then(() => undefined);
255
295
  }
256
296
  }
257
297
  exports.Client = Client;
258
- function redactor(hook) {
259
- return {
260
- onStart() { },
261
- onEnd(span) {
262
- for (const k of Object.keys(span.attributes))
263
- span.attributes[k] = hook(k, span.attributes[k]);
264
- },
265
- shutdown: () => Promise.resolve(),
266
- forceFlush: () => Promise.resolve(),
267
- };
298
+ // One state per process, not per module copy: a CommonJS and an ESM copy of this package (a `-r` preload plus an `import`
299
+ // app, say) must share the client or flush()/shutdown() in one copy would miss the init() of the other.
300
+ const state = (globalThis[Symbol.for('watchtower.node.state')] ??= {});
301
+ /**
302
+ * Register the global tracer provider, context manager, propagator and meter provider. OTel allows each to be set once
303
+ * (a second set returns false and changes nothing), so a provider the app or an earlier instrumentation registered is
304
+ * left alone and a warning says so; only globals a previous `init` installed are torn down on a re-init.
305
+ */
306
+ function registerGlobals(c) {
307
+ if (state.ownsGlobals) { // a re-init replaces what the previous init installed
308
+ api_1.trace.disable();
309
+ api_1.context.disable();
310
+ api_1.propagation.disable();
311
+ api_1.metrics.disable();
312
+ }
313
+ const { AsyncLocalStorageContextManager } = (0, load_js_1.need)('@opentelemetry/context-async-hooks');
314
+ const { CompositePropagator, W3CBaggagePropagator, W3CTraceContextPropagator } = (0, load_js_1.need)('@opentelemetry/core');
315
+ const ok = [
316
+ api_1.trace.setGlobalTracerProvider(c.provider),
317
+ api_1.context.setGlobalContextManager(new AsyncLocalStorageContextManager().enable()),
318
+ api_1.propagation.setGlobalPropagator(new CompositePropagator({ propagators: [new W3CTraceContextPropagator(), new W3CBaggagePropagator()] })),
319
+ ...(c.meterProvider ? [api_1.metrics.setGlobalMeterProvider(c.meterProvider)] : []),
320
+ ];
321
+ state.ownsGlobals = ok[0] === true;
322
+ if (ok.includes(false))
323
+ api_1.diag.warn('watchtower: an OpenTelemetry global (tracer provider, context manager, propagator or meter provider) was already registered and was kept; call init() before other OTel setup to own them');
268
324
  }
269
- let current;
270
325
  function init(opts) {
271
- if (current && current.uninstall)
272
- current.uninstall();
273
- void current?.profiler?.then((p) => p?.stop());
326
+ if (state.current && state.current.uninstall)
327
+ state.current.uninstall();
328
+ void state.current?.profiler?.then((p) => p?.stop());
274
329
  const c = new Client(opts);
275
- current = c;
276
- c.provider.register(); // global tracer + async context so trace.getActiveSpan() works
277
- if (c.meterProvider) {
278
- api_1.metrics.disable(); // a re-init replaces the previous client's provider
279
- api_1.metrics.setGlobalMeterProvider(c.meterProvider);
330
+ state.current = c;
331
+ registerGlobals(c);
332
+ if (opts?.instrument ?? !['0', 'off', 'false', 'no'].includes((process.env.WT_INSTRUMENT || '').trim().toLowerCase())) {
333
+ (0, instrument_js_1.instrumentAll)({ tracerProvider: c.provider, meterProvider: c.meterProvider }, [c.endpoint, c.apiUrl]);
280
334
  }
281
335
  if (process.env.WT_PROFILING === '1') {
282
- const attrs = c.provider.resource.attributes;
336
+ const attrs = c.resource.attributes;
283
337
  c.profiler = (0, profiling_js_1.startProfiling)({
284
338
  apiKey: c.headers.Authorization.slice('Bearer '.length),
285
339
  endpoint: c.endpoint,
@@ -293,11 +347,11 @@ function init(opts) {
293
347
  return c;
294
348
  }
295
349
  function need() {
296
- if (!current)
350
+ if (!state.current)
297
351
  throw new Error('watchtower.init() has not been called');
298
- return current;
352
+ return state.current;
299
353
  }
300
- const flush = () => (current ? current.forceFlush() : Promise.resolve());
354
+ const flush = () => (state.current ? state.current.forceFlush() : Promise.resolve());
301
355
  exports.flush = flush;
302
356
  const log = (l, b, a) => need().log(l, b, a);
303
357
  exports.log = log;
@@ -311,5 +365,5 @@ const evaluateResume = (d, t, s, prev, o) => need().evaluateResume(d, t, s, prev
311
365
  exports.evaluateResume = evaluateResume;
312
366
  const observe = (n, f) => need().observe(n, f);
313
367
  exports.observe = observe;
314
- const shutdown = () => (current ? current.shutdown() : Promise.resolve());
368
+ const shutdown = () => (state.current ? state.current.shutdown() : Promise.resolve());
315
369
  exports.shutdown = shutdown;
@@ -0,0 +1,7 @@
1
+ interface Providers {
2
+ tracerProvider: unknown;
3
+ meterProvider?: unknown;
4
+ }
5
+ /** Register whichever of the four instrumentations is installed. Returns the package names that were enabled. */
6
+ export declare function instrumentAll(p: Providers, selfUrls: string[]): string[];
7
+ export {};