braintrust 3.25.0 → 3.27.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 +4133 -1421
- package/dev/dist/index.mjs +3242 -530
- package/dist/apply-auto-instrumentation.js +526 -426
- package/dist/apply-auto-instrumentation.mjs +329 -229
- package/dist/auto-instrumentations/bundler/esbuild.cjs +1198 -364
- 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 +6 -8
- package/dist/auto-instrumentations/bundler/next.cjs +1227 -413
- package/dist/auto-instrumentations/bundler/next.mjs +7 -29
- package/dist/auto-instrumentations/bundler/rollup.cjs +1198 -364
- 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 +6 -8
- package/dist/auto-instrumentations/bundler/vite.cjs +1249 -387
- 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 +34 -8
- package/dist/auto-instrumentations/bundler/webpack-loader.cjs +1368 -324
- package/dist/auto-instrumentations/bundler/webpack-loader.d.ts +20 -12
- package/dist/auto-instrumentations/bundler/webpack.cjs +1198 -364
- 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 +6 -4
- package/dist/auto-instrumentations/chunk-2AMDGD65.mjs +1396 -0
- package/dist/auto-instrumentations/{chunk-7P6563SW.mjs → chunk-BW33ULMW.mjs} +2 -6
- package/dist/auto-instrumentations/{chunk-M6DLIJ2Z.mjs → chunk-I55G56ZL.mjs} +114 -221
- package/dist/auto-instrumentations/chunk-JPUFNW7X.mjs +649 -0
- package/dist/auto-instrumentations/{chunk-JPVCUKTY.mjs → chunk-XEYKUBLY.mjs} +191 -73
- package/dist/auto-instrumentations/{chunk-CZ24KNHT.mjs → chunk-ZNHTSSGI.mjs} +231 -69
- package/dist/auto-instrumentations/hook.mjs +406 -183
- package/dist/auto-instrumentations/index.cjs +853 -49
- package/dist/auto-instrumentations/index.d.mts +10 -2
- package/dist/auto-instrumentations/index.d.ts +10 -2
- package/dist/auto-instrumentations/index.mjs +10 -1
- package/dist/auto-instrumentations/loader/cjs-patch.cjs +566 -221
- 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 +871 -199
- package/dist/browser.d.ts +871 -199
- package/dist/browser.js +3737 -549
- package/dist/browser.mjs +3737 -549
- package/dist/{chunk-ABR2QWDP.mjs → chunk-CZM5JIQL.mjs} +788 -132
- package/dist/{chunk-P25IOOU4.js → chunk-MF7NU6BT.js} +795 -139
- package/dist/{chunk-GSIDVFE6.js → chunk-QRHGVBKU.js} +3580 -1463
- package/dist/{chunk-2SANLSWX.mjs → chunk-YKD22IMR.mjs} +2505 -388
- package/dist/cli.js +3244 -533
- package/dist/edge-light.d.mts +1 -1
- package/dist/edge-light.d.ts +1 -1
- package/dist/edge-light.js +3737 -552
- package/dist/edge-light.mjs +3737 -552
- package/dist/index.d.mts +871 -199
- package/dist/index.d.ts +871 -199
- package/dist/index.js +893 -446
- package/dist/index.mjs +463 -16
- package/dist/instrumentation/index.d.mts +138 -6
- package/dist/instrumentation/index.d.ts +138 -6
- package/dist/instrumentation/index.js +3362 -559
- package/dist/instrumentation/index.mjs +3362 -559
- package/dist/vitest-evals-reporter.js +45 -43
- package/dist/vitest-evals-reporter.mjs +5 -3
- package/dist/workerd.d.mts +1 -1
- package/dist/workerd.d.ts +1 -1
- package/dist/workerd.js +3737 -552
- package/dist/workerd.mjs +3737 -552
- package/licenses/node-diagnostics-channel/LICENSE +19 -0
- package/package.json +34 -35
- package/dist/auto-instrumentations/chunk-VT6DDNKM.mjs +0 -856
- package/dist/auto-instrumentations/plugin-D0KHwSJv.d.mts +0 -44
- package/dist/auto-instrumentations/plugin-G6fgxk1b.d.ts +0 -44
|
@@ -0,0 +1,1396 @@
|
|
|
1
|
+
// src/global-instrumentation-hooks.ts
|
|
2
|
+
var GLOBAL_INSTRUMENTATION_HOOKS_KEY = "__braintrust_instrumentation_hooks";
|
|
3
|
+
var GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION = 1;
|
|
4
|
+
var GLOBAL_INSTRUMENTATION_HOOKS_REGISTRY_BRAND = "braintrust.global-instrumentation-hooks.registry";
|
|
5
|
+
var GLOBAL_INSTRUMENTATION_HOOK_BRAND = "braintrust.global-instrumentation-hooks.hook";
|
|
6
|
+
var GLOBAL_INVOCATION_HOOK_BRAND = "braintrust.global-instrumentation-hooks.invocation-hook";
|
|
7
|
+
var registryBrand = Symbol.for(GLOBAL_INSTRUMENTATION_HOOKS_REGISTRY_BRAND);
|
|
8
|
+
var hookBrand = Symbol.for(GLOBAL_INSTRUMENTATION_HOOK_BRAND);
|
|
9
|
+
var invocationHookBrand = Symbol.for(GLOBAL_INVOCATION_HOOK_BRAND);
|
|
10
|
+
var errorReporter;
|
|
11
|
+
function setGlobalHookErrorReporter(reporter) {
|
|
12
|
+
const previousReporter = errorReporter;
|
|
13
|
+
errorReporter = reporter;
|
|
14
|
+
return () => {
|
|
15
|
+
if (errorReporter === reporter) {
|
|
16
|
+
errorReporter = previousReporter;
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function reportError(error) {
|
|
21
|
+
try {
|
|
22
|
+
errorReporter?.(error);
|
|
23
|
+
} catch {
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function setContextValue(context, key, value) {
|
|
27
|
+
try {
|
|
28
|
+
context[key] = value;
|
|
29
|
+
} catch (error) {
|
|
30
|
+
reportError(error);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function wrapStoreRun(store, message, next, transform) {
|
|
34
|
+
return () => {
|
|
35
|
+
let context;
|
|
36
|
+
try {
|
|
37
|
+
context = transform ? transform(message) : message;
|
|
38
|
+
} catch (error) {
|
|
39
|
+
reportError(error);
|
|
40
|
+
return next();
|
|
41
|
+
}
|
|
42
|
+
let called = false;
|
|
43
|
+
let result;
|
|
44
|
+
let providerError;
|
|
45
|
+
let providerThrew = false;
|
|
46
|
+
const runNext = () => {
|
|
47
|
+
if (called) {
|
|
48
|
+
reportError(
|
|
49
|
+
new Error(
|
|
50
|
+
"Instrumentation store invoked its callback more than once"
|
|
51
|
+
)
|
|
52
|
+
);
|
|
53
|
+
if (providerThrew) {
|
|
54
|
+
throw providerError;
|
|
55
|
+
}
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
58
|
+
called = true;
|
|
59
|
+
try {
|
|
60
|
+
result = next();
|
|
61
|
+
return result;
|
|
62
|
+
} catch (error) {
|
|
63
|
+
providerThrew = true;
|
|
64
|
+
providerError = error;
|
|
65
|
+
throw error;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
try {
|
|
69
|
+
store.run(context, runNext);
|
|
70
|
+
} catch (error) {
|
|
71
|
+
if (!providerThrew || error !== providerError) {
|
|
72
|
+
reportError(error);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (!called) {
|
|
76
|
+
reportError(
|
|
77
|
+
new Error("Instrumentation store did not invoke its callback")
|
|
78
|
+
);
|
|
79
|
+
return runNext();
|
|
80
|
+
}
|
|
81
|
+
if (providerThrew) {
|
|
82
|
+
throw providerError;
|
|
83
|
+
}
|
|
84
|
+
return result;
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
var HookChannel = class {
|
|
88
|
+
constructor(name) {
|
|
89
|
+
this.name = name;
|
|
90
|
+
}
|
|
91
|
+
name;
|
|
92
|
+
subscribers = [];
|
|
93
|
+
stores = /* @__PURE__ */ new Map();
|
|
94
|
+
get hasSubscribers() {
|
|
95
|
+
return this.subscribers.length > 0 || this.stores.size > 0;
|
|
96
|
+
}
|
|
97
|
+
subscribe(subscription) {
|
|
98
|
+
if (typeof subscription !== "function") {
|
|
99
|
+
throw new TypeError("subscription must be a function");
|
|
100
|
+
}
|
|
101
|
+
this.subscribers = [...this.subscribers, subscription];
|
|
102
|
+
}
|
|
103
|
+
unsubscribe(subscription) {
|
|
104
|
+
const index = this.subscribers.indexOf(subscription);
|
|
105
|
+
if (index === -1) {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
this.subscribers = [
|
|
109
|
+
...this.subscribers.slice(0, index),
|
|
110
|
+
...this.subscribers.slice(index + 1)
|
|
111
|
+
];
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
bindStore(store, transform) {
|
|
115
|
+
if (!store || typeof store.run !== "function") {
|
|
116
|
+
throw new TypeError("store must have a run method");
|
|
117
|
+
}
|
|
118
|
+
this.stores.set(
|
|
119
|
+
store,
|
|
120
|
+
transform
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
unbindStore(store) {
|
|
124
|
+
return this.stores.delete(store);
|
|
125
|
+
}
|
|
126
|
+
publish(message) {
|
|
127
|
+
const subscribers = this.subscribers;
|
|
128
|
+
for (const subscriber of subscribers) {
|
|
129
|
+
try {
|
|
130
|
+
subscriber(message, this.name);
|
|
131
|
+
} catch (error) {
|
|
132
|
+
reportError(error);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
runStores(message, fn, thisArg, ...args) {
|
|
137
|
+
let run = () => {
|
|
138
|
+
this.publish(message);
|
|
139
|
+
return Reflect.apply(fn, thisArg, args);
|
|
140
|
+
};
|
|
141
|
+
for (const [store, transform] of this.stores.entries()) {
|
|
142
|
+
run = wrapStoreRun(store, message, run, transform);
|
|
143
|
+
}
|
|
144
|
+
return run();
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
var traceEvents = [
|
|
148
|
+
"start",
|
|
149
|
+
"end",
|
|
150
|
+
"asyncStart",
|
|
151
|
+
"asyncEnd",
|
|
152
|
+
"error"
|
|
153
|
+
];
|
|
154
|
+
function traceInvocation(hook, operator, target, thisArg, args, additional, callbackIndex = -1) {
|
|
155
|
+
const context = {
|
|
156
|
+
...additional,
|
|
157
|
+
arguments: args,
|
|
158
|
+
self: thisArg
|
|
159
|
+
};
|
|
160
|
+
const invoke = () => hook.invoke(target, thisArg, args, additional);
|
|
161
|
+
if (operator === "traceCallback") {
|
|
162
|
+
return hook.traceCallback(invoke, callbackIndex, context);
|
|
163
|
+
}
|
|
164
|
+
if (operator === "tracePromise") {
|
|
165
|
+
return hook.tracePromise(invoke, context);
|
|
166
|
+
}
|
|
167
|
+
return hook.traceSync(invoke, context);
|
|
168
|
+
}
|
|
169
|
+
var InvocationHook = class {
|
|
170
|
+
interceptors = [];
|
|
171
|
+
get hasInterceptors() {
|
|
172
|
+
return this.interceptors.length > 0;
|
|
173
|
+
}
|
|
174
|
+
intercept(interceptor) {
|
|
175
|
+
if (typeof interceptor !== "function") {
|
|
176
|
+
throw new TypeError("interceptor must be a function");
|
|
177
|
+
}
|
|
178
|
+
this.interceptors = [...this.interceptors, interceptor];
|
|
179
|
+
let active = true;
|
|
180
|
+
return () => {
|
|
181
|
+
if (!active) {
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
active = false;
|
|
185
|
+
const index = this.interceptors.indexOf(interceptor);
|
|
186
|
+
if (index !== -1) {
|
|
187
|
+
this.interceptors = [
|
|
188
|
+
...this.interceptors.slice(0, index),
|
|
189
|
+
...this.interceptors.slice(index + 1)
|
|
190
|
+
];
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
invoke(target, thisArg, args, additional) {
|
|
195
|
+
const interceptors = this.interceptors;
|
|
196
|
+
if (interceptors.length === 0) {
|
|
197
|
+
return Reflect.apply(target, thisArg, args);
|
|
198
|
+
}
|
|
199
|
+
let next = target;
|
|
200
|
+
for (let index = interceptors.length - 1; index >= 0; index -= 1) {
|
|
201
|
+
const interceptor = interceptors[index];
|
|
202
|
+
const downstream = next;
|
|
203
|
+
next = function(...nextArgs) {
|
|
204
|
+
return interceptor(downstream, this, nextArgs, additional);
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
return Reflect.apply(next, thisArg, args);
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
var TracingHook = class {
|
|
211
|
+
start;
|
|
212
|
+
end;
|
|
213
|
+
asyncStart;
|
|
214
|
+
asyncEnd;
|
|
215
|
+
error;
|
|
216
|
+
invocationHook = new InvocationHook();
|
|
217
|
+
constructor(nameOrChannels) {
|
|
218
|
+
Object.defineProperty(this, hookBrand, {
|
|
219
|
+
configurable: false,
|
|
220
|
+
enumerable: false,
|
|
221
|
+
value: GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION,
|
|
222
|
+
writable: false
|
|
223
|
+
});
|
|
224
|
+
Object.defineProperty(this, invocationHookBrand, {
|
|
225
|
+
configurable: false,
|
|
226
|
+
enumerable: false,
|
|
227
|
+
value: GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION,
|
|
228
|
+
writable: false
|
|
229
|
+
});
|
|
230
|
+
if (typeof nameOrChannels === "string") {
|
|
231
|
+
this.start = new HookChannel(`tracing:${nameOrChannels}:start`);
|
|
232
|
+
this.end = new HookChannel(`tracing:${nameOrChannels}:end`);
|
|
233
|
+
this.asyncStart = new HookChannel(`tracing:${nameOrChannels}:asyncStart`);
|
|
234
|
+
this.asyncEnd = new HookChannel(`tracing:${nameOrChannels}:asyncEnd`);
|
|
235
|
+
this.error = new HookChannel(`tracing:${nameOrChannels}:error`);
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
this.start = nameOrChannels.start ?? new HookChannel("tracing:start");
|
|
239
|
+
this.end = nameOrChannels.end ?? new HookChannel("tracing:end");
|
|
240
|
+
this.asyncStart = nameOrChannels.asyncStart ?? new HookChannel("tracing:asyncStart");
|
|
241
|
+
this.asyncEnd = nameOrChannels.asyncEnd ?? new HookChannel("tracing:asyncEnd");
|
|
242
|
+
this.error = nameOrChannels.error ?? new HookChannel("tracing:error");
|
|
243
|
+
}
|
|
244
|
+
get hasSubscribers() {
|
|
245
|
+
return this.start.hasSubscribers || this.end.hasSubscribers || this.asyncStart.hasSubscribers || this.asyncEnd.hasSubscribers || this.error.hasSubscribers;
|
|
246
|
+
}
|
|
247
|
+
get hasInterceptors() {
|
|
248
|
+
return this.invocationHook.hasInterceptors;
|
|
249
|
+
}
|
|
250
|
+
intercept(interceptor) {
|
|
251
|
+
return this.invocationHook.intercept(interceptor);
|
|
252
|
+
}
|
|
253
|
+
invoke(target, thisArg, args, additional) {
|
|
254
|
+
return this.invocationHook.invoke(target, thisArg, args, additional);
|
|
255
|
+
}
|
|
256
|
+
traceInvocation(operator, target, thisArg, args, additional, callbackIndex = -1) {
|
|
257
|
+
return traceInvocation(
|
|
258
|
+
this,
|
|
259
|
+
operator,
|
|
260
|
+
target,
|
|
261
|
+
thisArg,
|
|
262
|
+
args,
|
|
263
|
+
additional,
|
|
264
|
+
callbackIndex
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
subscribe(handlers) {
|
|
268
|
+
for (const eventName of traceEvents) {
|
|
269
|
+
const handler = handlers[eventName];
|
|
270
|
+
if (handler) {
|
|
271
|
+
this[eventName].subscribe(handler);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
unsubscribe(handlers) {
|
|
276
|
+
let done = true;
|
|
277
|
+
for (const eventName of traceEvents) {
|
|
278
|
+
const handler = handlers[eventName];
|
|
279
|
+
if (handler && !this[eventName].unsubscribe(handler)) {
|
|
280
|
+
done = false;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
return done;
|
|
284
|
+
}
|
|
285
|
+
traceSync(fn, message = {}, thisArg, ...args) {
|
|
286
|
+
if (!this.hasSubscribers) {
|
|
287
|
+
return Reflect.apply(fn, thisArg, args);
|
|
288
|
+
}
|
|
289
|
+
const context = message;
|
|
290
|
+
return this.start.runStores(message, () => {
|
|
291
|
+
try {
|
|
292
|
+
const result = Reflect.apply(fn, thisArg, args);
|
|
293
|
+
setContextValue(context, "result", result);
|
|
294
|
+
return result;
|
|
295
|
+
} catch (error) {
|
|
296
|
+
setContextValue(context, "error", error);
|
|
297
|
+
this.error.publish(message);
|
|
298
|
+
throw error;
|
|
299
|
+
} finally {
|
|
300
|
+
this.end.publish(message);
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
tracePromise(fn, message = {}, thisArg, ...args) {
|
|
305
|
+
if (!this.hasSubscribers) {
|
|
306
|
+
return Reflect.apply(fn, thisArg, args);
|
|
307
|
+
}
|
|
308
|
+
const context = message;
|
|
309
|
+
return this.start.runStores(message, () => {
|
|
310
|
+
let result;
|
|
311
|
+
try {
|
|
312
|
+
result = Reflect.apply(fn, thisArg, args);
|
|
313
|
+
} catch (error) {
|
|
314
|
+
setContextValue(context, "error", error);
|
|
315
|
+
this.error.publish(message);
|
|
316
|
+
this.end.publish(message);
|
|
317
|
+
throw error;
|
|
318
|
+
}
|
|
319
|
+
this.end.publish(message);
|
|
320
|
+
if (!result || typeof result !== "object" && typeof result !== "function") {
|
|
321
|
+
setContextValue(context, "result", result);
|
|
322
|
+
this.asyncStart.publish(message);
|
|
323
|
+
this.asyncEnd.publish(message);
|
|
324
|
+
return result;
|
|
325
|
+
}
|
|
326
|
+
let terminalPublished = false;
|
|
327
|
+
const finishUnobservedResult = (error) => {
|
|
328
|
+
reportError(error);
|
|
329
|
+
if (!terminalPublished) {
|
|
330
|
+
terminalPublished = true;
|
|
331
|
+
setContextValue(context, "result", result);
|
|
332
|
+
this.asyncStart.publish(message);
|
|
333
|
+
this.asyncEnd.publish(message);
|
|
334
|
+
}
|
|
335
|
+
return result;
|
|
336
|
+
};
|
|
337
|
+
let then;
|
|
338
|
+
try {
|
|
339
|
+
then = result.then;
|
|
340
|
+
} catch (error) {
|
|
341
|
+
return finishUnobservedResult(error);
|
|
342
|
+
}
|
|
343
|
+
if (typeof then !== "function") {
|
|
344
|
+
setContextValue(context, "result", result);
|
|
345
|
+
this.asyncStart.publish(message);
|
|
346
|
+
this.asyncEnd.publish(message);
|
|
347
|
+
return result;
|
|
348
|
+
}
|
|
349
|
+
const resolve = (resolved) => {
|
|
350
|
+
if (!terminalPublished) {
|
|
351
|
+
terminalPublished = true;
|
|
352
|
+
setContextValue(context, "result", resolved);
|
|
353
|
+
this.asyncStart.publish(message);
|
|
354
|
+
this.asyncEnd.publish(message);
|
|
355
|
+
}
|
|
356
|
+
return resolved;
|
|
357
|
+
};
|
|
358
|
+
let rejectionThrown = false;
|
|
359
|
+
let rejectionError;
|
|
360
|
+
const reject = (error) => {
|
|
361
|
+
if (!terminalPublished) {
|
|
362
|
+
terminalPublished = true;
|
|
363
|
+
setContextValue(context, "error", error);
|
|
364
|
+
this.error.publish(message);
|
|
365
|
+
this.asyncStart.publish(message);
|
|
366
|
+
this.asyncEnd.publish(message);
|
|
367
|
+
}
|
|
368
|
+
rejectionThrown = true;
|
|
369
|
+
rejectionError = error;
|
|
370
|
+
throw error;
|
|
371
|
+
};
|
|
372
|
+
let isPlainPromise;
|
|
373
|
+
try {
|
|
374
|
+
isPlainPromise = result instanceof Promise && result.constructor === Promise;
|
|
375
|
+
} catch (error) {
|
|
376
|
+
return finishUnobservedResult(error);
|
|
377
|
+
}
|
|
378
|
+
try {
|
|
379
|
+
if (isPlainPromise) {
|
|
380
|
+
return Reflect.apply(then, result, [resolve, reject]);
|
|
381
|
+
}
|
|
382
|
+
Reflect.apply(then, result, [
|
|
383
|
+
resolve,
|
|
384
|
+
(error) => {
|
|
385
|
+
try {
|
|
386
|
+
reject(error);
|
|
387
|
+
} catch {
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
]);
|
|
391
|
+
} catch (error) {
|
|
392
|
+
if (rejectionThrown && error === rejectionError) {
|
|
393
|
+
return result;
|
|
394
|
+
}
|
|
395
|
+
return finishUnobservedResult(error);
|
|
396
|
+
}
|
|
397
|
+
return result;
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
traceCallback(fn, position = -1, message = {}, thisArg, ...args) {
|
|
401
|
+
if (!this.hasSubscribers) {
|
|
402
|
+
return Reflect.apply(fn, thisArg, args);
|
|
403
|
+
}
|
|
404
|
+
const context = message;
|
|
405
|
+
const callArgs = args.length > 0 ? args : context.arguments ?? args;
|
|
406
|
+
const callback = Array.prototype.at.call(callArgs, position);
|
|
407
|
+
if (typeof callback !== "function") {
|
|
408
|
+
return Reflect.apply(fn, thisArg, args);
|
|
409
|
+
}
|
|
410
|
+
const { asyncStart, asyncEnd, error: errorChannel } = this;
|
|
411
|
+
function wrappedCallback(error, result) {
|
|
412
|
+
if (error) {
|
|
413
|
+
setContextValue(context, "error", error);
|
|
414
|
+
errorChannel.publish(message);
|
|
415
|
+
} else {
|
|
416
|
+
setContextValue(context, "result", result);
|
|
417
|
+
}
|
|
418
|
+
return asyncStart.runStores(message, () => {
|
|
419
|
+
try {
|
|
420
|
+
return Reflect.apply(callback, this, arguments);
|
|
421
|
+
} finally {
|
|
422
|
+
asyncEnd.publish(message);
|
|
423
|
+
}
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
Array.prototype.splice.call(callArgs, position, 1, wrappedCallback);
|
|
427
|
+
return this.start.runStores(message, () => {
|
|
428
|
+
try {
|
|
429
|
+
return Reflect.apply(fn, thisArg, args);
|
|
430
|
+
} catch (error) {
|
|
431
|
+
setContextValue(context, "error", error);
|
|
432
|
+
this.error.publish(message);
|
|
433
|
+
throw error;
|
|
434
|
+
} finally {
|
|
435
|
+
this.end.publish(message);
|
|
436
|
+
}
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
};
|
|
440
|
+
var inertChannel = Object.freeze({
|
|
441
|
+
name: "braintrust:inert",
|
|
442
|
+
hasSubscribers: false,
|
|
443
|
+
subscribe() {
|
|
444
|
+
},
|
|
445
|
+
unsubscribe() {
|
|
446
|
+
return false;
|
|
447
|
+
},
|
|
448
|
+
bindStore() {
|
|
449
|
+
},
|
|
450
|
+
unbindStore() {
|
|
451
|
+
return false;
|
|
452
|
+
},
|
|
453
|
+
publish() {
|
|
454
|
+
},
|
|
455
|
+
runStores(_message, fn, thisArg, ...args) {
|
|
456
|
+
return Reflect.apply(fn, thisArg, args);
|
|
457
|
+
}
|
|
458
|
+
});
|
|
459
|
+
var inertTracingHook = new TracingHook({
|
|
460
|
+
start: inertChannel,
|
|
461
|
+
end: inertChannel,
|
|
462
|
+
asyncStart: inertChannel,
|
|
463
|
+
asyncEnd: inertChannel,
|
|
464
|
+
error: inertChannel
|
|
465
|
+
});
|
|
466
|
+
function isHookChannel(value) {
|
|
467
|
+
if (typeof value !== "object" && typeof value !== "function" || value === null) {
|
|
468
|
+
return false;
|
|
469
|
+
}
|
|
470
|
+
try {
|
|
471
|
+
const channel = value;
|
|
472
|
+
return typeof channel.hasSubscribers === "boolean" && typeof channel.subscribe === "function" && typeof channel.unsubscribe === "function" && typeof channel.bindStore === "function" && typeof channel.unbindStore === "function" && typeof channel.publish === "function" && typeof channel.runStores === "function";
|
|
473
|
+
} catch {
|
|
474
|
+
return false;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
function hasTracingHookShape(value) {
|
|
478
|
+
if (typeof value !== "object" && typeof value !== "function" || value === null) {
|
|
479
|
+
return false;
|
|
480
|
+
}
|
|
481
|
+
try {
|
|
482
|
+
const hook = value;
|
|
483
|
+
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);
|
|
484
|
+
} catch {
|
|
485
|
+
return false;
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
function hasInvocationHookShape(value) {
|
|
489
|
+
if (typeof value !== "object" && typeof value !== "function" || value === null) {
|
|
490
|
+
return false;
|
|
491
|
+
}
|
|
492
|
+
try {
|
|
493
|
+
const hook = value;
|
|
494
|
+
return value[invocationHookBrand] === GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION && typeof hook.hasInterceptors === "boolean" && typeof hook.intercept === "function" && typeof hook.invoke === "function";
|
|
495
|
+
} catch {
|
|
496
|
+
return false;
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
function installInvocationHook(value) {
|
|
500
|
+
const hasInvocationHook = hasInvocationHookShape(value);
|
|
501
|
+
const invocationHook = new InvocationHook();
|
|
502
|
+
try {
|
|
503
|
+
if (!hasInvocationHook) {
|
|
504
|
+
Object.defineProperties(value, {
|
|
505
|
+
[invocationHookBrand]: {
|
|
506
|
+
configurable: false,
|
|
507
|
+
enumerable: false,
|
|
508
|
+
value: GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION,
|
|
509
|
+
writable: false
|
|
510
|
+
},
|
|
511
|
+
hasInterceptors: {
|
|
512
|
+
configurable: false,
|
|
513
|
+
enumerable: false,
|
|
514
|
+
get: () => invocationHook.hasInterceptors
|
|
515
|
+
},
|
|
516
|
+
intercept: {
|
|
517
|
+
configurable: false,
|
|
518
|
+
enumerable: false,
|
|
519
|
+
value: invocationHook.intercept.bind(invocationHook),
|
|
520
|
+
writable: false
|
|
521
|
+
},
|
|
522
|
+
invoke: {
|
|
523
|
+
configurable: false,
|
|
524
|
+
enumerable: false,
|
|
525
|
+
value: invocationHook.invoke.bind(invocationHook),
|
|
526
|
+
writable: false
|
|
527
|
+
}
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
if (typeof value.traceInvocation !== "function") {
|
|
531
|
+
Object.defineProperty(value, "traceInvocation", {
|
|
532
|
+
configurable: false,
|
|
533
|
+
enumerable: false,
|
|
534
|
+
value: traceInvocation.bind(void 0, value),
|
|
535
|
+
writable: false
|
|
536
|
+
});
|
|
537
|
+
}
|
|
538
|
+
} catch (error) {
|
|
539
|
+
reportError(error);
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
function isCompatibleTracingHook(value) {
|
|
543
|
+
try {
|
|
544
|
+
return value[hookBrand] === GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION && hasTracingHookShape(value);
|
|
545
|
+
} catch {
|
|
546
|
+
return false;
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
function isCompatibleHookRegistry(value) {
|
|
550
|
+
try {
|
|
551
|
+
return value instanceof Map && value[registryBrand] === GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION;
|
|
552
|
+
} catch {
|
|
553
|
+
return false;
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
function getHookRegistry() {
|
|
557
|
+
let descriptor;
|
|
558
|
+
try {
|
|
559
|
+
descriptor = Object.getOwnPropertyDescriptor(
|
|
560
|
+
globalThis,
|
|
561
|
+
GLOBAL_INSTRUMENTATION_HOOKS_KEY
|
|
562
|
+
);
|
|
563
|
+
} catch (error) {
|
|
564
|
+
reportError(error);
|
|
565
|
+
return void 0;
|
|
566
|
+
}
|
|
567
|
+
if (descriptor && "value" in descriptor && isCompatibleHookRegistry(descriptor.value) && (descriptor.configurable || descriptor.enumerable === false)) {
|
|
568
|
+
if (descriptor.configurable || descriptor.writable) {
|
|
569
|
+
try {
|
|
570
|
+
Object.defineProperty(globalThis, GLOBAL_INSTRUMENTATION_HOOKS_KEY, {
|
|
571
|
+
configurable: false,
|
|
572
|
+
enumerable: false,
|
|
573
|
+
value: descriptor.value,
|
|
574
|
+
writable: false
|
|
575
|
+
});
|
|
576
|
+
} catch (error) {
|
|
577
|
+
reportError(error);
|
|
578
|
+
return void 0;
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
return descriptor.value;
|
|
582
|
+
}
|
|
583
|
+
if (descriptor && !descriptor.configurable) {
|
|
584
|
+
reportError(new Error("Incompatible global instrumentation hook registry"));
|
|
585
|
+
return void 0;
|
|
586
|
+
}
|
|
587
|
+
const registry = /* @__PURE__ */ new Map();
|
|
588
|
+
try {
|
|
589
|
+
Object.defineProperty(registry, registryBrand, {
|
|
590
|
+
configurable: false,
|
|
591
|
+
enumerable: false,
|
|
592
|
+
value: GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION,
|
|
593
|
+
writable: false
|
|
594
|
+
});
|
|
595
|
+
} catch (error) {
|
|
596
|
+
reportError(error);
|
|
597
|
+
return void 0;
|
|
598
|
+
}
|
|
599
|
+
try {
|
|
600
|
+
Object.defineProperty(globalThis, GLOBAL_INSTRUMENTATION_HOOKS_KEY, {
|
|
601
|
+
configurable: false,
|
|
602
|
+
enumerable: false,
|
|
603
|
+
value: registry,
|
|
604
|
+
writable: false
|
|
605
|
+
});
|
|
606
|
+
return registry;
|
|
607
|
+
} catch (error) {
|
|
608
|
+
reportError(error);
|
|
609
|
+
return void 0;
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
function newGlobalTracingChannel(nameOrChannels) {
|
|
613
|
+
if (typeof nameOrChannels !== "string") {
|
|
614
|
+
return new TracingHook(nameOrChannels);
|
|
615
|
+
}
|
|
616
|
+
const registry = getHookRegistry();
|
|
617
|
+
if (!registry) {
|
|
618
|
+
return inertTracingHook;
|
|
619
|
+
}
|
|
620
|
+
let existing;
|
|
621
|
+
try {
|
|
622
|
+
existing = Map.prototype.get.call(registry, nameOrChannels);
|
|
623
|
+
} catch (error) {
|
|
624
|
+
reportError(error);
|
|
625
|
+
return inertTracingHook;
|
|
626
|
+
}
|
|
627
|
+
if (isCompatibleTracingHook(existing)) {
|
|
628
|
+
installInvocationHook(existing);
|
|
629
|
+
return existing;
|
|
630
|
+
}
|
|
631
|
+
if (existing !== void 0) {
|
|
632
|
+
reportError(
|
|
633
|
+
new Error(`Invalid global instrumentation hook: ${nameOrChannels}`)
|
|
634
|
+
);
|
|
635
|
+
try {
|
|
636
|
+
Map.prototype.delete.call(registry, nameOrChannels);
|
|
637
|
+
} catch (error) {
|
|
638
|
+
reportError(error);
|
|
639
|
+
return inertTracingHook;
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
const hook = new TracingHook(nameOrChannels);
|
|
643
|
+
try {
|
|
644
|
+
Map.prototype.set.call(registry, nameOrChannels, hook);
|
|
645
|
+
} catch (error) {
|
|
646
|
+
reportError(error);
|
|
647
|
+
return inertTracingHook;
|
|
648
|
+
}
|
|
649
|
+
return hook;
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
// src/auto-instrumentations/loader/mastra-observability-patch.ts
|
|
653
|
+
var MASTRA_EXPORTER_FACTORY_GLOBAL = "__braintrustMastraExporterFactory";
|
|
654
|
+
function installMastraExporterFactory(factory) {
|
|
655
|
+
const globals = globalThis;
|
|
656
|
+
globals[MASTRA_EXPORTER_FACTORY_GLOBAL] ??= factory;
|
|
657
|
+
}
|
|
658
|
+
var MASTRA_CORE_PACKAGE = "@mastra/core";
|
|
659
|
+
var MASTRA_OBSERVABILITY_PACKAGE = "@mastra/observability";
|
|
660
|
+
var MASTRA_CORE_ENTRY_PATHS = /* @__PURE__ */ new Set([
|
|
661
|
+
"dist/index.js",
|
|
662
|
+
"dist/index.cjs",
|
|
663
|
+
"dist/mastra/index.js",
|
|
664
|
+
"dist/mastra/index.cjs"
|
|
665
|
+
]);
|
|
666
|
+
var MASTRA_OBSERVABILITY_ENTRY_PATHS = /* @__PURE__ */ new Set([
|
|
667
|
+
"dist/index.js",
|
|
668
|
+
"dist/index.cjs"
|
|
669
|
+
]);
|
|
670
|
+
function classifyMastraTarget(packageName, modulePath) {
|
|
671
|
+
if (packageName === MASTRA_CORE_PACKAGE && MASTRA_CORE_ENTRY_PATHS.has(modulePath)) {
|
|
672
|
+
return "core";
|
|
673
|
+
}
|
|
674
|
+
if (packageName === MASTRA_OBSERVABILITY_PACKAGE && MASTRA_OBSERVABILITY_ENTRY_PATHS.has(modulePath)) {
|
|
675
|
+
return "observability";
|
|
676
|
+
}
|
|
677
|
+
return null;
|
|
678
|
+
}
|
|
679
|
+
function extractChunkPath(source) {
|
|
680
|
+
const esmMatch = source.match(
|
|
681
|
+
/export\s*\{\s*Mastra(?:\s+as\s+\w+)?\s*\}\s*from\s*['"]([^'"]+)['"]/
|
|
682
|
+
);
|
|
683
|
+
if (esmMatch) return esmMatch[1];
|
|
684
|
+
const cjsMatch = source.match(
|
|
685
|
+
/require\s*\(\s*['"]([^'"]+chunk-[^'"]+)['"]\s*\)/
|
|
686
|
+
);
|
|
687
|
+
if (cjsMatch) return cjsMatch[1];
|
|
688
|
+
return null;
|
|
689
|
+
}
|
|
690
|
+
var EXPORTER_FACTORY_KEY = JSON.stringify(MASTRA_EXPORTER_FACTORY_GLOBAL);
|
|
691
|
+
var MASTRA_PROXY_HANDLER_BODY = `
|
|
692
|
+
{
|
|
693
|
+
construct(target, args, newTarget) {
|
|
694
|
+
var firstArg = args[0];
|
|
695
|
+
if (
|
|
696
|
+
(!firstArg || typeof firstArg !== "object" || !firstArg.observability) &&
|
|
697
|
+
__braintrustObservabilityClass
|
|
698
|
+
) {
|
|
699
|
+
try {
|
|
700
|
+
// serviceName is required by Mastra's Observability validator; pass
|
|
701
|
+
// something sensible by default. Users who want a different name
|
|
702
|
+
// should construct Observability themselves.
|
|
703
|
+
var observability = new __braintrustObservabilityClass({
|
|
704
|
+
configs: { default: { serviceName: "mastra" } },
|
|
705
|
+
});
|
|
706
|
+
args = args.slice();
|
|
707
|
+
args[0] = Object.assign({}, firstArg, { observability: observability });
|
|
708
|
+
} catch (e) {
|
|
709
|
+
// Fall through. Mastra will use its own NoOp; user code still works,
|
|
710
|
+
// just without auto-instrumentation.
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
return Reflect.construct(target, args, newTarget);
|
|
714
|
+
},
|
|
715
|
+
}`;
|
|
716
|
+
function buildMastraEsmWrapper(chunkPath) {
|
|
717
|
+
const chunk = JSON.stringify(chunkPath);
|
|
718
|
+
return `import { Mastra as __braintrustOrigMastra } from ${chunk};
|
|
719
|
+
import { createRequire as __braintrustCreateRequire } from "node:module";
|
|
720
|
+
|
|
721
|
+
let __braintrustObservabilityClass = null;
|
|
722
|
+
try {
|
|
723
|
+
// Resolve @mastra/observability relative to this module (the Mastra entry),
|
|
724
|
+
// so it's looked up from the user's node_modules tree.
|
|
725
|
+
const __braintrustRequire = __braintrustCreateRequire(import.meta.url);
|
|
726
|
+
__braintrustObservabilityClass =
|
|
727
|
+
__braintrustRequire("@mastra/observability").Observability;
|
|
728
|
+
} catch (e) {
|
|
729
|
+
// @mastra/observability isn't installed; the construct trap will skip the
|
|
730
|
+
// auto-construct branch and Mastra falls back to its own NoOp.
|
|
731
|
+
}
|
|
732
|
+
const Mastra = new Proxy(__braintrustOrigMastra, ${MASTRA_PROXY_HANDLER_BODY});
|
|
733
|
+
export { Mastra };
|
|
734
|
+
`;
|
|
735
|
+
}
|
|
736
|
+
function buildMastraCjsWrapper(chunkPath) {
|
|
737
|
+
const chunk = JSON.stringify(chunkPath);
|
|
738
|
+
return `'use strict';
|
|
739
|
+
const __braintrustChunk = require(${chunk});
|
|
740
|
+
|
|
741
|
+
let __braintrustObservabilityClass = null;
|
|
742
|
+
try {
|
|
743
|
+
__braintrustObservabilityClass = require("@mastra/observability").Observability;
|
|
744
|
+
} catch (e) {
|
|
745
|
+
// @mastra/observability isn't installed; same fallback as the ESM wrapper.
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
const __braintrustWrappedMastra = new Proxy(
|
|
749
|
+
__braintrustChunk.Mastra,
|
|
750
|
+
${MASTRA_PROXY_HANDLER_BODY},
|
|
751
|
+
);
|
|
752
|
+
Object.defineProperty(exports, "Mastra", {
|
|
753
|
+
enumerable: true,
|
|
754
|
+
configurable: true,
|
|
755
|
+
get: function () { return __braintrustWrappedMastra; }
|
|
756
|
+
});
|
|
757
|
+
`;
|
|
758
|
+
}
|
|
759
|
+
var OBSERVABILITY_APPEND_BODY = `
|
|
760
|
+
;(function __braintrustWrapObservability() {
|
|
761
|
+
// Top-level so we can both read and reassign the var binding the original
|
|
762
|
+
// entry declared.
|
|
763
|
+
if (typeof Observability === "undefined") return;
|
|
764
|
+
if (Observability.__braintrustWrapped) return;
|
|
765
|
+
function __braintrustEnsureExporter(rawConfig) {
|
|
766
|
+
try {
|
|
767
|
+
var factory = globalThis[${EXPORTER_FACTORY_KEY}];
|
|
768
|
+
if (typeof factory !== "function") return rawConfig;
|
|
769
|
+
var config = rawConfig && typeof rawConfig === "object" ? rawConfig : {};
|
|
770
|
+
var configsIn = config.configs && typeof config.configs === "object" ? config.configs : null;
|
|
771
|
+
var configsOut = {};
|
|
772
|
+
var hadEntries = false;
|
|
773
|
+
if (configsIn) {
|
|
774
|
+
for (var name in configsIn) {
|
|
775
|
+
if (!Object.prototype.hasOwnProperty.call(configsIn, name)) continue;
|
|
776
|
+
hadEntries = true;
|
|
777
|
+
var inst = configsIn[name] || {};
|
|
778
|
+
var existing = Array.isArray(inst.exporters) ? inst.exporters : [];
|
|
779
|
+
var hasOurs = existing.some(function (e) { return e && e.name === "braintrust"; });
|
|
780
|
+
configsOut[name] = Object.assign({}, inst, {
|
|
781
|
+
exporters: hasOurs ? existing : existing.concat([factory()]),
|
|
782
|
+
});
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
if (!hadEntries) {
|
|
786
|
+
configsOut.default = {
|
|
787
|
+
serviceName: "mastra",
|
|
788
|
+
exporters: [factory()],
|
|
789
|
+
};
|
|
790
|
+
}
|
|
791
|
+
return Object.assign({}, config, { configs: configsOut });
|
|
792
|
+
} catch (e) {
|
|
793
|
+
return rawConfig;
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
var __OriginalObservability = Observability;
|
|
797
|
+
Observability = new Proxy(__OriginalObservability, {
|
|
798
|
+
construct: function (target, args, newTarget) {
|
|
799
|
+
var nextArgs = args.slice();
|
|
800
|
+
nextArgs[0] = __braintrustEnsureExporter(nextArgs[0]);
|
|
801
|
+
return Reflect.construct(target, nextArgs, newTarget);
|
|
802
|
+
},
|
|
803
|
+
});
|
|
804
|
+
Observability.__braintrustWrapped = true;
|
|
805
|
+
if (typeof exports !== "undefined" && exports && typeof exports === "object") {
|
|
806
|
+
try {
|
|
807
|
+
Object.defineProperty(exports, "Observability", {
|
|
808
|
+
enumerable: true,
|
|
809
|
+
configurable: true,
|
|
810
|
+
get: function () { return Observability; },
|
|
811
|
+
});
|
|
812
|
+
} catch (e) {}
|
|
813
|
+
}
|
|
814
|
+
})();
|
|
815
|
+
`;
|
|
816
|
+
function patchMastraSource(source, target, format) {
|
|
817
|
+
if (target === "core") {
|
|
818
|
+
const chunkPath = extractChunkPath(source);
|
|
819
|
+
if (!chunkPath) return source;
|
|
820
|
+
return format === "esm" ? buildMastraEsmWrapper(chunkPath) : buildMastraCjsWrapper(chunkPath);
|
|
821
|
+
}
|
|
822
|
+
return source + OBSERVABILITY_APPEND_BODY;
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
// src/auto-instrumentations/orchestrion-js/matcher.ts
|
|
826
|
+
import semifies from "semifies";
|
|
827
|
+
|
|
828
|
+
// src/auto-instrumentations/orchestrion-js/transformer.ts
|
|
829
|
+
import esquery2 from "esquery";
|
|
830
|
+
import { generate } from "astring";
|
|
831
|
+
import { parse as parse2 } from "meriyah";
|
|
832
|
+
import { SourceMapGenerator } from "source-map";
|
|
833
|
+
|
|
834
|
+
// src/auto-instrumentations/orchestrion-js/transforms.ts
|
|
835
|
+
import esquery from "esquery";
|
|
836
|
+
import { parse } from "meriyah";
|
|
837
|
+
var CHANNEL_REGEX = /[^\w]/g;
|
|
838
|
+
var SHARED_HOOK_LOOKUP = "tr_ch_bt$get_hook";
|
|
839
|
+
function formatChannelVariable(channelName) {
|
|
840
|
+
return `tr_ch_bt$${channelName.replace(CHANNEL_REGEX, "_")}`;
|
|
841
|
+
}
|
|
842
|
+
function formatChannelGetter(channelName) {
|
|
843
|
+
return `tr_ch_bt$get_${channelName.replace(CHANNEL_REGEX, "_")}`;
|
|
844
|
+
}
|
|
845
|
+
var transforms = {
|
|
846
|
+
tracingHookDeclaration(state, node) {
|
|
847
|
+
const {
|
|
848
|
+
channelName,
|
|
849
|
+
module: { name }
|
|
850
|
+
} = state;
|
|
851
|
+
const channelVariable = formatChannelVariable(channelName);
|
|
852
|
+
const channelGetter = formatChannelGetter(channelName);
|
|
853
|
+
const hasSharedHookLookup = node.body.some(
|
|
854
|
+
(child) => child.declarations?.[0]?.id?.name === SHARED_HOOK_LOOKUP
|
|
855
|
+
);
|
|
856
|
+
if (node.body.some(
|
|
857
|
+
(child) => child.declarations?.[0]?.id?.name === channelGetter
|
|
858
|
+
)) {
|
|
859
|
+
return;
|
|
860
|
+
}
|
|
861
|
+
const sharedHookLookup = hasSharedHookLookup ? "" : `
|
|
862
|
+
const ${SHARED_HOOK_LOOKUP} = (__bt$hookName) => {
|
|
863
|
+
try {
|
|
864
|
+
const __bt$hooks = globalThis[${JSON.stringify(
|
|
865
|
+
GLOBAL_INSTRUMENTATION_HOOKS_KEY
|
|
866
|
+
)}];
|
|
867
|
+
if (
|
|
868
|
+
!(__bt$hooks instanceof Map) ||
|
|
869
|
+
__bt$hooks[Symbol.for(${JSON.stringify(
|
|
870
|
+
GLOBAL_INSTRUMENTATION_HOOKS_REGISTRY_BRAND
|
|
871
|
+
)})] !== ${GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION}
|
|
872
|
+
) return undefined;
|
|
873
|
+
const __bt$hook = Map.prototype.get.call(
|
|
874
|
+
__bt$hooks,
|
|
875
|
+
__bt$hookName
|
|
876
|
+
);
|
|
877
|
+
if (
|
|
878
|
+
(__bt$hook === null ||
|
|
879
|
+
(typeof __bt$hook !== "object" &&
|
|
880
|
+
typeof __bt$hook !== "function")) ||
|
|
881
|
+
__bt$hook[Symbol.for(${JSON.stringify(
|
|
882
|
+
GLOBAL_INSTRUMENTATION_HOOK_BRAND
|
|
883
|
+
)})] !== ${GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION} ||
|
|
884
|
+
typeof __bt$hook.traceInvocation !== "function"
|
|
885
|
+
) return undefined;
|
|
886
|
+
return __bt$hook;
|
|
887
|
+
} catch {
|
|
888
|
+
return undefined;
|
|
889
|
+
}
|
|
890
|
+
};
|
|
891
|
+
`;
|
|
892
|
+
const code = `
|
|
893
|
+
${sharedHookLookup}
|
|
894
|
+
let ${channelVariable};
|
|
895
|
+
const ${channelGetter} = () =>
|
|
896
|
+
${channelVariable} ??= ${SHARED_HOOK_LOOKUP}(
|
|
897
|
+
${JSON.stringify(`orchestrion:${name}:${channelName}`)}
|
|
898
|
+
);
|
|
899
|
+
`;
|
|
900
|
+
const sharedHookLookupIndex = node.body.findIndex(
|
|
901
|
+
(child) => child.declarations?.[0]?.id?.name === SHARED_HOOK_LOOKUP
|
|
902
|
+
);
|
|
903
|
+
const index = sharedHookLookupIndex === -1 ? node.body.findIndex(
|
|
904
|
+
(child) => child.directive === "use strict"
|
|
905
|
+
) : sharedHookLookupIndex;
|
|
906
|
+
node.body.splice(index + 1, 0, ...parse(code).body);
|
|
907
|
+
},
|
|
908
|
+
traceCallback: traceAny,
|
|
909
|
+
tracePromise: traceAny,
|
|
910
|
+
traceSync: traceAny
|
|
911
|
+
};
|
|
912
|
+
function traceAny(state, node, _parent, ancestry) {
|
|
913
|
+
const program = ancestry[ancestry.length - 1];
|
|
914
|
+
if (node.type === "ClassDeclaration" || node.type === "ClassExpression") {
|
|
915
|
+
traceInstanceMethod(state, node, program);
|
|
916
|
+
} else {
|
|
917
|
+
traceFunction(state, node, program);
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
function traceFunction(state, node, program) {
|
|
921
|
+
transforms.tracingHookDeclaration(state, program, null, []);
|
|
922
|
+
const { functionQuery } = state;
|
|
923
|
+
const methodName = "methodName" in functionQuery ? functionQuery.methodName : void 0;
|
|
924
|
+
const privateMethodName = "privateMethodName" in functionQuery ? functionQuery.privateMethodName : void 0;
|
|
925
|
+
const functionName = "functionName" in functionQuery ? functionQuery.functionName : void 0;
|
|
926
|
+
const isConstructor = methodName === "constructor" || !methodName && !privateMethodName && !functionName;
|
|
927
|
+
const type = isConstructor ? "ArrowFunctionExpression" : "FunctionExpression";
|
|
928
|
+
node.body = wrap(state, {
|
|
929
|
+
type,
|
|
930
|
+
params: node.params,
|
|
931
|
+
body: node.body,
|
|
932
|
+
async: node.async,
|
|
933
|
+
expression: false,
|
|
934
|
+
generator: node.generator
|
|
935
|
+
});
|
|
936
|
+
node.generator = false;
|
|
937
|
+
node.async = false;
|
|
938
|
+
wrapSuper(node);
|
|
939
|
+
}
|
|
940
|
+
function traceInstanceMethod(state, node, program) {
|
|
941
|
+
const { functionQuery, operator } = state;
|
|
942
|
+
const { methodName } = functionQuery;
|
|
943
|
+
if (!methodName) {
|
|
944
|
+
return;
|
|
945
|
+
}
|
|
946
|
+
const classBody = node.body;
|
|
947
|
+
if (classBody.body.some(({ key }) => key?.name === methodName)) {
|
|
948
|
+
return;
|
|
949
|
+
}
|
|
950
|
+
let ctor = classBody.body.find(({ kind }) => kind === "constructor");
|
|
951
|
+
transforms.tracingHookDeclaration(state, program, null, []);
|
|
952
|
+
if (!ctor) {
|
|
953
|
+
ctor = parse(
|
|
954
|
+
node.superClass ? "class A extends Object { constructor (...args) { super(...args) } }" : "class A { constructor () {} }"
|
|
955
|
+
).body[0].body.body[0];
|
|
956
|
+
classBody.body.unshift(ctor);
|
|
957
|
+
}
|
|
958
|
+
const ctorBody = parse(`
|
|
959
|
+
const __bt$${methodName} = this["${methodName}"]
|
|
960
|
+
this["${methodName}"] = function () {}
|
|
961
|
+
`).body;
|
|
962
|
+
const fn = ctorBody[1].expression.right;
|
|
963
|
+
fn.async = operator === "tracePromise";
|
|
964
|
+
fn.body = wrap(state, {
|
|
965
|
+
type: "Identifier",
|
|
966
|
+
name: `__bt$${methodName}`
|
|
967
|
+
});
|
|
968
|
+
wrapSuper(fn);
|
|
969
|
+
ctor.value.body.body.push(...ctorBody);
|
|
970
|
+
}
|
|
971
|
+
function wrap(state, node) {
|
|
972
|
+
const wrapper = wrapInvocation(state);
|
|
973
|
+
const block = wrapper.body[0].body;
|
|
974
|
+
const common = parse(
|
|
975
|
+
node.type === "ArrowFunctionExpression" ? `
|
|
976
|
+
const __bt$target = (...__bt$args) => {
|
|
977
|
+
const __bt$wrapped = () => {};
|
|
978
|
+
return __bt$wrapped(...__bt$args);
|
|
979
|
+
};
|
|
980
|
+
` : `
|
|
981
|
+
const __bt$target = function (...__bt$args) {
|
|
982
|
+
const __bt$wrapped = () => {};
|
|
983
|
+
return __bt$wrapped.apply(this, __bt$args);
|
|
984
|
+
};
|
|
985
|
+
`
|
|
986
|
+
).body;
|
|
987
|
+
block.body.unshift(...common);
|
|
988
|
+
esquery.query(block, "[id.name=__bt$wrapped]")[0].init = node;
|
|
989
|
+
return block;
|
|
990
|
+
}
|
|
991
|
+
function wrapSuper(node) {
|
|
992
|
+
const members = /* @__PURE__ */ new Set();
|
|
993
|
+
esquery.traverse(
|
|
994
|
+
node.body,
|
|
995
|
+
esquery.parse("[object.type=Super]"),
|
|
996
|
+
(node2, parent) => {
|
|
997
|
+
const { name } = node2.property;
|
|
998
|
+
let child;
|
|
999
|
+
if (parent.callee) {
|
|
1000
|
+
const { expression } = parse(`__bt$super['${name}'].call(this)`).body[0];
|
|
1001
|
+
parent.callee = child = expression.callee;
|
|
1002
|
+
parent.arguments.unshift(...expression.arguments);
|
|
1003
|
+
} else {
|
|
1004
|
+
parent.expression = child = parse(`__bt$super['${name}']`).body[0];
|
|
1005
|
+
}
|
|
1006
|
+
child.computed = parent.callee.computed;
|
|
1007
|
+
child.optional = parent.callee.optional;
|
|
1008
|
+
members.add(name);
|
|
1009
|
+
}
|
|
1010
|
+
);
|
|
1011
|
+
for (const name of members) {
|
|
1012
|
+
const member = parse(`
|
|
1013
|
+
class Wrapper {
|
|
1014
|
+
wrapper () {
|
|
1015
|
+
__bt$super['${name}'] = super['${name}']
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
`).body[0].body.body[0].value.body.body[0];
|
|
1019
|
+
node.body.body.unshift(member);
|
|
1020
|
+
}
|
|
1021
|
+
if (members.size > 0) {
|
|
1022
|
+
node.body.body.unshift(parse("const __bt$super = {}").body[0]);
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
function wrapInvocation(state) {
|
|
1026
|
+
const { channelName, moduleVersion, operator, functionQuery } = state;
|
|
1027
|
+
const channelGetter = formatChannelGetter(channelName);
|
|
1028
|
+
const callbackIndex = functionQuery.callbackIndex ?? -1;
|
|
1029
|
+
return parse(`
|
|
1030
|
+
function wrapper () {
|
|
1031
|
+
const __bt$hook = ${channelGetter}();
|
|
1032
|
+
if (!__bt$hook) return __bt$target.apply(this, arguments);
|
|
1033
|
+
return __bt$hook.traceInvocation(
|
|
1034
|
+
${JSON.stringify(operator)},
|
|
1035
|
+
__bt$target,
|
|
1036
|
+
this,
|
|
1037
|
+
arguments,
|
|
1038
|
+
{ moduleVersion: ${JSON.stringify(moduleVersion)} },
|
|
1039
|
+
${callbackIndex}
|
|
1040
|
+
);
|
|
1041
|
+
}
|
|
1042
|
+
`);
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
// src/auto-instrumentations/orchestrion-js/transformer.ts
|
|
1046
|
+
var Transformer = class {
|
|
1047
|
+
moduleName;
|
|
1048
|
+
version;
|
|
1049
|
+
filePath;
|
|
1050
|
+
configs = [];
|
|
1051
|
+
constructor(moduleName, version, filePath, configs) {
|
|
1052
|
+
this.moduleName = moduleName;
|
|
1053
|
+
this.version = version;
|
|
1054
|
+
this.filePath = filePath;
|
|
1055
|
+
this.configs = configs;
|
|
1056
|
+
}
|
|
1057
|
+
/**
|
|
1058
|
+
* Instruments `code` by injecting global tracing hooks around the
|
|
1059
|
+
* target functions defined by this transformer's configs.
|
|
1060
|
+
*/
|
|
1061
|
+
transform(code, moduleType) {
|
|
1062
|
+
if (Buffer.isBuffer(code)) {
|
|
1063
|
+
code = code.toString();
|
|
1064
|
+
}
|
|
1065
|
+
if (!code) {
|
|
1066
|
+
return { code };
|
|
1067
|
+
}
|
|
1068
|
+
let ast;
|
|
1069
|
+
let aliases = {};
|
|
1070
|
+
let injectionCount = 0;
|
|
1071
|
+
for (const config of this.configs) {
|
|
1072
|
+
const { astQuery, functionQuery } = config;
|
|
1073
|
+
if (!ast) {
|
|
1074
|
+
const options = {
|
|
1075
|
+
loc: true,
|
|
1076
|
+
ranges: true,
|
|
1077
|
+
raw: true,
|
|
1078
|
+
module: moduleType === "esm"
|
|
1079
|
+
};
|
|
1080
|
+
try {
|
|
1081
|
+
ast = parse2(code, options);
|
|
1082
|
+
} catch {
|
|
1083
|
+
ast = parse2(code, { ...options, module: !options.module });
|
|
1084
|
+
}
|
|
1085
|
+
if (moduleType === "esm") {
|
|
1086
|
+
aliases = this.collectExportAliases(ast);
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
const resolvedFunctionQuery = this.resolveExportAlias(
|
|
1090
|
+
functionQuery,
|
|
1091
|
+
aliases
|
|
1092
|
+
);
|
|
1093
|
+
const query = astQuery || this.fromFunctionQuery(resolvedFunctionQuery);
|
|
1094
|
+
const state = {
|
|
1095
|
+
...config,
|
|
1096
|
+
moduleVersion: this.version,
|
|
1097
|
+
functionQuery: resolvedFunctionQuery,
|
|
1098
|
+
operator: this.getOperator(resolvedFunctionQuery.kind)
|
|
1099
|
+
};
|
|
1100
|
+
esquery2.traverse(ast, esquery2.parse(query), (...args) => {
|
|
1101
|
+
injectionCount++;
|
|
1102
|
+
this.visit(state, ...args);
|
|
1103
|
+
});
|
|
1104
|
+
}
|
|
1105
|
+
if (injectionCount === 0 && this.configs.length > 0) {
|
|
1106
|
+
const names = this.configs.map(({ astQuery, functionQuery }) => {
|
|
1107
|
+
const resolvedQuery = this.resolveExportAlias(functionQuery, aliases);
|
|
1108
|
+
const queryName = (q) => q.methodName || q.privateMethodName || q.functionName || q.propertyName || astQuery || "unknown";
|
|
1109
|
+
const originalName = queryName(functionQuery);
|
|
1110
|
+
const originalAlias = functionQuery.className || functionQuery.functionName;
|
|
1111
|
+
const resolvedAlias = resolvedQuery.className || resolvedQuery.functionName;
|
|
1112
|
+
if (originalAlias && originalAlias !== resolvedAlias) {
|
|
1113
|
+
return `${originalAlias} (local name: ${resolvedAlias})`;
|
|
1114
|
+
}
|
|
1115
|
+
return originalName;
|
|
1116
|
+
});
|
|
1117
|
+
throw new Error(
|
|
1118
|
+
`Failed to find injection points for: ${JSON.stringify(names)}`
|
|
1119
|
+
);
|
|
1120
|
+
}
|
|
1121
|
+
if (ast) {
|
|
1122
|
+
const file = `${this.moduleName}/${this.filePath}`;
|
|
1123
|
+
const sourceMap = new SourceMapGenerator({ file });
|
|
1124
|
+
const transformedCode = generate(ast, { sourceMap });
|
|
1125
|
+
const map = sourceMap.toString();
|
|
1126
|
+
return { code: transformedCode, map };
|
|
1127
|
+
}
|
|
1128
|
+
return { code };
|
|
1129
|
+
}
|
|
1130
|
+
free() {
|
|
1131
|
+
}
|
|
1132
|
+
visit(state, ...args) {
|
|
1133
|
+
const transform = transforms[state.operator];
|
|
1134
|
+
const { index = 0 } = state.functionQuery;
|
|
1135
|
+
const [node] = args;
|
|
1136
|
+
const type = node.init?.type || node.type;
|
|
1137
|
+
if (type !== "ClassDeclaration" && type !== "ClassExpression") {
|
|
1138
|
+
if (node.type === "VariableDeclarator") {
|
|
1139
|
+
return;
|
|
1140
|
+
}
|
|
1141
|
+
state.functionIndex = state.functionIndex === void 0 ? 0 : state.functionIndex + 1;
|
|
1142
|
+
if (index !== null && index !== state.functionIndex) {
|
|
1143
|
+
return;
|
|
1144
|
+
}
|
|
1145
|
+
}
|
|
1146
|
+
transform(state, ...args);
|
|
1147
|
+
}
|
|
1148
|
+
getOperator(kind) {
|
|
1149
|
+
switch (kind) {
|
|
1150
|
+
case "Async":
|
|
1151
|
+
return "tracePromise";
|
|
1152
|
+
case "Callback":
|
|
1153
|
+
return "traceCallback";
|
|
1154
|
+
case "Sync":
|
|
1155
|
+
return "traceSync";
|
|
1156
|
+
}
|
|
1157
|
+
}
|
|
1158
|
+
collectExportAliases(ast) {
|
|
1159
|
+
const aliases = {};
|
|
1160
|
+
for (const node of ast.body) {
|
|
1161
|
+
if (node.type === "ExportNamedDeclaration" && !node.source) {
|
|
1162
|
+
for (const spec of node.specifiers) {
|
|
1163
|
+
if (spec.exported && spec.local) {
|
|
1164
|
+
const exportedName = spec.exported.name ?? spec.exported.value;
|
|
1165
|
+
const localName = spec.local.name ?? spec.local.value;
|
|
1166
|
+
if (exportedName && localName) {
|
|
1167
|
+
aliases[exportedName] = localName;
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
}
|
|
1173
|
+
return aliases;
|
|
1174
|
+
}
|
|
1175
|
+
resolveExportAlias(functionQuery, aliases) {
|
|
1176
|
+
if (!("isExportAlias" in functionQuery) || !functionQuery.isExportAlias) {
|
|
1177
|
+
return functionQuery;
|
|
1178
|
+
}
|
|
1179
|
+
if ("className" in functionQuery && aliases[functionQuery.className]) {
|
|
1180
|
+
return {
|
|
1181
|
+
...functionQuery,
|
|
1182
|
+
className: aliases[functionQuery.className]
|
|
1183
|
+
};
|
|
1184
|
+
}
|
|
1185
|
+
if ("functionName" in functionQuery && aliases[functionQuery.functionName]) {
|
|
1186
|
+
return {
|
|
1187
|
+
...functionQuery,
|
|
1188
|
+
functionName: aliases[functionQuery.functionName]
|
|
1189
|
+
};
|
|
1190
|
+
}
|
|
1191
|
+
return functionQuery;
|
|
1192
|
+
}
|
|
1193
|
+
functionQueryLabel(functionQuery) {
|
|
1194
|
+
if ("methodName" in functionQuery) {
|
|
1195
|
+
return functionQuery.methodName;
|
|
1196
|
+
}
|
|
1197
|
+
if ("privateMethodName" in functionQuery) {
|
|
1198
|
+
return functionQuery.privateMethodName;
|
|
1199
|
+
}
|
|
1200
|
+
if ("functionName" in functionQuery) {
|
|
1201
|
+
return functionQuery.functionName;
|
|
1202
|
+
}
|
|
1203
|
+
if ("propertyName" in functionQuery) {
|
|
1204
|
+
return functionQuery.propertyName;
|
|
1205
|
+
}
|
|
1206
|
+
return "unknown";
|
|
1207
|
+
}
|
|
1208
|
+
fromFunctionQuery(functionQuery) {
|
|
1209
|
+
const queries = [];
|
|
1210
|
+
if ("className" in functionQuery) {
|
|
1211
|
+
const { className } = functionQuery;
|
|
1212
|
+
const methodName = this.functionQueryLabel(functionQuery);
|
|
1213
|
+
const keyType = "privateMethodName" in functionQuery ? "PrivateIdentifier" : "Identifier";
|
|
1214
|
+
queries.push(
|
|
1215
|
+
`[id.name="${className}"]`,
|
|
1216
|
+
`[id.name="${className}"] > ClassExpression`,
|
|
1217
|
+
`[id.name="${className}"] > ClassBody > [key.name="${methodName}"][key.type=${keyType}] > [async]`,
|
|
1218
|
+
`[id.name="${className}"] > ClassExpression > ClassBody > [key.name="${methodName}"][key.type=${keyType}] > [async]`
|
|
1219
|
+
);
|
|
1220
|
+
} else if ("methodName" in functionQuery) {
|
|
1221
|
+
const { methodName } = functionQuery;
|
|
1222
|
+
queries.push(
|
|
1223
|
+
`ClassBody > [key.name="${methodName}"][key.type=Identifier] > [async]`,
|
|
1224
|
+
`Property[key.name="${methodName}"][key.type=Identifier] > [async]`
|
|
1225
|
+
);
|
|
1226
|
+
}
|
|
1227
|
+
if ("functionName" in functionQuery) {
|
|
1228
|
+
const { functionName } = functionQuery;
|
|
1229
|
+
queries.push(`FunctionDeclaration[id.name="${functionName}"][async]`);
|
|
1230
|
+
}
|
|
1231
|
+
if ("objectName" in functionQuery) {
|
|
1232
|
+
const { objectName, propertyName } = functionQuery;
|
|
1233
|
+
const objectSelector = objectName === "this" ? "left.object.type=ThisExpression" : `left.object.name="${objectName}"`;
|
|
1234
|
+
queries.push(
|
|
1235
|
+
`AssignmentExpression[${objectSelector}][left.property.name="${propertyName}"] > [async]`
|
|
1236
|
+
);
|
|
1237
|
+
}
|
|
1238
|
+
return queries.join(", ");
|
|
1239
|
+
}
|
|
1240
|
+
};
|
|
1241
|
+
|
|
1242
|
+
// src/auto-instrumentations/orchestrion-js/matcher.ts
|
|
1243
|
+
var InstrumentationMatcher = class {
|
|
1244
|
+
configs = [];
|
|
1245
|
+
transformers = {};
|
|
1246
|
+
constructor(configs) {
|
|
1247
|
+
this.configs = configs;
|
|
1248
|
+
}
|
|
1249
|
+
/**
|
|
1250
|
+
* Returns a Transformer for the given module/file/version, or undefined if no
|
|
1251
|
+
* registered config matches.
|
|
1252
|
+
*/
|
|
1253
|
+
getTransformer(moduleName, version, filePath) {
|
|
1254
|
+
filePath = filePath.replace(/\\/g, "/");
|
|
1255
|
+
const id = `${moduleName}/${filePath}@${version}`;
|
|
1256
|
+
if (this.transformers[id]) {
|
|
1257
|
+
return this.transformers[id];
|
|
1258
|
+
}
|
|
1259
|
+
const configs = this.configs.filter(
|
|
1260
|
+
({ module: mod }) => mod.name === moduleName && mod.filePath === filePath && semifies(version, mod.versionRange)
|
|
1261
|
+
);
|
|
1262
|
+
if (configs.length === 0) {
|
|
1263
|
+
return void 0;
|
|
1264
|
+
}
|
|
1265
|
+
this.transformers[id] = new Transformer(
|
|
1266
|
+
moduleName,
|
|
1267
|
+
version,
|
|
1268
|
+
filePath,
|
|
1269
|
+
configs
|
|
1270
|
+
);
|
|
1271
|
+
return this.transformers[id];
|
|
1272
|
+
}
|
|
1273
|
+
free() {
|
|
1274
|
+
this.transformers = {};
|
|
1275
|
+
}
|
|
1276
|
+
};
|
|
1277
|
+
|
|
1278
|
+
// src/auto-instrumentations/orchestrion-js/index.ts
|
|
1279
|
+
function create(configs) {
|
|
1280
|
+
return new InstrumentationMatcher(configs);
|
|
1281
|
+
}
|
|
1282
|
+
|
|
1283
|
+
// src/auto-instrumentations/loader/get-package-version.ts
|
|
1284
|
+
import { readFileSync, realpathSync } from "fs";
|
|
1285
|
+
import { join } from "path";
|
|
1286
|
+
var packageVersions = /* @__PURE__ */ new Map();
|
|
1287
|
+
var packageNames = /* @__PURE__ */ new Map();
|
|
1288
|
+
function readPackageJson(baseDir) {
|
|
1289
|
+
try {
|
|
1290
|
+
const packageJsonPath = join(baseDir, "package.json");
|
|
1291
|
+
const jsonFile = readFileSync(packageJsonPath, "utf8");
|
|
1292
|
+
return JSON.parse(jsonFile);
|
|
1293
|
+
} catch {
|
|
1294
|
+
return void 0;
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
function resolvePackageBaseDir(baseDir) {
|
|
1298
|
+
try {
|
|
1299
|
+
return realpathSync(baseDir);
|
|
1300
|
+
} catch {
|
|
1301
|
+
return baseDir;
|
|
1302
|
+
}
|
|
1303
|
+
}
|
|
1304
|
+
function readPackageJsonWithFallback(baseDir) {
|
|
1305
|
+
const packageJson = readPackageJson(baseDir);
|
|
1306
|
+
if (packageJson) {
|
|
1307
|
+
return packageJson;
|
|
1308
|
+
}
|
|
1309
|
+
const resolvedBaseDir = resolvePackageBaseDir(baseDir);
|
|
1310
|
+
if (resolvedBaseDir === baseDir) {
|
|
1311
|
+
return void 0;
|
|
1312
|
+
}
|
|
1313
|
+
return readPackageJson(resolvedBaseDir);
|
|
1314
|
+
}
|
|
1315
|
+
function getPackageVersion(baseDir) {
|
|
1316
|
+
if (packageVersions.has(baseDir)) {
|
|
1317
|
+
return packageVersions.get(baseDir);
|
|
1318
|
+
}
|
|
1319
|
+
const packageJson = readPackageJsonWithFallback(baseDir);
|
|
1320
|
+
if (typeof packageJson?.version === "string") {
|
|
1321
|
+
packageVersions.set(baseDir, packageJson.version);
|
|
1322
|
+
return packageJson.version;
|
|
1323
|
+
}
|
|
1324
|
+
return process.version.slice(1);
|
|
1325
|
+
}
|
|
1326
|
+
function getPackageName(baseDir) {
|
|
1327
|
+
if (packageNames.has(baseDir)) {
|
|
1328
|
+
return packageNames.get(baseDir);
|
|
1329
|
+
}
|
|
1330
|
+
const packageJson = readPackageJsonWithFallback(baseDir);
|
|
1331
|
+
if (typeof packageJson?.name === "string") {
|
|
1332
|
+
packageNames.set(baseDir, packageJson.name);
|
|
1333
|
+
return packageJson.name;
|
|
1334
|
+
}
|
|
1335
|
+
return void 0;
|
|
1336
|
+
}
|
|
1337
|
+
|
|
1338
|
+
// src/auto-instrumentations/loader/openai-api-promise-patch.ts
|
|
1339
|
+
var OPENAI_API_PROMISE_PATCH = `
|
|
1340
|
+
;(function __btPatchAPIPromise() {
|
|
1341
|
+
if (typeof APIPromise === "undefined" || APIPromise.prototype.__btParsePatched) return;
|
|
1342
|
+
APIPromise.prototype.__btParsePatched = true;
|
|
1343
|
+
var _origThen = APIPromise.prototype.then;
|
|
1344
|
+
APIPromise.prototype.then = function __btThen(onfulfilled, onrejected) {
|
|
1345
|
+
if (!this.__btParseWrapped && Object.prototype.hasOwnProperty.call(this, "parseResponse")) {
|
|
1346
|
+
this.__btParseWrapped = true;
|
|
1347
|
+
var _origParse = this.parseResponse;
|
|
1348
|
+
var _cached;
|
|
1349
|
+
this.parseResponse = function() {
|
|
1350
|
+
if (!_cached) _cached = _origParse.apply(this, arguments);
|
|
1351
|
+
return _cached;
|
|
1352
|
+
};
|
|
1353
|
+
}
|
|
1354
|
+
return _origThen.call(this, onfulfilled, onrejected);
|
|
1355
|
+
};
|
|
1356
|
+
})();
|
|
1357
|
+
`;
|
|
1358
|
+
|
|
1359
|
+
// src/auto-instrumentations/loader/special-case-patches.ts
|
|
1360
|
+
function applySpecialCasePatch(input) {
|
|
1361
|
+
if (input.packageName === "openai" && input.modulePath.includes("api-promise")) {
|
|
1362
|
+
return input.source + OPENAI_API_PROMISE_PATCH;
|
|
1363
|
+
}
|
|
1364
|
+
if (input.browser) {
|
|
1365
|
+
return null;
|
|
1366
|
+
}
|
|
1367
|
+
const mastraTarget = classifyMastraTarget(
|
|
1368
|
+
input.packageName,
|
|
1369
|
+
input.modulePath
|
|
1370
|
+
);
|
|
1371
|
+
if (mastraTarget) {
|
|
1372
|
+
return patchMastraSource(input.source, mastraTarget, input.format);
|
|
1373
|
+
}
|
|
1374
|
+
return null;
|
|
1375
|
+
}
|
|
1376
|
+
function isSpecialCaseTarget(packageName, modulePath) {
|
|
1377
|
+
if (packageName === "openai" && modulePath.includes("api-promise")) {
|
|
1378
|
+
return true;
|
|
1379
|
+
}
|
|
1380
|
+
if (classifyMastraTarget(packageName, modulePath) !== null) {
|
|
1381
|
+
return true;
|
|
1382
|
+
}
|
|
1383
|
+
return false;
|
|
1384
|
+
}
|
|
1385
|
+
|
|
1386
|
+
export {
|
|
1387
|
+
GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION,
|
|
1388
|
+
setGlobalHookErrorReporter,
|
|
1389
|
+
newGlobalTracingChannel,
|
|
1390
|
+
installMastraExporterFactory,
|
|
1391
|
+
create,
|
|
1392
|
+
getPackageVersion,
|
|
1393
|
+
getPackageName,
|
|
1394
|
+
applySpecialCasePatch,
|
|
1395
|
+
isSpecialCaseTarget
|
|
1396
|
+
};
|