@watchtowerobservability/node 0.0.0-stage → 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/LICENSE +202 -0
- package/README.md +304 -2
- package/dist/cjs/appsec.d.ts +95 -0
- package/dist/cjs/appsec.js +206 -0
- package/dist/cjs/buffer.d.ts +49 -0
- package/dist/cjs/buffer.js +134 -0
- package/dist/cjs/errors.d.ts +17 -0
- package/dist/cjs/errors.js +55 -0
- package/dist/cjs/index.d.ts +123 -0
- package/dist/cjs/index.js +315 -0
- package/dist/cjs/integrations/amqp.d.ts +13 -0
- package/dist/cjs/integrations/amqp.js +96 -0
- package/dist/cjs/integrations/azure-functions.d.ts +18 -0
- package/dist/cjs/integrations/azure-functions.js +97 -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/express.d.ts +25 -0
- package/dist/cjs/integrations/express.js +58 -0
- package/dist/cjs/integrations/fastify.d.ts +6 -0
- package/dist/cjs/integrations/fastify.js +44 -0
- package/dist/cjs/integrations/genai-agents.d.ts +36 -0
- package/dist/cjs/integrations/genai-agents.js +124 -0
- package/dist/cjs/integrations/genai-stream.d.ts +19 -0
- package/dist/cjs/integrations/genai-stream.js +135 -0
- package/dist/cjs/integrations/genai.d.ts +18 -0
- package/dist/cjs/integrations/genai.js +150 -0
- package/dist/cjs/integrations/graphile-worker.d.ts +22 -0
- package/dist/cjs/integrations/graphile-worker.js +50 -0
- package/dist/cjs/integrations/koa.d.ts +19 -0
- package/dist/cjs/integrations/koa.js +34 -0
- package/dist/cjs/integrations/lambda.d.ts +11 -0
- package/dist/cjs/integrations/lambda.js +68 -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/logs.d.ts +10 -0
- package/dist/cjs/integrations/logs.js +77 -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/nestjs.d.ts +16 -0
- package/dist/cjs/integrations/nestjs.js +30 -0
- package/dist/cjs/integrations/next.d.ts +11 -0
- package/dist/cjs/integrations/next.js +59 -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/prisma.d.ts +3 -0
- package/dist/cjs/integrations/prisma.js +33 -0
- package/dist/cjs/integrations/sqlite.d.ts +36 -0
- package/dist/cjs/integrations/sqlite.js +125 -0
- package/dist/cjs/integrations/util.d.ts +9 -0
- package/dist/cjs/integrations/util.js +37 -0
- package/dist/cjs/jest.d.ts +21 -0
- package/dist/cjs/jest.js +45 -0
- package/dist/cjs/load.d.ts +1 -0
- package/dist/cjs/load.js +11 -0
- package/dist/cjs/metrics.d.ts +10 -0
- package/dist/cjs/metrics.js +81 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/payload.d.ts +42 -0
- package/dist/cjs/payload.js +145 -0
- package/dist/cjs/pprof.d.ts +6 -0
- package/dist/cjs/pprof.js +88 -0
- package/dist/cjs/probes.d.ts +97 -0
- package/dist/cjs/probes.js +422 -0
- package/dist/cjs/profiling.d.ts +84 -0
- package/dist/cjs/profiling.js +263 -0
- package/dist/cjs/reporter-core.d.ts +32 -0
- package/dist/cjs/reporter-core.js +102 -0
- package/dist/cjs/sql.d.ts +19 -0
- package/dist/cjs/sql.js +332 -0
- package/dist/cjs/test-plan.d.ts +102 -0
- package/dist/cjs/test-plan.js +352 -0
- package/dist/cjs/vitest.d.ts +27 -0
- package/dist/cjs/vitest.js +52 -0
- package/dist/esm/appsec.d.ts +95 -0
- package/dist/esm/appsec.js +193 -0
- package/dist/esm/buffer.d.ts +49 -0
- package/dist/esm/buffer.js +92 -0
- package/dist/esm/errors.d.ts +17 -0
- package/dist/esm/errors.js +50 -0
- package/dist/esm/index.d.ts +123 -0
- package/dist/esm/index.js +264 -0
- package/dist/esm/integrations/amqp.d.ts +13 -0
- package/dist/esm/integrations/amqp.js +92 -0
- package/dist/esm/integrations/azure-functions.d.ts +18 -0
- package/dist/esm/integrations/azure-functions.js +60 -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/express.d.ts +25 -0
- package/dist/esm/integrations/express.js +54 -0
- package/dist/esm/integrations/fastify.d.ts +6 -0
- package/dist/esm/integrations/fastify.js +41 -0
- package/dist/esm/integrations/genai-agents.d.ts +36 -0
- package/dist/esm/integrations/genai-agents.js +118 -0
- package/dist/esm/integrations/genai-stream.d.ts +19 -0
- package/dist/esm/integrations/genai-stream.js +130 -0
- package/dist/esm/integrations/genai.d.ts +18 -0
- package/dist/esm/integrations/genai.js +138 -0
- package/dist/esm/integrations/graphile-worker.d.ts +22 -0
- package/dist/esm/integrations/graphile-worker.js +46 -0
- package/dist/esm/integrations/koa.d.ts +19 -0
- package/dist/esm/integrations/koa.js +31 -0
- package/dist/esm/integrations/lambda.d.ts +11 -0
- package/dist/esm/integrations/lambda.js +32 -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/logs.d.ts +10 -0
- package/dist/esm/integrations/logs.js +40 -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/nestjs.d.ts +16 -0
- package/dist/esm/integrations/nestjs.js +26 -0
- package/dist/esm/integrations/next.d.ts +11 -0
- package/dist/esm/integrations/next.js +22 -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/prisma.d.ts +3 -0
- package/dist/esm/integrations/prisma.js +30 -0
- package/dist/esm/integrations/sqlite.d.ts +36 -0
- package/dist/esm/integrations/sqlite.js +84 -0
- package/dist/esm/integrations/util.d.ts +9 -0
- package/dist/esm/integrations/util.js +32 -0
- package/dist/esm/jest.d.ts +21 -0
- package/dist/esm/jest.js +41 -0
- package/dist/esm/load.d.ts +1 -0
- package/dist/esm/load.js +5 -0
- package/dist/esm/metrics.d.ts +10 -0
- package/dist/esm/metrics.js +44 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/payload.d.ts +42 -0
- package/dist/esm/payload.js +131 -0
- package/dist/esm/pprof.d.ts +6 -0
- package/dist/esm/pprof.js +85 -0
- package/dist/esm/probes.d.ts +97 -0
- package/dist/esm/probes.js +412 -0
- package/dist/esm/profiling.d.ts +84 -0
- package/dist/esm/profiling.js +251 -0
- package/dist/esm/reporter-core.d.ts +32 -0
- package/dist/esm/reporter-core.js +63 -0
- package/dist/esm/sql.d.ts +19 -0
- package/dist/esm/sql.js +324 -0
- package/dist/esm/test-plan.d.ts +102 -0
- package/dist/esm/test-plan.js +336 -0
- package/dist/esm/vitest.d.ts +27 -0
- package/dist/esm/vitest.js +48 -0
- package/package.json +379 -4
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Profiler = void 0;
|
|
4
|
+
exports.enableAsyncSpanLabels = enableAsyncSpanLabels;
|
|
5
|
+
exports.withSpan = withSpan;
|
|
6
|
+
exports.takeIntervals = takeIntervals;
|
|
7
|
+
exports.foldProfile = foldProfile;
|
|
8
|
+
exports.foldedBody = foldedBody;
|
|
9
|
+
exports.profilesUrl = profilesUrl;
|
|
10
|
+
exports.ingestUrl = ingestUrl;
|
|
11
|
+
exports.startProfiling = startProfiling;
|
|
12
|
+
// Continuous CPU profiler on V8's own sampler (`node:inspector`, no native addon): every
|
|
13
|
+
// `periodMs` the CPU profile is folded into stacks, encoded as pprof and posted to the gateway's
|
|
14
|
+
// `/v1/profiles?format=pprof` with `service` and `version`. Best effort: a failed upload is
|
|
15
|
+
// counted and dropped. `withSpan` labels samples taken while its function runs, joined
|
|
16
|
+
// on V8's sample timestamps, so a trace links to its exact profile.
|
|
17
|
+
const node_async_hooks_1 = require("node:async_hooks");
|
|
18
|
+
const node_inspector_1 = require("node:inspector");
|
|
19
|
+
const node_v8_1 = require("node:v8");
|
|
20
|
+
const api_1 = require("@opentelemetry/api");
|
|
21
|
+
const pprof_js_1 = require("./pprof.js");
|
|
22
|
+
const intervals = [];
|
|
23
|
+
const MAX_INTERVALS = 100_000;
|
|
24
|
+
function nowUs() {
|
|
25
|
+
return Number(process.hrtime.bigint() / 1000n);
|
|
26
|
+
}
|
|
27
|
+
/** The span of the running code, carried across `await` once async labels are on. */
|
|
28
|
+
const spanStore = new node_async_hooks_1.AsyncLocalStorage();
|
|
29
|
+
/** Promises created under a span, and that span. */
|
|
30
|
+
const tagged = new WeakMap();
|
|
31
|
+
/** The interval each `before` opened, for its `after`; `null` for an unlabeled continuation. */
|
|
32
|
+
const running = [];
|
|
33
|
+
let asyncSpans = false;
|
|
34
|
+
function open(prefix) {
|
|
35
|
+
if (intervals.length >= MAX_INTERVALS)
|
|
36
|
+
return null;
|
|
37
|
+
const iv = { start: nowUs(), end: Number.MAX_SAFE_INTEGER, prefix };
|
|
38
|
+
intervals.push(iv);
|
|
39
|
+
return iv;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Opt-in (overhead unmeasured): `withSpan` also labels its async function's code after each
|
|
43
|
+
* `await`. The span rides in an AsyncLocalStorage; a promise created under it is tagged, and each
|
|
44
|
+
* run of its continuation (V8 promise hooks `before` to `after`) is one more interval. Plain
|
|
45
|
+
* callbacks it starts (timers, events) are labeled only from their own `await`s on.
|
|
46
|
+
*/
|
|
47
|
+
function enableAsyncSpanLabels() {
|
|
48
|
+
if (asyncSpans)
|
|
49
|
+
return;
|
|
50
|
+
asyncSpans = true;
|
|
51
|
+
node_v8_1.promiseHooks.createHook({
|
|
52
|
+
init(promise) {
|
|
53
|
+
const p = spanStore.getStore(); // init runs synchronously, in the creating code's context
|
|
54
|
+
if (p)
|
|
55
|
+
tagged.set(promise, p);
|
|
56
|
+
},
|
|
57
|
+
before(promise) {
|
|
58
|
+
if (running.length >= 1_000)
|
|
59
|
+
running.length = 0; // afters lost to an uncaught exception
|
|
60
|
+
const p = tagged.get(promise);
|
|
61
|
+
running.push(p ? open(p) : null);
|
|
62
|
+
},
|
|
63
|
+
after() {
|
|
64
|
+
const iv = running.pop();
|
|
65
|
+
if (iv)
|
|
66
|
+
iv.end = nowUs();
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Labels samples taken while `fn` runs with a span (default: the active OpenTelemetry span). Only
|
|
72
|
+
* `fn`'s synchronous part, unless [[enableAsyncSpanLabels]] (or the profiler's `asyncSpans`) is on.
|
|
73
|
+
*/
|
|
74
|
+
function withSpan(fn, ids) {
|
|
75
|
+
const sc = ids ?? api_1.trace.getActiveSpan()?.spanContext();
|
|
76
|
+
if (!sc || intervals.length >= MAX_INTERVALS)
|
|
77
|
+
return fn();
|
|
78
|
+
const prefix = `span_id=${sc.spanId},trace_id=${sc.traceId}\t`;
|
|
79
|
+
const iv = open(prefix);
|
|
80
|
+
try {
|
|
81
|
+
return asyncSpans ? spanStore.run(prefix, fn) : fn();
|
|
82
|
+
}
|
|
83
|
+
finally {
|
|
84
|
+
iv.end = nowUs();
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/** Takes the intervals a profile up to `until` can need; open ones stay for the next profile. */
|
|
88
|
+
function takeIntervals(until) {
|
|
89
|
+
const out = intervals.slice();
|
|
90
|
+
const keep = out.filter((iv) => iv.end > until);
|
|
91
|
+
intervals.length = 0;
|
|
92
|
+
intervals.push(...keep);
|
|
93
|
+
return out;
|
|
94
|
+
}
|
|
95
|
+
/** The innermost span at `t`; `sorted` is by start. */
|
|
96
|
+
function prefixAt(sorted, t) {
|
|
97
|
+
let lo = 0;
|
|
98
|
+
let hi = sorted.length;
|
|
99
|
+
while (lo < hi) {
|
|
100
|
+
const mid = (lo + hi) >> 1;
|
|
101
|
+
if (sorted[mid].start <= t)
|
|
102
|
+
lo = mid + 1;
|
|
103
|
+
else
|
|
104
|
+
hi = mid;
|
|
105
|
+
}
|
|
106
|
+
for (let i = lo - 1; i >= 0 && i >= lo - 16; i--)
|
|
107
|
+
if (sorted[i].end >= t)
|
|
108
|
+
return sorted[i].prefix;
|
|
109
|
+
return '';
|
|
110
|
+
}
|
|
111
|
+
const MAX_DEPTH = 128;
|
|
112
|
+
function frameName(f) {
|
|
113
|
+
const file = f.url ? f.url.split('/').pop() : '';
|
|
114
|
+
const name = f.functionName || '(anonymous)';
|
|
115
|
+
return (file ? `${name} (${file}:${f.lineNumber + 1})` : name).replace(/;/g, ':');
|
|
116
|
+
}
|
|
117
|
+
/** Root-first folded stacks with their sample counts; V8's synthetic `(root)` is left out. */
|
|
118
|
+
function foldProfile(p, spans = []) {
|
|
119
|
+
const parent = new Map();
|
|
120
|
+
const byId = new Map();
|
|
121
|
+
for (const n of p.nodes) {
|
|
122
|
+
byId.set(n.id, n);
|
|
123
|
+
for (const c of n.children ?? [])
|
|
124
|
+
parent.set(c, n.id);
|
|
125
|
+
}
|
|
126
|
+
const counts = new Map();
|
|
127
|
+
const sorted = [...spans].sort((a, b) => a.start - b.start);
|
|
128
|
+
let t = p.startTime ?? 0;
|
|
129
|
+
for (const [i, id] of (p.samples ?? []).entries()) {
|
|
130
|
+
t += p.timeDeltas?.[i] ?? 0;
|
|
131
|
+
const names = [];
|
|
132
|
+
for (let cur = id; cur !== undefined && names.length < MAX_DEPTH; cur = parent.get(cur)) {
|
|
133
|
+
const n = byId.get(cur);
|
|
134
|
+
if (!n || n.callFrame.functionName === '(root)')
|
|
135
|
+
break;
|
|
136
|
+
if (n.callFrame.functionName === '(idle)' || n.callFrame.functionName === '(program)') {
|
|
137
|
+
names.length = 0;
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
names.push(frameName(n.callFrame));
|
|
141
|
+
}
|
|
142
|
+
if (names.length === 0)
|
|
143
|
+
continue;
|
|
144
|
+
const key = prefixAt(sorted, t) + names.reverse().join(';');
|
|
145
|
+
counts.set(key, (counts.get(key) ?? 0) + 1);
|
|
146
|
+
}
|
|
147
|
+
return counts;
|
|
148
|
+
}
|
|
149
|
+
function foldedBody(counts) {
|
|
150
|
+
let out = '';
|
|
151
|
+
for (const [k, n] of counts)
|
|
152
|
+
out += `${k} ${n}\n`;
|
|
153
|
+
return out;
|
|
154
|
+
}
|
|
155
|
+
/** `/v1/profiles?format=pprof&service=…&version=…` on the gateway. */
|
|
156
|
+
function profilesUrl(endpoint, service, version) {
|
|
157
|
+
const q = new URLSearchParams({ format: 'pprof', service });
|
|
158
|
+
if (version)
|
|
159
|
+
q.set('version', version);
|
|
160
|
+
return `${endpoint.replace(/\/+$/, '')}/v1/profiles?${q}`;
|
|
161
|
+
}
|
|
162
|
+
/** Pyroscope-compatible `/ingest` URL for folded uploads (kept for callers; the Profiler sends pprof). */
|
|
163
|
+
function ingestUrl(endpoint, service, tags, fromS, untilS, hz) {
|
|
164
|
+
const labels = Object.entries(tags).filter(([, v]) => v).sort(([a], [b]) => a.localeCompare(b))
|
|
165
|
+
.map(([k, v]) => `${k}=${v}`).join(',');
|
|
166
|
+
const q = new URLSearchParams({
|
|
167
|
+
name: `${service}.cpu{${labels}}`, from: String(Math.floor(fromS)), until: String(Math.floor(untilS)),
|
|
168
|
+
format: 'folded', sampleRate: String(hz), spyName: 'watchtower-node', units: 'samples',
|
|
169
|
+
});
|
|
170
|
+
return `${endpoint.replace(/\/+$/, '')}/ingest?${q}`;
|
|
171
|
+
}
|
|
172
|
+
class Profiler {
|
|
173
|
+
uploaded = 0;
|
|
174
|
+
dropped = 0;
|
|
175
|
+
key;
|
|
176
|
+
endpoint;
|
|
177
|
+
service;
|
|
178
|
+
tags;
|
|
179
|
+
intervalUs;
|
|
180
|
+
periodMs;
|
|
181
|
+
session;
|
|
182
|
+
timer;
|
|
183
|
+
from = 0;
|
|
184
|
+
constructor(o = {}) {
|
|
185
|
+
const key = o.apiKey ?? process.env.WT_INGEST_KEY;
|
|
186
|
+
if (!key)
|
|
187
|
+
throw new Error('apiKey is required');
|
|
188
|
+
this.key = key;
|
|
189
|
+
this.endpoint = o.endpoint ?? process.env.WT_OTLP_ENDPOINT ?? 'http://localhost:4318';
|
|
190
|
+
this.service = o.service ?? process.env.WT_SERVICE ?? 'app';
|
|
191
|
+
this.tags = { env: process.env.WT_ENVIRONMENT, version: process.env.WT_RELEASE, ...o.tags };
|
|
192
|
+
this.intervalUs = Math.min(Math.max(o.intervalUs ?? 10_000, 1_000), 1_000_000);
|
|
193
|
+
this.periodMs = Math.max(o.periodMs ?? 60_000, 1_000);
|
|
194
|
+
if (o.asyncSpans)
|
|
195
|
+
enableAsyncSpanLabels();
|
|
196
|
+
}
|
|
197
|
+
/** Not printable with the key. */
|
|
198
|
+
[Symbol.for('nodejs.util.inspect.custom')]() {
|
|
199
|
+
return `Profiler { service: '${this.service}' }`;
|
|
200
|
+
}
|
|
201
|
+
post(method, params) {
|
|
202
|
+
return new Promise((resolve, reject) => {
|
|
203
|
+
this.session.post(method, params ?? {}, (err, res) => (err ? reject(err) : resolve(res)));
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
async start() {
|
|
207
|
+
if (this.session)
|
|
208
|
+
return this;
|
|
209
|
+
this.session = new node_inspector_1.Session();
|
|
210
|
+
this.session.connect();
|
|
211
|
+
await this.post('Profiler.enable');
|
|
212
|
+
await this.post('Profiler.setSamplingInterval', { interval: this.intervalUs });
|
|
213
|
+
await this.post('Profiler.start');
|
|
214
|
+
this.from = Date.now() / 1000;
|
|
215
|
+
this.timer = setInterval(() => void this.flush(true), this.periodMs);
|
|
216
|
+
this.timer.unref();
|
|
217
|
+
return this;
|
|
218
|
+
}
|
|
219
|
+
/** Stop the current profile, upload it and (when `restart`) start the next one. */
|
|
220
|
+
async flush(restart) {
|
|
221
|
+
if (!this.session)
|
|
222
|
+
return;
|
|
223
|
+
const until = Date.now() / 1000;
|
|
224
|
+
const { profile } = await this.post('Profiler.stop');
|
|
225
|
+
const from = this.from;
|
|
226
|
+
if (restart) {
|
|
227
|
+
await this.post('Profiler.start');
|
|
228
|
+
this.from = until;
|
|
229
|
+
}
|
|
230
|
+
const counts = foldProfile(profile, takeIntervals(nowUs()));
|
|
231
|
+
if (counts.size)
|
|
232
|
+
await this.upload(counts, from, until);
|
|
233
|
+
}
|
|
234
|
+
async upload(counts, fromS, untilS) {
|
|
235
|
+
try {
|
|
236
|
+
const body = (0, pprof_js_1.foldedToPprof)(counts, this.intervalUs * 1000, fromS * 1000, (untilS - fromS) * 1000);
|
|
237
|
+
const r = await fetch(profilesUrl(this.endpoint, this.service, this.tags.version), {
|
|
238
|
+
method: 'POST', body,
|
|
239
|
+
headers: { authorization: `Bearer ${this.key}`, 'content-type': 'application/octet-stream' },
|
|
240
|
+
signal: AbortSignal.timeout(10_000),
|
|
241
|
+
});
|
|
242
|
+
if (r.ok)
|
|
243
|
+
this.uploaded++;
|
|
244
|
+
else
|
|
245
|
+
this.dropped++;
|
|
246
|
+
}
|
|
247
|
+
catch {
|
|
248
|
+
this.dropped++;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
async stop() {
|
|
252
|
+
if (this.timer)
|
|
253
|
+
clearInterval(this.timer);
|
|
254
|
+
await this.flush(false).catch(() => undefined);
|
|
255
|
+
this.session?.disconnect();
|
|
256
|
+
this.session = undefined;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
exports.Profiler = Profiler;
|
|
260
|
+
/** Start a [[Profiler]] with the SDK's environment defaults. */
|
|
261
|
+
function startProfiling(o = {}) {
|
|
262
|
+
return new Profiler(o).start();
|
|
263
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { Client, InitOptions } from './index.js';
|
|
2
|
+
export interface ReporterOptions {
|
|
3
|
+
client?: Client;
|
|
4
|
+
apiKey?: string;
|
|
5
|
+
serviceName?: string;
|
|
6
|
+
/** Extra options passed to `init` when the reporter creates its own client. */
|
|
7
|
+
init?: InitOptions;
|
|
8
|
+
}
|
|
9
|
+
export type TestOutcome = 'passed' | 'failed' | 'skipped';
|
|
10
|
+
export interface TestRecord {
|
|
11
|
+
name: string;
|
|
12
|
+
outcome: TestOutcome;
|
|
13
|
+
durationMs?: number;
|
|
14
|
+
message?: string;
|
|
15
|
+
endMs?: number;
|
|
16
|
+
/** `suite::file > describe > test` (see test-plan.ts) */
|
|
17
|
+
id?: string;
|
|
18
|
+
/** ticket of a quarantined test; its failure did not fail the run */
|
|
19
|
+
quarantined?: string;
|
|
20
|
+
/** a test the project had never seen (early flake detection) */
|
|
21
|
+
isNew?: boolean;
|
|
22
|
+
/** failed attempts before the final result (retries) */
|
|
23
|
+
attempts?: number;
|
|
24
|
+
}
|
|
25
|
+
/** Client from `opts.client`, else `WT_INGEST_KEY` (+ `WT_PROJECT_ID`); `null` when neither is set. */
|
|
26
|
+
export declare function clientFor(opts?: ReporterOptions): Client | null;
|
|
27
|
+
/**
|
|
28
|
+
* tests: [{ name, outcome: 'passed'|'failed'|'skipped', durationMs, message?, endMs? }]
|
|
29
|
+
* Spans are created after the fact, so start/end times are set explicitly.
|
|
30
|
+
*/
|
|
31
|
+
export declare function reportTests(client: Client | null, tests: TestRecord[], framework?: string): Promise<number>;
|
|
32
|
+
export declare const outcome: (s?: string) => TestOutcome;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.outcome = void 0;
|
|
37
|
+
exports.clientFor = clientFor;
|
|
38
|
+
exports.reportTests = reportTests;
|
|
39
|
+
// Shared by the Jest and Vitest reporters: every finished test becomes a span named
|
|
40
|
+
// `test <full name>` with `test.outcome` / `test.duration_ms`, and, with a project, a
|
|
41
|
+
// `test_pass` score (1 passed, 0 failed; skipped tests get no score). Telemetry failures never
|
|
42
|
+
// fail the test run.
|
|
43
|
+
const api_1 = require("@opentelemetry/api");
|
|
44
|
+
const wt = __importStar(require("./index.js"));
|
|
45
|
+
/** Client from `opts.client`, else `WT_INGEST_KEY` (+ `WT_PROJECT_ID`); `null` when neither is set. */
|
|
46
|
+
function clientFor(opts) {
|
|
47
|
+
const o = opts || {};
|
|
48
|
+
if (o.client)
|
|
49
|
+
return o.client;
|
|
50
|
+
const apiKey = o.apiKey || process.env.WT_INGEST_KEY;
|
|
51
|
+
if (!apiKey)
|
|
52
|
+
return null;
|
|
53
|
+
return wt.init({ apiKey, serviceName: o.serviceName || 'tests', ...o.init });
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* tests: [{ name, outcome: 'passed'|'failed'|'skipped', durationMs, message?, endMs? }]
|
|
57
|
+
* Spans are created after the fact, so start/end times are set explicitly.
|
|
58
|
+
*/
|
|
59
|
+
async function reportTests(client, tests, framework = 'test') {
|
|
60
|
+
if (!client)
|
|
61
|
+
return 0;
|
|
62
|
+
let n = 0;
|
|
63
|
+
for (const t of tests) {
|
|
64
|
+
try {
|
|
65
|
+
const end = t.endMs || Date.now();
|
|
66
|
+
const span = client.tracer.startSpan(`test ${t.name}`, { startTime: end - (t.durationMs || 0) });
|
|
67
|
+
span.setAttribute('test.outcome', t.outcome);
|
|
68
|
+
span.setAttribute('test.duration_ms', t.durationMs || 0);
|
|
69
|
+
// OTel test semconv plus the attributes the pytest plugin sets
|
|
70
|
+
const id = t.id || t.name;
|
|
71
|
+
span.setAttribute('test.framework', framework);
|
|
72
|
+
span.setAttribute('test.suite.name', id.includes('::') ? id.slice(0, id.indexOf('::')) : framework);
|
|
73
|
+
span.setAttribute('test.case.name', id);
|
|
74
|
+
span.setAttribute('test.case.result.status', t.outcome === 'passed' ? 'pass' : t.outcome === 'failed' ? 'fail' : 'skip');
|
|
75
|
+
if (t.quarantined)
|
|
76
|
+
span.setAttribute('test.quarantined', true);
|
|
77
|
+
if (t.isNew) {
|
|
78
|
+
span.setAttribute('test.is_new', true);
|
|
79
|
+
span.setAttribute('test.efd.retries', t.attempts || 0);
|
|
80
|
+
span.setAttribute('test.is_flaky', (t.attempts || 0) > 0 && t.outcome === 'passed');
|
|
81
|
+
}
|
|
82
|
+
if (t.outcome === 'failed') {
|
|
83
|
+
span.setStatus({ code: api_1.SpanStatusCode.ERROR, message: String(t.message || 'failed').slice(0, 500) });
|
|
84
|
+
}
|
|
85
|
+
const traceId = span.spanContext().traceId;
|
|
86
|
+
span.end(end);
|
|
87
|
+
n += 1;
|
|
88
|
+
if (client.project && t.outcome !== 'skipped') {
|
|
89
|
+
await client.score(traceId, t.outcome === 'passed' ? 1 : 0, { name: 'test_pass', comment: t.name });
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
catch (_e) { /* telemetry down must not fail tests */ }
|
|
93
|
+
}
|
|
94
|
+
try {
|
|
95
|
+
await client.provider.forceFlush();
|
|
96
|
+
}
|
|
97
|
+
catch (_e) { /* same */ }
|
|
98
|
+
return n;
|
|
99
|
+
}
|
|
100
|
+
const OUTCOME = { passed: 'passed', pass: 'passed', failed: 'failed', fail: 'failed' };
|
|
101
|
+
const outcome = (s) => OUTCOME[s] || 'skipped';
|
|
102
|
+
exports.outcome = outcome;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** Longest statement looked at, in UTF-8 bytes; the rest is dropped. */
|
|
2
|
+
export declare const MAX_STATEMENT_BYTES: number;
|
|
3
|
+
/**
|
|
4
|
+
* Literal-free statement text: strings, numbers and `$1` become `?`, comments go, whitespace
|
|
5
|
+
* collapses, `IN (?, ?)` becomes `IN (?+)`. `pg` keeps short `"identifiers"` and the `#` operator.
|
|
6
|
+
*/
|
|
7
|
+
export declare function normalizeSql(stmt: string, pg?: boolean): string;
|
|
8
|
+
/** `[normalized text, decimal xxh3_64 of its UTF-8 bytes]`: the `db.query.hash` spans carry. */
|
|
9
|
+
export declare function normalizeAndHash(stmt: string, pg?: boolean): [string, string];
|
|
10
|
+
/** Systems whose dialect keeps `"identifier"` quotes and the `#` operator (as the worker decides). */
|
|
11
|
+
export declare const isPgDialect: (system: string | undefined) => system is "postgresql" | "cockroachdb" | "redshift";
|
|
12
|
+
/** `SELECT users`-style summary: the operation and first table, never values or the statement text. */
|
|
13
|
+
export declare function querySummary(text: string | null | undefined, fallback?: string): {
|
|
14
|
+
operation?: string;
|
|
15
|
+
target?: string;
|
|
16
|
+
summary: string;
|
|
17
|
+
};
|
|
18
|
+
/** XXH3_64bits(input), seed 0. */
|
|
19
|
+
export declare function xxh3(p: Uint8Array): bigint;
|