@refactorlabs/recount 0.5.5
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 +21 -0
- package/README.md +74 -0
- package/dist/chunk-XKR3GXTH.js +33 -0
- package/dist/chunk-XKR3GXTH.js.map +1 -0
- package/dist/index.cjs +980 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +440 -0
- package/dist/index.d.ts +440 -0
- package/dist/index.js +950 -0
- package/dist/index.js.map +1 -0
- package/dist/integrations/anthropic.cjs +137 -0
- package/dist/integrations/anthropic.cjs.map +1 -0
- package/dist/integrations/anthropic.d.cts +8 -0
- package/dist/integrations/anthropic.d.ts +8 -0
- package/dist/integrations/anthropic.js +120 -0
- package/dist/integrations/anthropic.js.map +1 -0
- package/dist/integrations/mongodb.cjs +282 -0
- package/dist/integrations/mongodb.cjs.map +1 -0
- package/dist/integrations/mongodb.d.cts +8 -0
- package/dist/integrations/mongodb.d.ts +8 -0
- package/dist/integrations/mongodb.js +265 -0
- package/dist/integrations/mongodb.js.map +1 -0
- package/dist/integrations/openai.cjs +142 -0
- package/dist/integrations/openai.cjs.map +1 -0
- package/dist/integrations/openai.d.cts +8 -0
- package/dist/integrations/openai.d.ts +8 -0
- package/dist/integrations/openai.js +125 -0
- package/dist/integrations/openai.js.map +1 -0
- package/llms.md +437 -0
- package/package.json +117 -0
- package/recount-instrument/SKILL.md +196 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,980 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var async_hooks = require('async_hooks');
|
|
6
|
+
|
|
7
|
+
var __typeError = (msg) => {
|
|
8
|
+
throw TypeError(msg);
|
|
9
|
+
};
|
|
10
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
11
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
12
|
+
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
13
|
+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), member.set(obj, value), value);
|
|
14
|
+
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
15
|
+
|
|
16
|
+
// src/lib/http.ts
|
|
17
|
+
var HTTP_METHODS = /* @__PURE__ */ new Set(["GET", "POST", "PUT", "PATCH", "DELETE"]);
|
|
18
|
+
function inferContentType(ct) {
|
|
19
|
+
if (ct.includes("application/json")) {
|
|
20
|
+
return "json";
|
|
21
|
+
}
|
|
22
|
+
if (ct.includes("application/x-www-form-urlencoded") || ct.includes("multipart/form-data")) {
|
|
23
|
+
return "form";
|
|
24
|
+
}
|
|
25
|
+
if (ct.startsWith("text/")) {
|
|
26
|
+
return "text";
|
|
27
|
+
}
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
function headersToRecord(headers) {
|
|
31
|
+
const out = {};
|
|
32
|
+
headers.forEach((v, k) => {
|
|
33
|
+
out[k] = v;
|
|
34
|
+
});
|
|
35
|
+
return out;
|
|
36
|
+
}
|
|
37
|
+
function initToRecord(headers) {
|
|
38
|
+
if (!headers) {
|
|
39
|
+
return {};
|
|
40
|
+
}
|
|
41
|
+
if (headers instanceof Headers) {
|
|
42
|
+
return headersToRecord(headers);
|
|
43
|
+
}
|
|
44
|
+
if (Array.isArray(headers)) {
|
|
45
|
+
return Object.fromEntries(headers);
|
|
46
|
+
}
|
|
47
|
+
return { ...headers };
|
|
48
|
+
}
|
|
49
|
+
function parseRequestBody(body, contentType) {
|
|
50
|
+
if (body == null) {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
if (body instanceof URLSearchParams) {
|
|
54
|
+
return Object.fromEntries(body);
|
|
55
|
+
}
|
|
56
|
+
if (typeof body !== "string") {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
if (contentType === "json") {
|
|
60
|
+
try {
|
|
61
|
+
return JSON.parse(body);
|
|
62
|
+
} catch {
|
|
63
|
+
return body;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (contentType === "form") {
|
|
67
|
+
return Object.fromEntries(new URLSearchParams(body));
|
|
68
|
+
}
|
|
69
|
+
return body;
|
|
70
|
+
}
|
|
71
|
+
async function parseResponseBody(response, contentType) {
|
|
72
|
+
if (contentType === "json") {
|
|
73
|
+
try {
|
|
74
|
+
return await response.json();
|
|
75
|
+
} catch {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (contentType === "text" || contentType === "form") {
|
|
80
|
+
return response.text();
|
|
81
|
+
}
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
async function executeFetch(url, init2, recordSpan) {
|
|
85
|
+
const method = init2?.method?.toUpperCase() ?? "GET";
|
|
86
|
+
const reqHeaders = initToRecord(init2?.headers);
|
|
87
|
+
const reqContentType = inferContentType(
|
|
88
|
+
reqHeaders["content-type"] ?? reqHeaders["Content-Type"] ?? ""
|
|
89
|
+
);
|
|
90
|
+
const reqBody = parseRequestBody(init2?.body, reqContentType);
|
|
91
|
+
const start = Date.now();
|
|
92
|
+
const response = await globalThis.fetch(url, init2);
|
|
93
|
+
const durationMs = Date.now() - start;
|
|
94
|
+
const resContentType = inferContentType(
|
|
95
|
+
response.headers.get("content-type") ?? ""
|
|
96
|
+
);
|
|
97
|
+
const resBody = await parseResponseBody(response, resContentType);
|
|
98
|
+
recordSpan(
|
|
99
|
+
{
|
|
100
|
+
method: HTTP_METHODS.has(method) ? method : "GET",
|
|
101
|
+
url: url.toString(),
|
|
102
|
+
headers: reqHeaders,
|
|
103
|
+
content_type: reqContentType,
|
|
104
|
+
body: reqBody
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
status: response.status,
|
|
108
|
+
headers: headersToRecord(response.headers),
|
|
109
|
+
content_type: resContentType,
|
|
110
|
+
body: resBody
|
|
111
|
+
},
|
|
112
|
+
durationMs
|
|
113
|
+
);
|
|
114
|
+
return resBody;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// src/lib/redact.ts
|
|
118
|
+
var REDACTED = { __redacted: true };
|
|
119
|
+
var SENSITIVE_HEADERS = /* @__PURE__ */ new Set([
|
|
120
|
+
"authorization",
|
|
121
|
+
"cookie",
|
|
122
|
+
"set-cookie",
|
|
123
|
+
"x-api-key",
|
|
124
|
+
"x-auth-token",
|
|
125
|
+
"x-api-secret",
|
|
126
|
+
"x-access-token",
|
|
127
|
+
"proxy-authorization"
|
|
128
|
+
]);
|
|
129
|
+
function redactHeaders(headers) {
|
|
130
|
+
return Object.fromEntries(
|
|
131
|
+
Object.entries(headers).map(
|
|
132
|
+
([k, v]) => SENSITIVE_HEADERS.has(k.toLowerCase()) ? [k, REDACTED] : [k, v]
|
|
133
|
+
)
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
function redactAt(obj, paths, currentPath) {
|
|
137
|
+
if (Array.isArray(obj)) {
|
|
138
|
+
return obj.map((item) => redactAt(item, paths, currentPath));
|
|
139
|
+
}
|
|
140
|
+
if (obj !== null && typeof obj === "object") {
|
|
141
|
+
return Object.fromEntries(
|
|
142
|
+
Object.entries(obj).map(([k, v]) => {
|
|
143
|
+
const childPath = [...currentPath, k.toLowerCase()];
|
|
144
|
+
const matches = paths.some(
|
|
145
|
+
(p) => p.length === childPath.length && p.every((seg, i) => seg === childPath[i])
|
|
146
|
+
);
|
|
147
|
+
if (matches) {
|
|
148
|
+
return [k, REDACTED];
|
|
149
|
+
}
|
|
150
|
+
return [k, redactAt(v, paths, childPath)];
|
|
151
|
+
})
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
return obj;
|
|
155
|
+
}
|
|
156
|
+
function redactObject(obj, fields) {
|
|
157
|
+
if (fields.length === 0) return obj;
|
|
158
|
+
const paths = fields.map((f) => f.toLowerCase().split("."));
|
|
159
|
+
return redactAt(obj, paths, []);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// src/trace.ts
|
|
163
|
+
var NOOP_RESULT = { traceId: "" };
|
|
164
|
+
function reservoirSample(items, k) {
|
|
165
|
+
if (k >= items.length) return items.slice();
|
|
166
|
+
const indexed = items.map((item, i) => ({ item, i, key: Math.random() }));
|
|
167
|
+
indexed.sort((a, b) => b.key - a.key);
|
|
168
|
+
return indexed.slice(0, k).sort((a, b) => a.i - b.i).map(({ item }) => item);
|
|
169
|
+
}
|
|
170
|
+
var _buffer;
|
|
171
|
+
var SpanGroup = class {
|
|
172
|
+
constructor() {
|
|
173
|
+
__privateAdd(this, _buffer, []);
|
|
174
|
+
}
|
|
175
|
+
span(type, name, options) {
|
|
176
|
+
__privateGet(this, _buffer).push({ type, name, opts: options });
|
|
177
|
+
return this;
|
|
178
|
+
}
|
|
179
|
+
addMessage(name, text, opts) {
|
|
180
|
+
return this.span("message", name, { text, ...opts });
|
|
181
|
+
}
|
|
182
|
+
_flush(push, options) {
|
|
183
|
+
const { limit, sample, sampleThreshold } = options ?? {};
|
|
184
|
+
let items = __privateGet(this, _buffer).filter(
|
|
185
|
+
({ type, opts }) => type === "group" || type === "message" || opts?.input !== void 0 || opts?.output !== void 0
|
|
186
|
+
);
|
|
187
|
+
if (sample !== void 0) {
|
|
188
|
+
const threshold = sampleThreshold ?? sample;
|
|
189
|
+
if (items.length > threshold) {
|
|
190
|
+
items = reservoirSample(items, sample);
|
|
191
|
+
}
|
|
192
|
+
} else if (limit !== void 0) {
|
|
193
|
+
items = items.slice(0, limit);
|
|
194
|
+
}
|
|
195
|
+
for (const { type, name, opts } of items) {
|
|
196
|
+
push(type, name, opts);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
_buffer = new WeakMap();
|
|
201
|
+
var _client, _name, _options, _spans, _startTime, _disabled, _log, _metadata, _environment, _Trace_instances, pushSpan_fn, applyRedaction_fn;
|
|
202
|
+
var Trace = class {
|
|
203
|
+
constructor(client, name, options = {}, disabled = false, log = () => {
|
|
204
|
+
}) {
|
|
205
|
+
__privateAdd(this, _Trace_instances);
|
|
206
|
+
__privateAdd(this, _client);
|
|
207
|
+
__privateAdd(this, _name);
|
|
208
|
+
__privateAdd(this, _options);
|
|
209
|
+
__privateAdd(this, _spans, []);
|
|
210
|
+
__privateAdd(this, _startTime);
|
|
211
|
+
__privateAdd(this, _disabled);
|
|
212
|
+
__privateAdd(this, _log);
|
|
213
|
+
__privateAdd(this, _metadata);
|
|
214
|
+
__privateAdd(this, _environment);
|
|
215
|
+
__privateSet(this, _client, client);
|
|
216
|
+
__privateSet(this, _name, name);
|
|
217
|
+
__privateSet(this, _options, options);
|
|
218
|
+
__privateSet(this, _startTime, Date.now());
|
|
219
|
+
__privateSet(this, _disabled, disabled);
|
|
220
|
+
__privateSet(this, _log, log);
|
|
221
|
+
__privateSet(this, _metadata, { ...options.metadata });
|
|
222
|
+
__privateSet(this, _environment, options.environment);
|
|
223
|
+
}
|
|
224
|
+
span(type, name, options) {
|
|
225
|
+
if (!__privateGet(this, _disabled)) {
|
|
226
|
+
__privateMethod(this, _Trace_instances, pushSpan_fn).call(this, type, name, options);
|
|
227
|
+
}
|
|
228
|
+
return this;
|
|
229
|
+
}
|
|
230
|
+
group(name, builder, options) {
|
|
231
|
+
if (!__privateGet(this, _disabled)) {
|
|
232
|
+
const groupOrder = __privateGet(this, _spans).length;
|
|
233
|
+
__privateGet(this, _log).call(this, `span type=group name=${name}`);
|
|
234
|
+
const g = new SpanGroup();
|
|
235
|
+
try {
|
|
236
|
+
builder(g);
|
|
237
|
+
} catch {
|
|
238
|
+
}
|
|
239
|
+
__privateGet(this, _spans).push({
|
|
240
|
+
type: "group",
|
|
241
|
+
name,
|
|
242
|
+
order: groupOrder,
|
|
243
|
+
...options?.description !== void 0 && {
|
|
244
|
+
description: options.description
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
const spanCountBeforeFlush = __privateGet(this, _spans).length;
|
|
248
|
+
g._flush(
|
|
249
|
+
(type, spanName, opts) => __privateMethod(this, _Trace_instances, pushSpan_fn).call(this, type, spanName, opts, groupOrder),
|
|
250
|
+
options
|
|
251
|
+
);
|
|
252
|
+
if (__privateGet(this, _spans).length === spanCountBeforeFlush) {
|
|
253
|
+
__privateGet(this, _spans).splice(groupOrder, 1);
|
|
254
|
+
__privateGet(this, _log).call(this, `span type=group name=${name} dropped (no children)`);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return this;
|
|
258
|
+
}
|
|
259
|
+
addMessage(name, text, opts) {
|
|
260
|
+
return this.span("message", name, { text, ...opts });
|
|
261
|
+
}
|
|
262
|
+
addMetadata(data) {
|
|
263
|
+
Object.assign(__privateGet(this, _metadata), data);
|
|
264
|
+
return this;
|
|
265
|
+
}
|
|
266
|
+
setEnvironment(environment) {
|
|
267
|
+
__privateSet(this, _environment, environment);
|
|
268
|
+
return this;
|
|
269
|
+
}
|
|
270
|
+
fetch(name, url, init2, options) {
|
|
271
|
+
return executeFetch(url, init2, (input, output, durationMs) => {
|
|
272
|
+
this.span("http", name, {
|
|
273
|
+
input,
|
|
274
|
+
output,
|
|
275
|
+
durationMs,
|
|
276
|
+
redact: options?.redact
|
|
277
|
+
});
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
async end() {
|
|
281
|
+
if (__privateGet(this, _disabled)) {
|
|
282
|
+
return NOOP_RESULT;
|
|
283
|
+
}
|
|
284
|
+
if (__privateGet(this, _spans).length === 0) {
|
|
285
|
+
__privateGet(this, _log).call(this, "end: no spans, skipping ingest");
|
|
286
|
+
return NOOP_RESULT;
|
|
287
|
+
}
|
|
288
|
+
try {
|
|
289
|
+
return await __privateGet(this, _client).ingest({
|
|
290
|
+
...__privateGet(this, _options),
|
|
291
|
+
name: __privateGet(this, _name),
|
|
292
|
+
environment: __privateGet(this, _environment),
|
|
293
|
+
durationMs: Date.now() - __privateGet(this, _startTime),
|
|
294
|
+
spans: __privateGet(this, _spans),
|
|
295
|
+
metadata: Object.keys(__privateGet(this, _metadata)).length > 0 ? __privateGet(this, _metadata) : void 0
|
|
296
|
+
});
|
|
297
|
+
} catch (err) {
|
|
298
|
+
__privateGet(this, _log).call(this, `ingest error: ${err instanceof Error ? err.message : String(err)}`);
|
|
299
|
+
return NOOP_RESULT;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
_client = new WeakMap();
|
|
304
|
+
_name = new WeakMap();
|
|
305
|
+
_options = new WeakMap();
|
|
306
|
+
_spans = new WeakMap();
|
|
307
|
+
_startTime = new WeakMap();
|
|
308
|
+
_disabled = new WeakMap();
|
|
309
|
+
_log = new WeakMap();
|
|
310
|
+
_metadata = new WeakMap();
|
|
311
|
+
_environment = new WeakMap();
|
|
312
|
+
_Trace_instances = new WeakSet();
|
|
313
|
+
pushSpan_fn = function(type, name, options, parentOrder) {
|
|
314
|
+
if (type !== "group" && type !== "message") {
|
|
315
|
+
if (options?.input === void 0 && options?.output === void 0) {
|
|
316
|
+
__privateGet(this, _log).call(this, `span type=${type} name=${name} dropped (no input or output)`);
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
if (type === "message") {
|
|
321
|
+
__privateGet(this, _spans).push({
|
|
322
|
+
type: "message",
|
|
323
|
+
name,
|
|
324
|
+
...options?.text !== void 0 && { text: options.text },
|
|
325
|
+
level: options?.level ?? "info",
|
|
326
|
+
order: __privateGet(this, _spans).length,
|
|
327
|
+
...parentOrder !== void 0 && { parentOrder },
|
|
328
|
+
...options?.description !== void 0 && {
|
|
329
|
+
description: options.description
|
|
330
|
+
},
|
|
331
|
+
...options?.alerts && { alerts: options.alerts },
|
|
332
|
+
...options?.durationMs !== void 0 && {
|
|
333
|
+
durationMs: options.durationMs
|
|
334
|
+
},
|
|
335
|
+
...options?.public !== void 0 && { public: options.public }
|
|
336
|
+
});
|
|
337
|
+
} else {
|
|
338
|
+
__privateGet(this, _spans).push({
|
|
339
|
+
type,
|
|
340
|
+
name,
|
|
341
|
+
order: __privateGet(this, _spans).length,
|
|
342
|
+
...parentOrder !== void 0 && { parentOrder },
|
|
343
|
+
...__privateMethod(this, _Trace_instances, applyRedaction_fn).call(this, type, options)
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
__privateGet(this, _log).call(this, `span type=${type} name=${name}`);
|
|
347
|
+
};
|
|
348
|
+
applyRedaction_fn = function(type, options) {
|
|
349
|
+
if (!options) return {};
|
|
350
|
+
const { redact, redactParams, ...rest } = options;
|
|
351
|
+
let { input, output } = rest;
|
|
352
|
+
if (type === "http") {
|
|
353
|
+
if (input && typeof input === "object" && !Array.isArray(input)) {
|
|
354
|
+
const inp = input;
|
|
355
|
+
if (inp.headers && typeof inp.headers === "object") {
|
|
356
|
+
input = {
|
|
357
|
+
...inp,
|
|
358
|
+
headers: redactHeaders(inp.headers)
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
if (output && typeof output === "object" && !Array.isArray(output)) {
|
|
363
|
+
const out = output;
|
|
364
|
+
if (out.headers && typeof out.headers === "object") {
|
|
365
|
+
output = {
|
|
366
|
+
...out,
|
|
367
|
+
headers: redactHeaders(out.headers)
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
if (redact && redact.length > 0) {
|
|
373
|
+
input = input != null ? redactObject(input, redact) : input;
|
|
374
|
+
output = output != null ? redactObject(output, redact) : output;
|
|
375
|
+
}
|
|
376
|
+
if (redactParams?.length && input != null && !Array.isArray(input)) {
|
|
377
|
+
const inp = input;
|
|
378
|
+
if (Array.isArray(inp.params)) {
|
|
379
|
+
input = {
|
|
380
|
+
...inp,
|
|
381
|
+
params: inp.params.map(
|
|
382
|
+
(p) => redactParams.includes(p) ? REDACTED : p
|
|
383
|
+
)
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
return { ...rest, input, output };
|
|
388
|
+
};
|
|
389
|
+
|
|
390
|
+
// src/lib/error.ts
|
|
391
|
+
var RecountError = class extends Error {
|
|
392
|
+
constructor(status, message) {
|
|
393
|
+
super(message);
|
|
394
|
+
this.name = "RecountError";
|
|
395
|
+
this.status = status;
|
|
396
|
+
}
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
// src/client.ts
|
|
400
|
+
var DEFAULT_API_URL = "https://api.recount.refactor.gg";
|
|
401
|
+
var NOOP_RESULT2 = { traceId: "" };
|
|
402
|
+
var MAX_RETRIES = 2;
|
|
403
|
+
var BASE_RETRY_MS = 1e3;
|
|
404
|
+
var MAX_RETRY_DELAY_MS = 6e4;
|
|
405
|
+
function retryDelayMs(res, attempt) {
|
|
406
|
+
const header = res.headers.get("retry-after");
|
|
407
|
+
if (header) {
|
|
408
|
+
const seconds = Number(header);
|
|
409
|
+
if (!isNaN(seconds))
|
|
410
|
+
return Math.min(MAX_RETRY_DELAY_MS, Math.max(0, seconds * 1e3));
|
|
411
|
+
const date = Date.parse(header);
|
|
412
|
+
if (!isNaN(date))
|
|
413
|
+
return Math.min(MAX_RETRY_DELAY_MS, Math.max(0, date - Date.now()));
|
|
414
|
+
}
|
|
415
|
+
return BASE_RETRY_MS * 2 ** (attempt - 1);
|
|
416
|
+
}
|
|
417
|
+
var _apiKey, _apiUrl, _environment2, _disabled2, _log2;
|
|
418
|
+
var Recount = class {
|
|
419
|
+
constructor(options = {}) {
|
|
420
|
+
__privateAdd(this, _apiKey);
|
|
421
|
+
__privateAdd(this, _apiUrl);
|
|
422
|
+
__privateAdd(this, _environment2);
|
|
423
|
+
__privateAdd(this, _disabled2);
|
|
424
|
+
__privateAdd(this, _log2);
|
|
425
|
+
const debug = options.debug ?? !!process.env.RECOUNT_DEBUG;
|
|
426
|
+
__privateSet(this, _log2, debug ? (msg) => console.log(`[recount] ${msg}`) : () => {
|
|
427
|
+
});
|
|
428
|
+
const apiKey = options.apiKey ?? process.env.RECOUNT_API_KEY;
|
|
429
|
+
if (!apiKey) {
|
|
430
|
+
console.warn("Recount: no API key found - tracing disabled.");
|
|
431
|
+
__privateSet(this, _disabled2, true);
|
|
432
|
+
__privateSet(this, _apiKey, "");
|
|
433
|
+
__privateSet(this, _apiUrl, "");
|
|
434
|
+
__privateSet(this, _environment2, void 0);
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
__privateSet(this, _disabled2, false);
|
|
438
|
+
__privateSet(this, _apiKey, apiKey);
|
|
439
|
+
__privateSet(this, _apiUrl, options.apiUrl ?? process.env.RECOUNT_API_URL ?? DEFAULT_API_URL);
|
|
440
|
+
__privateSet(this, _environment2, options.environment ?? process.env.RECOUNT_ENVIRONMENT ?? void 0);
|
|
441
|
+
const masked = __privateGet(this, _apiKey).length > 8 ? `${__privateGet(this, _apiKey).slice(0, 4)}...` : "[redacted]";
|
|
442
|
+
__privateGet(this, _log2).call(this, `initialized url=${__privateGet(this, _apiUrl)} key=${masked}`);
|
|
443
|
+
}
|
|
444
|
+
startTrace(name, options = {}) {
|
|
445
|
+
__privateGet(this, _log2).call(this, `trace started name=${name}`);
|
|
446
|
+
const traceOptions = __privateGet(this, _environment2) !== void 0 && options.environment === void 0 ? { environment: __privateGet(this, _environment2), ...options } : options;
|
|
447
|
+
return new Trace(this, name, traceOptions, __privateGet(this, _disabled2), __privateGet(this, _log2));
|
|
448
|
+
}
|
|
449
|
+
async ingest(payload) {
|
|
450
|
+
if (__privateGet(this, _disabled2)) {
|
|
451
|
+
return NOOP_RESULT2;
|
|
452
|
+
}
|
|
453
|
+
if (!payload.spans.length) {
|
|
454
|
+
console.warn("Recount: trace has no spans and will not be ingested.");
|
|
455
|
+
return NOOP_RESULT2;
|
|
456
|
+
}
|
|
457
|
+
__privateGet(this, _log2).call(this, `ingest name=${payload.name} spans=${payload.spans.length}`);
|
|
458
|
+
let body;
|
|
459
|
+
try {
|
|
460
|
+
body = JSON.stringify(payload);
|
|
461
|
+
} catch (err) {
|
|
462
|
+
__privateGet(this, _log2).call(this, `ingest serialize error: ${err instanceof Error ? err.message : String(err)}`);
|
|
463
|
+
return NOOP_RESULT2;
|
|
464
|
+
}
|
|
465
|
+
const headers = {
|
|
466
|
+
"Content-Type": "application/json",
|
|
467
|
+
"x-api-key": __privateGet(this, _apiKey)
|
|
468
|
+
};
|
|
469
|
+
for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
|
|
470
|
+
try {
|
|
471
|
+
const res = await globalThis.fetch(`${__privateGet(this, _apiUrl)}/ingest`, {
|
|
472
|
+
method: "POST",
|
|
473
|
+
headers,
|
|
474
|
+
body
|
|
475
|
+
});
|
|
476
|
+
if (res.status === 429 && attempt < MAX_RETRIES) {
|
|
477
|
+
const delay = retryDelayMs(res, attempt + 1);
|
|
478
|
+
__privateGet(this, _log2).call(this, `429 rate limited attempt=${attempt + 1}/${MAX_RETRIES + 1} retrying in ${delay}ms`);
|
|
479
|
+
await res.body?.cancel().catch(() => {
|
|
480
|
+
});
|
|
481
|
+
await new Promise((r) => setTimeout(r, delay));
|
|
482
|
+
continue;
|
|
483
|
+
}
|
|
484
|
+
if (!res.ok) {
|
|
485
|
+
const text = await res.text().catch(() => res.statusText);
|
|
486
|
+
__privateGet(this, _log2).call(this, `ingest error status=${res.status} message=${text}`);
|
|
487
|
+
return NOOP_RESULT2;
|
|
488
|
+
}
|
|
489
|
+
const json = await res.json();
|
|
490
|
+
__privateGet(this, _log2).call(this, `ingested traceId=${json.data.trace.id}`);
|
|
491
|
+
return { traceId: json.data.trace.id };
|
|
492
|
+
} catch (err) {
|
|
493
|
+
__privateGet(this, _log2).call(this, `ingest failed (network error): ${err instanceof Error ? err.message : String(err)}`);
|
|
494
|
+
return NOOP_RESULT2;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
return NOOP_RESULT2;
|
|
498
|
+
}
|
|
499
|
+
};
|
|
500
|
+
_apiKey = new WeakMap();
|
|
501
|
+
_apiUrl = new WeakMap();
|
|
502
|
+
_environment2 = new WeakMap();
|
|
503
|
+
_disabled2 = new WeakMap();
|
|
504
|
+
_log2 = new WeakMap();
|
|
505
|
+
var _key = /* @__PURE__ */ Symbol.for("@refactorlabs/recount.storage");
|
|
506
|
+
var _nameKey = /* @__PURE__ */ Symbol.for("@refactorlabs/recount.spanName");
|
|
507
|
+
var _g = globalThis;
|
|
508
|
+
if (!_g[_key]) _g[_key] = new async_hooks.AsyncLocalStorage();
|
|
509
|
+
if (!_g[_nameKey]) _g[_nameKey] = new async_hooks.AsyncLocalStorage();
|
|
510
|
+
var storage = _g[_key];
|
|
511
|
+
var nameStorage = _g[_nameKey];
|
|
512
|
+
function getActiveTrace() {
|
|
513
|
+
return storage.getStore();
|
|
514
|
+
}
|
|
515
|
+
function runWithTrace(trace, fn) {
|
|
516
|
+
return storage.run(trace, fn);
|
|
517
|
+
}
|
|
518
|
+
function runWithSpanName(name, fn) {
|
|
519
|
+
return nameStorage.run(name, fn);
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
// src/lib/table.ts
|
|
523
|
+
function spanCell(value, span) {
|
|
524
|
+
return { __cellSpan: true, span, value };
|
|
525
|
+
}
|
|
526
|
+
function roundValue(value, decimals) {
|
|
527
|
+
if (typeof value === "number") {
|
|
528
|
+
return +value.toFixed(decimals);
|
|
529
|
+
}
|
|
530
|
+
return value;
|
|
531
|
+
}
|
|
532
|
+
var TableBuilder = class {
|
|
533
|
+
constructor(title) {
|
|
534
|
+
this._rawKeys = [];
|
|
535
|
+
this._rawRows = [];
|
|
536
|
+
this._labels = {};
|
|
537
|
+
this._valueTransforms = {};
|
|
538
|
+
this._columnSpans = {};
|
|
539
|
+
this._columnHighlights = {};
|
|
540
|
+
this._cellHighlightFns = {};
|
|
541
|
+
this._extraRows = [];
|
|
542
|
+
this._redactFields = /* @__PURE__ */ new Set();
|
|
543
|
+
this._title = title;
|
|
544
|
+
}
|
|
545
|
+
redact(fields) {
|
|
546
|
+
this._redactFields = new Set(fields.map((f) => f.toLowerCase()));
|
|
547
|
+
return this;
|
|
548
|
+
}
|
|
549
|
+
_shouldRedact(key) {
|
|
550
|
+
return this._redactFields.has(key.toLowerCase());
|
|
551
|
+
}
|
|
552
|
+
fromObject(data) {
|
|
553
|
+
if (data == null || typeof data !== "object" || Array.isArray(data)) {
|
|
554
|
+
if (process.env.RECOUNT_DEBUG) {
|
|
555
|
+
console.log("[recount] table: fromObject expects a plain object");
|
|
556
|
+
}
|
|
557
|
+
return this;
|
|
558
|
+
}
|
|
559
|
+
this._source = "object";
|
|
560
|
+
this._rawKeys = Object.keys(data);
|
|
561
|
+
this._rawRows = Object.entries(data).map(([k, v]) => [k, v]);
|
|
562
|
+
return this;
|
|
563
|
+
}
|
|
564
|
+
fromRecords(data) {
|
|
565
|
+
this._source = "records";
|
|
566
|
+
if (!Array.isArray(data) || data.length === 0) {
|
|
567
|
+
this._rawKeys = [];
|
|
568
|
+
this._rawRows = [];
|
|
569
|
+
return this;
|
|
570
|
+
}
|
|
571
|
+
const first = data[0];
|
|
572
|
+
if (first == null || typeof first !== "object") {
|
|
573
|
+
if (process.env.RECOUNT_DEBUG) {
|
|
574
|
+
console.log("[recount] table: fromRecords expects an array of objects");
|
|
575
|
+
}
|
|
576
|
+
this._rawKeys = [];
|
|
577
|
+
this._rawRows = [];
|
|
578
|
+
return this;
|
|
579
|
+
}
|
|
580
|
+
this._rawKeys = Object.keys(first);
|
|
581
|
+
this._rawRows = data.filter((r) => r != null && typeof r === "object").map((r) => this._rawKeys.map((k) => r[k]));
|
|
582
|
+
return this;
|
|
583
|
+
}
|
|
584
|
+
pick(...keys) {
|
|
585
|
+
this._pick = keys;
|
|
586
|
+
return this;
|
|
587
|
+
}
|
|
588
|
+
label(map) {
|
|
589
|
+
Object.assign(this._labels, map);
|
|
590
|
+
return this;
|
|
591
|
+
}
|
|
592
|
+
round(decimals) {
|
|
593
|
+
this._round = decimals;
|
|
594
|
+
return this;
|
|
595
|
+
}
|
|
596
|
+
transformValues(map) {
|
|
597
|
+
Object.assign(this._valueTransforms, map);
|
|
598
|
+
return this;
|
|
599
|
+
}
|
|
600
|
+
spanColumn(key, span) {
|
|
601
|
+
this._columnSpans[key] = span;
|
|
602
|
+
return this;
|
|
603
|
+
}
|
|
604
|
+
highlightColumn(key, highlight) {
|
|
605
|
+
this._columnHighlights[key] = highlight;
|
|
606
|
+
return this;
|
|
607
|
+
}
|
|
608
|
+
highlightRows(fn) {
|
|
609
|
+
this._rowHighlightFn = fn;
|
|
610
|
+
return this;
|
|
611
|
+
}
|
|
612
|
+
highlightCells(key, fn) {
|
|
613
|
+
this._cellHighlightFns[key] = fn;
|
|
614
|
+
return this;
|
|
615
|
+
}
|
|
616
|
+
appendRow(cells, highlight) {
|
|
617
|
+
this._extraRows.push({ cells, ...highlight && { highlight } });
|
|
618
|
+
return this;
|
|
619
|
+
}
|
|
620
|
+
summary(stats) {
|
|
621
|
+
this._summary = stats;
|
|
622
|
+
return this;
|
|
623
|
+
}
|
|
624
|
+
build() {
|
|
625
|
+
return this._build();
|
|
626
|
+
}
|
|
627
|
+
buildSection() {
|
|
628
|
+
const result = this._build();
|
|
629
|
+
return {
|
|
630
|
+
...result.title !== void 0 && { title: result.title },
|
|
631
|
+
rows: result.rows,
|
|
632
|
+
...result.rowHighlights && { rowHighlights: result.rowHighlights },
|
|
633
|
+
...result.cellHighlights && { cellHighlights: result.cellHighlights },
|
|
634
|
+
...result.summary && { summary: result.summary }
|
|
635
|
+
};
|
|
636
|
+
}
|
|
637
|
+
_build() {
|
|
638
|
+
try {
|
|
639
|
+
if (!this._source) {
|
|
640
|
+
if (process.env.RECOUNT_DEBUG) {
|
|
641
|
+
console.log(
|
|
642
|
+
"[recount] table: call fromObject() or fromRecords() before build()"
|
|
643
|
+
);
|
|
644
|
+
}
|
|
645
|
+
return { columns: [], rows: [] };
|
|
646
|
+
}
|
|
647
|
+
if (this._source === "object") {
|
|
648
|
+
return this._buildFromObject();
|
|
649
|
+
}
|
|
650
|
+
return this._buildFromRecords();
|
|
651
|
+
} catch {
|
|
652
|
+
if (process.env.RECOUNT_DEBUG) {
|
|
653
|
+
console.log("[recount] table: build() encountered an unexpected error");
|
|
654
|
+
}
|
|
655
|
+
return { columns: [], rows: [] };
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
_resolveLabel(key) {
|
|
659
|
+
return this._labels[key] ?? key;
|
|
660
|
+
}
|
|
661
|
+
_resolveColumn(key, label) {
|
|
662
|
+
const span = this._columnSpans[key];
|
|
663
|
+
const highlight = this._columnHighlights[key];
|
|
664
|
+
if (span !== void 0 || highlight !== void 0) {
|
|
665
|
+
return {
|
|
666
|
+
label,
|
|
667
|
+
...span !== void 0 && { span },
|
|
668
|
+
...highlight !== void 0 && { highlight }
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
return label;
|
|
672
|
+
}
|
|
673
|
+
_mergeRowHighlights(highlights, rowCount) {
|
|
674
|
+
if (!highlights && !this._extraRows.some((r) => r.highlight))
|
|
675
|
+
return void 0;
|
|
676
|
+
return [
|
|
677
|
+
...highlights ?? Array(rowCount).fill(null),
|
|
678
|
+
...this._extraRows.map((r) => r.highlight ?? null)
|
|
679
|
+
];
|
|
680
|
+
}
|
|
681
|
+
_buildRowHighlights(activeKeys) {
|
|
682
|
+
if (!this._rowHighlightFn) {
|
|
683
|
+
return void 0;
|
|
684
|
+
}
|
|
685
|
+
const keyToIndex = new Map(this._rawKeys.map((k, i) => [k, i]));
|
|
686
|
+
return this._rawRows.map((row) => {
|
|
687
|
+
const obj = Object.fromEntries(
|
|
688
|
+
activeKeys.map((k) => [k, row[keyToIndex.get(k)]])
|
|
689
|
+
);
|
|
690
|
+
return this._rowHighlightFn(obj);
|
|
691
|
+
});
|
|
692
|
+
}
|
|
693
|
+
_buildFromObject() {
|
|
694
|
+
const pick = this._pick ? new Set(this._pick) : null;
|
|
695
|
+
let rows = this._rawRows.filter(([k]) => !pick || pick.has(k));
|
|
696
|
+
if (pick) {
|
|
697
|
+
const order = this._pick;
|
|
698
|
+
rows = order.filter((k) => rows.some(([rk]) => rk === k)).map((k) => rows.find(([rk]) => rk === k));
|
|
699
|
+
}
|
|
700
|
+
const finalRows = rows.map(([k, v]) => {
|
|
701
|
+
const key = k;
|
|
702
|
+
if (this._shouldRedact(key)) {
|
|
703
|
+
return [this._resolveLabel(key), REDACTED];
|
|
704
|
+
}
|
|
705
|
+
const transformed = this._valueTransforms[key] ? this._valueTransforms[key](v) : v;
|
|
706
|
+
return [
|
|
707
|
+
this._resolveLabel(key),
|
|
708
|
+
this._round !== void 0 ? roundValue(transformed, this._round) : transformed
|
|
709
|
+
];
|
|
710
|
+
});
|
|
711
|
+
const allRows = this._extraRows.length ? [...finalRows, ...this._extraRows.map((r) => r.cells)] : finalRows;
|
|
712
|
+
const rowHighlights = this._rowHighlightFn ? rows.map(([k, v]) => this._rowHighlightFn({ [k]: v })) : void 0;
|
|
713
|
+
const allRowHighlights = this._mergeRowHighlights(
|
|
714
|
+
rowHighlights,
|
|
715
|
+
finalRows.length
|
|
716
|
+
);
|
|
717
|
+
return {
|
|
718
|
+
title: this._title,
|
|
719
|
+
columns: ["Key", "Value"],
|
|
720
|
+
rows: allRows,
|
|
721
|
+
...allRowHighlights && { rowHighlights: allRowHighlights },
|
|
722
|
+
...this._summary && { summary: this._summary }
|
|
723
|
+
};
|
|
724
|
+
}
|
|
725
|
+
_buildFromRecords() {
|
|
726
|
+
const activeKeys = this._pick ?? this._rawKeys;
|
|
727
|
+
const columns = activeKeys.map(
|
|
728
|
+
(k) => this._resolveColumn(k, this._resolveLabel(k))
|
|
729
|
+
);
|
|
730
|
+
const keyToIndex = new Map(this._rawKeys.map((k, i) => [k, i]));
|
|
731
|
+
const rows = this._rawRows.map(
|
|
732
|
+
(row) => activeKeys.map((k) => {
|
|
733
|
+
if (this._shouldRedact(k)) return REDACTED;
|
|
734
|
+
const raw = row[keyToIndex.get(k)];
|
|
735
|
+
const value = this._valueTransforms[k] ? this._valueTransforms[k](raw) : raw;
|
|
736
|
+
return this._round !== void 0 ? roundValue(value, this._round) : value;
|
|
737
|
+
})
|
|
738
|
+
);
|
|
739
|
+
const rowHighlights = this._buildRowHighlights(activeKeys);
|
|
740
|
+
const hasCellFns = Object.keys(this._cellHighlightFns).length > 0;
|
|
741
|
+
const cellHighlights = hasCellFns ? rows.map(
|
|
742
|
+
(row) => activeKeys.map(
|
|
743
|
+
(k, j) => this._cellHighlightFns[k] ? this._cellHighlightFns[k](row[j]) : null
|
|
744
|
+
)
|
|
745
|
+
) : void 0;
|
|
746
|
+
const allRows = this._extraRows.length ? [...rows, ...this._extraRows.map((r) => r.cells)] : rows;
|
|
747
|
+
const allRowHighlights = this._mergeRowHighlights(
|
|
748
|
+
rowHighlights,
|
|
749
|
+
rows.length
|
|
750
|
+
);
|
|
751
|
+
const allCellHighlights = cellHighlights ? [...cellHighlights, ...this._extraRows.map(() => [])] : void 0;
|
|
752
|
+
return {
|
|
753
|
+
title: this._title,
|
|
754
|
+
columns,
|
|
755
|
+
rows: allRows,
|
|
756
|
+
...allRowHighlights && { rowHighlights: allRowHighlights },
|
|
757
|
+
...allCellHighlights && { cellHighlights: allCellHighlights },
|
|
758
|
+
...this._summary && { summary: this._summary }
|
|
759
|
+
};
|
|
760
|
+
}
|
|
761
|
+
};
|
|
762
|
+
function table(title) {
|
|
763
|
+
return new TableBuilder(title);
|
|
764
|
+
}
|
|
765
|
+
function tableFromObject({
|
|
766
|
+
title,
|
|
767
|
+
data
|
|
768
|
+
}) {
|
|
769
|
+
return table(title).fromObject(data).build();
|
|
770
|
+
}
|
|
771
|
+
function tableFromRecords({
|
|
772
|
+
title,
|
|
773
|
+
data
|
|
774
|
+
}) {
|
|
775
|
+
return table(title).fromRecords(data).build();
|
|
776
|
+
}
|
|
777
|
+
function isTableBuilderLike(s) {
|
|
778
|
+
return typeof s.buildSection === "function";
|
|
779
|
+
}
|
|
780
|
+
function tableSections({
|
|
781
|
+
title,
|
|
782
|
+
columns,
|
|
783
|
+
sections
|
|
784
|
+
}) {
|
|
785
|
+
let resolvedColumns = columns;
|
|
786
|
+
let cachedFirst;
|
|
787
|
+
if (!resolvedColumns) {
|
|
788
|
+
const first = sections[0];
|
|
789
|
+
if (isTableBuilderLike(first)) {
|
|
790
|
+
const built = first.build();
|
|
791
|
+
resolvedColumns = built.columns;
|
|
792
|
+
cachedFirst = {
|
|
793
|
+
...built.title !== void 0 && { title: built.title },
|
|
794
|
+
rows: built.rows,
|
|
795
|
+
...built.rowHighlights && { rowHighlights: built.rowHighlights },
|
|
796
|
+
...built.cellHighlights && { cellHighlights: built.cellHighlights },
|
|
797
|
+
...built.summary && { summary: built.summary }
|
|
798
|
+
};
|
|
799
|
+
} else {
|
|
800
|
+
throw new Error(
|
|
801
|
+
"tableSections: columns is required when sections are plain objects"
|
|
802
|
+
);
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
const resolved = sections.map((s, i) => {
|
|
806
|
+
if (i === 0 && cachedFirst !== void 0) return cachedFirst;
|
|
807
|
+
return isTableBuilderLike(s) ? s.buildSection() : s;
|
|
808
|
+
});
|
|
809
|
+
return {
|
|
810
|
+
...title !== void 0 && { title },
|
|
811
|
+
columns: resolvedColumns,
|
|
812
|
+
sections: resolved
|
|
813
|
+
};
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
// src/lib/chart.ts
|
|
817
|
+
var ChartBuilder = class {
|
|
818
|
+
constructor(type, title) {
|
|
819
|
+
this._labels = [];
|
|
820
|
+
this._series = [];
|
|
821
|
+
this._referenceLines = [];
|
|
822
|
+
this._type = type;
|
|
823
|
+
this._title = title;
|
|
824
|
+
}
|
|
825
|
+
labels(labels) {
|
|
826
|
+
this._labels = labels;
|
|
827
|
+
return this;
|
|
828
|
+
}
|
|
829
|
+
series(nameOrData, data) {
|
|
830
|
+
if (typeof nameOrData === "string") {
|
|
831
|
+
this._series.push({ name: nameOrData, data });
|
|
832
|
+
} else {
|
|
833
|
+
this._series.push({ data: nameOrData });
|
|
834
|
+
}
|
|
835
|
+
return this;
|
|
836
|
+
}
|
|
837
|
+
fromRecords(data, labelKey, seriesKeys) {
|
|
838
|
+
this._labels = data.map((r) => String(r[labelKey]));
|
|
839
|
+
this._series = seriesKeys.map((k) => ({
|
|
840
|
+
name: String(k),
|
|
841
|
+
data: data.map((r) => Number(r[k]))
|
|
842
|
+
}));
|
|
843
|
+
return this;
|
|
844
|
+
}
|
|
845
|
+
referenceLine(value, label) {
|
|
846
|
+
this._referenceLines.push({ value, label });
|
|
847
|
+
return this;
|
|
848
|
+
}
|
|
849
|
+
build() {
|
|
850
|
+
return this._build();
|
|
851
|
+
}
|
|
852
|
+
_build() {
|
|
853
|
+
return {
|
|
854
|
+
title: this._title,
|
|
855
|
+
type: this._type,
|
|
856
|
+
labels: this._labels,
|
|
857
|
+
series: this._series,
|
|
858
|
+
...this._referenceLines.length > 0 && {
|
|
859
|
+
referenceLines: this._referenceLines
|
|
860
|
+
}
|
|
861
|
+
};
|
|
862
|
+
}
|
|
863
|
+
};
|
|
864
|
+
function chart(type, title) {
|
|
865
|
+
return new ChartBuilder(type, title);
|
|
866
|
+
}
|
|
867
|
+
function chartFromRecords({
|
|
868
|
+
type,
|
|
869
|
+
title,
|
|
870
|
+
data,
|
|
871
|
+
labelKey,
|
|
872
|
+
seriesKeys
|
|
873
|
+
}) {
|
|
874
|
+
return chart(type, title).fromRecords(data, labelKey, seriesKeys).build();
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
// src/index.ts
|
|
878
|
+
var _client2;
|
|
879
|
+
function getClient() {
|
|
880
|
+
if (!_client2) {
|
|
881
|
+
_client2 = new Recount();
|
|
882
|
+
}
|
|
883
|
+
return _client2;
|
|
884
|
+
}
|
|
885
|
+
function init(options = {}) {
|
|
886
|
+
_client2 = new Recount(options);
|
|
887
|
+
}
|
|
888
|
+
async function startTrace(name, optionsOrFn, fn) {
|
|
889
|
+
const options = typeof optionsOrFn === "function" ? {} : optionsOrFn;
|
|
890
|
+
const callback = typeof optionsOrFn === "function" ? optionsOrFn : fn;
|
|
891
|
+
const trace = getClient().startTrace(name, options);
|
|
892
|
+
try {
|
|
893
|
+
return await runWithTrace(trace, callback);
|
|
894
|
+
} finally {
|
|
895
|
+
await trace.end();
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
function addSpan(type, name, a, b) {
|
|
899
|
+
if (typeof a === "function") {
|
|
900
|
+
return runSpanCallback(type, name, {}, a);
|
|
901
|
+
}
|
|
902
|
+
if (typeof b === "function") {
|
|
903
|
+
return runSpanCallback(type, name, a ?? {}, b);
|
|
904
|
+
}
|
|
905
|
+
getActiveTrace()?.span(type, name, a ?? {});
|
|
906
|
+
}
|
|
907
|
+
async function runSpanCallback(type, name, opts, fn) {
|
|
908
|
+
const { input, ...spanOpts } = opts;
|
|
909
|
+
const start = Date.now();
|
|
910
|
+
try {
|
|
911
|
+
const result = await fn();
|
|
912
|
+
getActiveTrace()?.span(type, name, {
|
|
913
|
+
...spanOpts,
|
|
914
|
+
...input != null ? { input } : {},
|
|
915
|
+
output: result != null ? result : void 0,
|
|
916
|
+
durationMs: Date.now() - start
|
|
917
|
+
});
|
|
918
|
+
return result;
|
|
919
|
+
} catch (err) {
|
|
920
|
+
getActiveTrace()?.span(type, name, {
|
|
921
|
+
...spanOpts,
|
|
922
|
+
...input != null ? { input } : {},
|
|
923
|
+
durationMs: Date.now() - start
|
|
924
|
+
});
|
|
925
|
+
throw err;
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
function addGroup(name, builder, options) {
|
|
929
|
+
getActiveTrace()?.group(name, builder, options);
|
|
930
|
+
}
|
|
931
|
+
function addMessage(name, text, opts) {
|
|
932
|
+
getActiveTrace()?.addMessage(name, text, opts);
|
|
933
|
+
}
|
|
934
|
+
function named(name, fn) {
|
|
935
|
+
return runWithSpanName(name, fn);
|
|
936
|
+
}
|
|
937
|
+
function addMetadata(data) {
|
|
938
|
+
getActiveTrace()?.addMetadata(data);
|
|
939
|
+
}
|
|
940
|
+
function fetch(name, url, init2, options) {
|
|
941
|
+
const active = getActiveTrace();
|
|
942
|
+
return executeFetch(url, init2, (input, output, durationMs) => {
|
|
943
|
+
active?.span("http", name, {
|
|
944
|
+
input,
|
|
945
|
+
output,
|
|
946
|
+
durationMs,
|
|
947
|
+
redact: options?.redact
|
|
948
|
+
});
|
|
949
|
+
});
|
|
950
|
+
}
|
|
951
|
+
var recount = {
|
|
952
|
+
// SDK lifecycle
|
|
953
|
+
init,
|
|
954
|
+
Recount,
|
|
955
|
+
RecountError,
|
|
956
|
+
// Tracing
|
|
957
|
+
startTrace,
|
|
958
|
+
named,
|
|
959
|
+
addSpan,
|
|
960
|
+
addGroup,
|
|
961
|
+
addMessage,
|
|
962
|
+
addMetadata,
|
|
963
|
+
fetch,
|
|
964
|
+
// Builders
|
|
965
|
+
table,
|
|
966
|
+
tableFromObject,
|
|
967
|
+
tableFromRecords,
|
|
968
|
+
tableSections,
|
|
969
|
+
spanCell,
|
|
970
|
+
chart,
|
|
971
|
+
chartFromRecords,
|
|
972
|
+
// Constants & sentinels
|
|
973
|
+
REDACTED
|
|
974
|
+
};
|
|
975
|
+
var index_default = recount;
|
|
976
|
+
|
|
977
|
+
exports.SpanGroup = SpanGroup;
|
|
978
|
+
exports.default = index_default;
|
|
979
|
+
//# sourceMappingURL=index.cjs.map
|
|
980
|
+
//# sourceMappingURL=index.cjs.map
|