braintrust 3.20.0 → 3.22.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 +20 -0
- package/README.md +6 -0
- package/dev/dist/index.d.mts +1338 -199
- package/dev/dist/index.d.ts +1338 -199
- package/dev/dist/index.js +2904 -997
- package/dev/dist/index.mjs +2418 -511
- package/dist/apply-auto-instrumentation.js +781 -199
- package/dist/apply-auto-instrumentation.mjs +588 -6
- package/dist/auto-instrumentations/bundler/esbuild.cjs +594 -2
- package/dist/auto-instrumentations/bundler/esbuild.d.mts +2 -2
- package/dist/auto-instrumentations/bundler/esbuild.d.ts +2 -2
- package/dist/auto-instrumentations/bundler/esbuild.mjs +3 -3
- package/dist/auto-instrumentations/bundler/next.cjs +594 -2
- package/dist/auto-instrumentations/bundler/next.mjs +4 -4
- package/dist/auto-instrumentations/bundler/rollup.cjs +594 -2
- package/dist/auto-instrumentations/bundler/rollup.d.mts +2 -2
- package/dist/auto-instrumentations/bundler/rollup.d.ts +2 -2
- package/dist/auto-instrumentations/bundler/rollup.mjs +3 -3
- package/dist/auto-instrumentations/bundler/vite.cjs +594 -2
- package/dist/auto-instrumentations/bundler/vite.d.mts +2 -2
- package/dist/auto-instrumentations/bundler/vite.d.ts +2 -2
- package/dist/auto-instrumentations/bundler/vite.mjs +3 -3
- package/dist/auto-instrumentations/bundler/webpack-loader.cjs +593 -9
- package/dist/auto-instrumentations/bundler/webpack-loader.d.ts +68 -1
- package/dist/auto-instrumentations/bundler/webpack.cjs +594 -2
- package/dist/auto-instrumentations/bundler/webpack.d.mts +2 -2
- package/dist/auto-instrumentations/bundler/webpack.d.ts +2 -2
- package/dist/auto-instrumentations/bundler/webpack.mjs +4 -4
- package/dist/auto-instrumentations/{chunk-W4E36GIW.mjs → chunk-BRQX23KL.mjs} +16 -0
- package/dist/auto-instrumentations/chunk-M6DLIJ2Z.mjs +784 -0
- package/dist/auto-instrumentations/{chunk-K74TZGGM.mjs → chunk-T6J4C7LX.mjs} +1 -1
- package/dist/auto-instrumentations/{chunk-S4374IP6.mjs → chunk-TKRPRPGD.mjs} +4 -6
- package/dist/auto-instrumentations/chunk-VT6DDNKM.mjs +856 -0
- package/dist/auto-instrumentations/hook.mjs +824 -56
- package/dist/auto-instrumentations/index.cjs +16 -0
- package/dist/auto-instrumentations/index.d.mts +2 -3
- package/dist/auto-instrumentations/index.d.ts +2 -3
- package/dist/auto-instrumentations/index.mjs +1 -1
- package/dist/auto-instrumentations/loader/cjs-patch.cjs +578 -4
- package/dist/auto-instrumentations/loader/cjs-patch.d.mts +1 -1
- package/dist/auto-instrumentations/loader/cjs-patch.d.ts +1 -1
- package/dist/auto-instrumentations/loader/cjs-patch.mjs +3 -7
- package/dist/auto-instrumentations/loader/esm-hook.mjs +2 -6
- package/dist/auto-instrumentations/{plugin-D7nDswtC.d.mts → plugin-D0KHwSJv.d.mts} +1 -1
- package/dist/auto-instrumentations/{plugin-D7nDswtC.d.ts → plugin-G6fgxk1b.d.ts} +1 -1
- package/dist/auto-instrumentations/types-RNPaKi9o.d.mts +70 -0
- package/dist/auto-instrumentations/types-RNPaKi9o.d.ts +70 -0
- package/dist/browser.d.mts +4411 -719
- package/dist/browser.d.ts +4411 -719
- package/dist/browser.js +4274 -480
- package/dist/browser.mjs +4274 -480
- package/dist/{chunk-LPC4W2WX.js → chunk-BFGIH2ZJ.js} +4 -0
- package/dist/{chunk-Y7W7WP6H.mjs → chunk-KMGUTPB7.mjs} +4 -0
- package/dist/{chunk-POCCIJAL.js → chunk-MWVVR5LR.js} +3278 -1334
- package/dist/{chunk-F73OMF66.mjs → chunk-ZG2O3XVF.mjs} +2542 -598
- package/dist/cli.js +2338 -419
- package/dist/edge-light.d.mts +1 -1
- package/dist/edge-light.d.ts +1 -1
- package/dist/edge-light.js +4274 -480
- package/dist/edge-light.mjs +4274 -480
- package/dist/index.d.mts +4926 -1234
- package/dist/index.d.ts +4926 -1234
- package/dist/index.js +2303 -435
- package/dist/index.mjs +1914 -46
- package/dist/instrumentation/index.d.mts +1026 -20
- package/dist/instrumentation/index.d.ts +1026 -20
- package/dist/instrumentation/index.js +3864 -483
- package/dist/instrumentation/index.mjs +3862 -483
- 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 +4274 -480
- package/dist/workerd.mjs +4274 -480
- package/licenses/import-in-the-middle/LICENSE +201 -0
- package/licenses/import-in-the-middle/LICENSE-3rdparty.csv +4 -0
- package/licenses/import-in-the-middle/NOTICE +34 -0
- package/licenses/orchestrion-js/LICENSE +202 -0
- package/licenses/orchestrion-js/LICENSE-3rdparty.csv +8 -0
- package/licenses/orchestrion-js/NOTICE +3 -0
- package/licenses/require-in-the-middle/LICENSE +23 -0
- package/package.json +16 -4
- package/util/dist/index.d.mts +1 -0
- package/util/dist/index.d.ts +1 -0
- package/util/dist/index.js +4 -4
- package/util/dist/index.mjs +2 -2
- package/dist/auto-instrumentations/chunk-J57YF7WS.mjs +0 -208
- package/dist/auto-instrumentations/chunk-QFMACSOL.mjs +0 -280
|
@@ -22,8 +22,582 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
22
22
|
mod
|
|
23
23
|
));
|
|
24
24
|
|
|
25
|
+
// src/auto-instrumentations/orchestrion-js/matcher.ts
|
|
26
|
+
var import_semifies = __toESM(require("semifies"));
|
|
27
|
+
|
|
28
|
+
// src/auto-instrumentations/orchestrion-js/transformer.ts
|
|
29
|
+
var import_esquery2 = __toESM(require("esquery"));
|
|
30
|
+
var import_astring = require("astring");
|
|
31
|
+
var import_meriyah2 = require("meriyah");
|
|
32
|
+
var import_source_map = require("source-map");
|
|
33
|
+
|
|
34
|
+
// src/auto-instrumentations/orchestrion-js/transforms.ts
|
|
35
|
+
var import_esquery = __toESM(require("esquery"));
|
|
36
|
+
var import_meriyah = require("meriyah");
|
|
37
|
+
var tracingChannelPredicate = (node) => node.declarations?.[0]?.id?.properties?.[0]?.value?.name === "tr_ch_apm_tracingChannel";
|
|
38
|
+
var CHANNEL_REGEX = /[^\w]/g;
|
|
39
|
+
function formatChannelVariable(channelName) {
|
|
40
|
+
return `tr_ch_apm$${channelName.replace(CHANNEL_REGEX, "_")}`;
|
|
41
|
+
}
|
|
42
|
+
var transforms = {
|
|
43
|
+
tracingChannelImport({ dcModule, moduleType }, node) {
|
|
44
|
+
if (node.body.some(tracingChannelPredicate)) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const options = { module: moduleType === "esm" };
|
|
48
|
+
const index = node.body.findIndex(
|
|
49
|
+
(child) => child.directive === "use strict"
|
|
50
|
+
);
|
|
51
|
+
const dc = moduleType === "esm" ? `import tr_ch_apm_dc from "${dcModule}"` : `const tr_ch_apm_dc = ${"require"}("${dcModule}")`;
|
|
52
|
+
const tracingChannel = "const { tracingChannel: tr_ch_apm_tracingChannel } = tr_ch_apm_dc";
|
|
53
|
+
const hasSubscribers = `const tr_ch_apm_hasSubscribers = ch => ch.start.hasSubscribers
|
|
54
|
+
|| ch.end.hasSubscribers
|
|
55
|
+
|| ch.asyncStart.hasSubscribers
|
|
56
|
+
|| ch.asyncEnd.hasSubscribers
|
|
57
|
+
|| ch.error.hasSubscribers`;
|
|
58
|
+
node.body.splice(
|
|
59
|
+
index + 1,
|
|
60
|
+
0,
|
|
61
|
+
(0, import_meriyah.parse)(dc, options).body[0],
|
|
62
|
+
(0, import_meriyah.parse)(tracingChannel, options).body[0],
|
|
63
|
+
(0, import_meriyah.parse)(hasSubscribers, options).body[0]
|
|
64
|
+
);
|
|
65
|
+
},
|
|
66
|
+
tracingChannelDeclaration(state, node) {
|
|
67
|
+
const {
|
|
68
|
+
channelName,
|
|
69
|
+
module: { name }
|
|
70
|
+
} = state;
|
|
71
|
+
const channelVariable = formatChannelVariable(channelName);
|
|
72
|
+
if (node.body.some(
|
|
73
|
+
(child) => child.declarations?.[0]?.id?.name === channelVariable
|
|
74
|
+
)) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
transforms.tracingChannelImport(state, node, null, []);
|
|
78
|
+
const index = node.body.findIndex(tracingChannelPredicate);
|
|
79
|
+
const code = `
|
|
80
|
+
const ${channelVariable} = tr_ch_apm_tracingChannel("orchestrion:${name}:${channelName}")
|
|
81
|
+
`;
|
|
82
|
+
node.body.splice(index + 1, 0, (0, import_meriyah.parse)(code).body[0]);
|
|
83
|
+
},
|
|
84
|
+
traceCallback: traceAny,
|
|
85
|
+
tracePromise: traceAny,
|
|
86
|
+
traceSync: traceAny
|
|
87
|
+
};
|
|
88
|
+
function traceAny(state, node, _parent, ancestry) {
|
|
89
|
+
const program = ancestry[ancestry.length - 1];
|
|
90
|
+
if (node.type === "ClassDeclaration" || node.type === "ClassExpression") {
|
|
91
|
+
traceInstanceMethod(state, node, program);
|
|
92
|
+
} else {
|
|
93
|
+
traceFunction(state, node, program);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
function traceFunction(state, node, program) {
|
|
97
|
+
transforms.tracingChannelDeclaration(state, program, null, []);
|
|
98
|
+
const { functionQuery } = state;
|
|
99
|
+
const methodName = "methodName" in functionQuery ? functionQuery.methodName : void 0;
|
|
100
|
+
const privateMethodName = "privateMethodName" in functionQuery ? functionQuery.privateMethodName : void 0;
|
|
101
|
+
const functionName = "functionName" in functionQuery ? functionQuery.functionName : void 0;
|
|
102
|
+
const isConstructor = methodName === "constructor" || !methodName && !privateMethodName && !functionName;
|
|
103
|
+
const type = isConstructor ? "ArrowFunctionExpression" : "FunctionExpression";
|
|
104
|
+
node.body = wrap(
|
|
105
|
+
state,
|
|
106
|
+
{
|
|
107
|
+
type,
|
|
108
|
+
params: node.params,
|
|
109
|
+
body: node.body,
|
|
110
|
+
async: node.async,
|
|
111
|
+
expression: false,
|
|
112
|
+
generator: node.generator
|
|
113
|
+
},
|
|
114
|
+
program
|
|
115
|
+
);
|
|
116
|
+
node.generator = false;
|
|
117
|
+
node.async = false;
|
|
118
|
+
wrapSuper(node);
|
|
119
|
+
}
|
|
120
|
+
function traceInstanceMethod(state, node, program) {
|
|
121
|
+
const { functionQuery, operator } = state;
|
|
122
|
+
const { methodName } = functionQuery;
|
|
123
|
+
if (!methodName) {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
const classBody = node.body;
|
|
127
|
+
if (classBody.body.some(({ key }) => key.name === methodName)) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
let ctor = classBody.body.find(({ kind }) => kind === "constructor");
|
|
131
|
+
transforms.tracingChannelDeclaration(state, program, null, []);
|
|
132
|
+
if (!ctor) {
|
|
133
|
+
ctor = (0, import_meriyah.parse)(
|
|
134
|
+
node.superClass ? "class A extends Object { constructor (...args) { super(...args) } }" : "class A { constructor () {} }"
|
|
135
|
+
).body[0].body.body[0];
|
|
136
|
+
classBody.body.unshift(ctor);
|
|
137
|
+
}
|
|
138
|
+
const ctorBody = (0, import_meriyah.parse)(`
|
|
139
|
+
const __apm$${methodName} = this["${methodName}"]
|
|
140
|
+
this["${methodName}"] = function () {}
|
|
141
|
+
`).body;
|
|
142
|
+
const fn = ctorBody[1].expression.right;
|
|
143
|
+
fn.async = operator === "tracePromise";
|
|
144
|
+
fn.body = wrap(
|
|
145
|
+
state,
|
|
146
|
+
{ type: "Identifier", name: `__apm$${methodName}` },
|
|
147
|
+
program
|
|
148
|
+
);
|
|
149
|
+
wrapSuper(fn);
|
|
150
|
+
ctor.value.body.body.push(...ctorBody);
|
|
151
|
+
}
|
|
152
|
+
function wrap(state, node, program) {
|
|
153
|
+
const { operator, moduleVersion } = state;
|
|
154
|
+
const wrapper = operator === "traceCallback" ? wrapCallback(state) : operator === "tracePromise" ? wrapPromise(state) : wrapSync(state);
|
|
155
|
+
const block = wrapper.body[0].body;
|
|
156
|
+
const common = (0, import_meriyah.parse)(
|
|
157
|
+
node.type === "ArrowFunctionExpression" ? `
|
|
158
|
+
const __apm$ctx = {
|
|
159
|
+
arguments,
|
|
160
|
+
moduleVersion: ${JSON.stringify(moduleVersion)}
|
|
161
|
+
};
|
|
162
|
+
const __apm$traced = () => {
|
|
163
|
+
const __apm$wrapped = () => {};
|
|
164
|
+
return __apm$wrapped(...arguments);
|
|
165
|
+
};
|
|
166
|
+
` : `
|
|
167
|
+
const __apm$ctx = {
|
|
168
|
+
arguments,
|
|
169
|
+
self: this,
|
|
170
|
+
moduleVersion: ${JSON.stringify(moduleVersion)}
|
|
171
|
+
};
|
|
172
|
+
const __apm$traced = () => {
|
|
173
|
+
const __apm$wrapped = () => {};
|
|
174
|
+
return __apm$wrapped.apply(this, arguments);
|
|
175
|
+
};
|
|
176
|
+
`
|
|
177
|
+
).body;
|
|
178
|
+
block.body.unshift(...common);
|
|
179
|
+
import_esquery.default.query(block, "[id.name=__apm$wrapped]")[0].init = node;
|
|
180
|
+
return block;
|
|
181
|
+
}
|
|
182
|
+
function wrapSuper(node) {
|
|
183
|
+
const members = /* @__PURE__ */ new Set();
|
|
184
|
+
import_esquery.default.traverse(
|
|
185
|
+
node.body,
|
|
186
|
+
import_esquery.default.parse("[object.type=Super]"),
|
|
187
|
+
(node2, parent) => {
|
|
188
|
+
const { name } = node2.property;
|
|
189
|
+
let child;
|
|
190
|
+
if (parent.callee) {
|
|
191
|
+
const { expression } = (0, import_meriyah.parse)(`__apm$super['${name}'].call(this)`).body[0];
|
|
192
|
+
parent.callee = child = expression.callee;
|
|
193
|
+
parent.arguments.unshift(...expression.arguments);
|
|
194
|
+
} else {
|
|
195
|
+
parent.expression = child = (0, import_meriyah.parse)(`__apm$super['${name}']`).body[0];
|
|
196
|
+
}
|
|
197
|
+
child.computed = parent.callee.computed;
|
|
198
|
+
child.optional = parent.callee.optional;
|
|
199
|
+
members.add(name);
|
|
200
|
+
}
|
|
201
|
+
);
|
|
202
|
+
for (const name of members) {
|
|
203
|
+
const member = (0, import_meriyah.parse)(`
|
|
204
|
+
class Wrapper {
|
|
205
|
+
wrapper () {
|
|
206
|
+
__apm$super['${name}'] = super['${name}']
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
`).body[0].body.body[0].value.body.body[0];
|
|
210
|
+
node.body.body.unshift(member);
|
|
211
|
+
}
|
|
212
|
+
if (members.size > 0) {
|
|
213
|
+
node.body.body.unshift((0, import_meriyah.parse)("const __apm$super = {}").body[0]);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
function wrapCallback(state) {
|
|
217
|
+
const {
|
|
218
|
+
channelName,
|
|
219
|
+
functionQuery: { callbackIndex = -1 }
|
|
220
|
+
} = state;
|
|
221
|
+
const channelVariable = formatChannelVariable(channelName);
|
|
222
|
+
return (0, import_meriyah.parse)(`
|
|
223
|
+
function wrapper () {
|
|
224
|
+
const __apm$cb = Array.prototype.at.call(arguments, ${callbackIndex});
|
|
225
|
+
|
|
226
|
+
if (!${channelVariable}.start.hasSubscribers) return __apm$traced();
|
|
227
|
+
|
|
228
|
+
function __apm$wrappedCb(err, res) {
|
|
229
|
+
if (err) {
|
|
230
|
+
__apm$ctx.error = err;
|
|
231
|
+
${channelVariable}.error.publish(__apm$ctx);
|
|
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
|
+
});
|
|
264
|
+
}
|
|
265
|
+
`);
|
|
266
|
+
}
|
|
267
|
+
function wrapPromise(state) {
|
|
268
|
+
const { channelName } = state;
|
|
269
|
+
const channelVariable = formatChannelVariable(channelName);
|
|
270
|
+
return (0, import_meriyah.parse)(`
|
|
271
|
+
function wrapper () {
|
|
272
|
+
if (!tr_ch_apm_hasSubscribers(${channelVariable})) return __apm$traced();
|
|
273
|
+
|
|
274
|
+
return ${channelVariable}.start.runStores(__apm$ctx, () => {
|
|
275
|
+
try {
|
|
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
|
+
});
|
|
327
|
+
}
|
|
328
|
+
`);
|
|
329
|
+
}
|
|
330
|
+
function wrapSync(state) {
|
|
331
|
+
const { channelName } = state;
|
|
332
|
+
const channelVariable = formatChannelVariable(channelName);
|
|
333
|
+
return (0, import_meriyah.parse)(`
|
|
334
|
+
function wrapper () {
|
|
335
|
+
if (!tr_ch_apm_hasSubscribers(${channelVariable})) return __apm$traced();
|
|
336
|
+
|
|
337
|
+
return ${channelVariable}.start.runStores(__apm$ctx, () => {
|
|
338
|
+
try {
|
|
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
|
+
});
|
|
351
|
+
}
|
|
352
|
+
`);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// src/auto-instrumentations/orchestrion-js/transformer.ts
|
|
356
|
+
var Transformer = class {
|
|
357
|
+
moduleName;
|
|
358
|
+
version;
|
|
359
|
+
filePath;
|
|
360
|
+
configs = [];
|
|
361
|
+
dcModule;
|
|
362
|
+
constructor(moduleName, version, filePath, configs, dcModule) {
|
|
363
|
+
this.moduleName = moduleName;
|
|
364
|
+
this.version = version;
|
|
365
|
+
this.filePath = filePath;
|
|
366
|
+
this.configs = configs;
|
|
367
|
+
this.dcModule = dcModule;
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* Instruments `code` by injecting diagnostics_channel tracing around the
|
|
371
|
+
* target functions defined by this transformer's configs.
|
|
372
|
+
*/
|
|
373
|
+
transform(code, moduleType) {
|
|
374
|
+
if (Buffer.isBuffer(code)) {
|
|
375
|
+
code = code.toString();
|
|
376
|
+
}
|
|
377
|
+
if (!code) {
|
|
378
|
+
return { code };
|
|
379
|
+
}
|
|
380
|
+
let ast;
|
|
381
|
+
let aliases = {};
|
|
382
|
+
let injectionCount = 0;
|
|
383
|
+
for (const config of this.configs) {
|
|
384
|
+
const { astQuery, functionQuery } = config;
|
|
385
|
+
if (!ast) {
|
|
386
|
+
const options = {
|
|
387
|
+
loc: true,
|
|
388
|
+
ranges: true,
|
|
389
|
+
raw: true,
|
|
390
|
+
module: moduleType === "esm"
|
|
391
|
+
};
|
|
392
|
+
try {
|
|
393
|
+
ast = (0, import_meriyah2.parse)(code, options);
|
|
394
|
+
} catch {
|
|
395
|
+
ast = (0, import_meriyah2.parse)(code, { ...options, module: !options.module });
|
|
396
|
+
}
|
|
397
|
+
if (moduleType === "esm") {
|
|
398
|
+
aliases = this.collectExportAliases(ast);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
const resolvedFunctionQuery = this.resolveExportAlias(
|
|
402
|
+
functionQuery,
|
|
403
|
+
aliases
|
|
404
|
+
);
|
|
405
|
+
const query = astQuery || this.fromFunctionQuery(resolvedFunctionQuery);
|
|
406
|
+
const state = {
|
|
407
|
+
...config,
|
|
408
|
+
dcModule: this.dcModule,
|
|
409
|
+
moduleType,
|
|
410
|
+
moduleVersion: this.version,
|
|
411
|
+
functionQuery: resolvedFunctionQuery,
|
|
412
|
+
operator: this.getOperator(resolvedFunctionQuery.kind)
|
|
413
|
+
};
|
|
414
|
+
import_esquery2.default.traverse(ast, import_esquery2.default.parse(query), (...args) => {
|
|
415
|
+
injectionCount++;
|
|
416
|
+
this.visit(state, ...args);
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
if (injectionCount === 0 && this.configs.length > 0) {
|
|
420
|
+
const names = this.configs.map(({ astQuery, functionQuery }) => {
|
|
421
|
+
const resolvedQuery = this.resolveExportAlias(functionQuery, aliases);
|
|
422
|
+
const queryName = (q) => q.methodName || q.privateMethodName || q.functionName || q.propertyName || astQuery || "unknown";
|
|
423
|
+
const originalName = queryName(functionQuery);
|
|
424
|
+
const originalAlias = functionQuery.className || functionQuery.functionName;
|
|
425
|
+
const resolvedAlias = resolvedQuery.className || resolvedQuery.functionName;
|
|
426
|
+
if (originalAlias && originalAlias !== resolvedAlias) {
|
|
427
|
+
return `${originalAlias} (local name: ${resolvedAlias})`;
|
|
428
|
+
}
|
|
429
|
+
return originalName;
|
|
430
|
+
});
|
|
431
|
+
throw new Error(
|
|
432
|
+
`Failed to find injection points for: ${JSON.stringify(names)}`
|
|
433
|
+
);
|
|
434
|
+
}
|
|
435
|
+
if (ast) {
|
|
436
|
+
const file = `${this.moduleName}/${this.filePath}`;
|
|
437
|
+
const sourceMap = new import_source_map.SourceMapGenerator({ file });
|
|
438
|
+
const transformedCode = (0, import_astring.generate)(ast, { sourceMap });
|
|
439
|
+
const map = sourceMap.toString();
|
|
440
|
+
return { code: transformedCode, map };
|
|
441
|
+
}
|
|
442
|
+
return { code };
|
|
443
|
+
}
|
|
444
|
+
free() {
|
|
445
|
+
}
|
|
446
|
+
visit(state, ...args) {
|
|
447
|
+
const transform = transforms[state.operator];
|
|
448
|
+
const { index = 0 } = state.functionQuery;
|
|
449
|
+
const [node] = args;
|
|
450
|
+
const type = node.init?.type || node.type;
|
|
451
|
+
if (type !== "ClassDeclaration" && type !== "ClassExpression") {
|
|
452
|
+
if (node.type === "VariableDeclarator") {
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
state.functionIndex = state.functionIndex === void 0 ? 0 : state.functionIndex + 1;
|
|
456
|
+
if (index !== null && index !== state.functionIndex) {
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
transform(state, ...args);
|
|
461
|
+
}
|
|
462
|
+
getOperator(kind) {
|
|
463
|
+
switch (kind) {
|
|
464
|
+
case "Async":
|
|
465
|
+
return "tracePromise";
|
|
466
|
+
case "Callback":
|
|
467
|
+
return "traceCallback";
|
|
468
|
+
case "Sync":
|
|
469
|
+
return "traceSync";
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
collectExportAliases(ast) {
|
|
473
|
+
const aliases = {};
|
|
474
|
+
for (const node of ast.body) {
|
|
475
|
+
if (node.type === "ExportNamedDeclaration" && !node.source) {
|
|
476
|
+
for (const spec of node.specifiers) {
|
|
477
|
+
if (spec.exported && spec.local) {
|
|
478
|
+
const exportedName = spec.exported.name ?? spec.exported.value;
|
|
479
|
+
const localName = spec.local.name ?? spec.local.value;
|
|
480
|
+
if (exportedName && localName) {
|
|
481
|
+
aliases[exportedName] = localName;
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
return aliases;
|
|
488
|
+
}
|
|
489
|
+
resolveExportAlias(functionQuery, aliases) {
|
|
490
|
+
if (!("isExportAlias" in functionQuery) || !functionQuery.isExportAlias) {
|
|
491
|
+
return functionQuery;
|
|
492
|
+
}
|
|
493
|
+
if ("className" in functionQuery && aliases[functionQuery.className]) {
|
|
494
|
+
return {
|
|
495
|
+
...functionQuery,
|
|
496
|
+
className: aliases[functionQuery.className]
|
|
497
|
+
};
|
|
498
|
+
}
|
|
499
|
+
if ("functionName" in functionQuery && aliases[functionQuery.functionName]) {
|
|
500
|
+
return {
|
|
501
|
+
...functionQuery,
|
|
502
|
+
functionName: aliases[functionQuery.functionName]
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
return functionQuery;
|
|
506
|
+
}
|
|
507
|
+
functionQueryLabel(functionQuery) {
|
|
508
|
+
if ("methodName" in functionQuery) {
|
|
509
|
+
return functionQuery.methodName;
|
|
510
|
+
}
|
|
511
|
+
if ("privateMethodName" in functionQuery) {
|
|
512
|
+
return functionQuery.privateMethodName;
|
|
513
|
+
}
|
|
514
|
+
if ("functionName" in functionQuery) {
|
|
515
|
+
return functionQuery.functionName;
|
|
516
|
+
}
|
|
517
|
+
if ("propertyName" in functionQuery) {
|
|
518
|
+
return functionQuery.propertyName;
|
|
519
|
+
}
|
|
520
|
+
return "unknown";
|
|
521
|
+
}
|
|
522
|
+
fromFunctionQuery(functionQuery) {
|
|
523
|
+
const queries = [];
|
|
524
|
+
if ("className" in functionQuery) {
|
|
525
|
+
const { className } = functionQuery;
|
|
526
|
+
const methodName = this.functionQueryLabel(functionQuery);
|
|
527
|
+
const keyType = "privateMethodName" in functionQuery ? "PrivateIdentifier" : "Identifier";
|
|
528
|
+
queries.push(
|
|
529
|
+
`[id.name="${className}"]`,
|
|
530
|
+
`[id.name="${className}"] > ClassExpression`,
|
|
531
|
+
`[id.name="${className}"] > ClassBody > [key.name="${methodName}"][key.type=${keyType}] > [async]`,
|
|
532
|
+
`[id.name="${className}"] > ClassExpression > ClassBody > [key.name="${methodName}"][key.type=${keyType}] > [async]`
|
|
533
|
+
);
|
|
534
|
+
} else if ("methodName" in functionQuery) {
|
|
535
|
+
const { methodName } = functionQuery;
|
|
536
|
+
queries.push(
|
|
537
|
+
`ClassBody > [key.name="${methodName}"][key.type=Identifier] > [async]`,
|
|
538
|
+
`Property[key.name="${methodName}"][key.type=Identifier] > [async]`
|
|
539
|
+
);
|
|
540
|
+
}
|
|
541
|
+
if ("functionName" in functionQuery) {
|
|
542
|
+
const { functionName } = functionQuery;
|
|
543
|
+
queries.push(`FunctionDeclaration[id.name="${functionName}"][async]`);
|
|
544
|
+
}
|
|
545
|
+
if ("objectName" in functionQuery) {
|
|
546
|
+
const { objectName, propertyName } = functionQuery;
|
|
547
|
+
const objectSelector = objectName === "this" ? "left.object.type=ThisExpression" : `left.object.name="${objectName}"`;
|
|
548
|
+
queries.push(
|
|
549
|
+
`AssignmentExpression[${objectSelector}][left.property.name="${propertyName}"] > [async]`
|
|
550
|
+
);
|
|
551
|
+
}
|
|
552
|
+
return queries.join(", ");
|
|
553
|
+
}
|
|
554
|
+
};
|
|
555
|
+
|
|
556
|
+
// src/auto-instrumentations/orchestrion-js/matcher.ts
|
|
557
|
+
var InstrumentationMatcher = class {
|
|
558
|
+
configs = [];
|
|
559
|
+
dcModule;
|
|
560
|
+
transformers = {};
|
|
561
|
+
constructor(configs, dcModule) {
|
|
562
|
+
this.configs = configs;
|
|
563
|
+
this.dcModule = dcModule || "diagnostics_channel";
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* Returns a Transformer for the given module/file/version, or undefined if no
|
|
567
|
+
* registered config matches.
|
|
568
|
+
*/
|
|
569
|
+
getTransformer(moduleName, version, filePath) {
|
|
570
|
+
filePath = filePath.replace(/\\/g, "/");
|
|
571
|
+
const id = `${moduleName}/${filePath}@${version}`;
|
|
572
|
+
if (this.transformers[id]) {
|
|
573
|
+
return this.transformers[id];
|
|
574
|
+
}
|
|
575
|
+
const configs = this.configs.filter(
|
|
576
|
+
({ module: mod }) => mod.name === moduleName && mod.filePath === filePath && (0, import_semifies.default)(version, mod.versionRange)
|
|
577
|
+
);
|
|
578
|
+
if (configs.length === 0) {
|
|
579
|
+
return void 0;
|
|
580
|
+
}
|
|
581
|
+
this.transformers[id] = new Transformer(
|
|
582
|
+
moduleName,
|
|
583
|
+
version,
|
|
584
|
+
filePath,
|
|
585
|
+
configs,
|
|
586
|
+
this.dcModule
|
|
587
|
+
);
|
|
588
|
+
return this.transformers[id];
|
|
589
|
+
}
|
|
590
|
+
free() {
|
|
591
|
+
this.transformers = {};
|
|
592
|
+
}
|
|
593
|
+
};
|
|
594
|
+
|
|
595
|
+
// src/auto-instrumentations/orchestrion-js/index.ts
|
|
596
|
+
function create(configs, dcModule) {
|
|
597
|
+
return new InstrumentationMatcher(configs, dcModule);
|
|
598
|
+
}
|
|
599
|
+
|
|
25
600
|
// src/auto-instrumentations/bundler/webpack-loader.ts
|
|
26
|
-
var import_code_transformer = require("@apm-js-collab/code-transformer");
|
|
27
601
|
var import_path = require("path");
|
|
28
602
|
var import_fs = require("fs");
|
|
29
603
|
var import_module_details_from_path = __toESM(require("module-details-from-path"));
|
|
@@ -312,6 +886,10 @@ var aiSDKChannels = defineChannels("ai", {
|
|
|
312
886
|
channelName: "ToolLoopAgent.stream",
|
|
313
887
|
kind: "async"
|
|
314
888
|
}),
|
|
889
|
+
workflowAgentStream: channel({
|
|
890
|
+
channelName: "WorkflowAgent.stream",
|
|
891
|
+
kind: "async"
|
|
892
|
+
}),
|
|
315
893
|
v7CreateTelemetryDispatcher: channel({
|
|
316
894
|
channelName: "createTelemetryDispatcher",
|
|
317
895
|
kind: "sync-stream"
|
|
@@ -510,6 +1088,18 @@ var aiSDKConfigs = [
|
|
|
510
1088
|
kind: "Sync"
|
|
511
1089
|
}
|
|
512
1090
|
},
|
|
1091
|
+
{
|
|
1092
|
+
channelName: aiSDKChannels.v7CreateTelemetryDispatcher.channelName,
|
|
1093
|
+
module: {
|
|
1094
|
+
name: "ai",
|
|
1095
|
+
versionRange: ">=7.0.0-0 <8.0.0",
|
|
1096
|
+
filePath: "dist/internal/index.js"
|
|
1097
|
+
},
|
|
1098
|
+
functionQuery: {
|
|
1099
|
+
functionName: "createTelemetryDispatcher",
|
|
1100
|
+
kind: "Sync"
|
|
1101
|
+
}
|
|
1102
|
+
},
|
|
513
1103
|
// streamObject - async function (v3 only, before the sync refactor in v4)
|
|
514
1104
|
{
|
|
515
1105
|
channelName: aiSDKChannels.streamObject.channelName,
|
|
@@ -3269,20 +3859,16 @@ function getMatcher(options) {
|
|
|
3269
3859
|
if (matcherCache.has(configHash)) {
|
|
3270
3860
|
return matcherCache.get(configHash);
|
|
3271
3861
|
}
|
|
3272
|
-
for (const
|
|
3862
|
+
for (const hash of matcherCache.keys()) {
|
|
3273
3863
|
if (hash !== configHash) {
|
|
3274
|
-
matcher2.free();
|
|
3275
3864
|
matcherCache.delete(hash);
|
|
3276
3865
|
}
|
|
3277
3866
|
}
|
|
3278
|
-
const matcher =
|
|
3867
|
+
const matcher = create(allInstrumentations, dcModule ?? null);
|
|
3279
3868
|
matcherCache.set(configHash, matcher);
|
|
3280
3869
|
return matcher;
|
|
3281
3870
|
}
|
|
3282
3871
|
process.on("exit", () => {
|
|
3283
|
-
for (const matcher of matcherCache.values()) {
|
|
3284
|
-
matcher.free();
|
|
3285
|
-
}
|
|
3286
3872
|
matcherCache.clear();
|
|
3287
3873
|
});
|
|
3288
3874
|
function codeTransformerLoader(code, inputSourceMap) {
|
|
@@ -3327,8 +3913,6 @@ function codeTransformerLoader(code, inputSourceMap) {
|
|
|
3327
3913
|
error
|
|
3328
3914
|
);
|
|
3329
3915
|
callback(null, code, inputSourceMap);
|
|
3330
|
-
} finally {
|
|
3331
|
-
transformer.free();
|
|
3332
3916
|
}
|
|
3333
3917
|
}
|
|
3334
3918
|
module.exports = codeTransformerLoader;
|