braintrust 3.25.0 → 3.26.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/NOTICE +6 -0
- package/dev/dist/index.d.mts +130 -2
- package/dev/dist/index.d.ts +130 -2
- package/dev/dist/index.js +2455 -1116
- package/dev/dist/index.mjs +1599 -260
- package/dist/apply-auto-instrumentation.js +439 -398
- package/dist/apply-auto-instrumentation.mjs +242 -201
- package/dist/auto-instrumentations/bundler/esbuild.cjs +853 -337
- package/dist/auto-instrumentations/bundler/esbuild.d.mts +4 -4
- package/dist/auto-instrumentations/bundler/esbuild.d.ts +4 -4
- package/dist/auto-instrumentations/bundler/esbuild.mjs +5 -8
- package/dist/auto-instrumentations/bundler/next.cjs +852 -356
- package/dist/auto-instrumentations/bundler/next.mjs +6 -29
- package/dist/auto-instrumentations/bundler/rollup.cjs +853 -337
- package/dist/auto-instrumentations/bundler/rollup.d.mts +4 -4
- package/dist/auto-instrumentations/bundler/rollup.d.ts +4 -4
- package/dist/auto-instrumentations/bundler/rollup.mjs +5 -8
- package/dist/auto-instrumentations/bundler/vite.cjs +853 -337
- package/dist/auto-instrumentations/bundler/vite.d.mts +4 -4
- package/dist/auto-instrumentations/bundler/vite.d.ts +4 -4
- package/dist/auto-instrumentations/bundler/vite.mjs +5 -8
- package/dist/auto-instrumentations/bundler/webpack-loader.cjs +1006 -266
- package/dist/auto-instrumentations/bundler/webpack-loader.d.ts +20 -12
- package/dist/auto-instrumentations/bundler/webpack.cjs +853 -337
- package/dist/auto-instrumentations/bundler/webpack.d.mts +4 -4
- package/dist/auto-instrumentations/bundler/webpack.d.ts +4 -4
- package/dist/auto-instrumentations/bundler/webpack.mjs +5 -4
- package/dist/auto-instrumentations/{chunk-JPVCUKTY.mjs → chunk-6E22MYSW.mjs} +165 -73
- package/dist/auto-instrumentations/{chunk-VT6DDNKM.mjs → chunk-AKKPLXTP.mjs} +631 -194
- package/dist/auto-instrumentations/chunk-NRE4QUQR.mjs +509 -0
- package/dist/auto-instrumentations/{chunk-7P6563SW.mjs → chunk-V5LEODRX.mjs} +2 -6
- package/dist/auto-instrumentations/{chunk-M6DLIJ2Z.mjs → chunk-XYN63IG5.mjs} +124 -194
- package/dist/auto-instrumentations/{chunk-CZ24KNHT.mjs → chunk-XZHHE4Y3.mjs} +77 -68
- package/dist/auto-instrumentations/hook.mjs +247 -182
- package/dist/auto-instrumentations/index.cjs +557 -48
- package/dist/auto-instrumentations/index.d.mts +6 -2
- package/dist/auto-instrumentations/index.d.ts +6 -2
- package/dist/auto-instrumentations/index.mjs +6 -1
- package/dist/auto-instrumentations/loader/cjs-patch.cjs +492 -195
- package/dist/auto-instrumentations/loader/cjs-patch.d.mts +2 -2
- package/dist/auto-instrumentations/loader/cjs-patch.d.ts +2 -2
- package/dist/auto-instrumentations/loader/cjs-patch.mjs +3 -2
- package/dist/auto-instrumentations/loader/esm-hook.mjs +1 -1
- package/dist/auto-instrumentations/plugin-B-aZh7EP.d.ts +30 -0
- package/dist/auto-instrumentations/plugin-Bnj4E_6B.d.mts +30 -0
- package/dist/auto-instrumentations/{types-RNPaKi9o.d.mts → types-C1fVeiCp.d.mts} +1 -1
- package/dist/auto-instrumentations/{types-RNPaKi9o.d.ts → types-C1fVeiCp.d.ts} +1 -1
- package/dist/browser.d.mts +817 -197
- package/dist/browser.d.ts +817 -197
- package/dist/browser.js +1883 -265
- package/dist/browser.mjs +1883 -265
- package/dist/{chunk-ABR2QWDP.mjs → chunk-PN7EWK66.mjs} +581 -133
- package/dist/{chunk-GSIDVFE6.js → chunk-VRZZQPAA.js} +1670 -736
- package/dist/{chunk-P25IOOU4.js → chunk-VYNNEKSA.js} +586 -138
- package/dist/{chunk-2SANLSWX.mjs → chunk-XIZOM2HO.mjs} +1052 -118
- package/dist/cli.js +1601 -263
- package/dist/edge-light.d.mts +1 -1
- package/dist/edge-light.d.ts +1 -1
- package/dist/edge-light.js +1883 -268
- package/dist/edge-light.mjs +1883 -268
- package/dist/index.d.mts +817 -197
- package/dist/index.d.ts +817 -197
- package/dist/index.js +695 -435
- package/dist/index.mjs +264 -4
- package/dist/instrumentation/index.d.mts +130 -6
- package/dist/instrumentation/index.d.ts +130 -6
- package/dist/instrumentation/index.js +1510 -80
- package/dist/instrumentation/index.mjs +1510 -80
- package/dist/vitest-evals-reporter.js +16 -16
- package/dist/vitest-evals-reporter.mjs +2 -2
- package/dist/workerd.d.mts +1 -1
- package/dist/workerd.d.ts +1 -1
- package/dist/workerd.js +1883 -268
- package/dist/workerd.mjs +1883 -268
- package/licenses/node-diagnostics-channel/LICENSE +19 -0
- package/package.json +34 -35
- package/dist/auto-instrumentations/plugin-D0KHwSJv.d.mts +0 -44
- package/dist/auto-instrumentations/plugin-G6fgxk1b.d.ts +0 -44
|
@@ -34,52 +34,583 @@ var import_source_map = require("source-map");
|
|
|
34
34
|
// src/auto-instrumentations/orchestrion-js/transforms.ts
|
|
35
35
|
var import_esquery = __toESM(require("esquery"));
|
|
36
36
|
var import_meriyah = require("meriyah");
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
37
|
+
|
|
38
|
+
// src/global-instrumentation-hooks.ts
|
|
39
|
+
var GLOBAL_INSTRUMENTATION_HOOKS_KEY = "__braintrust_instrumentation_hooks";
|
|
40
|
+
var GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION = 1;
|
|
41
|
+
var GLOBAL_INSTRUMENTATION_HOOKS_REGISTRY_BRAND = "braintrust.global-instrumentation-hooks.registry";
|
|
42
|
+
var GLOBAL_INSTRUMENTATION_HOOK_BRAND = "braintrust.global-instrumentation-hooks.hook";
|
|
43
|
+
var registryBrand = Symbol.for(GLOBAL_INSTRUMENTATION_HOOKS_REGISTRY_BRAND);
|
|
44
|
+
var hookBrand = Symbol.for(GLOBAL_INSTRUMENTATION_HOOK_BRAND);
|
|
45
|
+
var errorReporter;
|
|
46
|
+
function reportError(error) {
|
|
47
|
+
try {
|
|
48
|
+
errorReporter?.(error);
|
|
49
|
+
} catch {
|
|
50
|
+
}
|
|
41
51
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
52
|
+
function setContextValue(context, key, value) {
|
|
53
|
+
try {
|
|
54
|
+
context[key] = value;
|
|
55
|
+
} catch (error) {
|
|
56
|
+
reportError(error);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function wrapStoreRun(store, message, next, transform) {
|
|
60
|
+
return () => {
|
|
61
|
+
let context;
|
|
62
|
+
try {
|
|
63
|
+
context = transform ? transform(message) : message;
|
|
64
|
+
} catch (error) {
|
|
65
|
+
reportError(error);
|
|
66
|
+
return next();
|
|
67
|
+
}
|
|
68
|
+
let called = false;
|
|
69
|
+
let result;
|
|
70
|
+
let providerError;
|
|
71
|
+
let providerThrew = false;
|
|
72
|
+
const runNext = () => {
|
|
73
|
+
if (called) {
|
|
74
|
+
reportError(
|
|
75
|
+
new Error(
|
|
76
|
+
"Instrumentation store invoked its callback more than once"
|
|
77
|
+
)
|
|
78
|
+
);
|
|
79
|
+
if (providerThrew) {
|
|
80
|
+
throw providerError;
|
|
81
|
+
}
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
called = true;
|
|
85
|
+
try {
|
|
86
|
+
result = next();
|
|
87
|
+
return result;
|
|
88
|
+
} catch (error) {
|
|
89
|
+
providerThrew = true;
|
|
90
|
+
providerError = error;
|
|
91
|
+
throw error;
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
try {
|
|
95
|
+
store.run(context, runNext);
|
|
96
|
+
} catch (error) {
|
|
97
|
+
if (!providerThrew || error !== providerError) {
|
|
98
|
+
reportError(error);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (!called) {
|
|
102
|
+
reportError(
|
|
103
|
+
new Error("Instrumentation store did not invoke its callback")
|
|
104
|
+
);
|
|
105
|
+
return runNext();
|
|
106
|
+
}
|
|
107
|
+
if (providerThrew) {
|
|
108
|
+
throw providerError;
|
|
109
|
+
}
|
|
110
|
+
return result;
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
var HookChannel = class {
|
|
114
|
+
constructor(name) {
|
|
115
|
+
this.name = name;
|
|
116
|
+
}
|
|
117
|
+
name;
|
|
118
|
+
subscribers = [];
|
|
119
|
+
stores = /* @__PURE__ */ new Map();
|
|
120
|
+
get hasSubscribers() {
|
|
121
|
+
return this.subscribers.length > 0 || this.stores.size > 0;
|
|
122
|
+
}
|
|
123
|
+
subscribe(subscription) {
|
|
124
|
+
if (typeof subscription !== "function") {
|
|
125
|
+
throw new TypeError("subscription must be a function");
|
|
126
|
+
}
|
|
127
|
+
this.subscribers = [...this.subscribers, subscription];
|
|
128
|
+
}
|
|
129
|
+
unsubscribe(subscription) {
|
|
130
|
+
const index = this.subscribers.indexOf(subscription);
|
|
131
|
+
if (index === -1) {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
this.subscribers = [
|
|
135
|
+
...this.subscribers.slice(0, index),
|
|
136
|
+
...this.subscribers.slice(index + 1)
|
|
137
|
+
];
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
bindStore(store, transform) {
|
|
141
|
+
if (!store || typeof store.run !== "function") {
|
|
142
|
+
throw new TypeError("store must have a run method");
|
|
143
|
+
}
|
|
144
|
+
this.stores.set(
|
|
145
|
+
store,
|
|
146
|
+
transform
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
unbindStore(store) {
|
|
150
|
+
return this.stores.delete(store);
|
|
151
|
+
}
|
|
152
|
+
publish(message) {
|
|
153
|
+
const subscribers = this.subscribers;
|
|
154
|
+
for (const subscriber of subscribers) {
|
|
155
|
+
try {
|
|
156
|
+
subscriber(message, this.name);
|
|
157
|
+
} catch (error) {
|
|
158
|
+
reportError(error);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
runStores(message, fn, thisArg, ...args) {
|
|
163
|
+
let run = () => {
|
|
164
|
+
this.publish(message);
|
|
165
|
+
return Reflect.apply(fn, thisArg, args);
|
|
166
|
+
};
|
|
167
|
+
for (const [store, transform] of this.stores.entries()) {
|
|
168
|
+
run = wrapStoreRun(store, message, run, transform);
|
|
169
|
+
}
|
|
170
|
+
return run();
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
var traceEvents = [
|
|
174
|
+
"start",
|
|
175
|
+
"end",
|
|
176
|
+
"asyncStart",
|
|
177
|
+
"asyncEnd",
|
|
178
|
+
"error"
|
|
179
|
+
];
|
|
180
|
+
var TracingHook = class {
|
|
181
|
+
start;
|
|
182
|
+
end;
|
|
183
|
+
asyncStart;
|
|
184
|
+
asyncEnd;
|
|
185
|
+
error;
|
|
186
|
+
constructor(nameOrChannels) {
|
|
187
|
+
Object.defineProperty(this, hookBrand, {
|
|
188
|
+
configurable: false,
|
|
189
|
+
enumerable: false,
|
|
190
|
+
value: GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION,
|
|
191
|
+
writable: false
|
|
192
|
+
});
|
|
193
|
+
if (typeof nameOrChannels === "string") {
|
|
194
|
+
this.start = new HookChannel(`tracing:${nameOrChannels}:start`);
|
|
195
|
+
this.end = new HookChannel(`tracing:${nameOrChannels}:end`);
|
|
196
|
+
this.asyncStart = new HookChannel(`tracing:${nameOrChannels}:asyncStart`);
|
|
197
|
+
this.asyncEnd = new HookChannel(`tracing:${nameOrChannels}:asyncEnd`);
|
|
198
|
+
this.error = new HookChannel(`tracing:${nameOrChannels}:error`);
|
|
45
199
|
return;
|
|
46
200
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
201
|
+
this.start = nameOrChannels.start ?? new HookChannel("tracing:start");
|
|
202
|
+
this.end = nameOrChannels.end ?? new HookChannel("tracing:end");
|
|
203
|
+
this.asyncStart = nameOrChannels.asyncStart ?? new HookChannel("tracing:asyncStart");
|
|
204
|
+
this.asyncEnd = nameOrChannels.asyncEnd ?? new HookChannel("tracing:asyncEnd");
|
|
205
|
+
this.error = nameOrChannels.error ?? new HookChannel("tracing:error");
|
|
206
|
+
}
|
|
207
|
+
get hasSubscribers() {
|
|
208
|
+
return this.start.hasSubscribers || this.end.hasSubscribers || this.asyncStart.hasSubscribers || this.asyncEnd.hasSubscribers || this.error.hasSubscribers;
|
|
209
|
+
}
|
|
210
|
+
subscribe(handlers) {
|
|
211
|
+
for (const eventName of traceEvents) {
|
|
212
|
+
const handler = handlers[eventName];
|
|
213
|
+
if (handler) {
|
|
214
|
+
this[eventName].subscribe(handler);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
unsubscribe(handlers) {
|
|
219
|
+
let done = true;
|
|
220
|
+
for (const eventName of traceEvents) {
|
|
221
|
+
const handler = handlers[eventName];
|
|
222
|
+
if (handler && !this[eventName].unsubscribe(handler)) {
|
|
223
|
+
done = false;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
return done;
|
|
227
|
+
}
|
|
228
|
+
traceSync(fn, message = {}, thisArg, ...args) {
|
|
229
|
+
if (!this.hasSubscribers) {
|
|
230
|
+
return Reflect.apply(fn, thisArg, args);
|
|
231
|
+
}
|
|
232
|
+
const context = message;
|
|
233
|
+
return this.start.runStores(message, () => {
|
|
234
|
+
try {
|
|
235
|
+
const result = Reflect.apply(fn, thisArg, args);
|
|
236
|
+
setContextValue(context, "result", result);
|
|
237
|
+
return result;
|
|
238
|
+
} catch (error) {
|
|
239
|
+
setContextValue(context, "error", error);
|
|
240
|
+
this.error.publish(message);
|
|
241
|
+
throw error;
|
|
242
|
+
} finally {
|
|
243
|
+
this.end.publish(message);
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
tracePromise(fn, message = {}, thisArg, ...args) {
|
|
248
|
+
if (!this.hasSubscribers) {
|
|
249
|
+
return Reflect.apply(fn, thisArg, args);
|
|
250
|
+
}
|
|
251
|
+
const context = message;
|
|
252
|
+
return this.start.runStores(message, () => {
|
|
253
|
+
let result;
|
|
254
|
+
try {
|
|
255
|
+
result = Reflect.apply(fn, thisArg, args);
|
|
256
|
+
} catch (error) {
|
|
257
|
+
setContextValue(context, "error", error);
|
|
258
|
+
this.error.publish(message);
|
|
259
|
+
this.end.publish(message);
|
|
260
|
+
throw error;
|
|
261
|
+
}
|
|
262
|
+
this.end.publish(message);
|
|
263
|
+
if (!result || typeof result !== "object" && typeof result !== "function") {
|
|
264
|
+
setContextValue(context, "result", result);
|
|
265
|
+
this.asyncStart.publish(message);
|
|
266
|
+
this.asyncEnd.publish(message);
|
|
267
|
+
return result;
|
|
268
|
+
}
|
|
269
|
+
let terminalPublished = false;
|
|
270
|
+
const finishUnobservedResult = (error) => {
|
|
271
|
+
reportError(error);
|
|
272
|
+
if (!terminalPublished) {
|
|
273
|
+
terminalPublished = true;
|
|
274
|
+
setContextValue(context, "result", result);
|
|
275
|
+
this.asyncStart.publish(message);
|
|
276
|
+
this.asyncEnd.publish(message);
|
|
277
|
+
}
|
|
278
|
+
return result;
|
|
279
|
+
};
|
|
280
|
+
let then;
|
|
281
|
+
try {
|
|
282
|
+
then = result.then;
|
|
283
|
+
} catch (error) {
|
|
284
|
+
return finishUnobservedResult(error);
|
|
285
|
+
}
|
|
286
|
+
if (typeof then !== "function") {
|
|
287
|
+
setContextValue(context, "result", result);
|
|
288
|
+
this.asyncStart.publish(message);
|
|
289
|
+
this.asyncEnd.publish(message);
|
|
290
|
+
return result;
|
|
291
|
+
}
|
|
292
|
+
const resolve = (resolved) => {
|
|
293
|
+
if (!terminalPublished) {
|
|
294
|
+
terminalPublished = true;
|
|
295
|
+
setContextValue(context, "result", resolved);
|
|
296
|
+
this.asyncStart.publish(message);
|
|
297
|
+
this.asyncEnd.publish(message);
|
|
298
|
+
}
|
|
299
|
+
return resolved;
|
|
300
|
+
};
|
|
301
|
+
let rejectionThrown = false;
|
|
302
|
+
let rejectionError;
|
|
303
|
+
const reject = (error) => {
|
|
304
|
+
if (!terminalPublished) {
|
|
305
|
+
terminalPublished = true;
|
|
306
|
+
setContextValue(context, "error", error);
|
|
307
|
+
this.error.publish(message);
|
|
308
|
+
this.asyncStart.publish(message);
|
|
309
|
+
this.asyncEnd.publish(message);
|
|
310
|
+
}
|
|
311
|
+
rejectionThrown = true;
|
|
312
|
+
rejectionError = error;
|
|
313
|
+
throw error;
|
|
314
|
+
};
|
|
315
|
+
let isPlainPromise;
|
|
316
|
+
try {
|
|
317
|
+
isPlainPromise = result instanceof Promise && result.constructor === Promise;
|
|
318
|
+
} catch (error) {
|
|
319
|
+
return finishUnobservedResult(error);
|
|
320
|
+
}
|
|
321
|
+
try {
|
|
322
|
+
if (isPlainPromise) {
|
|
323
|
+
return Reflect.apply(then, result, [resolve, reject]);
|
|
324
|
+
}
|
|
325
|
+
Reflect.apply(then, result, [
|
|
326
|
+
resolve,
|
|
327
|
+
(error) => {
|
|
328
|
+
try {
|
|
329
|
+
reject(error);
|
|
330
|
+
} catch {
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
]);
|
|
334
|
+
} catch (error) {
|
|
335
|
+
if (rejectionThrown && error === rejectionError) {
|
|
336
|
+
return result;
|
|
337
|
+
}
|
|
338
|
+
return finishUnobservedResult(error);
|
|
339
|
+
}
|
|
340
|
+
return result;
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
traceCallback(fn, position = -1, message = {}, thisArg, ...args) {
|
|
344
|
+
if (!this.hasSubscribers) {
|
|
345
|
+
return Reflect.apply(fn, thisArg, args);
|
|
346
|
+
}
|
|
347
|
+
const context = message;
|
|
348
|
+
const callArgs = args.length > 0 ? args : context.arguments ?? args;
|
|
349
|
+
const callback = Array.prototype.at.call(callArgs, position);
|
|
350
|
+
if (typeof callback !== "function") {
|
|
351
|
+
return Reflect.apply(fn, thisArg, args);
|
|
352
|
+
}
|
|
353
|
+
const { asyncStart, asyncEnd, error: errorChannel } = this;
|
|
354
|
+
function wrappedCallback(error, result) {
|
|
355
|
+
if (error) {
|
|
356
|
+
setContextValue(context, "error", error);
|
|
357
|
+
errorChannel.publish(message);
|
|
358
|
+
} else {
|
|
359
|
+
setContextValue(context, "result", result);
|
|
360
|
+
}
|
|
361
|
+
return asyncStart.runStores(message, () => {
|
|
362
|
+
try {
|
|
363
|
+
return Reflect.apply(callback, this, arguments);
|
|
364
|
+
} finally {
|
|
365
|
+
asyncEnd.publish(message);
|
|
366
|
+
}
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
Array.prototype.splice.call(callArgs, position, 1, wrappedCallback);
|
|
370
|
+
return this.start.runStores(message, () => {
|
|
371
|
+
try {
|
|
372
|
+
return Reflect.apply(fn, thisArg, args);
|
|
373
|
+
} catch (error) {
|
|
374
|
+
setContextValue(context, "error", error);
|
|
375
|
+
this.error.publish(message);
|
|
376
|
+
throw error;
|
|
377
|
+
} finally {
|
|
378
|
+
this.end.publish(message);
|
|
379
|
+
}
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
};
|
|
383
|
+
var inertChannel = Object.freeze({
|
|
384
|
+
name: "braintrust:inert",
|
|
385
|
+
hasSubscribers: false,
|
|
386
|
+
subscribe() {
|
|
387
|
+
},
|
|
388
|
+
unsubscribe() {
|
|
389
|
+
return false;
|
|
390
|
+
},
|
|
391
|
+
bindStore() {
|
|
392
|
+
},
|
|
393
|
+
unbindStore() {
|
|
394
|
+
return false;
|
|
395
|
+
},
|
|
396
|
+
publish() {
|
|
397
|
+
},
|
|
398
|
+
runStores(_message, fn, thisArg, ...args) {
|
|
399
|
+
return Reflect.apply(fn, thisArg, args);
|
|
400
|
+
}
|
|
401
|
+
});
|
|
402
|
+
var inertTracingHook = new TracingHook({
|
|
403
|
+
start: inertChannel,
|
|
404
|
+
end: inertChannel,
|
|
405
|
+
asyncStart: inertChannel,
|
|
406
|
+
asyncEnd: inertChannel,
|
|
407
|
+
error: inertChannel
|
|
408
|
+
});
|
|
409
|
+
function isHookChannel(value) {
|
|
410
|
+
if (typeof value !== "object" && typeof value !== "function" || value === null) {
|
|
411
|
+
return false;
|
|
412
|
+
}
|
|
413
|
+
try {
|
|
414
|
+
const channel2 = value;
|
|
415
|
+
return typeof channel2.hasSubscribers === "boolean" && typeof channel2.subscribe === "function" && typeof channel2.unsubscribe === "function" && typeof channel2.bindStore === "function" && typeof channel2.unbindStore === "function" && typeof channel2.publish === "function" && typeof channel2.runStores === "function";
|
|
416
|
+
} catch {
|
|
417
|
+
return false;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
function hasTracingHookShape(value) {
|
|
421
|
+
if (typeof value !== "object" && typeof value !== "function" || value === null) {
|
|
422
|
+
return false;
|
|
423
|
+
}
|
|
424
|
+
try {
|
|
425
|
+
const hook = value;
|
|
426
|
+
return typeof hook.hasSubscribers === "boolean" && typeof hook.subscribe === "function" && typeof hook.unsubscribe === "function" && typeof hook.traceSync === "function" && typeof hook.tracePromise === "function" && typeof hook.traceCallback === "function" && isHookChannel(hook.start) && isHookChannel(hook.end) && isHookChannel(hook.asyncStart) && isHookChannel(hook.asyncEnd) && isHookChannel(hook.error);
|
|
427
|
+
} catch {
|
|
428
|
+
return false;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
function isCompatibleTracingHook(value) {
|
|
432
|
+
try {
|
|
433
|
+
return value[hookBrand] === GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION && hasTracingHookShape(value);
|
|
434
|
+
} catch {
|
|
435
|
+
return false;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
function isCompatibleHookRegistry(value) {
|
|
439
|
+
try {
|
|
440
|
+
return value instanceof Map && value[registryBrand] === GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION;
|
|
441
|
+
} catch {
|
|
442
|
+
return false;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
function getHookRegistry() {
|
|
446
|
+
let descriptor;
|
|
447
|
+
try {
|
|
448
|
+
descriptor = Object.getOwnPropertyDescriptor(
|
|
449
|
+
globalThis,
|
|
450
|
+
GLOBAL_INSTRUMENTATION_HOOKS_KEY
|
|
50
451
|
);
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
452
|
+
} catch (error) {
|
|
453
|
+
reportError(error);
|
|
454
|
+
return void 0;
|
|
455
|
+
}
|
|
456
|
+
if (descriptor && "value" in descriptor && isCompatibleHookRegistry(descriptor.value) && (descriptor.configurable || descriptor.enumerable === false)) {
|
|
457
|
+
if (descriptor.configurable || descriptor.writable) {
|
|
458
|
+
try {
|
|
459
|
+
Object.defineProperty(globalThis, GLOBAL_INSTRUMENTATION_HOOKS_KEY, {
|
|
460
|
+
configurable: false,
|
|
461
|
+
enumerable: false,
|
|
462
|
+
value: descriptor.value,
|
|
463
|
+
writable: false
|
|
464
|
+
});
|
|
465
|
+
} catch (error) {
|
|
466
|
+
reportError(error);
|
|
467
|
+
return void 0;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
return descriptor.value;
|
|
471
|
+
}
|
|
472
|
+
if (descriptor && !descriptor.configurable) {
|
|
473
|
+
reportError(new Error("Incompatible global instrumentation hook registry"));
|
|
474
|
+
return void 0;
|
|
475
|
+
}
|
|
476
|
+
const registry = /* @__PURE__ */ new Map();
|
|
477
|
+
try {
|
|
478
|
+
Object.defineProperty(registry, registryBrand, {
|
|
479
|
+
configurable: false,
|
|
480
|
+
enumerable: false,
|
|
481
|
+
value: GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION,
|
|
482
|
+
writable: false
|
|
483
|
+
});
|
|
484
|
+
} catch (error) {
|
|
485
|
+
reportError(error);
|
|
486
|
+
return void 0;
|
|
487
|
+
}
|
|
488
|
+
try {
|
|
489
|
+
Object.defineProperty(globalThis, GLOBAL_INSTRUMENTATION_HOOKS_KEY, {
|
|
490
|
+
configurable: false,
|
|
491
|
+
enumerable: false,
|
|
492
|
+
value: registry,
|
|
493
|
+
writable: false
|
|
494
|
+
});
|
|
495
|
+
return registry;
|
|
496
|
+
} catch (error) {
|
|
497
|
+
reportError(error);
|
|
498
|
+
return void 0;
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
function newGlobalTracingChannel(nameOrChannels) {
|
|
502
|
+
if (typeof nameOrChannels !== "string") {
|
|
503
|
+
return new TracingHook(nameOrChannels);
|
|
504
|
+
}
|
|
505
|
+
const registry = getHookRegistry();
|
|
506
|
+
if (!registry) {
|
|
507
|
+
return inertTracingHook;
|
|
508
|
+
}
|
|
509
|
+
let existing;
|
|
510
|
+
try {
|
|
511
|
+
existing = Map.prototype.get.call(registry, nameOrChannels);
|
|
512
|
+
} catch (error) {
|
|
513
|
+
reportError(error);
|
|
514
|
+
return inertTracingHook;
|
|
515
|
+
}
|
|
516
|
+
if (isCompatibleTracingHook(existing)) {
|
|
517
|
+
return existing;
|
|
518
|
+
}
|
|
519
|
+
if (existing !== void 0) {
|
|
520
|
+
reportError(
|
|
521
|
+
new Error(`Invalid global instrumentation hook: ${nameOrChannels}`)
|
|
64
522
|
);
|
|
65
|
-
|
|
66
|
-
|
|
523
|
+
try {
|
|
524
|
+
Map.prototype.delete.call(registry, nameOrChannels);
|
|
525
|
+
} catch (error) {
|
|
526
|
+
reportError(error);
|
|
527
|
+
return inertTracingHook;
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
const hook = new TracingHook(nameOrChannels);
|
|
531
|
+
try {
|
|
532
|
+
Map.prototype.set.call(registry, nameOrChannels, hook);
|
|
533
|
+
} catch (error) {
|
|
534
|
+
reportError(error);
|
|
535
|
+
return inertTracingHook;
|
|
536
|
+
}
|
|
537
|
+
return hook;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
// src/auto-instrumentations/orchestrion-js/transforms.ts
|
|
541
|
+
var CHANNEL_REGEX = /[^\w]/g;
|
|
542
|
+
var SHARED_HOOK_LOOKUP = "tr_ch_bt$get_hook";
|
|
543
|
+
function formatChannelVariable(channelName) {
|
|
544
|
+
return `tr_ch_bt$${channelName.replace(CHANNEL_REGEX, "_")}`;
|
|
545
|
+
}
|
|
546
|
+
function formatChannelGetter(channelName) {
|
|
547
|
+
return `tr_ch_bt$get_${channelName.replace(CHANNEL_REGEX, "_")}`;
|
|
548
|
+
}
|
|
549
|
+
var transforms = {
|
|
550
|
+
tracingHookDeclaration(state, node) {
|
|
67
551
|
const {
|
|
68
552
|
channelName,
|
|
69
|
-
module: { name }
|
|
553
|
+
module: { name },
|
|
554
|
+
operator
|
|
70
555
|
} = state;
|
|
71
556
|
const channelVariable = formatChannelVariable(channelName);
|
|
557
|
+
const channelGetter = formatChannelGetter(channelName);
|
|
558
|
+
const hasSharedHookLookup = node.body.some(
|
|
559
|
+
(child) => child.declarations?.[0]?.id?.name === SHARED_HOOK_LOOKUP
|
|
560
|
+
);
|
|
72
561
|
if (node.body.some(
|
|
73
|
-
(child) => child.declarations?.[0]?.id?.name ===
|
|
562
|
+
(child) => child.declarations?.[0]?.id?.name === channelGetter
|
|
74
563
|
)) {
|
|
75
564
|
return;
|
|
76
565
|
}
|
|
77
|
-
|
|
78
|
-
|
|
566
|
+
const sharedHookLookup = hasSharedHookLookup ? "" : `
|
|
567
|
+
const ${SHARED_HOOK_LOOKUP} = (__bt$hookName, __bt$operator) => {
|
|
568
|
+
try {
|
|
569
|
+
const __bt$hooks = globalThis[${JSON.stringify(
|
|
570
|
+
GLOBAL_INSTRUMENTATION_HOOKS_KEY
|
|
571
|
+
)}];
|
|
572
|
+
if (
|
|
573
|
+
!(__bt$hooks instanceof Map) ||
|
|
574
|
+
__bt$hooks[Symbol.for(${JSON.stringify(
|
|
575
|
+
GLOBAL_INSTRUMENTATION_HOOKS_REGISTRY_BRAND
|
|
576
|
+
)})] !== ${GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION}
|
|
577
|
+
) return undefined;
|
|
578
|
+
const __bt$hook = Map.prototype.get.call(
|
|
579
|
+
__bt$hooks,
|
|
580
|
+
__bt$hookName
|
|
581
|
+
);
|
|
582
|
+
if (
|
|
583
|
+
(__bt$hook === null ||
|
|
584
|
+
(typeof __bt$hook !== "object" &&
|
|
585
|
+
typeof __bt$hook !== "function")) ||
|
|
586
|
+
__bt$hook[Symbol.for(${JSON.stringify(
|
|
587
|
+
GLOBAL_INSTRUMENTATION_HOOK_BRAND
|
|
588
|
+
)})] !== ${GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION} ||
|
|
589
|
+
typeof __bt$hook.hasSubscribers !== "boolean" ||
|
|
590
|
+
typeof __bt$hook[__bt$operator] !== "function"
|
|
591
|
+
) return undefined;
|
|
592
|
+
return __bt$hook;
|
|
593
|
+
} catch {
|
|
594
|
+
return undefined;
|
|
595
|
+
}
|
|
596
|
+
};
|
|
597
|
+
`;
|
|
79
598
|
const code = `
|
|
80
|
-
|
|
599
|
+
${sharedHookLookup}
|
|
600
|
+
let ${channelVariable};
|
|
601
|
+
const ${channelGetter} = () =>
|
|
602
|
+
${channelVariable} ??= ${SHARED_HOOK_LOOKUP}(
|
|
603
|
+
${JSON.stringify(`orchestrion:${name}:${channelName}`)},
|
|
604
|
+
${JSON.stringify(operator)}
|
|
605
|
+
);
|
|
81
606
|
`;
|
|
82
|
-
node.body.
|
|
607
|
+
const sharedHookLookupIndex = node.body.findIndex(
|
|
608
|
+
(child) => child.declarations?.[0]?.id?.name === SHARED_HOOK_LOOKUP
|
|
609
|
+
);
|
|
610
|
+
const index = sharedHookLookupIndex === -1 ? node.body.findIndex(
|
|
611
|
+
(child) => child.directive === "use strict"
|
|
612
|
+
) : sharedHookLookupIndex;
|
|
613
|
+
node.body.splice(index + 1, 0, ...(0, import_meriyah.parse)(code).body);
|
|
83
614
|
},
|
|
84
615
|
traceCallback: traceAny,
|
|
85
616
|
tracePromise: traceAny,
|
|
@@ -94,25 +625,21 @@ function traceAny(state, node, _parent, ancestry) {
|
|
|
94
625
|
}
|
|
95
626
|
}
|
|
96
627
|
function traceFunction(state, node, program) {
|
|
97
|
-
transforms.
|
|
628
|
+
transforms.tracingHookDeclaration(state, program, null, []);
|
|
98
629
|
const { functionQuery } = state;
|
|
99
630
|
const methodName = "methodName" in functionQuery ? functionQuery.methodName : void 0;
|
|
100
631
|
const privateMethodName = "privateMethodName" in functionQuery ? functionQuery.privateMethodName : void 0;
|
|
101
632
|
const functionName = "functionName" in functionQuery ? functionQuery.functionName : void 0;
|
|
102
633
|
const isConstructor = methodName === "constructor" || !methodName && !privateMethodName && !functionName;
|
|
103
634
|
const type = isConstructor ? "ArrowFunctionExpression" : "FunctionExpression";
|
|
104
|
-
node.body = wrap(
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
generator: node.generator
|
|
113
|
-
},
|
|
114
|
-
program
|
|
115
|
-
);
|
|
635
|
+
node.body = wrap(state, {
|
|
636
|
+
type,
|
|
637
|
+
params: node.params,
|
|
638
|
+
body: node.body,
|
|
639
|
+
async: node.async,
|
|
640
|
+
expression: false,
|
|
641
|
+
generator: node.generator
|
|
642
|
+
});
|
|
116
643
|
node.generator = false;
|
|
117
644
|
node.async = false;
|
|
118
645
|
wrapSuper(node);
|
|
@@ -124,11 +651,11 @@ function traceInstanceMethod(state, node, program) {
|
|
|
124
651
|
return;
|
|
125
652
|
}
|
|
126
653
|
const classBody = node.body;
|
|
127
|
-
if (classBody.body.some(({ key }) => key
|
|
654
|
+
if (classBody.body.some(({ key }) => key?.name === methodName)) {
|
|
128
655
|
return;
|
|
129
656
|
}
|
|
130
657
|
let ctor = classBody.body.find(({ kind }) => kind === "constructor");
|
|
131
|
-
transforms.
|
|
658
|
+
transforms.tracingHookDeclaration(state, program, null, []);
|
|
132
659
|
if (!ctor) {
|
|
133
660
|
ctor = (0, import_meriyah.parse)(
|
|
134
661
|
node.superClass ? "class A extends Object { constructor (...args) { super(...args) } }" : "class A { constructor () {} }"
|
|
@@ -136,47 +663,46 @@ function traceInstanceMethod(state, node, program) {
|
|
|
136
663
|
classBody.body.unshift(ctor);
|
|
137
664
|
}
|
|
138
665
|
const ctorBody = (0, import_meriyah.parse)(`
|
|
139
|
-
const
|
|
666
|
+
const __bt$${methodName} = this["${methodName}"]
|
|
140
667
|
this["${methodName}"] = function () {}
|
|
141
668
|
`).body;
|
|
142
669
|
const fn = ctorBody[1].expression.right;
|
|
143
670
|
fn.async = operator === "tracePromise";
|
|
144
|
-
fn.body = wrap(
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
);
|
|
671
|
+
fn.body = wrap(state, {
|
|
672
|
+
type: "Identifier",
|
|
673
|
+
name: `__bt$${methodName}`
|
|
674
|
+
});
|
|
149
675
|
wrapSuper(fn);
|
|
150
676
|
ctor.value.body.body.push(...ctorBody);
|
|
151
677
|
}
|
|
152
|
-
function wrap(state, node
|
|
678
|
+
function wrap(state, node) {
|
|
153
679
|
const { operator, moduleVersion } = state;
|
|
154
680
|
const wrapper = operator === "traceCallback" ? wrapCallback(state) : operator === "tracePromise" ? wrapPromise(state) : wrapSync(state);
|
|
155
681
|
const block = wrapper.body[0].body;
|
|
156
682
|
const common = (0, import_meriyah.parse)(
|
|
157
683
|
node.type === "ArrowFunctionExpression" ? `
|
|
158
|
-
const
|
|
684
|
+
const __bt$ctx = {
|
|
159
685
|
arguments,
|
|
160
686
|
moduleVersion: ${JSON.stringify(moduleVersion)}
|
|
161
687
|
};
|
|
162
|
-
const
|
|
163
|
-
const
|
|
164
|
-
return
|
|
688
|
+
const __bt$traced = () => {
|
|
689
|
+
const __bt$wrapped = () => {};
|
|
690
|
+
return __bt$wrapped(...arguments);
|
|
165
691
|
};
|
|
166
692
|
` : `
|
|
167
|
-
const
|
|
693
|
+
const __bt$ctx = {
|
|
168
694
|
arguments,
|
|
169
695
|
self: this,
|
|
170
696
|
moduleVersion: ${JSON.stringify(moduleVersion)}
|
|
171
697
|
};
|
|
172
|
-
const
|
|
173
|
-
const
|
|
174
|
-
return
|
|
698
|
+
const __bt$traced = () => {
|
|
699
|
+
const __bt$wrapped = () => {};
|
|
700
|
+
return __bt$wrapped.apply(this, arguments);
|
|
175
701
|
};
|
|
176
702
|
`
|
|
177
703
|
).body;
|
|
178
704
|
block.body.unshift(...common);
|
|
179
|
-
import_esquery.default.query(block, "[id.name=
|
|
705
|
+
import_esquery.default.query(block, "[id.name=__bt$wrapped]")[0].init = node;
|
|
180
706
|
return block;
|
|
181
707
|
}
|
|
182
708
|
function wrapSuper(node) {
|
|
@@ -188,11 +714,11 @@ function wrapSuper(node) {
|
|
|
188
714
|
const { name } = node2.property;
|
|
189
715
|
let child;
|
|
190
716
|
if (parent.callee) {
|
|
191
|
-
const { expression } = (0, import_meriyah.parse)(`
|
|
717
|
+
const { expression } = (0, import_meriyah.parse)(`__bt$super['${name}'].call(this)`).body[0];
|
|
192
718
|
parent.callee = child = expression.callee;
|
|
193
719
|
parent.arguments.unshift(...expression.arguments);
|
|
194
720
|
} else {
|
|
195
|
-
parent.expression = child = (0, import_meriyah.parse)(`
|
|
721
|
+
parent.expression = child = (0, import_meriyah.parse)(`__bt$super['${name}']`).body[0];
|
|
196
722
|
}
|
|
197
723
|
child.computed = parent.callee.computed;
|
|
198
724
|
child.optional = parent.callee.optional;
|
|
@@ -203,14 +729,14 @@ function wrapSuper(node) {
|
|
|
203
729
|
const member = (0, import_meriyah.parse)(`
|
|
204
730
|
class Wrapper {
|
|
205
731
|
wrapper () {
|
|
206
|
-
|
|
732
|
+
__bt$super['${name}'] = super['${name}']
|
|
207
733
|
}
|
|
208
734
|
}
|
|
209
735
|
`).body[0].body.body[0].value.body.body[0];
|
|
210
736
|
node.body.body.unshift(member);
|
|
211
737
|
}
|
|
212
738
|
if (members.size > 0) {
|
|
213
|
-
node.body.body.unshift((0, import_meriyah.parse)("const
|
|
739
|
+
node.body.body.unshift((0, import_meriyah.parse)("const __bt$super = {}").body[0]);
|
|
214
740
|
}
|
|
215
741
|
}
|
|
216
742
|
function wrapCallback(state) {
|
|
@@ -218,136 +744,41 @@ function wrapCallback(state) {
|
|
|
218
744
|
channelName,
|
|
219
745
|
functionQuery: { callbackIndex = -1 }
|
|
220
746
|
} = state;
|
|
221
|
-
const
|
|
747
|
+
const channelGetter = formatChannelGetter(channelName);
|
|
222
748
|
return (0, import_meriyah.parse)(`
|
|
223
749
|
function wrapper () {
|
|
224
|
-
const
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
} else {
|
|
233
|
-
__apm$ctx.result = res;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
${channelVariable}.asyncStart.runStores(__apm$ctx, () => {
|
|
237
|
-
try {
|
|
238
|
-
if (__apm$cb) {
|
|
239
|
-
return __apm$cb.apply(this, arguments);
|
|
240
|
-
}
|
|
241
|
-
} finally {
|
|
242
|
-
${channelVariable}.asyncEnd.publish(__apm$ctx);
|
|
243
|
-
}
|
|
244
|
-
});
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
if (typeof __apm$cb !== 'function') {
|
|
248
|
-
return __apm$traced();
|
|
249
|
-
}
|
|
250
|
-
Array.prototype.splice.call(arguments, ${callbackIndex}, 1, __apm$wrappedCb);
|
|
251
|
-
|
|
252
|
-
return ${channelVariable}.start.runStores(__apm$ctx, () => {
|
|
253
|
-
try {
|
|
254
|
-
return __apm$traced();
|
|
255
|
-
} catch (err) {
|
|
256
|
-
__apm$ctx.error = err;
|
|
257
|
-
${channelVariable}.error.publish(__apm$ctx);
|
|
258
|
-
throw err;
|
|
259
|
-
} finally {
|
|
260
|
-
__apm$ctx.self ??= this;
|
|
261
|
-
${channelVariable}.end.publish(__apm$ctx);
|
|
262
|
-
}
|
|
263
|
-
});
|
|
750
|
+
const __bt$hook = ${channelGetter}();
|
|
751
|
+
if (!__bt$hook?.hasSubscribers) return __bt$traced();
|
|
752
|
+
__bt$ctx.self ??= this;
|
|
753
|
+
return __bt$hook.traceCallback(
|
|
754
|
+
__bt$traced,
|
|
755
|
+
${callbackIndex},
|
|
756
|
+
__bt$ctx
|
|
757
|
+
);
|
|
264
758
|
}
|
|
265
759
|
`);
|
|
266
760
|
}
|
|
267
761
|
function wrapPromise(state) {
|
|
268
762
|
const { channelName } = state;
|
|
269
|
-
const
|
|
763
|
+
const channelGetter = formatChannelGetter(channelName);
|
|
270
764
|
return (0, import_meriyah.parse)(`
|
|
271
765
|
function wrapper () {
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
let promise = __apm$traced();
|
|
277
|
-
if (typeof promise?.then !== 'function') {
|
|
278
|
-
__apm$ctx.result = promise;
|
|
279
|
-
return promise;
|
|
280
|
-
}
|
|
281
|
-
// Mirror Node.js core diagnostics_channel behaviour: for native Promise
|
|
282
|
-
// instances, chain normally (safe since there is no subclass API to
|
|
283
|
-
// preserve). For Promise subclasses and other thenables, side-chain the
|
|
284
|
-
// callbacks for event publishing and return the original so that any
|
|
285
|
-
// subclass-specific methods (e.g. APIPromise.withResponse()) remain
|
|
286
|
-
// accessible to the caller.
|
|
287
|
-
if (promise instanceof Promise && promise.constructor === Promise) {
|
|
288
|
-
return promise.then(
|
|
289
|
-
result => {
|
|
290
|
-
__apm$ctx.result = result;
|
|
291
|
-
${channelVariable}.asyncStart.publish(__apm$ctx);
|
|
292
|
-
${channelVariable}.asyncEnd.publish(__apm$ctx);
|
|
293
|
-
return result;
|
|
294
|
-
},
|
|
295
|
-
err => {
|
|
296
|
-
__apm$ctx.error = err;
|
|
297
|
-
${channelVariable}.error.publish(__apm$ctx);
|
|
298
|
-
${channelVariable}.asyncStart.publish(__apm$ctx);
|
|
299
|
-
${channelVariable}.asyncEnd.publish(__apm$ctx);
|
|
300
|
-
throw err;
|
|
301
|
-
}
|
|
302
|
-
);
|
|
303
|
-
}
|
|
304
|
-
promise.then(
|
|
305
|
-
result => {
|
|
306
|
-
__apm$ctx.result = result;
|
|
307
|
-
${channelVariable}.asyncStart.publish(__apm$ctx);
|
|
308
|
-
${channelVariable}.asyncEnd.publish(__apm$ctx);
|
|
309
|
-
},
|
|
310
|
-
err => {
|
|
311
|
-
__apm$ctx.error = err;
|
|
312
|
-
${channelVariable}.error.publish(__apm$ctx);
|
|
313
|
-
${channelVariable}.asyncStart.publish(__apm$ctx);
|
|
314
|
-
${channelVariable}.asyncEnd.publish(__apm$ctx);
|
|
315
|
-
}
|
|
316
|
-
);
|
|
317
|
-
return promise;
|
|
318
|
-
} catch (err) {
|
|
319
|
-
__apm$ctx.error = err;
|
|
320
|
-
${channelVariable}.error.publish(__apm$ctx);
|
|
321
|
-
throw err;
|
|
322
|
-
} finally {
|
|
323
|
-
__apm$ctx.self ??= this;
|
|
324
|
-
${channelVariable}.end.publish(__apm$ctx);
|
|
325
|
-
}
|
|
326
|
-
});
|
|
766
|
+
const __bt$hook = ${channelGetter}();
|
|
767
|
+
if (!__bt$hook?.hasSubscribers) return __bt$traced();
|
|
768
|
+
__bt$ctx.self ??= this;
|
|
769
|
+
return __bt$hook.tracePromise(__bt$traced, __bt$ctx);
|
|
327
770
|
}
|
|
328
771
|
`);
|
|
329
772
|
}
|
|
330
773
|
function wrapSync(state) {
|
|
331
774
|
const { channelName } = state;
|
|
332
|
-
const
|
|
775
|
+
const channelGetter = formatChannelGetter(channelName);
|
|
333
776
|
return (0, import_meriyah.parse)(`
|
|
334
777
|
function wrapper () {
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
const result = __apm$traced();
|
|
340
|
-
__apm$ctx.result = result;
|
|
341
|
-
return result;
|
|
342
|
-
} catch (err) {
|
|
343
|
-
__apm$ctx.error = err;
|
|
344
|
-
${channelVariable}.error.publish(__apm$ctx);
|
|
345
|
-
throw err;
|
|
346
|
-
} finally {
|
|
347
|
-
__apm$ctx.self ??= this;
|
|
348
|
-
${channelVariable}.end.publish(__apm$ctx);
|
|
349
|
-
}
|
|
350
|
-
});
|
|
778
|
+
const __bt$hook = ${channelGetter}();
|
|
779
|
+
if (!__bt$hook?.hasSubscribers) return __bt$traced();
|
|
780
|
+
__bt$ctx.self ??= this;
|
|
781
|
+
return __bt$hook.traceSync(__bt$traced, __bt$ctx);
|
|
351
782
|
}
|
|
352
783
|
`);
|
|
353
784
|
}
|
|
@@ -358,16 +789,14 @@ var Transformer = class {
|
|
|
358
789
|
version;
|
|
359
790
|
filePath;
|
|
360
791
|
configs = [];
|
|
361
|
-
|
|
362
|
-
constructor(moduleName, version, filePath, configs, dcModule) {
|
|
792
|
+
constructor(moduleName, version, filePath, configs) {
|
|
363
793
|
this.moduleName = moduleName;
|
|
364
794
|
this.version = version;
|
|
365
795
|
this.filePath = filePath;
|
|
366
796
|
this.configs = configs;
|
|
367
|
-
this.dcModule = dcModule;
|
|
368
797
|
}
|
|
369
798
|
/**
|
|
370
|
-
* Instruments `code` by injecting
|
|
799
|
+
* Instruments `code` by injecting global tracing hooks around the
|
|
371
800
|
* target functions defined by this transformer's configs.
|
|
372
801
|
*/
|
|
373
802
|
transform(code, moduleType) {
|
|
@@ -405,8 +834,6 @@ var Transformer = class {
|
|
|
405
834
|
const query = astQuery || this.fromFunctionQuery(resolvedFunctionQuery);
|
|
406
835
|
const state = {
|
|
407
836
|
...config,
|
|
408
|
-
dcModule: this.dcModule,
|
|
409
|
-
moduleType,
|
|
410
837
|
moduleVersion: this.version,
|
|
411
838
|
functionQuery: resolvedFunctionQuery,
|
|
412
839
|
operator: this.getOperator(resolvedFunctionQuery.kind)
|
|
@@ -556,11 +983,9 @@ var Transformer = class {
|
|
|
556
983
|
// src/auto-instrumentations/orchestrion-js/matcher.ts
|
|
557
984
|
var InstrumentationMatcher = class {
|
|
558
985
|
configs = [];
|
|
559
|
-
dcModule;
|
|
560
986
|
transformers = {};
|
|
561
|
-
constructor(configs
|
|
987
|
+
constructor(configs) {
|
|
562
988
|
this.configs = configs;
|
|
563
|
-
this.dcModule = dcModule || "diagnostics_channel";
|
|
564
989
|
}
|
|
565
990
|
/**
|
|
566
991
|
* Returns a Transformer for the given module/file/version, or undefined if no
|
|
@@ -582,8 +1007,7 @@ var InstrumentationMatcher = class {
|
|
|
582
1007
|
moduleName,
|
|
583
1008
|
version,
|
|
584
1009
|
filePath,
|
|
585
|
-
configs
|
|
586
|
-
this.dcModule
|
|
1010
|
+
configs
|
|
587
1011
|
);
|
|
588
1012
|
return this.transformers[id];
|
|
589
1013
|
}
|
|
@@ -593,8 +1017,8 @@ var InstrumentationMatcher = class {
|
|
|
593
1017
|
};
|
|
594
1018
|
|
|
595
1019
|
// src/auto-instrumentations/orchestrion-js/index.ts
|
|
596
|
-
function create(configs
|
|
597
|
-
return new InstrumentationMatcher(configs
|
|
1020
|
+
function create(configs) {
|
|
1021
|
+
return new InstrumentationMatcher(configs);
|
|
598
1022
|
}
|
|
599
1023
|
|
|
600
1024
|
// src/auto-instrumentations/bundler/webpack-loader.ts
|
|
@@ -619,6 +1043,9 @@ var envIntegrationAliases = {
|
|
|
619
1043
|
strandsagentsdk: "strandsAgentSDK",
|
|
620
1044
|
"strands-agent-sdk": "strandsAgentSDK",
|
|
621
1045
|
"@strands-agents/sdk": "strandsAgentSDK",
|
|
1046
|
+
agents: "cloudflareAgents",
|
|
1047
|
+
"cloudflare-agents": "cloudflareAgents",
|
|
1048
|
+
cloudflareagents: "cloudflareAgents",
|
|
622
1049
|
anthropic: "anthropic",
|
|
623
1050
|
aisdk: "aisdk",
|
|
624
1051
|
"ai-sdk": "aisdk",
|
|
@@ -626,6 +1053,9 @@ var envIntegrationAliases = {
|
|
|
626
1053
|
vercel: "vercel",
|
|
627
1054
|
claudeagentsdk: "claudeAgentSDK",
|
|
628
1055
|
"claude-agent-sdk": "claudeAgentSDK",
|
|
1056
|
+
cloudflareaichat: "cloudflareAIChat",
|
|
1057
|
+
"cloudflare-ai-chat": "cloudflareAIChat",
|
|
1058
|
+
"@cloudflare/ai-chat": "cloudflareAIChat",
|
|
629
1059
|
cursor: "cursor",
|
|
630
1060
|
"cursor-sdk": "cursorSDK",
|
|
631
1061
|
cursorsdk: "cursorSDK",
|
|
@@ -640,6 +1070,8 @@ var envIntegrationAliases = {
|
|
|
640
1070
|
"google-genai": "googleGenAI",
|
|
641
1071
|
googlegenai: "googleGenAI",
|
|
642
1072
|
huggingface: "huggingface",
|
|
1073
|
+
"@huggingface/transformers": "huggingface",
|
|
1074
|
+
transformers: "huggingface",
|
|
643
1075
|
openrouter: "openrouter",
|
|
644
1076
|
openrouteragent: "openrouterAgent",
|
|
645
1077
|
"openrouter-agent": "openrouterAgent",
|
|
@@ -696,73 +1128,6 @@ var DefaultAsyncLocalStorage = class {
|
|
|
696
1128
|
return void 0;
|
|
697
1129
|
}
|
|
698
1130
|
};
|
|
699
|
-
var DefaultChannel = class {
|
|
700
|
-
constructor(name) {
|
|
701
|
-
this.name = name;
|
|
702
|
-
}
|
|
703
|
-
name;
|
|
704
|
-
hasSubscribers = false;
|
|
705
|
-
subscribe(_subscription) {
|
|
706
|
-
}
|
|
707
|
-
unsubscribe(_subscription) {
|
|
708
|
-
return false;
|
|
709
|
-
}
|
|
710
|
-
bindStore(_store, _transform) {
|
|
711
|
-
}
|
|
712
|
-
unbindStore(_store) {
|
|
713
|
-
return false;
|
|
714
|
-
}
|
|
715
|
-
publish(_message) {
|
|
716
|
-
}
|
|
717
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
718
|
-
runStores(_message, fn, thisArg, ...args) {
|
|
719
|
-
return fn.apply(thisArg, args);
|
|
720
|
-
}
|
|
721
|
-
};
|
|
722
|
-
var DefaultTracingChannel = class {
|
|
723
|
-
start;
|
|
724
|
-
end;
|
|
725
|
-
asyncStart;
|
|
726
|
-
asyncEnd;
|
|
727
|
-
error;
|
|
728
|
-
constructor(nameOrChannels) {
|
|
729
|
-
if (typeof nameOrChannels === "string") {
|
|
730
|
-
this.start = new DefaultChannel(`tracing:${nameOrChannels}:start`);
|
|
731
|
-
this.end = new DefaultChannel(`tracing:${nameOrChannels}:end`);
|
|
732
|
-
this.asyncStart = new DefaultChannel(
|
|
733
|
-
`tracing:${nameOrChannels}:asyncStart`
|
|
734
|
-
);
|
|
735
|
-
this.asyncEnd = new DefaultChannel(`tracing:${nameOrChannels}:asyncEnd`);
|
|
736
|
-
this.error = new DefaultChannel(`tracing:${nameOrChannels}:error`);
|
|
737
|
-
return;
|
|
738
|
-
}
|
|
739
|
-
this.start = nameOrChannels.start ?? new DefaultChannel("tracing:start");
|
|
740
|
-
this.end = nameOrChannels.end ?? new DefaultChannel("tracing:end");
|
|
741
|
-
this.asyncStart = nameOrChannels.asyncStart ?? new DefaultChannel("tracing:asyncStart");
|
|
742
|
-
this.asyncEnd = nameOrChannels.asyncEnd ?? new DefaultChannel("tracing:asyncEnd");
|
|
743
|
-
this.error = nameOrChannels.error ?? new DefaultChannel("tracing:error");
|
|
744
|
-
}
|
|
745
|
-
get hasSubscribers() {
|
|
746
|
-
return this.start.hasSubscribers || this.end.hasSubscribers || this.asyncStart.hasSubscribers || this.asyncEnd.hasSubscribers || this.error.hasSubscribers;
|
|
747
|
-
}
|
|
748
|
-
subscribe(_handlers) {
|
|
749
|
-
}
|
|
750
|
-
unsubscribe(_handlers) {
|
|
751
|
-
return false;
|
|
752
|
-
}
|
|
753
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
754
|
-
traceSync(fn, _message, thisArg, ...args) {
|
|
755
|
-
return fn.apply(thisArg, args);
|
|
756
|
-
}
|
|
757
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
758
|
-
tracePromise(fn, _message, thisArg, ...args) {
|
|
759
|
-
return fn.apply(thisArg, args);
|
|
760
|
-
}
|
|
761
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
762
|
-
traceCallback(fn, _position, _message, thisArg, ...args) {
|
|
763
|
-
return fn.apply(thisArg, args);
|
|
764
|
-
}
|
|
765
|
-
};
|
|
766
1131
|
var iso = {
|
|
767
1132
|
buildType: "unknown",
|
|
768
1133
|
// Will be set by configureBrowser() or configureNode()
|
|
@@ -773,7 +1138,7 @@ var iso = {
|
|
|
773
1138
|
getCallerLocation: () => void 0,
|
|
774
1139
|
newAsyncLocalStorage: () => new DefaultAsyncLocalStorage(),
|
|
775
1140
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
776
|
-
newTracingChannel: (nameOrChannels) =>
|
|
1141
|
+
newTracingChannel: (nameOrChannels) => newGlobalTracingChannel(nameOrChannels),
|
|
777
1142
|
processOn: (_0, _1) => {
|
|
778
1143
|
},
|
|
779
1144
|
basename: (filepath) => filepath.split(/[\\/]/).pop() || filepath,
|
|
@@ -838,6 +1203,8 @@ var INSTRUMENTATION_NAMES = {
|
|
|
838
1203
|
BEDROCK_RUNTIME: "bedrock-runtime",
|
|
839
1204
|
BRAINTRUST_JS_LOGGER: "braintrust-js-logger",
|
|
840
1205
|
CLAUDE_AGENT_SDK: "claude-agent-sdk",
|
|
1206
|
+
CLOUDFLARE_AI_CHAT: "cloudflare-ai-chat",
|
|
1207
|
+
CLOUDFLARE_AGENTS: "cloudflare-agents",
|
|
841
1208
|
COHERE: "cohere",
|
|
842
1209
|
CURSOR_SDK: "cursor-sdk",
|
|
843
1210
|
EVE: "eve",
|
|
@@ -1615,6 +1982,74 @@ var claudeAgentSDKConfigs = [
|
|
|
1615
1982
|
}
|
|
1616
1983
|
];
|
|
1617
1984
|
|
|
1985
|
+
// src/instrumentation/plugins/cloudflare-ai-chat-channels.ts
|
|
1986
|
+
var cloudflareAIChatChannels = defineChannels(
|
|
1987
|
+
"@cloudflare/ai-chat",
|
|
1988
|
+
{
|
|
1989
|
+
runExclusiveChatTurn: channel({
|
|
1990
|
+
channelName: "AIChatAgent._runExclusiveChatTurn",
|
|
1991
|
+
kind: "async"
|
|
1992
|
+
}),
|
|
1993
|
+
onChatResponse: channel({
|
|
1994
|
+
channelName: "AIChatAgent.onChatResponse",
|
|
1995
|
+
kind: "sync-stream"
|
|
1996
|
+
})
|
|
1997
|
+
},
|
|
1998
|
+
{ instrumentationName: INSTRUMENTATION_NAMES.CLOUDFLARE_AI_CHAT }
|
|
1999
|
+
);
|
|
2000
|
+
|
|
2001
|
+
// src/auto-instrumentations/configs/cloudflare-ai-chat.ts
|
|
2002
|
+
var cloudflareAIChatVersionRange = ">=0.9.0 <0.10.0";
|
|
2003
|
+
var cloudflareAIChatConfigs = [
|
|
2004
|
+
{
|
|
2005
|
+
// AIChatAgent subclasses replace onChatMessage, so there is no stable
|
|
2006
|
+
// public implementation for the transformer to target. This runner is
|
|
2007
|
+
// the narrowest shared boundary around every chat turn; keep the version
|
|
2008
|
+
// range tight because it is an internal method.
|
|
2009
|
+
channelName: cloudflareAIChatChannels.runExclusiveChatTurn.channelName,
|
|
2010
|
+
module: {
|
|
2011
|
+
name: "@cloudflare/ai-chat",
|
|
2012
|
+
versionRange: cloudflareAIChatVersionRange,
|
|
2013
|
+
filePath: "dist/index.js"
|
|
2014
|
+
},
|
|
2015
|
+
functionQuery: {
|
|
2016
|
+
className: "AIChatAgent",
|
|
2017
|
+
methodName: "_runExclusiveChatTurn",
|
|
2018
|
+
kind: "Async"
|
|
2019
|
+
}
|
|
2020
|
+
}
|
|
2021
|
+
];
|
|
2022
|
+
|
|
2023
|
+
// src/instrumentation/plugins/cloudflare-agents-channels.ts
|
|
2024
|
+
var cloudflareAgentsChannels = defineChannels(
|
|
2025
|
+
"agents",
|
|
2026
|
+
{
|
|
2027
|
+
runAgentTool: channel({
|
|
2028
|
+
channelName: "Agent.runAgentTool",
|
|
2029
|
+
kind: "async"
|
|
2030
|
+
})
|
|
2031
|
+
},
|
|
2032
|
+
{ instrumentationName: INSTRUMENTATION_NAMES.CLOUDFLARE_AGENTS }
|
|
2033
|
+
);
|
|
2034
|
+
|
|
2035
|
+
// src/auto-instrumentations/configs/cloudflare-agents.ts
|
|
2036
|
+
var cloudflareAgentsVersionRange = ">=0.17.0 <0.18.0";
|
|
2037
|
+
var cloudflareAgentsConfigs = [
|
|
2038
|
+
{
|
|
2039
|
+
channelName: cloudflareAgentsChannels.runAgentTool.channelName,
|
|
2040
|
+
module: {
|
|
2041
|
+
name: "agents",
|
|
2042
|
+
versionRange: cloudflareAgentsVersionRange,
|
|
2043
|
+
filePath: "dist/index.js"
|
|
2044
|
+
},
|
|
2045
|
+
functionQuery: {
|
|
2046
|
+
className: "Agent",
|
|
2047
|
+
methodName: "runAgentTool",
|
|
2048
|
+
kind: "Async"
|
|
2049
|
+
}
|
|
2050
|
+
}
|
|
2051
|
+
];
|
|
2052
|
+
|
|
1618
2053
|
// src/instrumentation/plugins/cohere-channels.ts
|
|
1619
2054
|
var cohereChannels = defineChannels(
|
|
1620
2055
|
"cohere-ai",
|
|
@@ -2977,6 +3412,83 @@ var huggingFaceConfigs = [
|
|
|
2977
3412
|
}
|
|
2978
3413
|
];
|
|
2979
3414
|
|
|
3415
|
+
// src/instrumentation/plugins/huggingface-transformers-channels.ts
|
|
3416
|
+
var huggingFaceTransformersChannels = defineChannels(
|
|
3417
|
+
"@huggingface/transformers",
|
|
3418
|
+
{
|
|
3419
|
+
pipeline: channel({
|
|
3420
|
+
channelName: "pipeline",
|
|
3421
|
+
kind: "async"
|
|
3422
|
+
}),
|
|
3423
|
+
pipelineCall: channel({
|
|
3424
|
+
channelName: "pipeline.call",
|
|
3425
|
+
kind: "async"
|
|
3426
|
+
})
|
|
3427
|
+
},
|
|
3428
|
+
{ instrumentationName: INSTRUMENTATION_NAMES.HUGGINGFACE }
|
|
3429
|
+
);
|
|
3430
|
+
|
|
3431
|
+
// src/auto-instrumentations/configs/huggingface-transformers.ts
|
|
3432
|
+
var moduleFiles = [
|
|
3433
|
+
{
|
|
3434
|
+
filePath: "dist/transformers.mjs",
|
|
3435
|
+
versionRange: ">=3.0.0 <3.4.0"
|
|
3436
|
+
},
|
|
3437
|
+
{
|
|
3438
|
+
filePath: "dist/transformers.cjs",
|
|
3439
|
+
versionRange: ">=3.0.0 <3.4.0"
|
|
3440
|
+
},
|
|
3441
|
+
{
|
|
3442
|
+
filePath: "dist/transformers.js",
|
|
3443
|
+
versionRange: ">=3.0.0 <3.4.0"
|
|
3444
|
+
},
|
|
3445
|
+
{
|
|
3446
|
+
filePath: "dist/transformers.node.mjs",
|
|
3447
|
+
versionRange: ">=3.4.0 <5.0.0"
|
|
3448
|
+
},
|
|
3449
|
+
{
|
|
3450
|
+
filePath: "dist/transformers.node.cjs",
|
|
3451
|
+
versionRange: ">=3.4.0 <5.0.0"
|
|
3452
|
+
},
|
|
3453
|
+
{
|
|
3454
|
+
filePath: "dist/transformers.web.js",
|
|
3455
|
+
versionRange: ">=3.4.0 <5.0.0"
|
|
3456
|
+
}
|
|
3457
|
+
];
|
|
3458
|
+
var pipelineClasses = [
|
|
3459
|
+
"TextGenerationPipeline",
|
|
3460
|
+
"Text2TextGenerationPipeline",
|
|
3461
|
+
"FeatureExtractionPipeline",
|
|
3462
|
+
"QuestionAnsweringPipeline"
|
|
3463
|
+
];
|
|
3464
|
+
var huggingFaceTransformersConfigs = moduleFiles.flatMap(({ filePath, versionRange: versionRange2 }) => [
|
|
3465
|
+
{
|
|
3466
|
+
channelName: huggingFaceTransformersChannels.pipeline.channelName,
|
|
3467
|
+
module: {
|
|
3468
|
+
name: "@huggingface/transformers",
|
|
3469
|
+
versionRange: versionRange2,
|
|
3470
|
+
filePath
|
|
3471
|
+
},
|
|
3472
|
+
functionQuery: {
|
|
3473
|
+
functionName: "pipeline",
|
|
3474
|
+
kind: "Async"
|
|
3475
|
+
}
|
|
3476
|
+
},
|
|
3477
|
+
...pipelineClasses.map((className) => ({
|
|
3478
|
+
channelName: huggingFaceTransformersChannels.pipelineCall.channelName,
|
|
3479
|
+
module: {
|
|
3480
|
+
name: "@huggingface/transformers",
|
|
3481
|
+
versionRange: versionRange2,
|
|
3482
|
+
filePath
|
|
3483
|
+
},
|
|
3484
|
+
functionQuery: {
|
|
3485
|
+
className,
|
|
3486
|
+
methodName: "_call",
|
|
3487
|
+
kind: "Async"
|
|
3488
|
+
}
|
|
3489
|
+
}))
|
|
3490
|
+
]);
|
|
3491
|
+
|
|
2980
3492
|
// src/instrumentation/plugins/langchain-channels.ts
|
|
2981
3493
|
var langChainChannels = defineChannels(
|
|
2982
3494
|
"@langchain/core",
|
|
@@ -3995,6 +4507,14 @@ var defaultInstrumentationConfigGroups = [
|
|
|
3995
4507
|
integrations: ["claudeAgentSDK"],
|
|
3996
4508
|
configs: claudeAgentSDKConfigs
|
|
3997
4509
|
},
|
|
4510
|
+
{
|
|
4511
|
+
integrations: ["cloudflareAIChat"],
|
|
4512
|
+
configs: cloudflareAIChatConfigs
|
|
4513
|
+
},
|
|
4514
|
+
{
|
|
4515
|
+
integrations: ["cloudflareAgents"],
|
|
4516
|
+
configs: cloudflareAgentsConfigs
|
|
4517
|
+
},
|
|
3998
4518
|
{ integrations: ["cursor", "cursorSDK"], configs: cursorSDKConfigs },
|
|
3999
4519
|
{
|
|
4000
4520
|
integrations: ["openAIAgents"],
|
|
@@ -4004,7 +4524,10 @@ var defaultInstrumentationConfigGroups = [
|
|
|
4004
4524
|
integrations: ["google", "googleGenAI"],
|
|
4005
4525
|
configs: googleGenAIConfigs
|
|
4006
4526
|
},
|
|
4007
|
-
{
|
|
4527
|
+
{
|
|
4528
|
+
integrations: ["huggingface"],
|
|
4529
|
+
configs: [...huggingFaceConfigs, ...huggingFaceTransformersConfigs]
|
|
4530
|
+
},
|
|
4008
4531
|
{
|
|
4009
4532
|
integrations: ["langchain", "langgraph"],
|
|
4010
4533
|
configs: langchainConfigs
|
|
@@ -4063,6 +4586,214 @@ function getDefaultInstrumentationConfigs({
|
|
|
4063
4586
|
];
|
|
4064
4587
|
}
|
|
4065
4588
|
|
|
4589
|
+
// src/auto-instrumentations/loader/mastra-observability-patch.ts
|
|
4590
|
+
var MASTRA_EXPORTER_FACTORY_GLOBAL = "__braintrustMastraExporterFactory";
|
|
4591
|
+
var MASTRA_CORE_PACKAGE = "@mastra/core";
|
|
4592
|
+
var MASTRA_OBSERVABILITY_PACKAGE = "@mastra/observability";
|
|
4593
|
+
var MASTRA_CORE_ENTRY_PATHS = /* @__PURE__ */ new Set([
|
|
4594
|
+
"dist/index.js",
|
|
4595
|
+
"dist/index.cjs",
|
|
4596
|
+
"dist/mastra/index.js",
|
|
4597
|
+
"dist/mastra/index.cjs"
|
|
4598
|
+
]);
|
|
4599
|
+
var MASTRA_OBSERVABILITY_ENTRY_PATHS = /* @__PURE__ */ new Set([
|
|
4600
|
+
"dist/index.js",
|
|
4601
|
+
"dist/index.cjs"
|
|
4602
|
+
]);
|
|
4603
|
+
function classifyMastraTarget(packageName, modulePath) {
|
|
4604
|
+
if (packageName === MASTRA_CORE_PACKAGE && MASTRA_CORE_ENTRY_PATHS.has(modulePath)) {
|
|
4605
|
+
return "core";
|
|
4606
|
+
}
|
|
4607
|
+
if (packageName === MASTRA_OBSERVABILITY_PACKAGE && MASTRA_OBSERVABILITY_ENTRY_PATHS.has(modulePath)) {
|
|
4608
|
+
return "observability";
|
|
4609
|
+
}
|
|
4610
|
+
return null;
|
|
4611
|
+
}
|
|
4612
|
+
function extractChunkPath(source) {
|
|
4613
|
+
const esmMatch = source.match(
|
|
4614
|
+
/export\s*\{\s*Mastra(?:\s+as\s+\w+)?\s*\}\s*from\s*['"]([^'"]+)['"]/
|
|
4615
|
+
);
|
|
4616
|
+
if (esmMatch) return esmMatch[1];
|
|
4617
|
+
const cjsMatch = source.match(
|
|
4618
|
+
/require\s*\(\s*['"]([^'"]+chunk-[^'"]+)['"]\s*\)/
|
|
4619
|
+
);
|
|
4620
|
+
if (cjsMatch) return cjsMatch[1];
|
|
4621
|
+
return null;
|
|
4622
|
+
}
|
|
4623
|
+
var EXPORTER_FACTORY_KEY = JSON.stringify(MASTRA_EXPORTER_FACTORY_GLOBAL);
|
|
4624
|
+
var MASTRA_PROXY_HANDLER_BODY = `
|
|
4625
|
+
{
|
|
4626
|
+
construct(target, args, newTarget) {
|
|
4627
|
+
var firstArg = args[0];
|
|
4628
|
+
if (
|
|
4629
|
+
(!firstArg || typeof firstArg !== "object" || !firstArg.observability) &&
|
|
4630
|
+
__braintrustObservabilityClass
|
|
4631
|
+
) {
|
|
4632
|
+
try {
|
|
4633
|
+
// serviceName is required by Mastra's Observability validator; pass
|
|
4634
|
+
// something sensible by default. Users who want a different name
|
|
4635
|
+
// should construct Observability themselves.
|
|
4636
|
+
var observability = new __braintrustObservabilityClass({
|
|
4637
|
+
configs: { default: { serviceName: "mastra" } },
|
|
4638
|
+
});
|
|
4639
|
+
args = args.slice();
|
|
4640
|
+
args[0] = Object.assign({}, firstArg, { observability: observability });
|
|
4641
|
+
} catch (e) {
|
|
4642
|
+
// Fall through. Mastra will use its own NoOp; user code still works,
|
|
4643
|
+
// just without auto-instrumentation.
|
|
4644
|
+
}
|
|
4645
|
+
}
|
|
4646
|
+
return Reflect.construct(target, args, newTarget);
|
|
4647
|
+
},
|
|
4648
|
+
}`;
|
|
4649
|
+
function buildMastraEsmWrapper(chunkPath) {
|
|
4650
|
+
const chunk = JSON.stringify(chunkPath);
|
|
4651
|
+
return `import { Mastra as __braintrustOrigMastra } from ${chunk};
|
|
4652
|
+
import { createRequire as __braintrustCreateRequire } from "node:module";
|
|
4653
|
+
|
|
4654
|
+
let __braintrustObservabilityClass = null;
|
|
4655
|
+
try {
|
|
4656
|
+
// Resolve @mastra/observability relative to this module (the Mastra entry),
|
|
4657
|
+
// so it's looked up from the user's node_modules tree.
|
|
4658
|
+
const __braintrustRequire = __braintrustCreateRequire(import.meta.url);
|
|
4659
|
+
__braintrustObservabilityClass =
|
|
4660
|
+
__braintrustRequire("@mastra/observability").Observability;
|
|
4661
|
+
} catch (e) {
|
|
4662
|
+
// @mastra/observability isn't installed; the construct trap will skip the
|
|
4663
|
+
// auto-construct branch and Mastra falls back to its own NoOp.
|
|
4664
|
+
}
|
|
4665
|
+
const Mastra = new Proxy(__braintrustOrigMastra, ${MASTRA_PROXY_HANDLER_BODY});
|
|
4666
|
+
export { Mastra };
|
|
4667
|
+
`;
|
|
4668
|
+
}
|
|
4669
|
+
function buildMastraCjsWrapper(chunkPath) {
|
|
4670
|
+
const chunk = JSON.stringify(chunkPath);
|
|
4671
|
+
return `'use strict';
|
|
4672
|
+
const __braintrustChunk = require(${chunk});
|
|
4673
|
+
|
|
4674
|
+
let __braintrustObservabilityClass = null;
|
|
4675
|
+
try {
|
|
4676
|
+
__braintrustObservabilityClass = require("@mastra/observability").Observability;
|
|
4677
|
+
} catch (e) {
|
|
4678
|
+
// @mastra/observability isn't installed; same fallback as the ESM wrapper.
|
|
4679
|
+
}
|
|
4680
|
+
|
|
4681
|
+
const __braintrustWrappedMastra = new Proxy(
|
|
4682
|
+
__braintrustChunk.Mastra,
|
|
4683
|
+
${MASTRA_PROXY_HANDLER_BODY},
|
|
4684
|
+
);
|
|
4685
|
+
Object.defineProperty(exports, "Mastra", {
|
|
4686
|
+
enumerable: true,
|
|
4687
|
+
configurable: true,
|
|
4688
|
+
get: function () { return __braintrustWrappedMastra; }
|
|
4689
|
+
});
|
|
4690
|
+
`;
|
|
4691
|
+
}
|
|
4692
|
+
var OBSERVABILITY_APPEND_BODY = `
|
|
4693
|
+
;(function __braintrustWrapObservability() {
|
|
4694
|
+
// Top-level so we can both read and reassign the var binding the original
|
|
4695
|
+
// entry declared.
|
|
4696
|
+
if (typeof Observability === "undefined") return;
|
|
4697
|
+
if (Observability.__braintrustWrapped) return;
|
|
4698
|
+
function __braintrustEnsureExporter(rawConfig) {
|
|
4699
|
+
try {
|
|
4700
|
+
var factory = globalThis[${EXPORTER_FACTORY_KEY}];
|
|
4701
|
+
if (typeof factory !== "function") return rawConfig;
|
|
4702
|
+
var config = rawConfig && typeof rawConfig === "object" ? rawConfig : {};
|
|
4703
|
+
var configsIn = config.configs && typeof config.configs === "object" ? config.configs : null;
|
|
4704
|
+
var configsOut = {};
|
|
4705
|
+
var hadEntries = false;
|
|
4706
|
+
if (configsIn) {
|
|
4707
|
+
for (var name in configsIn) {
|
|
4708
|
+
if (!Object.prototype.hasOwnProperty.call(configsIn, name)) continue;
|
|
4709
|
+
hadEntries = true;
|
|
4710
|
+
var inst = configsIn[name] || {};
|
|
4711
|
+
var existing = Array.isArray(inst.exporters) ? inst.exporters : [];
|
|
4712
|
+
var hasOurs = existing.some(function (e) { return e && e.name === "braintrust"; });
|
|
4713
|
+
configsOut[name] = Object.assign({}, inst, {
|
|
4714
|
+
exporters: hasOurs ? existing : existing.concat([factory()]),
|
|
4715
|
+
});
|
|
4716
|
+
}
|
|
4717
|
+
}
|
|
4718
|
+
if (!hadEntries) {
|
|
4719
|
+
configsOut.default = {
|
|
4720
|
+
serviceName: "mastra",
|
|
4721
|
+
exporters: [factory()],
|
|
4722
|
+
};
|
|
4723
|
+
}
|
|
4724
|
+
return Object.assign({}, config, { configs: configsOut });
|
|
4725
|
+
} catch (e) {
|
|
4726
|
+
return rawConfig;
|
|
4727
|
+
}
|
|
4728
|
+
}
|
|
4729
|
+
var __OriginalObservability = Observability;
|
|
4730
|
+
Observability = new Proxy(__OriginalObservability, {
|
|
4731
|
+
construct: function (target, args, newTarget) {
|
|
4732
|
+
var nextArgs = args.slice();
|
|
4733
|
+
nextArgs[0] = __braintrustEnsureExporter(nextArgs[0]);
|
|
4734
|
+
return Reflect.construct(target, nextArgs, newTarget);
|
|
4735
|
+
},
|
|
4736
|
+
});
|
|
4737
|
+
Observability.__braintrustWrapped = true;
|
|
4738
|
+
if (typeof exports !== "undefined" && exports && typeof exports === "object") {
|
|
4739
|
+
try {
|
|
4740
|
+
Object.defineProperty(exports, "Observability", {
|
|
4741
|
+
enumerable: true,
|
|
4742
|
+
configurable: true,
|
|
4743
|
+
get: function () { return Observability; },
|
|
4744
|
+
});
|
|
4745
|
+
} catch (e) {}
|
|
4746
|
+
}
|
|
4747
|
+
})();
|
|
4748
|
+
`;
|
|
4749
|
+
function patchMastraSource(source, target, format) {
|
|
4750
|
+
if (target === "core") {
|
|
4751
|
+
const chunkPath = extractChunkPath(source);
|
|
4752
|
+
if (!chunkPath) return source;
|
|
4753
|
+
return format === "esm" ? buildMastraEsmWrapper(chunkPath) : buildMastraCjsWrapper(chunkPath);
|
|
4754
|
+
}
|
|
4755
|
+
return source + OBSERVABILITY_APPEND_BODY;
|
|
4756
|
+
}
|
|
4757
|
+
|
|
4758
|
+
// src/auto-instrumentations/loader/openai-api-promise-patch.ts
|
|
4759
|
+
var OPENAI_API_PROMISE_PATCH = `
|
|
4760
|
+
;(function __btPatchAPIPromise() {
|
|
4761
|
+
if (typeof APIPromise === "undefined" || APIPromise.prototype.__btParsePatched) return;
|
|
4762
|
+
APIPromise.prototype.__btParsePatched = true;
|
|
4763
|
+
var _origThen = APIPromise.prototype.then;
|
|
4764
|
+
APIPromise.prototype.then = function __btThen(onfulfilled, onrejected) {
|
|
4765
|
+
if (!this.__btParseWrapped && Object.prototype.hasOwnProperty.call(this, "parseResponse")) {
|
|
4766
|
+
this.__btParseWrapped = true;
|
|
4767
|
+
var _origParse = this.parseResponse;
|
|
4768
|
+
var _cached;
|
|
4769
|
+
this.parseResponse = function() {
|
|
4770
|
+
if (!_cached) _cached = _origParse.apply(this, arguments);
|
|
4771
|
+
return _cached;
|
|
4772
|
+
};
|
|
4773
|
+
}
|
|
4774
|
+
return _origThen.call(this, onfulfilled, onrejected);
|
|
4775
|
+
};
|
|
4776
|
+
})();
|
|
4777
|
+
`;
|
|
4778
|
+
|
|
4779
|
+
// src/auto-instrumentations/loader/special-case-patches.ts
|
|
4780
|
+
function applySpecialCasePatch(input) {
|
|
4781
|
+
if (input.packageName === "openai" && input.modulePath.includes("api-promise")) {
|
|
4782
|
+
return input.source + OPENAI_API_PROMISE_PATCH;
|
|
4783
|
+
}
|
|
4784
|
+
if (input.browser) {
|
|
4785
|
+
return null;
|
|
4786
|
+
}
|
|
4787
|
+
const mastraTarget = classifyMastraTarget(
|
|
4788
|
+
input.packageName,
|
|
4789
|
+
input.modulePath
|
|
4790
|
+
);
|
|
4791
|
+
if (mastraTarget) {
|
|
4792
|
+
return patchMastraSource(input.source, mastraTarget, input.format);
|
|
4793
|
+
}
|
|
4794
|
+
return null;
|
|
4795
|
+
}
|
|
4796
|
+
|
|
4066
4797
|
// src/auto-instrumentations/bundler/webpack-loader.ts
|
|
4067
4798
|
function getModuleVersion(basedir) {
|
|
4068
4799
|
try {
|
|
@@ -4080,8 +4811,7 @@ function getMatcher(options) {
|
|
|
4080
4811
|
const allInstrumentations = getDefaultInstrumentationConfigs({
|
|
4081
4812
|
additionalInstrumentations: options.instrumentations
|
|
4082
4813
|
});
|
|
4083
|
-
const
|
|
4084
|
-
const configHash = JSON.stringify({ allInstrumentations, dcModule });
|
|
4814
|
+
const configHash = JSON.stringify({ allInstrumentations });
|
|
4085
4815
|
if (matcherCache.has(configHash)) {
|
|
4086
4816
|
return matcherCache.get(configHash);
|
|
4087
4817
|
}
|
|
@@ -4090,7 +4820,7 @@ function getMatcher(options) {
|
|
|
4090
4820
|
matcherCache.delete(hash);
|
|
4091
4821
|
}
|
|
4092
4822
|
}
|
|
4093
|
-
const matcher = create(allInstrumentations
|
|
4823
|
+
const matcher = create(allInstrumentations);
|
|
4094
4824
|
matcherCache.set(configHash, matcher);
|
|
4095
4825
|
return matcher;
|
|
4096
4826
|
}
|
|
@@ -4115,11 +4845,21 @@ function codeTransformerLoader(code, inputSourceMap) {
|
|
|
4115
4845
|
return callback(null, code, inputSourceMap);
|
|
4116
4846
|
}
|
|
4117
4847
|
const moduleName = moduleDetails.name;
|
|
4848
|
+
const normalizedModulePath = moduleDetails.path.replace(/\\/g, "/");
|
|
4849
|
+
const patched = applySpecialCasePatch({
|
|
4850
|
+
packageName: moduleName,
|
|
4851
|
+
modulePath: normalizedModulePath,
|
|
4852
|
+
source: code,
|
|
4853
|
+
format: isModule ? "esm" : "cjs",
|
|
4854
|
+
browser: options.browser ?? options.useDiagnosticChannelCompatShim ?? false
|
|
4855
|
+
});
|
|
4856
|
+
if (patched !== null) {
|
|
4857
|
+
return callback(null, patched);
|
|
4858
|
+
}
|
|
4118
4859
|
const moduleVersion = getModuleVersion(moduleDetails.basedir);
|
|
4119
4860
|
if (!moduleVersion) {
|
|
4120
4861
|
return callback(null, code, inputSourceMap);
|
|
4121
4862
|
}
|
|
4122
|
-
const normalizedModulePath = moduleDetails.path.replace(/\\/g, "/");
|
|
4123
4863
|
const matcher = getMatcher(options);
|
|
4124
4864
|
const transformer = matcher.getTransformer(
|
|
4125
4865
|
moduleName,
|