@watchtowerobservability/node 0.2.0 → 0.3.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.
- package/README.md +186 -3
- package/dist/cjs/appsec.d.ts +95 -0
- package/dist/cjs/appsec.js +206 -0
- package/dist/cjs/index.d.ts +3 -0
- package/dist/cjs/index.js +14 -1
- package/dist/cjs/integrations/amqp.d.ts +13 -0
- package/dist/cjs/integrations/amqp.js +96 -0
- package/dist/cjs/integrations/bullmq.d.ts +19 -0
- package/dist/cjs/integrations/bullmq.js +83 -0
- package/dist/cjs/integrations/cache.d.ts +13 -0
- package/dist/cjs/integrations/cache.js +51 -0
- package/dist/cjs/integrations/db.d.ts +62 -0
- package/dist/cjs/integrations/db.js +288 -0
- package/dist/cjs/integrations/duckdb.d.ts +22 -0
- package/dist/cjs/integrations/duckdb.js +66 -0
- package/dist/cjs/integrations/graphile-worker.d.ts +22 -0
- package/dist/cjs/integrations/graphile-worker.js +50 -0
- package/dist/cjs/integrations/level.d.ts +18 -0
- package/dist/cjs/integrations/level.js +71 -0
- package/dist/cjs/integrations/lmdb.d.ts +26 -0
- package/dist/cjs/integrations/lmdb.js +78 -0
- package/dist/cjs/integrations/messaging.d.ts +17 -0
- package/dist/cjs/integrations/messaging.js +73 -0
- package/dist/cjs/integrations/mqtt.d.ts +7 -0
- package/dist/cjs/integrations/mqtt.js +41 -0
- package/dist/cjs/integrations/nats.d.ts +17 -0
- package/dist/cjs/integrations/nats.js +73 -0
- package/dist/cjs/integrations/orm.d.ts +47 -0
- package/dist/cjs/integrations/orm.js +93 -0
- package/dist/cjs/integrations/pg-boss.d.ts +8 -0
- package/dist/cjs/integrations/pg-boss.js +63 -0
- package/dist/cjs/integrations/pools.d.ts +41 -0
- package/dist/cjs/integrations/pools.js +94 -0
- package/dist/cjs/integrations/postgres.d.ts +12 -0
- package/dist/cjs/integrations/postgres.js +92 -0
- package/dist/cjs/integrations/sqlite.d.ts +36 -0
- package/dist/cjs/integrations/sqlite.js +125 -0
- package/dist/cjs/pprof.d.ts +6 -0
- package/dist/cjs/pprof.js +88 -0
- package/dist/cjs/profiling.d.ts +4 -1
- package/dist/cjs/profiling.js +18 -8
- package/dist/cjs/sql.d.ts +19 -0
- package/dist/cjs/sql.js +332 -0
- package/dist/cjs/test-plan.d.ts +10 -1
- package/dist/cjs/test-plan.js +91 -1
- package/dist/esm/appsec.d.ts +95 -0
- package/dist/esm/appsec.js +193 -0
- package/dist/esm/index.d.ts +3 -0
- package/dist/esm/index.js +14 -1
- package/dist/esm/integrations/amqp.d.ts +13 -0
- package/dist/esm/integrations/amqp.js +92 -0
- package/dist/esm/integrations/bullmq.d.ts +19 -0
- package/dist/esm/integrations/bullmq.js +79 -0
- package/dist/esm/integrations/cache.d.ts +13 -0
- package/dist/esm/integrations/cache.js +46 -0
- package/dist/esm/integrations/db.d.ts +62 -0
- package/dist/esm/integrations/db.js +239 -0
- package/dist/esm/integrations/duckdb.d.ts +22 -0
- package/dist/esm/integrations/duckdb.js +29 -0
- package/dist/esm/integrations/graphile-worker.d.ts +22 -0
- package/dist/esm/integrations/graphile-worker.js +46 -0
- package/dist/esm/integrations/level.d.ts +18 -0
- package/dist/esm/integrations/level.js +35 -0
- package/dist/esm/integrations/lmdb.d.ts +26 -0
- package/dist/esm/integrations/lmdb.js +42 -0
- package/dist/esm/integrations/messaging.d.ts +17 -0
- package/dist/esm/integrations/messaging.js +64 -0
- package/dist/esm/integrations/mqtt.d.ts +7 -0
- package/dist/esm/integrations/mqtt.js +38 -0
- package/dist/esm/integrations/nats.d.ts +17 -0
- package/dist/esm/integrations/nats.js +70 -0
- package/dist/esm/integrations/orm.d.ts +47 -0
- package/dist/esm/integrations/orm.js +86 -0
- package/dist/esm/integrations/pg-boss.d.ts +8 -0
- package/dist/esm/integrations/pg-boss.js +60 -0
- package/dist/esm/integrations/pools.d.ts +41 -0
- package/dist/esm/integrations/pools.js +89 -0
- package/dist/esm/integrations/postgres.d.ts +12 -0
- package/dist/esm/integrations/postgres.js +55 -0
- package/dist/esm/integrations/sqlite.d.ts +36 -0
- package/dist/esm/integrations/sqlite.js +84 -0
- package/dist/esm/pprof.d.ts +6 -0
- package/dist/esm/pprof.js +85 -0
- package/dist/esm/profiling.d.ts +4 -1
- package/dist/esm/profiling.js +17 -8
- package/dist/esm/sql.d.ts +19 -0
- package/dist/esm/sql.js +324 -0
- package/dist/esm/test-plan.d.ts +10 -1
- package/dist/esm/test-plan.js +89 -1
- package/package.json +161 -1
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
export type Field = 'path' | 'query' | 'user_agent' | 'body' | 'headers';
|
|
2
|
+
export interface AppsecRule {
|
|
3
|
+
id: string;
|
|
4
|
+
name?: string;
|
|
5
|
+
category: string;
|
|
6
|
+
severity?: string;
|
|
7
|
+
fields: Field[];
|
|
8
|
+
pattern: string;
|
|
9
|
+
}
|
|
10
|
+
export interface DenyEntry {
|
|
11
|
+
value: string;
|
|
12
|
+
/** Unix seconds; null: no expiry. */
|
|
13
|
+
expires_at?: number | null;
|
|
14
|
+
}
|
|
15
|
+
export interface Ruleset {
|
|
16
|
+
mode: 'monitor' | 'block';
|
|
17
|
+
rules: AppsecRule[];
|
|
18
|
+
denylist?: {
|
|
19
|
+
ip?: DenyEntry[];
|
|
20
|
+
user?: DenyEntry[];
|
|
21
|
+
};
|
|
22
|
+
poll_s?: number;
|
|
23
|
+
}
|
|
24
|
+
/** One request as the inspector sees it (decoded). */
|
|
25
|
+
export interface Inspected {
|
|
26
|
+
path: string;
|
|
27
|
+
query?: string;
|
|
28
|
+
user_agent?: string;
|
|
29
|
+
body?: string;
|
|
30
|
+
headers?: string;
|
|
31
|
+
ip?: string;
|
|
32
|
+
user?: string;
|
|
33
|
+
}
|
|
34
|
+
export interface Decision {
|
|
35
|
+
/** `allow`: nothing matched; `monitor`: matched, let through; `block`: answer 403. */
|
|
36
|
+
action: 'allow' | 'monitor' | 'block';
|
|
37
|
+
rule_id?: string;
|
|
38
|
+
category?: string;
|
|
39
|
+
match?: string;
|
|
40
|
+
}
|
|
41
|
+
/** Rule id recorded for denylist blocks (the worker reports them as their own signal). */
|
|
42
|
+
export declare const DENYLIST = "denylist";
|
|
43
|
+
/** Replace the active ruleset (what the poller calls; tests may call it directly). */
|
|
44
|
+
export declare function setRuleset(r: Ruleset | null): void;
|
|
45
|
+
/** Whether `id` is on the user denylist (call after authentication to refuse a listed user). */
|
|
46
|
+
export declare const isUserBlocked: (id: string, now?: number) => boolean;
|
|
47
|
+
/** Judge one request. Denylisted callers are blocked in either mode. */
|
|
48
|
+
export declare function inspect(req: Inspected, now?: number): Decision;
|
|
49
|
+
type Headers = Record<string, string | string[] | undefined>;
|
|
50
|
+
/** The slice of a Node request the inspector reads. */
|
|
51
|
+
export interface NodeRequest {
|
|
52
|
+
url?: string;
|
|
53
|
+
originalUrl?: string;
|
|
54
|
+
headers?: Headers;
|
|
55
|
+
socket?: {
|
|
56
|
+
remoteAddress?: string;
|
|
57
|
+
};
|
|
58
|
+
body?: unknown;
|
|
59
|
+
}
|
|
60
|
+
/** The slice of a Node response used to answer 403. */
|
|
61
|
+
export interface NodeResponse {
|
|
62
|
+
statusCode: number;
|
|
63
|
+
setHeader?(name: string, value: string): unknown;
|
|
64
|
+
end(body?: string): unknown;
|
|
65
|
+
}
|
|
66
|
+
/** Client IP: first `X-Forwarded-For` hop, else `X-Real-IP`, else the socket peer.
|
|
67
|
+
* shortcut: trusts the forwarding headers; run behind a proxy that overwrites them. */
|
|
68
|
+
export declare function clientIp(req: NodeRequest): string | undefined;
|
|
69
|
+
/** Decode a Node request into the inspector's fields. */
|
|
70
|
+
export declare function fromNode(req: NodeRequest, user?: string): Inspected;
|
|
71
|
+
/** Record a decision on the active span. */
|
|
72
|
+
export declare function annotate(d: Decision, ip?: string): void;
|
|
73
|
+
/** Inspect and, when blocked, answer 403. Returns true when the request was answered. */
|
|
74
|
+
export declare function handleRequest(req: NodeRequest, res: NodeResponse, user?: string): boolean;
|
|
75
|
+
/** Connect-style middleware; `user(req)` names the caller when the app already knows it. */
|
|
76
|
+
export declare function appsecMiddleware(o?: {
|
|
77
|
+
user?: (req: NodeRequest) => string | undefined;
|
|
78
|
+
}): (req: NodeRequest, res: NodeResponse, next: (err?: unknown) => void) => void;
|
|
79
|
+
/** Mark the current request as a login attempt; failed ones feed the account-takeover rules. */
|
|
80
|
+
export declare function trackLogin(login: string, success: boolean): void;
|
|
81
|
+
export interface AppsecOptions {
|
|
82
|
+
/** API base; default `WT_API_URL` or https://api.watchtower.dev */
|
|
83
|
+
apiUrl?: string;
|
|
84
|
+
/** Ingestion key with scope `probes:read`; default `WT_INGEST_KEY`. */
|
|
85
|
+
key?: string;
|
|
86
|
+
/** Default `WT_SERVICE` or `OTEL_SERVICE_NAME`. */
|
|
87
|
+
service?: string;
|
|
88
|
+
fetch?: typeof fetch;
|
|
89
|
+
}
|
|
90
|
+
/** Fetch the ruleset now and keep polling; `stop()` ends the polling. */
|
|
91
|
+
export declare function startAppsec(o?: AppsecOptions): {
|
|
92
|
+
ready: Promise<void>;
|
|
93
|
+
stop(): void;
|
|
94
|
+
};
|
|
95
|
+
export {};
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
// App and API Protection in-app WAF (W52). `startAppsec()` polls the project's ruleset from the
|
|
2
|
+
// edge (`GET /v1/appsec/ruleset`, an ingestion key with scope `probes:read`, ETag, every `poll_s`):
|
|
3
|
+
// mode, enabled pattern rules and the denylist. `appsecMiddleware()` (Express, Connect, any
|
|
4
|
+
// `(req, res, next)` stack) or `handleRequest(req, res)` (plain `http`, Fastify `onRequest`, Koa)
|
|
5
|
+
// inspects each request before the handler: path, query, User-Agent, other header values and an
|
|
6
|
+
// already parsed body, percent-decoded once, against the same rules the worker runs over stored
|
|
7
|
+
// spans. A match is recorded on the active server span (`appsec.event`, `appsec.rule_id`,
|
|
8
|
+
// `appsec.category`, `appsec.blocked`); in `block` mode, and for denylisted IPs or users in any
|
|
9
|
+
// mode, the request is answered with 403 problem+json and the handler never runs.
|
|
10
|
+
// Nothing is fetched or inspected until `startAppsec` is called; with no ruleset yet every
|
|
11
|
+
// request passes. Rules are matched case-insensitively; a rule that does not compile is skipped.
|
|
12
|
+
import { trace } from '@opentelemetry/api';
|
|
13
|
+
/** Rule id recorded for denylist blocks (the worker reports them as their own signal). */
|
|
14
|
+
export const DENYLIST = 'denylist';
|
|
15
|
+
const BODY_MAX = 64 * 1024;
|
|
16
|
+
let mode = 'monitor';
|
|
17
|
+
let compiled = [];
|
|
18
|
+
let denyIp = new Map();
|
|
19
|
+
let denyUser = new Map();
|
|
20
|
+
const expiryMap = (list) => new Map((list || []).map((e) => [e.value, e.expires_at ? e.expires_at * 1000 : Infinity]));
|
|
21
|
+
/** Replace the active ruleset (what the poller calls; tests may call it directly). */
|
|
22
|
+
export function setRuleset(r) {
|
|
23
|
+
mode = r?.mode === 'block' ? 'block' : 'monitor';
|
|
24
|
+
compiled = [];
|
|
25
|
+
for (const rule of r?.rules || []) {
|
|
26
|
+
try {
|
|
27
|
+
compiled.push({ rule, re: new RegExp(rule.pattern, 'i') });
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
// not portable to JavaScript: skipped here, the worker still evaluates it
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
denyIp = expiryMap(r?.denylist?.ip);
|
|
34
|
+
denyUser = expiryMap(r?.denylist?.user);
|
|
35
|
+
}
|
|
36
|
+
const listed = (m, v, now) => !!v && (m.get(v) ?? 0) > now;
|
|
37
|
+
/** Whether `id` is on the user denylist (call after authentication to refuse a listed user). */
|
|
38
|
+
export const isUserBlocked = (id, now = Date.now()) => listed(denyUser, id, now);
|
|
39
|
+
/** Judge one request. Denylisted callers are blocked in either mode. */
|
|
40
|
+
export function inspect(req, now = Date.now()) {
|
|
41
|
+
if (listed(denyIp, req.ip, now) || listed(denyUser, req.user, now)) {
|
|
42
|
+
return { action: 'block', rule_id: DENYLIST, category: 'abuse' };
|
|
43
|
+
}
|
|
44
|
+
for (const { rule, re } of compiled) {
|
|
45
|
+
for (const f of rule.fields) {
|
|
46
|
+
const v = req[f];
|
|
47
|
+
if (!v)
|
|
48
|
+
continue;
|
|
49
|
+
const m = re.exec(v);
|
|
50
|
+
if (m) {
|
|
51
|
+
return { action: mode === 'block' ? 'block' : 'monitor', rule_id: rule.id, category: rule.category, match: m[0].slice(0, 200) };
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return { action: 'allow' };
|
|
56
|
+
}
|
|
57
|
+
const decode = (s, form = false) => {
|
|
58
|
+
const x = form ? s.replace(/\+/g, ' ') : s;
|
|
59
|
+
try {
|
|
60
|
+
return decodeURIComponent(x);
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return x;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
const first = (v) => (Array.isArray(v) ? v[0] : v);
|
|
67
|
+
/** Client IP: first `X-Forwarded-For` hop, else `X-Real-IP`, else the socket peer.
|
|
68
|
+
* shortcut: trusts the forwarding headers; run behind a proxy that overwrites them. */
|
|
69
|
+
export function clientIp(req) {
|
|
70
|
+
const xff = first(req.headers?.['x-forwarded-for']);
|
|
71
|
+
if (xff)
|
|
72
|
+
return xff.split(',')[0].trim();
|
|
73
|
+
return first(req.headers?.['x-real-ip']) || req.socket?.remoteAddress?.replace(/^::ffff:/, '');
|
|
74
|
+
}
|
|
75
|
+
/** Decode a Node request into the inspector's fields. */
|
|
76
|
+
export function fromNode(req, user) {
|
|
77
|
+
const url = req.originalUrl || req.url || '/';
|
|
78
|
+
const q = url.indexOf('?');
|
|
79
|
+
const headers = Object.entries(req.headers || {})
|
|
80
|
+
.filter(([k]) => k !== 'user-agent')
|
|
81
|
+
.map(([, v]) => (Array.isArray(v) ? v.join(', ') : v || ''))
|
|
82
|
+
.join('\n');
|
|
83
|
+
let body;
|
|
84
|
+
if (typeof req.body === 'string')
|
|
85
|
+
body = req.body.slice(0, BODY_MAX);
|
|
86
|
+
else if (req.body && typeof req.body === 'object') {
|
|
87
|
+
try {
|
|
88
|
+
body = JSON.stringify(req.body).slice(0, BODY_MAX);
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
body = undefined;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
path: decode(q < 0 ? url : url.slice(0, q)),
|
|
96
|
+
query: q < 0 ? '' : decode(url.slice(q + 1), true),
|
|
97
|
+
user_agent: first(req.headers?.['user-agent']) || '',
|
|
98
|
+
headers,
|
|
99
|
+
body,
|
|
100
|
+
ip: clientIp(req),
|
|
101
|
+
user,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
/** Record a decision on the active span. */
|
|
105
|
+
export function annotate(d, ip) {
|
|
106
|
+
const span = trace.getActiveSpan();
|
|
107
|
+
if (!span)
|
|
108
|
+
return;
|
|
109
|
+
if (ip)
|
|
110
|
+
span.setAttribute('appsec.client_ip', ip);
|
|
111
|
+
if (d.action === 'allow')
|
|
112
|
+
return;
|
|
113
|
+
span.setAttributes({
|
|
114
|
+
'appsec.event': true,
|
|
115
|
+
'appsec.rule_id': d.rule_id || '',
|
|
116
|
+
'appsec.category': d.category || '',
|
|
117
|
+
'appsec.blocked': d.action === 'block',
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
const PROBLEM = JSON.stringify({
|
|
121
|
+
type: 'about:blank',
|
|
122
|
+
title: 'Forbidden',
|
|
123
|
+
status: 403,
|
|
124
|
+
detail: 'Request blocked by Watchtower App and API Protection',
|
|
125
|
+
});
|
|
126
|
+
/** Inspect and, when blocked, answer 403. Returns true when the request was answered. */
|
|
127
|
+
export function handleRequest(req, res, user) {
|
|
128
|
+
if (!compiled.length && !denyIp.size && !denyUser.size)
|
|
129
|
+
return false;
|
|
130
|
+
const r = fromNode(req, user);
|
|
131
|
+
const d = inspect(r);
|
|
132
|
+
annotate(d, r.ip);
|
|
133
|
+
if (d.action !== 'block')
|
|
134
|
+
return false;
|
|
135
|
+
res.statusCode = 403;
|
|
136
|
+
res.setHeader?.('content-type', 'application/problem+json');
|
|
137
|
+
res.end(PROBLEM);
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
/** Connect-style middleware; `user(req)` names the caller when the app already knows it. */
|
|
141
|
+
export function appsecMiddleware(o = {}) {
|
|
142
|
+
return (req, res, next) => {
|
|
143
|
+
if (!handleRequest(req, res, o.user?.(req)))
|
|
144
|
+
next();
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
/** Mark the current request as a login attempt; failed ones feed the account-takeover rules. */
|
|
148
|
+
export function trackLogin(login, success) {
|
|
149
|
+
const span = trace.getActiveSpan();
|
|
150
|
+
span?.setAttributes({ 'appsec.login': login, 'appsec.login.success': success });
|
|
151
|
+
if (success)
|
|
152
|
+
span?.setAttribute('appsec.auth', true);
|
|
153
|
+
}
|
|
154
|
+
/** Fetch the ruleset now and keep polling; `stop()` ends the polling. */
|
|
155
|
+
export function startAppsec(o = {}) {
|
|
156
|
+
const env = process.env;
|
|
157
|
+
const base = (o.apiUrl || env.WT_API_URL || 'https://api.watchtower.dev').replace(/\/$/, '');
|
|
158
|
+
const key = o.key || env.WT_INGEST_KEY || '';
|
|
159
|
+
const service = o.service || env.WT_SERVICE || env.OTEL_SERVICE_NAME || 'unknown';
|
|
160
|
+
const f = o.fetch || globalThis.fetch;
|
|
161
|
+
let etag = '';
|
|
162
|
+
let timer;
|
|
163
|
+
let stopped = false;
|
|
164
|
+
let pollS = 30;
|
|
165
|
+
const poll = async () => {
|
|
166
|
+
try {
|
|
167
|
+
const res = await f(`${base}/v1/appsec/ruleset?service=${encodeURIComponent(service)}`, {
|
|
168
|
+
headers: { Authorization: `Bearer ${key}`, ...(etag ? { 'If-None-Match': etag } : {}) },
|
|
169
|
+
});
|
|
170
|
+
if (res.status === 200) {
|
|
171
|
+
const body = (await res.json());
|
|
172
|
+
setRuleset(body);
|
|
173
|
+
etag = res.headers.get('etag') || '';
|
|
174
|
+
pollS = Math.max(10, body.poll_s || 30);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
catch {
|
|
178
|
+
// keep the last ruleset; try again on the next poll
|
|
179
|
+
}
|
|
180
|
+
if (!stopped) {
|
|
181
|
+
timer = setTimeout(() => void poll(), pollS * 1000);
|
|
182
|
+
timer.unref?.();
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
return {
|
|
186
|
+
ready: poll(),
|
|
187
|
+
stop() {
|
|
188
|
+
stopped = true;
|
|
189
|
+
if (timer)
|
|
190
|
+
clearTimeout(timer);
|
|
191
|
+
},
|
|
192
|
+
};
|
|
193
|
+
}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { LoggerProvider } from '@opentelemetry/sdk-logs';
|
|
|
6
6
|
import type { MeterProvider } from '@opentelemetry/sdk-metrics';
|
|
7
7
|
import { captureException } from './errors.js';
|
|
8
8
|
import type { CaptureContext } from './errors.js';
|
|
9
|
+
import type { Profiler } from './profiling.js';
|
|
9
10
|
export { captureException, metrics, trace };
|
|
10
11
|
export { setCaptureRules, capturePayload } from './payload.js';
|
|
11
12
|
export type { CaptureRule } from './payload.js';
|
|
@@ -86,6 +87,8 @@ export declare class Client {
|
|
|
86
87
|
readonly meterProvider?: MeterProvider;
|
|
87
88
|
private readonly stopRuntime;
|
|
88
89
|
uninstall?: () => void;
|
|
90
|
+
/** Set when WT_PROFILING=1 started the continuous profiler. */
|
|
91
|
+
profiler?: Promise<Profiler | undefined>;
|
|
89
92
|
constructor(opts: InitOptions);
|
|
90
93
|
/** Emit an OTLP log record; trace and span ids come from the active context. */
|
|
91
94
|
log(level: LogLevel, body: string, attributes?: Record<string, unknown>): void;
|
package/dist/esm/index.js
CHANGED
|
@@ -13,6 +13,7 @@ import { startMetrics, switchOn } from './metrics.js';
|
|
|
13
13
|
import * as nodePath from 'node:path';
|
|
14
14
|
import { captureException, installProcessHandlers, dropReserved } from './errors.js';
|
|
15
15
|
import { loadCaptureRules } from './payload.js';
|
|
16
|
+
import { startProfiling } from './profiling.js';
|
|
16
17
|
export { captureException, metrics, trace };
|
|
17
18
|
export { setCaptureRules, capturePayload } from './payload.js';
|
|
18
19
|
export const DEFAULT_ENDPOINT = 'http://localhost:4318';
|
|
@@ -37,6 +38,8 @@ export class Client {
|
|
|
37
38
|
meterProvider;
|
|
38
39
|
stopRuntime = () => { };
|
|
39
40
|
uninstall;
|
|
41
|
+
/** Set when WT_PROFILING=1 started the continuous profiler. */
|
|
42
|
+
profiler;
|
|
40
43
|
constructor(opts) {
|
|
41
44
|
const o = opts || {};
|
|
42
45
|
const env = process.env;
|
|
@@ -206,7 +209,7 @@ export class Client {
|
|
|
206
209
|
}
|
|
207
210
|
shutdown() {
|
|
208
211
|
this.stopRuntime();
|
|
209
|
-
return Promise.all([this.provider.shutdown(), this.loggerProvider.shutdown(), this.meterProvider?.shutdown()]).then(() => undefined);
|
|
212
|
+
return Promise.all([this.profiler?.then((p) => p?.stop()), this.provider.shutdown(), this.loggerProvider.shutdown(), this.meterProvider?.shutdown()]).then(() => undefined);
|
|
210
213
|
}
|
|
211
214
|
}
|
|
212
215
|
function redactor(hook) {
|
|
@@ -224,6 +227,7 @@ let current;
|
|
|
224
227
|
export function init(opts) {
|
|
225
228
|
if (current && current.uninstall)
|
|
226
229
|
current.uninstall();
|
|
230
|
+
void current?.profiler?.then((p) => p?.stop());
|
|
227
231
|
const c = new Client(opts);
|
|
228
232
|
current = c;
|
|
229
233
|
c.provider.register(); // global tracer + async context so trace.getActiveSpan() works
|
|
@@ -231,6 +235,15 @@ export function init(opts) {
|
|
|
231
235
|
metrics.disable(); // a re-init replaces the previous client's provider
|
|
232
236
|
metrics.setGlobalMeterProvider(c.meterProvider);
|
|
233
237
|
}
|
|
238
|
+
if (process.env.WT_PROFILING === '1') {
|
|
239
|
+
const attrs = c.provider.resource.attributes;
|
|
240
|
+
c.profiler = startProfiling({
|
|
241
|
+
apiKey: c.headers.Authorization.slice('Bearer '.length),
|
|
242
|
+
endpoint: c.endpoint,
|
|
243
|
+
service: String(attrs['service.name']),
|
|
244
|
+
tags: { version: attrs['service.version'] },
|
|
245
|
+
}).catch(() => undefined); // best effort: never break init
|
|
246
|
+
}
|
|
234
247
|
if ((opts || {}).captureProcessErrors !== false) {
|
|
235
248
|
c.uninstall = installProcessHandlers(() => c.forceFlush());
|
|
236
249
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `instrumentAmqplib(await conn.createChannel())`: patches that channel's class (every channel of
|
|
3
|
+
* it): PRODUCER spans on `publish`/`sendToQueue` with the trace context in `options.headers`, and a
|
|
4
|
+
* CONSUMER span (child of and linked to the producer) around each `consume` callback.
|
|
5
|
+
* shortcut: a ConfirmChannel is its own class; pass one of those too if you use both kinds.
|
|
6
|
+
*/
|
|
7
|
+
export declare function instrumentAmqplib(channel: object): void;
|
|
8
|
+
/**
|
|
9
|
+
* `instrumentRabbitmqClient(new Connection(url))` for rabbitmq-client: publishers made by
|
|
10
|
+
* `createPublisher` get PRODUCER spans on `send` (context in the envelope headers); handlers given
|
|
11
|
+
* to `createConsumer` run in a CONSUMER span. Call it before creating publishers and consumers.
|
|
12
|
+
*/
|
|
13
|
+
export declare function instrumentRabbitmqClient(connection: object): void;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { patch } from './db.js';
|
|
2
|
+
import { consume, produce } from './messaging.js';
|
|
3
|
+
const sendAttrs = (dest, routingKey, op, messageId, size) => ({
|
|
4
|
+
'messaging.system': 'rabbitmq',
|
|
5
|
+
'messaging.destination.name': dest,
|
|
6
|
+
'messaging.operation.type': 'send',
|
|
7
|
+
'messaging.operation.name': op,
|
|
8
|
+
'messaging.rabbitmq.destination.routing_key': routingKey,
|
|
9
|
+
...(typeof messageId === 'string' ? { 'messaging.message.id': messageId } : {}),
|
|
10
|
+
...(typeof size === 'number' ? { 'messaging.message.body.size': size } : {}),
|
|
11
|
+
});
|
|
12
|
+
function processAttrs(queue, m) {
|
|
13
|
+
const a = {
|
|
14
|
+
'messaging.system': 'rabbitmq',
|
|
15
|
+
'messaging.destination.name': queue,
|
|
16
|
+
'messaging.operation.type': 'process',
|
|
17
|
+
'messaging.operation.name': 'consume',
|
|
18
|
+
};
|
|
19
|
+
if (m.properties?.messageId)
|
|
20
|
+
a['messaging.message.id'] = m.properties.messageId;
|
|
21
|
+
if (typeof m.content?.length === 'number')
|
|
22
|
+
a['messaging.message.body.size'] = m.content.length;
|
|
23
|
+
if (m.fields?.deliveryTag !== undefined)
|
|
24
|
+
a['messaging.rabbitmq.message.delivery_tag'] = m.fields.deliveryTag;
|
|
25
|
+
if (m.fields?.redelivered !== undefined)
|
|
26
|
+
a['messaging.rabbitmq.message.redelivered'] = m.fields.redelivered;
|
|
27
|
+
if (m.fields?.consumerTag)
|
|
28
|
+
a['messaging.consumer.group.name'] = m.fields.consumerTag;
|
|
29
|
+
return a;
|
|
30
|
+
}
|
|
31
|
+
const withHeaders = (o, h) => ({ ...o, headers: { ...o?.headers, ...h } });
|
|
32
|
+
/**
|
|
33
|
+
* `instrumentAmqplib(await conn.createChannel())`: patches that channel's class (every channel of
|
|
34
|
+
* it): PRODUCER spans on `publish`/`sendToQueue` with the trace context in `options.headers`, and a
|
|
35
|
+
* CONSUMER span (child of and linked to the producer) around each `consume` callback.
|
|
36
|
+
* shortcut: a ConfirmChannel is its own class; pass one of those too if you use both kinds.
|
|
37
|
+
*/
|
|
38
|
+
export function instrumentAmqplib(channel) {
|
|
39
|
+
const proto = Object.getPrototypeOf(channel);
|
|
40
|
+
patch(proto, 'publish', (orig) => function (exchange, routingKey, content, options, ...rest) {
|
|
41
|
+
const dest = exchange || routingKey;
|
|
42
|
+
return produce(`publish ${dest}`, sendAttrs(dest, routingKey, 'publish', options?.messageId, content?.length), (h) => orig.call(this, exchange, routingKey, content, withHeaders(options, h), ...rest));
|
|
43
|
+
});
|
|
44
|
+
// amqplib's sendToQueue calls publish('', queue): that inner call adds no second span
|
|
45
|
+
patch(proto, 'sendToQueue', (orig) => function (queue, content, options, ...rest) {
|
|
46
|
+
return produce(`publish ${queue}`, sendAttrs(queue, queue, 'sendToQueue', options?.messageId, content?.length), (h) => orig.call(this, queue, content, withHeaders(options, h), ...rest));
|
|
47
|
+
});
|
|
48
|
+
patch(proto, 'consume', (orig) => function (queue, onMessage, ...rest) {
|
|
49
|
+
if (typeof onMessage !== 'function')
|
|
50
|
+
return orig.call(this, queue, onMessage, ...rest);
|
|
51
|
+
const wrapped = (m) => (m
|
|
52
|
+
? consume(`process ${queue}`, processAttrs(queue, m), [m.properties?.headers], () => onMessage(m))
|
|
53
|
+
: onMessage(m)); // null: the server cancelled the consumer
|
|
54
|
+
return orig.call(this, queue, wrapped, ...rest);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* `instrumentRabbitmqClient(new Connection(url))` for rabbitmq-client: publishers made by
|
|
59
|
+
* `createPublisher` get PRODUCER spans on `send` (context in the envelope headers); handlers given
|
|
60
|
+
* to `createConsumer` run in a CONSUMER span. Call it before creating publishers and consumers.
|
|
61
|
+
*/
|
|
62
|
+
export function instrumentRabbitmqClient(connection) {
|
|
63
|
+
patch(connection, 'createPublisher', (orig) => function (...a) {
|
|
64
|
+
const pub = orig.apply(this, a);
|
|
65
|
+
patch(pub, 'send', (send) => function (envelope, body, ...rest) {
|
|
66
|
+
const env = typeof envelope === 'string' ? { routingKey: envelope } : { ...envelope };
|
|
67
|
+
const rk = env.routingKey ?? '';
|
|
68
|
+
const dest = env.exchange || rk;
|
|
69
|
+
const size = typeof body === 'string' || body instanceof Uint8Array ? body.length : undefined;
|
|
70
|
+
return produce(`publish ${dest}`, sendAttrs(dest, rk, 'send', env.messageId, size), (h) => send.call(this, { ...env, headers: { ...env.headers, ...h } }, body, ...rest));
|
|
71
|
+
});
|
|
72
|
+
return pub;
|
|
73
|
+
});
|
|
74
|
+
patch(connection, 'createConsumer', (orig) => function (props, handler, ...rest) {
|
|
75
|
+
if (typeof handler !== 'function')
|
|
76
|
+
return orig.call(this, props, handler, ...rest);
|
|
77
|
+
const queue = (typeof props === 'string' ? props : props?.queue) ?? '';
|
|
78
|
+
const wrapped = function (m, ...more) {
|
|
79
|
+
const a = {
|
|
80
|
+
'messaging.system': 'rabbitmq',
|
|
81
|
+
'messaging.destination.name': queue,
|
|
82
|
+
'messaging.operation.type': 'process',
|
|
83
|
+
'messaging.operation.name': 'consume',
|
|
84
|
+
...(m?.messageId ? { 'messaging.message.id': m.messageId } : {}),
|
|
85
|
+
...(m?.deliveryTag !== undefined ? { 'messaging.rabbitmq.message.delivery_tag': m.deliveryTag } : {}),
|
|
86
|
+
...(m?.redelivered !== undefined ? { 'messaging.rabbitmq.message.redelivered': m.redelivered } : {}),
|
|
87
|
+
};
|
|
88
|
+
return consume(`process ${queue}`, a, [m?.headers], () => handler.call(this, m, ...more));
|
|
89
|
+
};
|
|
90
|
+
return orig.call(this, props, wrapped, ...rest);
|
|
91
|
+
});
|
|
92
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { CONTEXT_KEY } from './messaging.js';
|
|
2
|
+
export { CONTEXT_KEY };
|
|
3
|
+
interface BullMQLike {
|
|
4
|
+
Queue: {
|
|
5
|
+
prototype: Record<string | symbol, unknown>;
|
|
6
|
+
};
|
|
7
|
+
Worker: {
|
|
8
|
+
prototype: Record<string | symbol, unknown>;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* `import * as bullmq from 'bullmq'; instrumentBullMQ(bullmq)` (ESM or CJS): PRODUCER spans on
|
|
13
|
+
* `Queue.add`/`addBulk`, CONSUMER spans around each job a `Worker` processes, parented by the
|
|
14
|
+
* producer's context carried in job data under `_wt` (stripped before your processor runs; object
|
|
15
|
+
* data only). Call it once, before creating queues and workers. Returns false when this bullmq
|
|
16
|
+
* has no `Worker.callProcessJob` (consumer spans off; producer spans still on).
|
|
17
|
+
* shortcut: sandboxed (file path) processors run in a child process and get no consumer span.
|
|
18
|
+
*/
|
|
19
|
+
export declare function instrumentBullMQ(bullmq: BullMQLike): boolean;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { context, propagation, trace, SpanKind, SpanStatusCode } from '@opentelemetry/api';
|
|
2
|
+
import { CONTEXT_KEY, injectData as inject, isPlainObject } from './messaging.js';
|
|
3
|
+
export { CONTEXT_KEY };
|
|
4
|
+
const PATCHED = Symbol.for('watchtower.bullmq');
|
|
5
|
+
const tracer = () => trace.getTracer('watchtower');
|
|
6
|
+
const base = (queue, op, name) => ({
|
|
7
|
+
'messaging.system': 'bullmq',
|
|
8
|
+
'messaging.destination.name': queue,
|
|
9
|
+
'messaging.operation.type': op,
|
|
10
|
+
'messaging.operation.name': name,
|
|
11
|
+
});
|
|
12
|
+
async function run(span, fn) {
|
|
13
|
+
try {
|
|
14
|
+
return await fn();
|
|
15
|
+
}
|
|
16
|
+
catch (e) {
|
|
17
|
+
span.recordException(e);
|
|
18
|
+
span.setStatus({ code: SpanStatusCode.ERROR, message: e?.message });
|
|
19
|
+
throw e;
|
|
20
|
+
}
|
|
21
|
+
finally {
|
|
22
|
+
span.end();
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* `import * as bullmq from 'bullmq'; instrumentBullMQ(bullmq)` (ESM or CJS): PRODUCER spans on
|
|
27
|
+
* `Queue.add`/`addBulk`, CONSUMER spans around each job a `Worker` processes, parented by the
|
|
28
|
+
* producer's context carried in job data under `_wt` (stripped before your processor runs; object
|
|
29
|
+
* data only). Call it once, before creating queues and workers. Returns false when this bullmq
|
|
30
|
+
* has no `Worker.callProcessJob` (consumer spans off; producer spans still on).
|
|
31
|
+
* shortcut: sandboxed (file path) processors run in a child process and get no consumer span.
|
|
32
|
+
*/
|
|
33
|
+
export function instrumentBullMQ(bullmq) {
|
|
34
|
+
const q = bullmq.Queue.prototype;
|
|
35
|
+
const w = bullmq.Worker.prototype;
|
|
36
|
+
if (q[PATCHED])
|
|
37
|
+
return typeof w.callProcessJob === 'function';
|
|
38
|
+
q[PATCHED] = true;
|
|
39
|
+
const add = q.add;
|
|
40
|
+
q.add = function (name, data, opts) {
|
|
41
|
+
const attrs = { ...base(this.name, 'send', 'add'), 'messaging.bullmq.job.name': name };
|
|
42
|
+
if (opts?.delay)
|
|
43
|
+
attrs['messaging.bullmq.job.delay_ms'] = opts.delay;
|
|
44
|
+
return tracer().startActiveSpan(`send ${this.name}`, { kind: SpanKind.PRODUCER, attributes: attrs }, (span) => run(span, async () => {
|
|
45
|
+
const job = (await add.call(this, name, inject(data), opts));
|
|
46
|
+
if (job?.id)
|
|
47
|
+
span.setAttribute('messaging.message.id', job.id);
|
|
48
|
+
return job;
|
|
49
|
+
}));
|
|
50
|
+
};
|
|
51
|
+
const addBulk = q.addBulk;
|
|
52
|
+
if (addBulk) {
|
|
53
|
+
q.addBulk = function (jobs) {
|
|
54
|
+
const attrs = { ...base(this.name, 'send', 'addBulk'), 'messaging.batch.message_count': jobs.length };
|
|
55
|
+
return tracer().startActiveSpan(`send ${this.name}`, { kind: SpanKind.PRODUCER, attributes: attrs }, (span) => run(span, () => addBulk.call(this, jobs.map((j) => ({ ...j, data: inject(j.data) })))));
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
const call = w.callProcessJob;
|
|
59
|
+
if (typeof call !== 'function')
|
|
60
|
+
return false;
|
|
61
|
+
w.callProcessJob = function (job, ...rest) {
|
|
62
|
+
let parent = context.active();
|
|
63
|
+
if (isPlainObject(job.data) && isPlainObject(job.data[CONTEXT_KEY])) {
|
|
64
|
+
parent = propagation.extract(parent, job.data[CONTEXT_KEY]);
|
|
65
|
+
delete job.data[CONTEXT_KEY];
|
|
66
|
+
}
|
|
67
|
+
const attrs = {
|
|
68
|
+
...base(this.name, 'process', 'process'),
|
|
69
|
+
'messaging.bullmq.job.name': job.name,
|
|
70
|
+
'messaging.bullmq.job.attempt': (job.attemptsMade ?? 0) + 1,
|
|
71
|
+
};
|
|
72
|
+
if (job.id)
|
|
73
|
+
attrs['messaging.message.id'] = job.id;
|
|
74
|
+
if (job.opts?.delay)
|
|
75
|
+
attrs['messaging.bullmq.job.delay_ms'] = job.opts.delay;
|
|
76
|
+
return tracer().startActiveSpan(`process ${this.name}`, { kind: SpanKind.CONSUMER, attributes: attrs }, parent, (span) => run(span, () => call.call(this, job, ...rest)));
|
|
77
|
+
};
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Hit/miss counters and entry count for a node-cache instance (`get`, `mget`). Returns `stop()`. */
|
|
2
|
+
export declare function instrumentNodeCache(cache: {
|
|
3
|
+
getStats?(): {
|
|
4
|
+
keys?: number;
|
|
5
|
+
};
|
|
6
|
+
}, name?: string): () => void;
|
|
7
|
+
/** Hit/miss counters and entry count for an lru-cache instance (`get`). Returns `stop()`.
|
|
8
|
+
* shortcut: `fetch()` (the async loader) is not counted. */
|
|
9
|
+
export declare function instrumentLruCache(cache: {
|
|
10
|
+
size?: number;
|
|
11
|
+
}, name?: string): () => void;
|
|
12
|
+
/** Hit/miss counters for a Keyv instance (`get` with one key or an array; also `getMany`). */
|
|
13
|
+
export declare function instrumentKeyv(keyv: object, name?: string): void;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { cacheCounters, observeEmbedded, patch } from './db.js';
|
|
2
|
+
/** Count hits and misses of `get`-like methods: `undefined` is a miss; arrays and objects count per key. */
|
|
3
|
+
function count(c, keys, v) {
|
|
4
|
+
if (Array.isArray(keys)) {
|
|
5
|
+
const found = Array.isArray(v) ? v.filter((x) => x !== undefined).length
|
|
6
|
+
: v && typeof v === 'object' ? Object.keys(v).length : 0;
|
|
7
|
+
c.hit(found);
|
|
8
|
+
c.miss(keys.length - found);
|
|
9
|
+
}
|
|
10
|
+
else if (v === undefined) {
|
|
11
|
+
c.miss();
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
c.hit();
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
function wrapGets(cache, system, name, methods) {
|
|
18
|
+
const c = cacheCounters(system, name);
|
|
19
|
+
for (const m of methods) {
|
|
20
|
+
patch(cache, m, (orig) => function (key, ...a) {
|
|
21
|
+
const v = orig.call(this, key, ...a);
|
|
22
|
+
if (v && typeof v.then === 'function') {
|
|
23
|
+
v.then((x) => count(c, key, x), () => { });
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
count(c, key, v);
|
|
27
|
+
}
|
|
28
|
+
return v;
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/** Hit/miss counters and entry count for a node-cache instance (`get`, `mget`). Returns `stop()`. */
|
|
33
|
+
export function instrumentNodeCache(cache, name = 'default') {
|
|
34
|
+
wrapGets(cache, 'node-cache', name, ['get', 'mget']);
|
|
35
|
+
return observeEmbedded('node-cache', name, ['cache.entries'], () => ({ 'cache.entries': cache.getStats?.().keys }));
|
|
36
|
+
}
|
|
37
|
+
/** Hit/miss counters and entry count for an lru-cache instance (`get`). Returns `stop()`.
|
|
38
|
+
* shortcut: `fetch()` (the async loader) is not counted. */
|
|
39
|
+
export function instrumentLruCache(cache, name = 'default') {
|
|
40
|
+
wrapGets(cache, 'lru-cache', name, ['get']);
|
|
41
|
+
return observeEmbedded('lru-cache', name, ['cache.entries'], () => ({ 'cache.entries': cache.size }));
|
|
42
|
+
}
|
|
43
|
+
/** Hit/miss counters for a Keyv instance (`get` with one key or an array; also `getMany`). */
|
|
44
|
+
export function instrumentKeyv(keyv, name = 'default') {
|
|
45
|
+
wrapGets(keyv, 'keyv', name, ['get', 'getMany']);
|
|
46
|
+
}
|