@watchtowerobservability/node 0.6.0 → 0.7.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 (54) hide show
  1. package/README.md +51 -3
  2. package/dist/cjs/checkin.d.ts +7 -0
  3. package/dist/cjs/checkin.js +18 -0
  4. package/dist/cjs/filter.d.ts +2 -0
  5. package/dist/cjs/filter.js +16 -0
  6. package/dist/cjs/index.d.ts +21 -3
  7. package/dist/cjs/index.js +34 -6
  8. package/dist/cjs/instrument.d.ts +5 -2
  9. package/dist/cjs/instrument.js +26 -3
  10. package/dist/cjs/integrations/db.d.ts +3 -0
  11. package/dist/cjs/integrations/db.js +4 -1
  12. package/dist/cjs/integrations/next-build.d.ts +31 -0
  13. package/dist/cjs/integrations/next-build.js +95 -0
  14. package/dist/cjs/integrations/next-tunnel.d.ts +14 -0
  15. package/dist/cjs/integrations/next-tunnel.js +52 -0
  16. package/dist/cjs/integrations/next.d.ts +21 -1
  17. package/dist/cjs/integrations/next.js +28 -2
  18. package/dist/cjs/load.js +1 -0
  19. package/dist/cjs/next-sourcemaps-cli.d.ts +2 -0
  20. package/dist/cjs/next-sourcemaps-cli.js +9 -0
  21. package/dist/cjs/scrub.d.ts +7 -0
  22. package/dist/cjs/scrub.js +39 -0
  23. package/dist/cjs/shutdown.d.ts +14 -0
  24. package/dist/cjs/shutdown.js +26 -0
  25. package/dist/cjs/sql.js +11 -1
  26. package/dist/cjs/version.d.ts +2 -0
  27. package/dist/cjs/version.js +5 -0
  28. package/dist/esm/checkin.d.ts +7 -0
  29. package/dist/esm/checkin.js +15 -0
  30. package/dist/esm/filter.d.ts +2 -0
  31. package/dist/esm/filter.js +13 -0
  32. package/dist/esm/index.d.ts +21 -3
  33. package/dist/esm/index.js +32 -5
  34. package/dist/esm/instrument.d.ts +5 -2
  35. package/dist/esm/instrument.js +26 -3
  36. package/dist/esm/integrations/db.d.ts +3 -0
  37. package/dist/esm/integrations/db.js +5 -2
  38. package/dist/esm/integrations/next-build.d.ts +31 -0
  39. package/dist/esm/integrations/next-build.js +58 -0
  40. package/dist/esm/integrations/next-tunnel.d.ts +14 -0
  41. package/dist/esm/integrations/next-tunnel.js +49 -0
  42. package/dist/esm/integrations/next.d.ts +21 -1
  43. package/dist/esm/integrations/next.js +22 -1
  44. package/dist/esm/load.js +2 -0
  45. package/dist/esm/next-sourcemaps-cli.d.ts +2 -0
  46. package/dist/esm/next-sourcemaps-cli.js +7 -0
  47. package/dist/esm/scrub.d.ts +7 -0
  48. package/dist/esm/scrub.js +34 -0
  49. package/dist/esm/shutdown.d.ts +14 -0
  50. package/dist/esm/shutdown.js +23 -0
  51. package/dist/esm/sql.js +11 -1
  52. package/dist/esm/version.d.ts +2 -0
  53. package/dist/esm/version.js +2 -0
  54. package/package.json +8 -1
@@ -0,0 +1,49 @@
1
+ // First-party tunnel for browser telemetry (ad blockers block third-party telemetry hosts).
2
+ // app/api/wt/[...path]/route.ts: export const { GET, POST } = tunnelHandler({ endpoint: process.env.WT_EDGE_URL! });
3
+ // Browser SDK: init({ endpoint: '/api/wt', publishableKey: 'wtp_...' }).
4
+ // only what the browser SDK calls: /v1/rum/*, /v1/replay/<session>/chunks and /v1/logs
5
+ const ALLOWED = /^\/v1\/(rum\/[A-Za-z0-9/_-]+|replay\/[A-Za-z0-9_-]+\/chunks|logs)$/;
6
+ const FORWARD = ['content-type', 'content-encoding', 'user-agent'];
7
+ /** Next route handlers `{ GET, POST }` forwarding allow-listed RUM paths to the edge. Only the publishable key (`?k=` or a `wtp_` bearer) is forwarded as auth; cookies and other headers are not. */
8
+ export function tunnelHandler(o) {
9
+ const base = (o.basePath ?? '/api/wt').replace(/\/+$/, '');
10
+ const max = o.maxBodyBytes ?? 1024 * 1024;
11
+ const edge = o.endpoint.replace(/\/+$/, '');
12
+ const send = o.fetch ?? fetch;
13
+ const handle = async (req) => {
14
+ const url = new URL(req.url);
15
+ const path = url.pathname.startsWith(base) ? url.pathname.slice(base.length) : url.pathname;
16
+ if (!ALLOWED.test(path) || path.includes('..'))
17
+ return new Response('not found', { status: 404 });
18
+ const headers = {};
19
+ for (const h of FORWARD) {
20
+ const v = req.headers.get(h);
21
+ if (v)
22
+ headers[h] = v;
23
+ }
24
+ const bearer = /^Bearer (wtp_\S+)$/.exec(req.headers.get('authorization') ?? '');
25
+ if (bearer)
26
+ headers.authorization = `Bearer ${bearer[1]}`;
27
+ const k = url.searchParams.get('k');
28
+ let body;
29
+ if (req.method === 'POST') {
30
+ if (Number(req.headers.get('content-length') ?? 0) > max)
31
+ return new Response('payload too large', { status: 413 });
32
+ body = await req.arrayBuffer();
33
+ if (body.byteLength > max)
34
+ return new Response('payload too large', { status: 413 });
35
+ }
36
+ try {
37
+ const res = await send(`${edge}${path}${k ? `?k=${encodeURIComponent(k)}` : ''}`, { method: req.method, headers, body });
38
+ const out = new Headers();
39
+ const ct = res.headers.get('content-type');
40
+ if (ct)
41
+ out.set('content-type', ct);
42
+ return new Response(res.status === 204 ? null : await res.arrayBuffer(), { status: res.status, headers: out });
43
+ }
44
+ catch {
45
+ return new Response('bad gateway', { status: 502 });
46
+ }
47
+ };
48
+ return { GET: handle, POST: handle };
49
+ }
@@ -1,6 +1,26 @@
1
+ import type { ReadableSpan } from '@opentelemetry/sdk-trace';
1
2
  import * as wt from '../index.js';
3
+ export { withWatchtower, uploadSourcemaps } from './next-build.js';
4
+ export type { SourcemapOptions } from './next-build.js';
5
+ export { tunnelHandler } from './next-tunnel.js';
6
+ export type { TunnelOptions } from './next-tunnel.js';
7
+ export interface NextOptions extends wt.InitOptions {
8
+ /**
9
+ * Next.js's own spans (`next.span_type` set: load route module, prepare route module, resolve page components, start
10
+ * response, middleware, generateMetadata ...). `"sample"` (default) keeps `nextInternalsRate` of them, `"drop"` none,
11
+ * `"keep"` all. Request (`BaseServer.handleRequest`), route handler, render, server action and fetch spans, every
12
+ * SERVER/CLIENT span and every errored span are always kept. A dropped span's children stay in the trace.
13
+ */
14
+ nextInternals?: 'drop' | 'sample' | 'keep';
15
+ /** Share of internal spans kept with `nextInternals: "sample"`; default 0.1. */
16
+ nextInternalsRate?: number;
17
+ }
18
+ /** Whether `span` is one of Next's internal spans (not a request, route handler, render, server action or fetch). */
19
+ export declare function isNextInternal(span: ReadableSpan): boolean;
20
+ /** The `spanFilter` for a `nextInternals` mode; `rand` is injectable for tests. */
21
+ export declare function nextInternalsFilter(mode?: 'drop' | 'sample' | 'keep', rate?: number, rand?: () => number): (s: ReadableSpan) => boolean;
2
22
  /** Build a Next.js instrumentation `register` that passes `options` to `init`; no-op off the Node.js runtime or without a key. */
3
- export declare function makeRegister(options?: wt.InitOptions): () => Promise<void>;
23
+ export declare function makeRegister(options?: NextOptions): () => Promise<void>;
4
24
  /** Next.js instrumentation hook: init from WT_* env on the Node.js runtime; no-op without WT_INGEST_KEY. */
5
25
  export declare const register: () => Promise<void>;
6
26
  /** Next.js onRequestError: server render, route handler and server action errors. */
@@ -1,13 +1,34 @@
1
1
  // instrumentation.js: module.exports = require('@watchtowerobservability/node/next');
2
2
  // or, with options: export const register = makeRegister({ redact }); export { onRequestError } from '@watchtowerobservability/node/next';
3
+ import { SpanStatusCode } from '@opentelemetry/api';
3
4
  import * as wt from '../index.js';
5
+ export { withWatchtower, uploadSourcemaps } from './next-build.js';
6
+ export { tunnelHandler } from './next-tunnel.js';
7
+ const KEEP_TYPES = new Set([
8
+ 'BaseServer.handleRequest', 'AppRouteRouteHandlers.runHandler', 'AppRender.fetch', 'AppRender.getBodyResult',
9
+ 'Render.getServerSideProps', 'Render.getStaticProps', 'NextNodeServer.runApi',
10
+ ]);
11
+ const KEEP_NAMES = /^(render route|executing api route|fetch |.*server action)/i;
12
+ /** Whether `span` is one of Next's internal spans (not a request, route handler, render, server action or fetch). */
13
+ export function isNextInternal(span) {
14
+ const type = span.attributes['next.span_type'];
15
+ if (typeof type !== 'string')
16
+ return false;
17
+ return !KEEP_TYPES.has(type) && !KEEP_NAMES.test(span.name) && span.kind !== 1 && span.kind !== 2 && span.status.code !== SpanStatusCode.ERROR;
18
+ }
19
+ /** The `spanFilter` for a `nextInternals` mode; `rand` is injectable for tests. */
20
+ export function nextInternalsFilter(mode = 'sample', rate = 0.1, rand = Math.random) {
21
+ return (s) => mode === 'keep' || !isNextInternal(s) || (mode === 'sample' && rand() < rate);
22
+ }
4
23
  /** Build a Next.js instrumentation `register` that passes `options` to `init`; no-op off the Node.js runtime or without a key. */
5
24
  export function makeRegister(options = {}) {
6
25
  return async function register() {
7
26
  const rt = process.env.NEXT_RUNTIME;
8
27
  if ((rt && rt !== 'nodejs') || !(options.apiKey || process.env.WT_INGEST_KEY))
9
28
  return;
10
- wt.init(options);
29
+ const { nextInternals, nextInternalsRate, ...init } = options;
30
+ const own = nextInternalsFilter(nextInternals, nextInternalsRate);
31
+ wt.init({ ...init, spanFilter: init.spanFilter ? (s) => own(s) && init.spanFilter(s) : own });
11
32
  };
12
33
  }
13
34
  /** Next.js instrumentation hook: init from WT_* env on the Node.js runtime; no-op without WT_INGEST_KEY. */
package/dist/esm/load.js CHANGED
@@ -20,6 +20,7 @@ try { pre["@opentelemetry/instrumentation-http"] = await import("@opentelemetry/
20
20
  try { pre["@opentelemetry/instrumentation-undici"] = await import("@opentelemetry/instrumentation-undici"); } catch (e) { pre["@opentelemetry/instrumentation-undici"] = e; }
21
21
  try { pre["@opentelemetry/instrumentation-pg"] = await import("@opentelemetry/instrumentation-pg"); } catch (e) { pre["@opentelemetry/instrumentation-pg"] = e; }
22
22
  try { pre["@opentelemetry/instrumentation-ioredis"] = await import("@opentelemetry/instrumentation-ioredis"); } catch (e) { pre["@opentelemetry/instrumentation-ioredis"] = e; }
23
+ try { pre["@opentelemetry/instrumentation-mysql2"] = await import("@opentelemetry/instrumentation-mysql2"); } catch (e) { pre["@opentelemetry/instrumentation-mysql2"] = e; }
23
24
  try { pre["winston-transport"] = await import("winston-transport"); } catch (e) { pre["winston-transport"] = e; }
24
25
  const nodeRequire = createRequire(import.meta.url);
25
26
  const require = (id) => {
@@ -48,6 +49,7 @@ function known(id) {
48
49
  case '@opentelemetry/instrumentation-undici': return require('@opentelemetry/instrumentation-undici');
49
50
  case '@opentelemetry/instrumentation-pg': return require('@opentelemetry/instrumentation-pg');
50
51
  case '@opentelemetry/instrumentation-ioredis': return require('@opentelemetry/instrumentation-ioredis');
52
+ case '@opentelemetry/instrumentation-mysql2': return require('@opentelemetry/instrumentation-mysql2');
51
53
  case 'winston-transport': return require('winston-transport');
52
54
  default: return NONE;
53
55
  }
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env node
2
+ // `wt-next-sourcemaps [dir]`: upload .next/static source maps after `next build` (env: WT_API_KEY, WT_PROJECT, WT_RELEASE, WT_API_URL), then delete them.
3
+ import { uploadSourcemaps } from './integrations/next-build.js';
4
+ uploadSourcemaps({ dir: process.argv[2], log: (l) => console.log(`[watchtower] ${l}`) }).catch((e) => {
5
+ console.error(`[watchtower] source map upload failed: ${e.message}`);
6
+ process.exit(1);
7
+ });
@@ -0,0 +1,7 @@
1
+ import type { SpanProcessor } from '@opentelemetry/sdk-trace';
2
+ /** A path segment that reads like a secret or an opaque id: >= 24 chars, or >= 10 chars mixing a letter and a digit (UUIDs included). */
3
+ export declare function looksSecret(seg: string): boolean;
4
+ /** Drop the query string and fragment, strip `user:pass@`, and replace secret-looking path segments with `:redacted`. */
5
+ export declare function scrubUrl(value: string): string;
6
+ /** Span processor: scrubs URL attributes (query dropped, `url.query` removed, secret path segments redacted) and URLs inside the span name. `http.route` is kept. */
7
+ export declare function urlScrubber(): SpanProcessor;
@@ -0,0 +1,34 @@
1
+ const URL_KEYS = ['http.target', 'url.full', 'url.path', 'http.url', 'http.request.url'];
2
+ const QUERY_KEYS = ['url.query'];
3
+ /** A path segment that reads like a secret or an opaque id: >= 24 chars, or >= 10 chars mixing a letter and a digit (UUIDs included). */
4
+ export function looksSecret(seg) {
5
+ return seg.length >= 24 || (seg.length >= 10 && /[A-Za-z]/.test(seg) && /\d/.test(seg));
6
+ }
7
+ /** Drop the query string and fragment, strip `user:pass@`, and replace secret-looking path segments with `:redacted`. */
8
+ export function scrubUrl(value) {
9
+ const m = /^(?:([a-z][a-z0-9+.-]*:\/\/)([^/?#]*))?([^?#]*)/i.exec(value);
10
+ const origin = m[1] ? m[1] + m[2].replace(/^[^@]*@/, '') : '';
11
+ return origin + m[3].split('/').map((s) => (looksSecret(s) ? ':redacted' : s)).join('/');
12
+ }
13
+ function scrubName(name) {
14
+ return name.split(' ').map((t) => (/^(https?:\/\/|\/)/i.test(t) ? scrubUrl(t) : t)).join(' ');
15
+ }
16
+ /** Span processor: scrubs URL attributes (query dropped, `url.query` removed, secret path segments redacted) and URLs inside the span name. `http.route` is kept. */
17
+ export function urlScrubber() {
18
+ return {
19
+ onStart() { },
20
+ onEnd(span) {
21
+ const a = span.attributes;
22
+ for (const k of URL_KEYS)
23
+ if (typeof a[k] === 'string')
24
+ a[k] = scrubUrl(a[k]);
25
+ for (const k of QUERY_KEYS)
26
+ delete a[k];
27
+ const n = scrubName(span.name);
28
+ if (n !== span.name)
29
+ span.name = n;
30
+ },
31
+ shutdown: () => Promise.resolve(),
32
+ forceFlush: () => Promise.resolve(),
33
+ };
34
+ }
@@ -0,0 +1,14 @@
1
+ export interface SignalProc {
2
+ on(event: string, l: () => void): unknown;
3
+ off(event: string, l: () => void): unknown;
4
+ listenerCount(event: string): number;
5
+ kill(pid: number, signal: string): unknown;
6
+ readonly pid: number;
7
+ }
8
+ /**
9
+ * On SIGTERM and SIGINT: flush (at most `timeoutMs`). Adding a signal listener turns off Node's default exit, so when
10
+ * ours is the only listener it removes itself and re-sends the signal after the flush; when the app or a framework
11
+ * (`next start`) has its own handler, that handler decides how the process ends and ours only starts the flush.
12
+ * Returns the uninstall function.
13
+ */
14
+ export declare function installSignalFlush(flush: () => Promise<unknown>, timeoutMs?: number, proc?: SignalProc): () => void;
@@ -0,0 +1,23 @@
1
+ // Flush on SIGTERM / SIGINT without changing how the process ends.
2
+ /**
3
+ * On SIGTERM and SIGINT: flush (at most `timeoutMs`). Adding a signal listener turns off Node's default exit, so when
4
+ * ours is the only listener it removes itself and re-sends the signal after the flush; when the app or a framework
5
+ * (`next start`) has its own handler, that handler decides how the process ends and ours only starts the flush.
6
+ * Returns the uninstall function.
7
+ */
8
+ export function installSignalFlush(flush, timeoutMs = 2000, proc = process) {
9
+ const handlers = new Map();
10
+ for (const sig of ['SIGTERM', 'SIGINT']) {
11
+ const h = () => {
12
+ const alone = proc.listenerCount(sig) <= 1;
13
+ const done = Promise.race([flush().catch(() => undefined), new Promise((r) => setTimeout(r, timeoutMs).unref())]);
14
+ if (!alone)
15
+ return;
16
+ void done.finally(() => { proc.off(sig, h); proc.kill(proc.pid, sig); });
17
+ };
18
+ handlers.set(sig, h);
19
+ proc.on(sig, h);
20
+ }
21
+ return () => { for (const [sig, h] of handlers)
22
+ proc.off(sig, h); };
23
+ }
package/dist/esm/sql.js CHANGED
@@ -204,9 +204,19 @@ export function normalizeAndHash(stmt, pg = false) {
204
204
  /** Systems whose dialect keeps `"identifier"` quotes and the `#` operator (as the worker decides). */
205
205
  export const isPgDialect = (system) => system === 'postgresql' || system === 'cockroachdb' || system === 'redshift';
206
206
  const OP = /^\s*(?:with\b[\s\S]*?\)\s*)?([a-z]+)/i;
207
- const TARGET = /\b(?:from|into|update|join|table)\s+((?:"[^"]+"|[\w$]+)(?:\.(?:"[^"]+"|[\w$]+))?)/i;
207
+ const NAME = '(?:"[^"]+"|[\\w$]+)(?:\\.(?:"[^"]+"|[\\w$]+))?';
208
+ const TARGET = new RegExp(`\\b(?:from|into|update|join|table)\\s+(?:if\\s+(?:not\\s+)?exists\\s+)?(${NAME})`, 'i');
209
+ // DDL: `CREATE [TEMP|VIRTUAL|UNIQUE...] TABLE|INDEX|VIEW [IF NOT EXISTS] name [ON table]`; an index is named after its table
210
+ const CREATE = new RegExp(`^\\s*create\\s+(?:(?:temp|temporary|virtual|unique|fulltext|spatial|clustered|nonclustered|materialized|or\\s+replace)\\s+)*(table|index|view)\\s+(?:if\\s+not\\s+exists\\s+)?(?:concurrently\\s+)?(${NAME})(?:\\s+on\\s+(?:only\\s+)?(${NAME}))?`, 'i');
208
211
  /** `SELECT users`-style summary: the operation and first table, never values or the statement text. */
209
212
  export function querySummary(text, fallback = 'postgres') {
213
+ const ddl = text?.match(CREATE);
214
+ if (ddl) {
215
+ const kind = ddl[1].toLowerCase();
216
+ const operation = `CREATE ${kind.toUpperCase()}`;
217
+ const target = (kind === 'index' ? ddl[3] ?? ddl[2] : ddl[2]).replace(/"/g, '');
218
+ return { operation, target, summary: `${operation} ${target}` };
219
+ }
210
220
  const operation = text?.match(OP)?.[1]?.toUpperCase();
211
221
  const target = text?.match(TARGET)?.[1]?.replace(/"/g, '');
212
222
  return { operation, target, summary: [operation ?? fallback, target].filter(Boolean).join(' ') };
@@ -0,0 +1,2 @@
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.0";
@@ -0,0 +1,2 @@
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.0';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@watchtowerobservability/node",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/watchtowerobservability/watchtower-node.git"
@@ -13,6 +13,9 @@
13
13
  "main": "./dist/cjs/index.js",
14
14
  "module": "./dist/esm/index.js",
15
15
  "types": "./dist/cjs/index.d.ts",
16
+ "bin": {
17
+ "wt-next-sourcemaps": "./dist/cjs/next-sourcemaps-cli.js"
18
+ },
16
19
  "files": [
17
20
  "dist"
18
21
  ],
@@ -381,6 +384,7 @@
381
384
  "@opentelemetry/instrumentation": ">=0.200.0",
382
385
  "@opentelemetry/instrumentation-http": ">=0.200.0",
383
386
  "@opentelemetry/instrumentation-ioredis": ">=0.50.0",
387
+ "@opentelemetry/instrumentation-mysql2": ">=0.40.0",
384
388
  "@opentelemetry/instrumentation-pg": ">=0.50.0",
385
389
  "@opentelemetry/instrumentation-runtime-node": ">=0.38.0",
386
390
  "@opentelemetry/instrumentation-undici": ">=0.10.0",
@@ -416,6 +420,9 @@
416
420
  "@opentelemetry/instrumentation-ioredis": {
417
421
  "optional": true
418
422
  },
423
+ "@opentelemetry/instrumentation-mysql2": {
424
+ "optional": true
425
+ },
419
426
  "@opentelemetry/instrumentation-pg": {
420
427
  "optional": true
421
428
  },