@watchtowerobservability/node 0.7.6 → 0.7.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.7.8 (2026-11-16)
4
+
5
+ - Fix: on a non-cloud VM the OpenTelemetry resource detectors probed `169.254.169.254` and the 10 s timeout surfaced as a `UND_ERR_CONNECT_TIMEOUT` issue. When `OTEL_NODE_RESOURCE_DETECTORS` is unset, `init()` now sets it to `env,host,os,process`, adding `aws`, `gcp` or `azure` only when an environment variable shows that cloud (for example `AWS_REGION`, `K_SERVICE`, `WEBSITE_SITE_NAME`). Set `OTEL_NODE_RESOURCE_DETECTORS` yourself to override. Call `init()` (or use the `register` preload) before your own `NodeSDK` starts.
6
+ - The http and undici instrumentations ignore requests to cloud metadata endpoints (`169.254.169.254`, `169.254.170.2`, `metadata.google.internal`, `metadata.azure.internal`), so a failed probe is never reported as an error.
7
+
8
+ ## 0.7.7 (2026-11-16)
9
+
10
+ - Quieter traces by default: `pg-pool.connect`, bare `BEGIN` / `COMMIT` / `ROLLBACK` statement spans and Express middleware layer spans (`middleware - *`, `expressInit`, `query`) are dropped before export; errored spans are always kept. `noisySpans: true` (or `WT_NOISY_SPANS=1`) keeps them. Your own `spanFilter` still runs on what is left.
11
+ - When the ingest endpoint refuses traces with HTTP 429 (the project's daily quota is spent), the SDK prints one `[watchtower] traces are being dropped: daily quota reached (...)` warning instead of failing silently. The server now answers with a body naming the quota and `Retry-After` until 00:00 UTC.
12
+ - `tunnelHandler` takes `otlpEndpoint` (base URL for `/v1/traces` and `/v1/logs`, default `endpoint`) and logs one warning when the upstream answers 404 for those paths. The edge now serves `/v1/traces` for publishable keys, so the documented single-endpoint tunnel works.
13
+
3
14
  ## 0.7.6 (2026-11-15)
4
15
 
5
16
  - Next.js: `withWatchtower` now keeps every OpenTelemetry instrumentation package the SDK loads (`instrumentation-http`, `-undici`, `-runtime-node`, `-pg`, `-mysql2`, `-ioredis`) in `serverExternalPackages`. Bundled, they extended an undefined `InstrumentationBase` and a standalone build failed every request with `Class extends value undefined`. A test keeps the list in step with `package.json`.
package/dist/cjs/env.d.ts CHANGED
@@ -20,3 +20,10 @@ export declare function gitHeadSha(from: string): string | undefined;
20
20
  * (`WT_BUILD_STAMP`, `BUILD_STAMP`), then the `.git` HEAD found upward from `gitFrom` (skipped when not given). Empty and `unknown` count as absent.
21
21
  */
22
22
  export declare function resolveRelease(opt: string | undefined, env: NodeJS.ProcessEnv, otelResource: Record<string, string>, gitFrom?: string): string | undefined;
23
+ /** Cloud metadata endpoints; probing them from a non-cloud host times out, so they never become spans or issues. */
24
+ export declare const METADATA_HOSTS: Set<string>;
25
+ /**
26
+ * When OTEL_NODE_RESOURCE_DETECTORS is unset, limit the OTel Node resource detectors to local ones; a cloud detector is added only
27
+ * when an env var shows that cloud. Otherwise the detectors probe 169.254.169.254 and a non-cloud VM waits out a 10 s connect timeout.
28
+ */
29
+ export declare function defaultResourceDetectors(env?: NodeJS.ProcessEnv): void;
package/dist/cjs/env.js CHANGED
@@ -33,10 +33,12 @@ var __importStar = (this && this.__importStar) || (function () {
33
33
  };
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.METADATA_HOSTS = void 0;
36
37
  exports.parseKv = parseKv;
37
38
  exports.otelEnv = otelEnv;
38
39
  exports.gitHeadSha = gitHeadSha;
39
40
  exports.resolveRelease = resolveRelease;
41
+ exports.defaultResourceDetectors = defaultResourceDetectors;
40
42
  const fs = __importStar(require("node:fs"));
41
43
  const nodePath = __importStar(require("node:path"));
42
44
  // Standard OTEL_* environment variables, used as fallbacks after the WT_* ones.
@@ -132,3 +134,22 @@ function resolveRelease(opt, env, otelResource, gitFrom) {
132
134
  }
133
135
  return gitFrom ? gitHeadSha(gitFrom) : undefined;
134
136
  }
137
+ /** Cloud metadata endpoints; probing them from a non-cloud host times out, so they never become spans or issues. */
138
+ exports.METADATA_HOSTS = new Set(['169.254.169.254', 'metadata.google.internal', '169.254.170.2', 'metadata.azure.internal']);
139
+ /**
140
+ * When OTEL_NODE_RESOURCE_DETECTORS is unset, limit the OTel Node resource detectors to local ones; a cloud detector is added only
141
+ * when an env var shows that cloud. Otherwise the detectors probe 169.254.169.254 and a non-cloud VM waits out a 10 s connect timeout.
142
+ */
143
+ function defaultResourceDetectors(env = process.env) {
144
+ if (env.OTEL_NODE_RESOURCE_DETECTORS)
145
+ return;
146
+ const on = (...k) => k.some((x) => !!env[x]);
147
+ const d = ['env', 'host', 'os', 'process'];
148
+ if (on('AWS_REGION', 'AWS_DEFAULT_REGION', 'AWS_EXECUTION_ENV', 'ECS_CONTAINER_METADATA_URI'))
149
+ d.push('aws');
150
+ if (on('GOOGLE_CLOUD_PROJECT', 'GCP_PROJECT', 'K_SERVICE', 'GAE_SERVICE'))
151
+ d.push('gcp');
152
+ if (on('WEBSITE_SITE_NAME', 'AZURE_FUNCTIONS_ENVIRONMENT', 'AZURE_CLIENT_ID', 'AZURE_TENANT_ID'))
153
+ d.push('azure');
154
+ env.OTEL_NODE_RESOURCE_DETECTORS = d.join(',');
155
+ }
@@ -1,3 +1,13 @@
1
1
  // @ts-ignore optional peer: absent -> any (keeps skipLibCheck:false consumers compiling)
2
2
  import type { ReadableSpan, SpanExporter } from '@opentelemetry/sdk-trace';
3
3
  export declare function filterExporter(inner: SpanExporter, keep: (s: ReadableSpan) => boolean): SpanExporter;
4
+ /**
5
+ * Low-value auto-instrumentation spans dropped by default (`noisySpans: true` keeps them): the `pg-pool.connect` wait, bare
6
+ * `BEGIN` / `COMMIT` / `ROLLBACK` statements, and Express middleware layers (`middleware - *`, `expressInit`, `query`).
7
+ * Errored spans are always kept.
8
+ */
9
+ export declare function isNoisySpan(s: ReadableSpan): boolean;
10
+ /** Logs once when the ingest endpoint answers 429 (daily quota or rate limit), which the OTLP exporter would otherwise swallow. */
11
+ export declare function quotaWarnExporter(inner: SpanExporter, warn?: (msg: string) => void): SpanExporter;
12
+ /** Test hook: lets the once-only warning fire again. */
13
+ export declare function resetQuotaWarning(): void;
@@ -1,6 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.filterExporter = filterExporter;
4
+ exports.isNoisySpan = isNoisySpan;
5
+ exports.quotaWarnExporter = quotaWarnExporter;
6
+ exports.resetQuotaWarning = resetQuotaWarning;
4
7
  function filterExporter(inner, keep) {
5
8
  return {
6
9
  export(spans, done) {
@@ -14,3 +17,45 @@ function filterExporter(inner, keep) {
14
17
  forceFlush: () => inner.forceFlush?.() ?? Promise.resolve(),
15
18
  };
16
19
  }
20
+ const BRACKET = /^\s*(BEGIN|COMMIT|ROLLBACK)\s*;?\s*$/i;
21
+ const BRACKET_NAME = /^(pg\.query:)?(BEGIN|COMMIT|ROLLBACK)$/i;
22
+ const EXPRESS_LAYER = /^(middleware - |router - )?(query|expressInit)$/;
23
+ /**
24
+ * Low-value auto-instrumentation spans dropped by default (`noisySpans: true` keeps them): the `pg-pool.connect` wait, bare
25
+ * `BEGIN` / `COMMIT` / `ROLLBACK` statements, and Express middleware layers (`middleware - *`, `expressInit`, `query`).
26
+ * Errored spans are always kept.
27
+ */
28
+ function isNoisySpan(s) {
29
+ if (s.status.code === 2)
30
+ return false; // SpanStatusCode.ERROR
31
+ const name = s.name;
32
+ if (name === 'pg-pool.connect')
33
+ return true;
34
+ const stmt = s.attributes['db.query.text'] ?? s.attributes['db.statement'];
35
+ if (typeof stmt === 'string' ? BRACKET.test(stmt) : BRACKET_NAME.test(name))
36
+ return true;
37
+ return name.startsWith('middleware - ') || s.attributes['express.type'] === 'middleware' || EXPRESS_LAYER.test(name);
38
+ }
39
+ let quotaWarned = false;
40
+ /** Logs once when the ingest endpoint answers 429 (daily quota or rate limit), which the OTLP exporter would otherwise swallow. */
41
+ function quotaWarnExporter(inner, warn = (m) => console.warn(m)) {
42
+ return {
43
+ export(spans, done) {
44
+ inner.export(spans, (r) => {
45
+ const e = r.error;
46
+ if (!quotaWarned && e && (e.code === 429 || /\b429\b|daily quota/.test(`${e.message} ${e.data ?? ''}`))) {
47
+ quotaWarned = true;
48
+ const why = /daily quota reached[^"\n]*/.exec(`${e.data ?? ''} ${e.message}`)?.[0] ?? 'ingest answered 429, usually the daily quota; check GET /v1/projects/{prj}/settings/quotas';
49
+ warn(`[watchtower] traces are being dropped: daily quota reached (${why})`);
50
+ }
51
+ done(r);
52
+ });
53
+ },
54
+ shutdown: () => inner.shutdown(),
55
+ forceFlush: () => inner.forceFlush?.() ?? Promise.resolve(),
56
+ };
57
+ }
58
+ /** Test hook: lets the once-only warning fire again. */
59
+ function resetQuotaWarning() {
60
+ quotaWarned = false;
61
+ }
@@ -96,6 +96,12 @@ export interface InitOptions {
96
96
  otel?: 'auto' | 'own' | 'attach';
97
97
  /** Return false to drop a finished span before export (the `/next` entry uses it for `nextInternals`). */
98
98
  spanFilter?: (span: ReadableSpan) => boolean;
99
+ /**
100
+ * Keep the low-value auto-instrumentation spans that are dropped by default: `pg-pool.connect`, bare `BEGIN` / `COMMIT` /
101
+ * `ROLLBACK` statements and Express middleware layers (`middleware - *`, `expressInit`, `query`). Errored spans are always kept.
102
+ * Default false; `WT_NOISY_SPANS=1` sets it.
103
+ */
104
+ noisySpans?: boolean;
99
105
  /**
100
106
  * Next.js's own spans (`next.span_type` set: load route module, prepare route module, resolve page components, start
101
107
  * response, middleware, generateMetadata ...), root ones included. `"sample"` keeps `nextInternalsRate` of them, `"drop"`
package/dist/cjs/index.js CHANGED
@@ -163,8 +163,9 @@ class Client {
163
163
  if (o.bufferDir)
164
164
  exporter = new buffer_js_1.SpoolExporter(exporter, nodePath.join(o.bufferDir, 'traces'), url, this.headers, this.fetch);
165
165
  }
166
- if (o.spanFilter)
167
- exporter = (0, filter_js_1.filterExporter)(exporter, o.spanFilter);
166
+ const keepNoisy = o.noisySpans ?? env.WT_NOISY_SPANS === '1';
167
+ const user = o.spanFilter;
168
+ exporter = (0, filter_js_1.filterExporter)((0, filter_js_1.quotaWarnExporter)(exporter), keepNoisy ? user ?? (() => true) : user ? (s) => !(0, filter_js_1.isNoisySpan)(s) && user(s) : (s) => !(0, filter_js_1.isNoisySpan)(s));
168
169
  // the scrubber and redactor run first, so the batch processor exports scrubbed attributes (the user's `redact` sees scrubbed URLs)
169
170
  const { BatchSpanProcessor, TracerProvider: TP } = (0, load_js_1.need)('@opentelemetry/sdk-trace');
170
171
  let sampler;
@@ -404,6 +405,7 @@ function registerGlobals(c) {
404
405
  }
405
406
  }
406
407
  function init(opts) {
408
+ (0, env_js_1.defaultResourceDetectors)(); // before any NodeSDK reads OTEL_NODE_RESOURCE_DETECTORS
407
409
  const missing = (0, load_js_1.missingPeers)();
408
410
  if (missing.length)
409
411
  console.warn(`[watchtower] missing OpenTelemetry package(s): ${missing.join(', ')}. Next.js standalone: ${load_js_1.STANDALONE_HINT}. Otherwise: npm install ${missing.join(' ')}`);
@@ -7,19 +7,26 @@ exports.instrumentAll = instrumentAll;
7
7
  const load_js_1 = require("./load.js");
8
8
  const sqlite_js_1 = require("./integrations/sqlite.js");
9
9
  const postgres_js_1 = require("./integrations/postgres.js");
10
+ const env_js_1 = require("./env.js");
10
11
  const util_js_1 = require("./integrations/util.js");
11
12
  /** Instrumentation packages already registered in this process (by `init` or `instrument()`), so none is patched twice. */
12
13
  exports.registered = new Set();
14
+ const hostOf = (o) => { try {
15
+ return new URL(o ?? '').hostname;
16
+ }
17
+ catch {
18
+ return '';
19
+ } };
13
20
  const ENTRIES = [
14
21
  {
15
22
  pkg: '@opentelemetry/instrumentation-http', ctor: 'HttpInstrumentation',
16
23
  // our own exports and API calls must not become spans
17
24
  // routeNamer: server spans become `GET /users/:id` from the matched Express / Nest route (http alone names them just `GET`)
18
- opts: (self) => ({ applyCustomAttributesOnSpan: util_js_1.routeNamer, ignoreOutgoingRequestHook: (r) => self.some((u) => (r.hostname || r.host) === u.hostname && String(r.port ?? (u.protocol === 'https:' ? 443 : 80)) === String(u.port || (u.protocol === 'https:' ? 443 : 80))) }),
25
+ opts: (self) => ({ applyCustomAttributesOnSpan: util_js_1.routeNamer, ignoreOutgoingRequestHook: (r) => env_js_1.METADATA_HOSTS.has(String(r.hostname || r.host).replace(/:\d+$/, '')) || self.some((u) => (r.hostname || r.host) === u.hostname && String(r.port ?? (u.protocol === 'https:' ? 443 : 80)) === String(u.port || (u.protocol === 'https:' ? 443 : 80))) }),
19
26
  },
20
27
  {
21
28
  pkg: '@opentelemetry/instrumentation-undici', ctor: 'UndiciInstrumentation',
22
- opts: (self) => ({ ignoreRequestHook: (r) => self.some((u) => r.origin === u.origin) }),
29
+ opts: (self) => ({ ignoreRequestHook: (r) => env_js_1.METADATA_HOSTS.has(hostOf(r.origin)) || self.some((u) => r.origin === u.origin) }),
23
30
  },
24
31
  { pkg: '@opentelemetry/instrumentation-pg', ctor: 'PgInstrumentation', opts: () => ({}) },
25
32
  { pkg: '@opentelemetry/instrumentation-ioredis', ctor: 'IORedisInstrumentation', opts: () => ({}) },
@@ -1,6 +1,8 @@
1
1
  export interface TunnelOptions {
2
2
  /** Edge base URL the requests are forwarded to. */
3
3
  endpoint: string;
4
+ /** Base URL for `/v1/traces` and `/v1/logs` (for example the ingest host); default: `endpoint`. */
5
+ otlpEndpoint?: string;
4
6
  /** Route prefix to strip; default `/api/wt`. */
5
7
  basePath?: string;
6
8
  /** Request body cap in bytes; default 1 MiB (over it: 413). */
@@ -15,7 +15,9 @@ function tunnelHandler(o) {
15
15
  const max = o.maxBodyBytes ?? 1024 * 1024;
16
16
  const edge = o.endpoint.replace(/\/+$/, '');
17
17
  const allow = o.allowKey === undefined ? [] : [].concat(o.allowKey);
18
+ const otlp = (o.otlpEndpoint ?? o.endpoint).replace(/\/+$/, '');
18
19
  const send = o.fetch ?? fetch;
20
+ let warned = false;
19
21
  const handle = async (req) => {
20
22
  const url = new URL(req.url);
21
23
  const path = url.pathname.startsWith(base) ? url.pathname.slice(base.length) : url.pathname;
@@ -48,7 +50,12 @@ function tunnelHandler(o) {
48
50
  return new Response('payload too large', { status: 413 });
49
51
  }
50
52
  try {
51
- const res = await send(`${edge}${path}${k ? `?k=${encodeURIComponent(k)}` : ''}`, { method: req.method, headers, body });
53
+ const isOtlp = path === '/v1/traces' || path === '/v1/logs';
54
+ const res = await send(`${isOtlp ? otlp : edge}${path}${k ? `?k=${encodeURIComponent(k)}` : ''}`, { method: req.method, headers, body });
55
+ if (isOtlp && res.status === 404 && !warned) {
56
+ warned = true; // once per process: a 404 here drops every browser span silently
57
+ console.warn(`[watchtower] tunnel: ${isOtlp ? otlp : edge}${path} answered 404; upgrade the edge or set tunnelHandler({ otlpEndpoint })`);
58
+ }
52
59
  const out = new Headers();
53
60
  const ct = res.headers.get('content-type');
54
61
  if (ct)
@@ -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.6";
2
+ export declare const VERSION = "0.7.8";
@@ -2,4 +2,4 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.VERSION = void 0;
4
4
  /** This package's version (test/distro.test.ts checks it against package.json); sent as `telemetry.distro.version`. */
5
- exports.VERSION = '0.7.6';
5
+ exports.VERSION = '0.7.8';
package/dist/esm/env.d.ts CHANGED
@@ -20,3 +20,10 @@ export declare function gitHeadSha(from: string): string | undefined;
20
20
  * (`WT_BUILD_STAMP`, `BUILD_STAMP`), then the `.git` HEAD found upward from `gitFrom` (skipped when not given). Empty and `unknown` count as absent.
21
21
  */
22
22
  export declare function resolveRelease(opt: string | undefined, env: NodeJS.ProcessEnv, otelResource: Record<string, string>, gitFrom?: string): string | undefined;
23
+ /** Cloud metadata endpoints; probing them from a non-cloud host times out, so they never become spans or issues. */
24
+ export declare const METADATA_HOSTS: Set<string>;
25
+ /**
26
+ * When OTEL_NODE_RESOURCE_DETECTORS is unset, limit the OTel Node resource detectors to local ones; a cloud detector is added only
27
+ * when an env var shows that cloud. Otherwise the detectors probe 169.254.169.254 and a non-cloud VM waits out a 10 s connect timeout.
28
+ */
29
+ export declare function defaultResourceDetectors(env?: NodeJS.ProcessEnv): void;
package/dist/esm/env.js CHANGED
@@ -93,3 +93,22 @@ export function resolveRelease(opt, env, otelResource, gitFrom) {
93
93
  }
94
94
  return gitFrom ? gitHeadSha(gitFrom) : undefined;
95
95
  }
96
+ /** Cloud metadata endpoints; probing them from a non-cloud host times out, so they never become spans or issues. */
97
+ export const METADATA_HOSTS = new Set(['169.254.169.254', 'metadata.google.internal', '169.254.170.2', 'metadata.azure.internal']);
98
+ /**
99
+ * When OTEL_NODE_RESOURCE_DETECTORS is unset, limit the OTel Node resource detectors to local ones; a cloud detector is added only
100
+ * when an env var shows that cloud. Otherwise the detectors probe 169.254.169.254 and a non-cloud VM waits out a 10 s connect timeout.
101
+ */
102
+ export function defaultResourceDetectors(env = process.env) {
103
+ if (env.OTEL_NODE_RESOURCE_DETECTORS)
104
+ return;
105
+ const on = (...k) => k.some((x) => !!env[x]);
106
+ const d = ['env', 'host', 'os', 'process'];
107
+ if (on('AWS_REGION', 'AWS_DEFAULT_REGION', 'AWS_EXECUTION_ENV', 'ECS_CONTAINER_METADATA_URI'))
108
+ d.push('aws');
109
+ if (on('GOOGLE_CLOUD_PROJECT', 'GCP_PROJECT', 'K_SERVICE', 'GAE_SERVICE'))
110
+ d.push('gcp');
111
+ if (on('WEBSITE_SITE_NAME', 'AZURE_FUNCTIONS_ENVIRONMENT', 'AZURE_CLIENT_ID', 'AZURE_TENANT_ID'))
112
+ d.push('azure');
113
+ env.OTEL_NODE_RESOURCE_DETECTORS = d.join(',');
114
+ }
@@ -1,3 +1,13 @@
1
1
  // @ts-ignore optional peer: absent -> any (keeps skipLibCheck:false consumers compiling)
2
2
  import type { ReadableSpan, SpanExporter } from '@opentelemetry/sdk-trace';
3
3
  export declare function filterExporter(inner: SpanExporter, keep: (s: ReadableSpan) => boolean): SpanExporter;
4
+ /**
5
+ * Low-value auto-instrumentation spans dropped by default (`noisySpans: true` keeps them): the `pg-pool.connect` wait, bare
6
+ * `BEGIN` / `COMMIT` / `ROLLBACK` statements, and Express middleware layers (`middleware - *`, `expressInit`, `query`).
7
+ * Errored spans are always kept.
8
+ */
9
+ export declare function isNoisySpan(s: ReadableSpan): boolean;
10
+ /** Logs once when the ingest endpoint answers 429 (daily quota or rate limit), which the OTLP exporter would otherwise swallow. */
11
+ export declare function quotaWarnExporter(inner: SpanExporter, warn?: (msg: string) => void): SpanExporter;
12
+ /** Test hook: lets the once-only warning fire again. */
13
+ export declare function resetQuotaWarning(): void;
@@ -11,3 +11,45 @@ export function filterExporter(inner, keep) {
11
11
  forceFlush: () => inner.forceFlush?.() ?? Promise.resolve(),
12
12
  };
13
13
  }
14
+ const BRACKET = /^\s*(BEGIN|COMMIT|ROLLBACK)\s*;?\s*$/i;
15
+ const BRACKET_NAME = /^(pg\.query:)?(BEGIN|COMMIT|ROLLBACK)$/i;
16
+ const EXPRESS_LAYER = /^(middleware - |router - )?(query|expressInit)$/;
17
+ /**
18
+ * Low-value auto-instrumentation spans dropped by default (`noisySpans: true` keeps them): the `pg-pool.connect` wait, bare
19
+ * `BEGIN` / `COMMIT` / `ROLLBACK` statements, and Express middleware layers (`middleware - *`, `expressInit`, `query`).
20
+ * Errored spans are always kept.
21
+ */
22
+ export function isNoisySpan(s) {
23
+ if (s.status.code === 2)
24
+ return false; // SpanStatusCode.ERROR
25
+ const name = s.name;
26
+ if (name === 'pg-pool.connect')
27
+ return true;
28
+ const stmt = s.attributes['db.query.text'] ?? s.attributes['db.statement'];
29
+ if (typeof stmt === 'string' ? BRACKET.test(stmt) : BRACKET_NAME.test(name))
30
+ return true;
31
+ return name.startsWith('middleware - ') || s.attributes['express.type'] === 'middleware' || EXPRESS_LAYER.test(name);
32
+ }
33
+ let quotaWarned = false;
34
+ /** Logs once when the ingest endpoint answers 429 (daily quota or rate limit), which the OTLP exporter would otherwise swallow. */
35
+ export function quotaWarnExporter(inner, warn = (m) => console.warn(m)) {
36
+ return {
37
+ export(spans, done) {
38
+ inner.export(spans, (r) => {
39
+ const e = r.error;
40
+ if (!quotaWarned && e && (e.code === 429 || /\b429\b|daily quota/.test(`${e.message} ${e.data ?? ''}`))) {
41
+ quotaWarned = true;
42
+ const why = /daily quota reached[^"\n]*/.exec(`${e.data ?? ''} ${e.message}`)?.[0] ?? 'ingest answered 429, usually the daily quota; check GET /v1/projects/{prj}/settings/quotas';
43
+ warn(`[watchtower] traces are being dropped: daily quota reached (${why})`);
44
+ }
45
+ done(r);
46
+ });
47
+ },
48
+ shutdown: () => inner.shutdown(),
49
+ forceFlush: () => inner.forceFlush?.() ?? Promise.resolve(),
50
+ };
51
+ }
52
+ /** Test hook: lets the once-only warning fire again. */
53
+ export function resetQuotaWarning() {
54
+ quotaWarned = false;
55
+ }
@@ -96,6 +96,12 @@ export interface InitOptions {
96
96
  otel?: 'auto' | 'own' | 'attach';
97
97
  /** Return false to drop a finished span before export (the `/next` entry uses it for `nextInternals`). */
98
98
  spanFilter?: (span: ReadableSpan) => boolean;
99
+ /**
100
+ * Keep the low-value auto-instrumentation spans that are dropped by default: `pg-pool.connect`, bare `BEGIN` / `COMMIT` /
101
+ * `ROLLBACK` statements and Express middleware layers (`middleware - *`, `expressInit`, `query`). Errored spans are always kept.
102
+ * Default false; `WT_NOISY_SPANS=1` sets it.
103
+ */
104
+ noisySpans?: boolean;
99
105
  /**
100
106
  * Next.js's own spans (`next.span_type` set: load route module, prepare route module, resolve page components, start
101
107
  * response, middleware, generateMetadata ...), root ones included. `"sample"` keeps `nextInternalsRate` of them, `"drop"`
package/dist/esm/index.js CHANGED
@@ -3,14 +3,14 @@
3
3
  import { inspect } from 'node:util';
4
4
  import { context, diag, metrics, propagation, trace } from '@opentelemetry/api';
5
5
  import { missingPeers, need as needPeer, STANDALONE_HINT } from './load.js';
6
- import { otelEnv, resolveRelease } from './env.js';
6
+ import { defaultResourceDetectors, otelEnv, resolveRelease } from './env.js';
7
7
  import { dbRequireParent, setHttpServerActive } from './integrations/db.js';
8
8
  import { instrumentAll } from './instrument.js';
9
9
  import { LOG_BODY_KEY, redactor } from './redact.js';
10
10
  import { resolveRedact } from './redact-presets.js';
11
11
  import { foreignProvider, attachProcessors } from './attach.js';
12
12
  import { scrubUrl, urlScrubber } from './scrub.js';
13
- import { filterExporter } from './filter.js';
13
+ import { filterExporter, isNoisySpan, quotaWarnExporter } from './filter.js';
14
14
  import { nextInternalsFilter } from './next-filter.js';
15
15
  import { checkin as runCheckin } from './checkin.js';
16
16
  import { checkIn as checkInFor, withMonitor as withMonitorFor } from './cron/monitor.js';
@@ -116,8 +116,9 @@ export class Client {
116
116
  if (o.bufferDir)
117
117
  exporter = new SpoolExporter(exporter, nodePath.join(o.bufferDir, 'traces'), url, this.headers, this.fetch);
118
118
  }
119
- if (o.spanFilter)
120
- exporter = filterExporter(exporter, o.spanFilter);
119
+ const keepNoisy = o.noisySpans ?? env.WT_NOISY_SPANS === '1';
120
+ const user = o.spanFilter;
121
+ exporter = filterExporter(quotaWarnExporter(exporter), keepNoisy ? user ?? (() => true) : user ? (s) => !isNoisySpan(s) && user(s) : (s) => !isNoisySpan(s));
121
122
  // the scrubber and redactor run first, so the batch processor exports scrubbed attributes (the user's `redact` sees scrubbed URLs)
122
123
  const { BatchSpanProcessor, TracerProvider: TP } = needPeer('@opentelemetry/sdk-trace');
123
124
  let sampler;
@@ -356,6 +357,7 @@ function registerGlobals(c) {
356
357
  }
357
358
  }
358
359
  export function init(opts) {
360
+ defaultResourceDetectors(); // before any NodeSDK reads OTEL_NODE_RESOURCE_DETECTORS
359
361
  const missing = missingPeers();
360
362
  if (missing.length)
361
363
  console.warn(`[watchtower] missing OpenTelemetry package(s): ${missing.join(', ')}. Next.js standalone: ${STANDALONE_HINT}. Otherwise: npm install ${missing.join(' ')}`);
@@ -2,19 +2,26 @@
2
2
  import { load } from './load.js';
3
3
  import { instrumentBetterSqlite3 } from './integrations/sqlite.js';
4
4
  import { instrumentPostgres } from './integrations/postgres.js';
5
+ import { METADATA_HOSTS } from './env.js';
5
6
  import { routeNamer } from './integrations/util.js';
6
7
  /** Instrumentation packages already registered in this process (by `init` or `instrument()`), so none is patched twice. */
7
8
  export const registered = new Set();
9
+ const hostOf = (o) => { try {
10
+ return new URL(o ?? '').hostname;
11
+ }
12
+ catch {
13
+ return '';
14
+ } };
8
15
  const ENTRIES = [
9
16
  {
10
17
  pkg: '@opentelemetry/instrumentation-http', ctor: 'HttpInstrumentation',
11
18
  // our own exports and API calls must not become spans
12
19
  // routeNamer: server spans become `GET /users/:id` from the matched Express / Nest route (http alone names them just `GET`)
13
- opts: (self) => ({ applyCustomAttributesOnSpan: routeNamer, ignoreOutgoingRequestHook: (r) => self.some((u) => (r.hostname || r.host) === u.hostname && String(r.port ?? (u.protocol === 'https:' ? 443 : 80)) === String(u.port || (u.protocol === 'https:' ? 443 : 80))) }),
20
+ opts: (self) => ({ applyCustomAttributesOnSpan: routeNamer, ignoreOutgoingRequestHook: (r) => METADATA_HOSTS.has(String(r.hostname || r.host).replace(/:\d+$/, '')) || self.some((u) => (r.hostname || r.host) === u.hostname && String(r.port ?? (u.protocol === 'https:' ? 443 : 80)) === String(u.port || (u.protocol === 'https:' ? 443 : 80))) }),
14
21
  },
15
22
  {
16
23
  pkg: '@opentelemetry/instrumentation-undici', ctor: 'UndiciInstrumentation',
17
- opts: (self) => ({ ignoreRequestHook: (r) => self.some((u) => r.origin === u.origin) }),
24
+ opts: (self) => ({ ignoreRequestHook: (r) => METADATA_HOSTS.has(hostOf(r.origin)) || self.some((u) => r.origin === u.origin) }),
18
25
  },
19
26
  { pkg: '@opentelemetry/instrumentation-pg', ctor: 'PgInstrumentation', opts: () => ({}) },
20
27
  { pkg: '@opentelemetry/instrumentation-ioredis', ctor: 'IORedisInstrumentation', opts: () => ({}) },
@@ -1,6 +1,8 @@
1
1
  export interface TunnelOptions {
2
2
  /** Edge base URL the requests are forwarded to. */
3
3
  endpoint: string;
4
+ /** Base URL for `/v1/traces` and `/v1/logs` (for example the ingest host); default: `endpoint`. */
5
+ otlpEndpoint?: string;
4
6
  /** Route prefix to strip; default `/api/wt`. */
5
7
  basePath?: string;
6
8
  /** Request body cap in bytes; default 1 MiB (over it: 413). */
@@ -12,7 +12,9 @@ export function tunnelHandler(o) {
12
12
  const max = o.maxBodyBytes ?? 1024 * 1024;
13
13
  const edge = o.endpoint.replace(/\/+$/, '');
14
14
  const allow = o.allowKey === undefined ? [] : [].concat(o.allowKey);
15
+ const otlp = (o.otlpEndpoint ?? o.endpoint).replace(/\/+$/, '');
15
16
  const send = o.fetch ?? fetch;
17
+ let warned = false;
16
18
  const handle = async (req) => {
17
19
  const url = new URL(req.url);
18
20
  const path = url.pathname.startsWith(base) ? url.pathname.slice(base.length) : url.pathname;
@@ -45,7 +47,12 @@ export function tunnelHandler(o) {
45
47
  return new Response('payload too large', { status: 413 });
46
48
  }
47
49
  try {
48
- const res = await send(`${edge}${path}${k ? `?k=${encodeURIComponent(k)}` : ''}`, { method: req.method, headers, body });
50
+ const isOtlp = path === '/v1/traces' || path === '/v1/logs';
51
+ const res = await send(`${isOtlp ? otlp : edge}${path}${k ? `?k=${encodeURIComponent(k)}` : ''}`, { method: req.method, headers, body });
52
+ if (isOtlp && res.status === 404 && !warned) {
53
+ warned = true; // once per process: a 404 here drops every browser span silently
54
+ console.warn(`[watchtower] tunnel: ${isOtlp ? otlp : edge}${path} answered 404; upgrade the edge or set tunnelHandler({ otlpEndpoint })`);
55
+ }
49
56
  const out = new Headers();
50
57
  const ct = res.headers.get('content-type');
51
58
  if (ct)
@@ -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.6";
2
+ export declare const VERSION = "0.7.8";
@@ -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.6';
2
+ export const VERSION = '0.7.8';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@watchtowerobservability/node",
3
- "version": "0.7.6",
3
+ "version": "0.7.8",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/watchtowerobservability/watchtower-node.git"