braintrust 3.19.0 → 3.21.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 +2 -0
- package/dev/dist/index.d.mts +309 -199
- package/dev/dist/index.d.ts +309 -199
- package/dev/dist/index.js +2833 -987
- package/dev/dist/index.mjs +2294 -448
- 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 +749 -45
- 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 +1369 -848
- package/dist/browser.d.ts +1369 -848
- package/dist/browser.js +2735 -473
- package/dist/browser.mjs +2735 -473
- package/dist/{chunk-LPC4W2WX.js → chunk-BFGIH2ZJ.js} +4 -0
- package/dist/{chunk-37RLJF2U.js → chunk-FY7DAKA5.js} +3149 -1273
- package/dist/{chunk-Y7W7WP6H.mjs → chunk-KMGUTPB7.mjs} +4 -0
- package/dist/{chunk-VEQ2PCMC.mjs → chunk-O2P765XK.mjs} +2399 -523
- package/dist/cli.js +2267 -412
- package/dist/edge-light.d.mts +1 -1
- package/dist/edge-light.d.ts +1 -1
- package/dist/edge-light.js +2735 -473
- package/dist/edge-light.mjs +2735 -473
- package/dist/index.d.mts +1848 -1327
- package/dist/index.d.ts +1848 -1327
- package/dist/index.js +754 -352
- package/dist/index.mjs +448 -46
- package/dist/instrumentation/index.d.mts +119 -21
- package/dist/instrumentation/index.d.ts +119 -21
- package/dist/instrumentation/index.js +2345 -512
- package/dist/instrumentation/index.mjs +2344 -512
- 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 +2735 -473
- package/dist/workerd.mjs +2735 -473
- 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 +13 -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
|
@@ -0,0 +1,856 @@
|
|
|
1
|
+
// src/auto-instrumentations/loader/mastra-observability-patch.ts
|
|
2
|
+
var MASTRA_EXPORTER_FACTORY_GLOBAL = "__braintrustMastraExporterFactory";
|
|
3
|
+
function installMastraExporterFactory(factory) {
|
|
4
|
+
const globals = globalThis;
|
|
5
|
+
globals[MASTRA_EXPORTER_FACTORY_GLOBAL] ??= factory;
|
|
6
|
+
}
|
|
7
|
+
var MASTRA_CORE_PACKAGE = "@mastra/core";
|
|
8
|
+
var MASTRA_OBSERVABILITY_PACKAGE = "@mastra/observability";
|
|
9
|
+
var MASTRA_CORE_ENTRY_PATHS = /* @__PURE__ */ new Set([
|
|
10
|
+
"dist/index.js",
|
|
11
|
+
"dist/index.cjs",
|
|
12
|
+
"dist/mastra/index.js",
|
|
13
|
+
"dist/mastra/index.cjs"
|
|
14
|
+
]);
|
|
15
|
+
var MASTRA_OBSERVABILITY_ENTRY_PATHS = /* @__PURE__ */ new Set([
|
|
16
|
+
"dist/index.js",
|
|
17
|
+
"dist/index.cjs"
|
|
18
|
+
]);
|
|
19
|
+
function classifyMastraTarget(packageName, modulePath) {
|
|
20
|
+
if (packageName === MASTRA_CORE_PACKAGE && MASTRA_CORE_ENTRY_PATHS.has(modulePath)) {
|
|
21
|
+
return "core";
|
|
22
|
+
}
|
|
23
|
+
if (packageName === MASTRA_OBSERVABILITY_PACKAGE && MASTRA_OBSERVABILITY_ENTRY_PATHS.has(modulePath)) {
|
|
24
|
+
return "observability";
|
|
25
|
+
}
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
function extractChunkPath(source) {
|
|
29
|
+
const esmMatch = source.match(
|
|
30
|
+
/export\s*\{\s*Mastra(?:\s+as\s+\w+)?\s*\}\s*from\s*['"]([^'"]+)['"]/
|
|
31
|
+
);
|
|
32
|
+
if (esmMatch) return esmMatch[1];
|
|
33
|
+
const cjsMatch = source.match(
|
|
34
|
+
/require\s*\(\s*['"]([^'"]+chunk-[^'"]+)['"]\s*\)/
|
|
35
|
+
);
|
|
36
|
+
if (cjsMatch) return cjsMatch[1];
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
var EXPORTER_FACTORY_KEY = JSON.stringify(MASTRA_EXPORTER_FACTORY_GLOBAL);
|
|
40
|
+
var MASTRA_PROXY_HANDLER_BODY = `
|
|
41
|
+
{
|
|
42
|
+
construct(target, args, newTarget) {
|
|
43
|
+
var firstArg = args[0];
|
|
44
|
+
if (
|
|
45
|
+
(!firstArg || typeof firstArg !== "object" || !firstArg.observability) &&
|
|
46
|
+
__braintrustObservabilityClass
|
|
47
|
+
) {
|
|
48
|
+
try {
|
|
49
|
+
// serviceName is required by Mastra's Observability validator; pass
|
|
50
|
+
// something sensible by default. Users who want a different name
|
|
51
|
+
// should construct Observability themselves.
|
|
52
|
+
var observability = new __braintrustObservabilityClass({
|
|
53
|
+
configs: { default: { serviceName: "mastra" } },
|
|
54
|
+
});
|
|
55
|
+
args = args.slice();
|
|
56
|
+
args[0] = Object.assign({}, firstArg, { observability: observability });
|
|
57
|
+
} catch (e) {
|
|
58
|
+
// Fall through. Mastra will use its own NoOp; user code still works,
|
|
59
|
+
// just without auto-instrumentation.
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return Reflect.construct(target, args, newTarget);
|
|
63
|
+
},
|
|
64
|
+
}`;
|
|
65
|
+
function buildMastraEsmWrapper(chunkPath) {
|
|
66
|
+
const chunk = JSON.stringify(chunkPath);
|
|
67
|
+
return `import { Mastra as __braintrustOrigMastra } from ${chunk};
|
|
68
|
+
import { createRequire as __braintrustCreateRequire } from "node:module";
|
|
69
|
+
|
|
70
|
+
let __braintrustObservabilityClass = null;
|
|
71
|
+
try {
|
|
72
|
+
// Resolve @mastra/observability relative to this module (the Mastra entry),
|
|
73
|
+
// so it's looked up from the user's node_modules tree.
|
|
74
|
+
const __braintrustRequire = __braintrustCreateRequire(import.meta.url);
|
|
75
|
+
__braintrustObservabilityClass =
|
|
76
|
+
__braintrustRequire("@mastra/observability").Observability;
|
|
77
|
+
} catch (e) {
|
|
78
|
+
// @mastra/observability isn't installed; the construct trap will skip the
|
|
79
|
+
// auto-construct branch and Mastra falls back to its own NoOp.
|
|
80
|
+
}
|
|
81
|
+
const Mastra = new Proxy(__braintrustOrigMastra, ${MASTRA_PROXY_HANDLER_BODY});
|
|
82
|
+
export { Mastra };
|
|
83
|
+
`;
|
|
84
|
+
}
|
|
85
|
+
function buildMastraCjsWrapper(chunkPath) {
|
|
86
|
+
const chunk = JSON.stringify(chunkPath);
|
|
87
|
+
return `'use strict';
|
|
88
|
+
const __braintrustChunk = require(${chunk});
|
|
89
|
+
|
|
90
|
+
let __braintrustObservabilityClass = null;
|
|
91
|
+
try {
|
|
92
|
+
__braintrustObservabilityClass = require("@mastra/observability").Observability;
|
|
93
|
+
} catch (e) {
|
|
94
|
+
// @mastra/observability isn't installed; same fallback as the ESM wrapper.
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const __braintrustWrappedMastra = new Proxy(
|
|
98
|
+
__braintrustChunk.Mastra,
|
|
99
|
+
${MASTRA_PROXY_HANDLER_BODY},
|
|
100
|
+
);
|
|
101
|
+
Object.defineProperty(exports, "Mastra", {
|
|
102
|
+
enumerable: true,
|
|
103
|
+
configurable: true,
|
|
104
|
+
get: function () { return __braintrustWrappedMastra; }
|
|
105
|
+
});
|
|
106
|
+
`;
|
|
107
|
+
}
|
|
108
|
+
var OBSERVABILITY_APPEND_BODY = `
|
|
109
|
+
;(function __braintrustWrapObservability() {
|
|
110
|
+
// Top-level so we can both read and reassign the var binding the original
|
|
111
|
+
// entry declared.
|
|
112
|
+
if (typeof Observability === "undefined") return;
|
|
113
|
+
if (Observability.__braintrustWrapped) return;
|
|
114
|
+
function __braintrustEnsureExporter(rawConfig) {
|
|
115
|
+
try {
|
|
116
|
+
var factory = globalThis[${EXPORTER_FACTORY_KEY}];
|
|
117
|
+
if (typeof factory !== "function") return rawConfig;
|
|
118
|
+
var config = rawConfig && typeof rawConfig === "object" ? rawConfig : {};
|
|
119
|
+
var configsIn = config.configs && typeof config.configs === "object" ? config.configs : null;
|
|
120
|
+
var configsOut = {};
|
|
121
|
+
var hadEntries = false;
|
|
122
|
+
if (configsIn) {
|
|
123
|
+
for (var name in configsIn) {
|
|
124
|
+
if (!Object.prototype.hasOwnProperty.call(configsIn, name)) continue;
|
|
125
|
+
hadEntries = true;
|
|
126
|
+
var inst = configsIn[name] || {};
|
|
127
|
+
var existing = Array.isArray(inst.exporters) ? inst.exporters : [];
|
|
128
|
+
var hasOurs = existing.some(function (e) { return e && e.name === "braintrust"; });
|
|
129
|
+
configsOut[name] = Object.assign({}, inst, {
|
|
130
|
+
exporters: hasOurs ? existing : existing.concat([factory()]),
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
if (!hadEntries) {
|
|
135
|
+
configsOut.default = {
|
|
136
|
+
serviceName: "mastra",
|
|
137
|
+
exporters: [factory()],
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
return Object.assign({}, config, { configs: configsOut });
|
|
141
|
+
} catch (e) {
|
|
142
|
+
return rawConfig;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
var __OriginalObservability = Observability;
|
|
146
|
+
Observability = new Proxy(__OriginalObservability, {
|
|
147
|
+
construct: function (target, args, newTarget) {
|
|
148
|
+
var nextArgs = args.slice();
|
|
149
|
+
nextArgs[0] = __braintrustEnsureExporter(nextArgs[0]);
|
|
150
|
+
return Reflect.construct(target, nextArgs, newTarget);
|
|
151
|
+
},
|
|
152
|
+
});
|
|
153
|
+
Observability.__braintrustWrapped = true;
|
|
154
|
+
if (typeof exports !== "undefined" && exports && typeof exports === "object") {
|
|
155
|
+
try {
|
|
156
|
+
Object.defineProperty(exports, "Observability", {
|
|
157
|
+
enumerable: true,
|
|
158
|
+
configurable: true,
|
|
159
|
+
get: function () { return Observability; },
|
|
160
|
+
});
|
|
161
|
+
} catch (e) {}
|
|
162
|
+
}
|
|
163
|
+
})();
|
|
164
|
+
`;
|
|
165
|
+
function patchMastraSource(source, target, format) {
|
|
166
|
+
if (target === "core") {
|
|
167
|
+
const chunkPath = extractChunkPath(source);
|
|
168
|
+
if (!chunkPath) return source;
|
|
169
|
+
return format === "esm" ? buildMastraEsmWrapper(chunkPath) : buildMastraCjsWrapper(chunkPath);
|
|
170
|
+
}
|
|
171
|
+
return source + OBSERVABILITY_APPEND_BODY;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// src/auto-instrumentations/orchestrion-js/matcher.ts
|
|
175
|
+
import semifies from "semifies";
|
|
176
|
+
|
|
177
|
+
// src/auto-instrumentations/orchestrion-js/transformer.ts
|
|
178
|
+
import esquery2 from "esquery";
|
|
179
|
+
import { generate } from "astring";
|
|
180
|
+
import { parse as parse2 } from "meriyah";
|
|
181
|
+
import { SourceMapGenerator } from "source-map";
|
|
182
|
+
|
|
183
|
+
// src/auto-instrumentations/orchestrion-js/transforms.ts
|
|
184
|
+
import esquery from "esquery";
|
|
185
|
+
import { parse } from "meriyah";
|
|
186
|
+
var tracingChannelPredicate = (node) => node.declarations?.[0]?.id?.properties?.[0]?.value?.name === "tr_ch_apm_tracingChannel";
|
|
187
|
+
var CHANNEL_REGEX = /[^\w]/g;
|
|
188
|
+
function formatChannelVariable(channelName) {
|
|
189
|
+
return `tr_ch_apm$${channelName.replace(CHANNEL_REGEX, "_")}`;
|
|
190
|
+
}
|
|
191
|
+
var transforms = {
|
|
192
|
+
tracingChannelImport({ dcModule, moduleType }, node) {
|
|
193
|
+
if (node.body.some(tracingChannelPredicate)) {
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
const options = { module: moduleType === "esm" };
|
|
197
|
+
const index = node.body.findIndex(
|
|
198
|
+
(child) => child.directive === "use strict"
|
|
199
|
+
);
|
|
200
|
+
const dc = moduleType === "esm" ? `import tr_ch_apm_dc from "${dcModule}"` : `const tr_ch_apm_dc = ${"require"}("${dcModule}")`;
|
|
201
|
+
const tracingChannel = "const { tracingChannel: tr_ch_apm_tracingChannel } = tr_ch_apm_dc";
|
|
202
|
+
const hasSubscribers = `const tr_ch_apm_hasSubscribers = ch => ch.start.hasSubscribers
|
|
203
|
+
|| ch.end.hasSubscribers
|
|
204
|
+
|| ch.asyncStart.hasSubscribers
|
|
205
|
+
|| ch.asyncEnd.hasSubscribers
|
|
206
|
+
|| ch.error.hasSubscribers`;
|
|
207
|
+
node.body.splice(
|
|
208
|
+
index + 1,
|
|
209
|
+
0,
|
|
210
|
+
parse(dc, options).body[0],
|
|
211
|
+
parse(tracingChannel, options).body[0],
|
|
212
|
+
parse(hasSubscribers, options).body[0]
|
|
213
|
+
);
|
|
214
|
+
},
|
|
215
|
+
tracingChannelDeclaration(state, node) {
|
|
216
|
+
const {
|
|
217
|
+
channelName,
|
|
218
|
+
module: { name }
|
|
219
|
+
} = state;
|
|
220
|
+
const channelVariable = formatChannelVariable(channelName);
|
|
221
|
+
if (node.body.some(
|
|
222
|
+
(child) => child.declarations?.[0]?.id?.name === channelVariable
|
|
223
|
+
)) {
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
transforms.tracingChannelImport(state, node, null, []);
|
|
227
|
+
const index = node.body.findIndex(tracingChannelPredicate);
|
|
228
|
+
const code = `
|
|
229
|
+
const ${channelVariable} = tr_ch_apm_tracingChannel("orchestrion:${name}:${channelName}")
|
|
230
|
+
`;
|
|
231
|
+
node.body.splice(index + 1, 0, parse(code).body[0]);
|
|
232
|
+
},
|
|
233
|
+
traceCallback: traceAny,
|
|
234
|
+
tracePromise: traceAny,
|
|
235
|
+
traceSync: traceAny
|
|
236
|
+
};
|
|
237
|
+
function traceAny(state, node, _parent, ancestry) {
|
|
238
|
+
const program = ancestry[ancestry.length - 1];
|
|
239
|
+
if (node.type === "ClassDeclaration" || node.type === "ClassExpression") {
|
|
240
|
+
traceInstanceMethod(state, node, program);
|
|
241
|
+
} else {
|
|
242
|
+
traceFunction(state, node, program);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
function traceFunction(state, node, program) {
|
|
246
|
+
transforms.tracingChannelDeclaration(state, program, null, []);
|
|
247
|
+
const { functionQuery } = state;
|
|
248
|
+
const methodName = "methodName" in functionQuery ? functionQuery.methodName : void 0;
|
|
249
|
+
const privateMethodName = "privateMethodName" in functionQuery ? functionQuery.privateMethodName : void 0;
|
|
250
|
+
const functionName = "functionName" in functionQuery ? functionQuery.functionName : void 0;
|
|
251
|
+
const isConstructor = methodName === "constructor" || !methodName && !privateMethodName && !functionName;
|
|
252
|
+
const type = isConstructor ? "ArrowFunctionExpression" : "FunctionExpression";
|
|
253
|
+
node.body = wrap(
|
|
254
|
+
state,
|
|
255
|
+
{
|
|
256
|
+
type,
|
|
257
|
+
params: node.params,
|
|
258
|
+
body: node.body,
|
|
259
|
+
async: node.async,
|
|
260
|
+
expression: false,
|
|
261
|
+
generator: node.generator
|
|
262
|
+
},
|
|
263
|
+
program
|
|
264
|
+
);
|
|
265
|
+
node.generator = false;
|
|
266
|
+
node.async = false;
|
|
267
|
+
wrapSuper(node);
|
|
268
|
+
}
|
|
269
|
+
function traceInstanceMethod(state, node, program) {
|
|
270
|
+
const { functionQuery, operator } = state;
|
|
271
|
+
const { methodName } = functionQuery;
|
|
272
|
+
if (!methodName) {
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
const classBody = node.body;
|
|
276
|
+
if (classBody.body.some(({ key }) => key.name === methodName)) {
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
let ctor = classBody.body.find(({ kind }) => kind === "constructor");
|
|
280
|
+
transforms.tracingChannelDeclaration(state, program, null, []);
|
|
281
|
+
if (!ctor) {
|
|
282
|
+
ctor = parse(
|
|
283
|
+
node.superClass ? "class A extends Object { constructor (...args) { super(...args) } }" : "class A { constructor () {} }"
|
|
284
|
+
).body[0].body.body[0];
|
|
285
|
+
classBody.body.unshift(ctor);
|
|
286
|
+
}
|
|
287
|
+
const ctorBody = parse(`
|
|
288
|
+
const __apm$${methodName} = this["${methodName}"]
|
|
289
|
+
this["${methodName}"] = function () {}
|
|
290
|
+
`).body;
|
|
291
|
+
const fn = ctorBody[1].expression.right;
|
|
292
|
+
fn.async = operator === "tracePromise";
|
|
293
|
+
fn.body = wrap(
|
|
294
|
+
state,
|
|
295
|
+
{ type: "Identifier", name: `__apm$${methodName}` },
|
|
296
|
+
program
|
|
297
|
+
);
|
|
298
|
+
wrapSuper(fn);
|
|
299
|
+
ctor.value.body.body.push(...ctorBody);
|
|
300
|
+
}
|
|
301
|
+
function wrap(state, node, program) {
|
|
302
|
+
const { operator, moduleVersion } = state;
|
|
303
|
+
const wrapper = operator === "traceCallback" ? wrapCallback(state) : operator === "tracePromise" ? wrapPromise(state) : wrapSync(state);
|
|
304
|
+
const block = wrapper.body[0].body;
|
|
305
|
+
const common = parse(
|
|
306
|
+
node.type === "ArrowFunctionExpression" ? `
|
|
307
|
+
const __apm$ctx = {
|
|
308
|
+
arguments,
|
|
309
|
+
moduleVersion: ${JSON.stringify(moduleVersion)}
|
|
310
|
+
};
|
|
311
|
+
const __apm$traced = () => {
|
|
312
|
+
const __apm$wrapped = () => {};
|
|
313
|
+
return __apm$wrapped(...arguments);
|
|
314
|
+
};
|
|
315
|
+
` : `
|
|
316
|
+
const __apm$ctx = {
|
|
317
|
+
arguments,
|
|
318
|
+
self: this,
|
|
319
|
+
moduleVersion: ${JSON.stringify(moduleVersion)}
|
|
320
|
+
};
|
|
321
|
+
const __apm$traced = () => {
|
|
322
|
+
const __apm$wrapped = () => {};
|
|
323
|
+
return __apm$wrapped.apply(this, arguments);
|
|
324
|
+
};
|
|
325
|
+
`
|
|
326
|
+
).body;
|
|
327
|
+
block.body.unshift(...common);
|
|
328
|
+
esquery.query(block, "[id.name=__apm$wrapped]")[0].init = node;
|
|
329
|
+
return block;
|
|
330
|
+
}
|
|
331
|
+
function wrapSuper(node) {
|
|
332
|
+
const members = /* @__PURE__ */ new Set();
|
|
333
|
+
esquery.traverse(
|
|
334
|
+
node.body,
|
|
335
|
+
esquery.parse("[object.type=Super]"),
|
|
336
|
+
(node2, parent) => {
|
|
337
|
+
const { name } = node2.property;
|
|
338
|
+
let child;
|
|
339
|
+
if (parent.callee) {
|
|
340
|
+
const { expression } = parse(`__apm$super['${name}'].call(this)`).body[0];
|
|
341
|
+
parent.callee = child = expression.callee;
|
|
342
|
+
parent.arguments.unshift(...expression.arguments);
|
|
343
|
+
} else {
|
|
344
|
+
parent.expression = child = parse(`__apm$super['${name}']`).body[0];
|
|
345
|
+
}
|
|
346
|
+
child.computed = parent.callee.computed;
|
|
347
|
+
child.optional = parent.callee.optional;
|
|
348
|
+
members.add(name);
|
|
349
|
+
}
|
|
350
|
+
);
|
|
351
|
+
for (const name of members) {
|
|
352
|
+
const member = parse(`
|
|
353
|
+
class Wrapper {
|
|
354
|
+
wrapper () {
|
|
355
|
+
__apm$super['${name}'] = super['${name}']
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
`).body[0].body.body[0].value.body.body[0];
|
|
359
|
+
node.body.body.unshift(member);
|
|
360
|
+
}
|
|
361
|
+
if (members.size > 0) {
|
|
362
|
+
node.body.body.unshift(parse("const __apm$super = {}").body[0]);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
function wrapCallback(state) {
|
|
366
|
+
const {
|
|
367
|
+
channelName,
|
|
368
|
+
functionQuery: { callbackIndex = -1 }
|
|
369
|
+
} = state;
|
|
370
|
+
const channelVariable = formatChannelVariable(channelName);
|
|
371
|
+
return parse(`
|
|
372
|
+
function wrapper () {
|
|
373
|
+
const __apm$cb = Array.prototype.at.call(arguments, ${callbackIndex});
|
|
374
|
+
|
|
375
|
+
if (!${channelVariable}.start.hasSubscribers) return __apm$traced();
|
|
376
|
+
|
|
377
|
+
function __apm$wrappedCb(err, res) {
|
|
378
|
+
if (err) {
|
|
379
|
+
__apm$ctx.error = err;
|
|
380
|
+
${channelVariable}.error.publish(__apm$ctx);
|
|
381
|
+
} else {
|
|
382
|
+
__apm$ctx.result = res;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
${channelVariable}.asyncStart.runStores(__apm$ctx, () => {
|
|
386
|
+
try {
|
|
387
|
+
if (__apm$cb) {
|
|
388
|
+
return __apm$cb.apply(this, arguments);
|
|
389
|
+
}
|
|
390
|
+
} finally {
|
|
391
|
+
${channelVariable}.asyncEnd.publish(__apm$ctx);
|
|
392
|
+
}
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
if (typeof __apm$cb !== 'function') {
|
|
397
|
+
return __apm$traced();
|
|
398
|
+
}
|
|
399
|
+
Array.prototype.splice.call(arguments, ${callbackIndex}, 1, __apm$wrappedCb);
|
|
400
|
+
|
|
401
|
+
return ${channelVariable}.start.runStores(__apm$ctx, () => {
|
|
402
|
+
try {
|
|
403
|
+
return __apm$traced();
|
|
404
|
+
} catch (err) {
|
|
405
|
+
__apm$ctx.error = err;
|
|
406
|
+
${channelVariable}.error.publish(__apm$ctx);
|
|
407
|
+
throw err;
|
|
408
|
+
} finally {
|
|
409
|
+
__apm$ctx.self ??= this;
|
|
410
|
+
${channelVariable}.end.publish(__apm$ctx);
|
|
411
|
+
}
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
`);
|
|
415
|
+
}
|
|
416
|
+
function wrapPromise(state) {
|
|
417
|
+
const { channelName } = state;
|
|
418
|
+
const channelVariable = formatChannelVariable(channelName);
|
|
419
|
+
return parse(`
|
|
420
|
+
function wrapper () {
|
|
421
|
+
if (!tr_ch_apm_hasSubscribers(${channelVariable})) return __apm$traced();
|
|
422
|
+
|
|
423
|
+
return ${channelVariable}.start.runStores(__apm$ctx, () => {
|
|
424
|
+
try {
|
|
425
|
+
let promise = __apm$traced();
|
|
426
|
+
if (typeof promise?.then !== 'function') {
|
|
427
|
+
__apm$ctx.result = promise;
|
|
428
|
+
return promise;
|
|
429
|
+
}
|
|
430
|
+
// Mirror Node.js core diagnostics_channel behaviour: for native Promise
|
|
431
|
+
// instances, chain normally (safe since there is no subclass API to
|
|
432
|
+
// preserve). For Promise subclasses and other thenables, side-chain the
|
|
433
|
+
// callbacks for event publishing and return the original so that any
|
|
434
|
+
// subclass-specific methods (e.g. APIPromise.withResponse()) remain
|
|
435
|
+
// accessible to the caller.
|
|
436
|
+
if (promise instanceof Promise && promise.constructor === Promise) {
|
|
437
|
+
return promise.then(
|
|
438
|
+
result => {
|
|
439
|
+
__apm$ctx.result = result;
|
|
440
|
+
${channelVariable}.asyncStart.publish(__apm$ctx);
|
|
441
|
+
${channelVariable}.asyncEnd.publish(__apm$ctx);
|
|
442
|
+
return result;
|
|
443
|
+
},
|
|
444
|
+
err => {
|
|
445
|
+
__apm$ctx.error = err;
|
|
446
|
+
${channelVariable}.error.publish(__apm$ctx);
|
|
447
|
+
${channelVariable}.asyncStart.publish(__apm$ctx);
|
|
448
|
+
${channelVariable}.asyncEnd.publish(__apm$ctx);
|
|
449
|
+
throw err;
|
|
450
|
+
}
|
|
451
|
+
);
|
|
452
|
+
}
|
|
453
|
+
promise.then(
|
|
454
|
+
result => {
|
|
455
|
+
__apm$ctx.result = result;
|
|
456
|
+
${channelVariable}.asyncStart.publish(__apm$ctx);
|
|
457
|
+
${channelVariable}.asyncEnd.publish(__apm$ctx);
|
|
458
|
+
},
|
|
459
|
+
err => {
|
|
460
|
+
__apm$ctx.error = err;
|
|
461
|
+
${channelVariable}.error.publish(__apm$ctx);
|
|
462
|
+
${channelVariable}.asyncStart.publish(__apm$ctx);
|
|
463
|
+
${channelVariable}.asyncEnd.publish(__apm$ctx);
|
|
464
|
+
}
|
|
465
|
+
);
|
|
466
|
+
return promise;
|
|
467
|
+
} catch (err) {
|
|
468
|
+
__apm$ctx.error = err;
|
|
469
|
+
${channelVariable}.error.publish(__apm$ctx);
|
|
470
|
+
throw err;
|
|
471
|
+
} finally {
|
|
472
|
+
__apm$ctx.self ??= this;
|
|
473
|
+
${channelVariable}.end.publish(__apm$ctx);
|
|
474
|
+
}
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
`);
|
|
478
|
+
}
|
|
479
|
+
function wrapSync(state) {
|
|
480
|
+
const { channelName } = state;
|
|
481
|
+
const channelVariable = formatChannelVariable(channelName);
|
|
482
|
+
return parse(`
|
|
483
|
+
function wrapper () {
|
|
484
|
+
if (!tr_ch_apm_hasSubscribers(${channelVariable})) return __apm$traced();
|
|
485
|
+
|
|
486
|
+
return ${channelVariable}.start.runStores(__apm$ctx, () => {
|
|
487
|
+
try {
|
|
488
|
+
const result = __apm$traced();
|
|
489
|
+
__apm$ctx.result = result;
|
|
490
|
+
return result;
|
|
491
|
+
} catch (err) {
|
|
492
|
+
__apm$ctx.error = err;
|
|
493
|
+
${channelVariable}.error.publish(__apm$ctx);
|
|
494
|
+
throw err;
|
|
495
|
+
} finally {
|
|
496
|
+
__apm$ctx.self ??= this;
|
|
497
|
+
${channelVariable}.end.publish(__apm$ctx);
|
|
498
|
+
}
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
`);
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
// src/auto-instrumentations/orchestrion-js/transformer.ts
|
|
505
|
+
var Transformer = class {
|
|
506
|
+
moduleName;
|
|
507
|
+
version;
|
|
508
|
+
filePath;
|
|
509
|
+
configs = [];
|
|
510
|
+
dcModule;
|
|
511
|
+
constructor(moduleName, version, filePath, configs, dcModule) {
|
|
512
|
+
this.moduleName = moduleName;
|
|
513
|
+
this.version = version;
|
|
514
|
+
this.filePath = filePath;
|
|
515
|
+
this.configs = configs;
|
|
516
|
+
this.dcModule = dcModule;
|
|
517
|
+
}
|
|
518
|
+
/**
|
|
519
|
+
* Instruments `code` by injecting diagnostics_channel tracing around the
|
|
520
|
+
* target functions defined by this transformer's configs.
|
|
521
|
+
*/
|
|
522
|
+
transform(code, moduleType) {
|
|
523
|
+
if (Buffer.isBuffer(code)) {
|
|
524
|
+
code = code.toString();
|
|
525
|
+
}
|
|
526
|
+
if (!code) {
|
|
527
|
+
return { code };
|
|
528
|
+
}
|
|
529
|
+
let ast;
|
|
530
|
+
let aliases = {};
|
|
531
|
+
let injectionCount = 0;
|
|
532
|
+
for (const config of this.configs) {
|
|
533
|
+
const { astQuery, functionQuery } = config;
|
|
534
|
+
if (!ast) {
|
|
535
|
+
const options = {
|
|
536
|
+
loc: true,
|
|
537
|
+
ranges: true,
|
|
538
|
+
raw: true,
|
|
539
|
+
module: moduleType === "esm"
|
|
540
|
+
};
|
|
541
|
+
try {
|
|
542
|
+
ast = parse2(code, options);
|
|
543
|
+
} catch {
|
|
544
|
+
ast = parse2(code, { ...options, module: !options.module });
|
|
545
|
+
}
|
|
546
|
+
if (moduleType === "esm") {
|
|
547
|
+
aliases = this.collectExportAliases(ast);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
const resolvedFunctionQuery = this.resolveExportAlias(
|
|
551
|
+
functionQuery,
|
|
552
|
+
aliases
|
|
553
|
+
);
|
|
554
|
+
const query = astQuery || this.fromFunctionQuery(resolvedFunctionQuery);
|
|
555
|
+
const state = {
|
|
556
|
+
...config,
|
|
557
|
+
dcModule: this.dcModule,
|
|
558
|
+
moduleType,
|
|
559
|
+
moduleVersion: this.version,
|
|
560
|
+
functionQuery: resolvedFunctionQuery,
|
|
561
|
+
operator: this.getOperator(resolvedFunctionQuery.kind)
|
|
562
|
+
};
|
|
563
|
+
esquery2.traverse(ast, esquery2.parse(query), (...args) => {
|
|
564
|
+
injectionCount++;
|
|
565
|
+
this.visit(state, ...args);
|
|
566
|
+
});
|
|
567
|
+
}
|
|
568
|
+
if (injectionCount === 0 && this.configs.length > 0) {
|
|
569
|
+
const names = this.configs.map(({ astQuery, functionQuery }) => {
|
|
570
|
+
const resolvedQuery = this.resolveExportAlias(functionQuery, aliases);
|
|
571
|
+
const queryName = (q) => q.methodName || q.privateMethodName || q.functionName || q.propertyName || astQuery || "unknown";
|
|
572
|
+
const originalName = queryName(functionQuery);
|
|
573
|
+
const originalAlias = functionQuery.className || functionQuery.functionName;
|
|
574
|
+
const resolvedAlias = resolvedQuery.className || resolvedQuery.functionName;
|
|
575
|
+
if (originalAlias && originalAlias !== resolvedAlias) {
|
|
576
|
+
return `${originalAlias} (local name: ${resolvedAlias})`;
|
|
577
|
+
}
|
|
578
|
+
return originalName;
|
|
579
|
+
});
|
|
580
|
+
throw new Error(
|
|
581
|
+
`Failed to find injection points for: ${JSON.stringify(names)}`
|
|
582
|
+
);
|
|
583
|
+
}
|
|
584
|
+
if (ast) {
|
|
585
|
+
const file = `${this.moduleName}/${this.filePath}`;
|
|
586
|
+
const sourceMap = new SourceMapGenerator({ file });
|
|
587
|
+
const transformedCode = generate(ast, { sourceMap });
|
|
588
|
+
const map = sourceMap.toString();
|
|
589
|
+
return { code: transformedCode, map };
|
|
590
|
+
}
|
|
591
|
+
return { code };
|
|
592
|
+
}
|
|
593
|
+
free() {
|
|
594
|
+
}
|
|
595
|
+
visit(state, ...args) {
|
|
596
|
+
const transform = transforms[state.operator];
|
|
597
|
+
const { index = 0 } = state.functionQuery;
|
|
598
|
+
const [node] = args;
|
|
599
|
+
const type = node.init?.type || node.type;
|
|
600
|
+
if (type !== "ClassDeclaration" && type !== "ClassExpression") {
|
|
601
|
+
if (node.type === "VariableDeclarator") {
|
|
602
|
+
return;
|
|
603
|
+
}
|
|
604
|
+
state.functionIndex = state.functionIndex === void 0 ? 0 : state.functionIndex + 1;
|
|
605
|
+
if (index !== null && index !== state.functionIndex) {
|
|
606
|
+
return;
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
transform(state, ...args);
|
|
610
|
+
}
|
|
611
|
+
getOperator(kind) {
|
|
612
|
+
switch (kind) {
|
|
613
|
+
case "Async":
|
|
614
|
+
return "tracePromise";
|
|
615
|
+
case "Callback":
|
|
616
|
+
return "traceCallback";
|
|
617
|
+
case "Sync":
|
|
618
|
+
return "traceSync";
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
collectExportAliases(ast) {
|
|
622
|
+
const aliases = {};
|
|
623
|
+
for (const node of ast.body) {
|
|
624
|
+
if (node.type === "ExportNamedDeclaration" && !node.source) {
|
|
625
|
+
for (const spec of node.specifiers) {
|
|
626
|
+
if (spec.exported && spec.local) {
|
|
627
|
+
const exportedName = spec.exported.name ?? spec.exported.value;
|
|
628
|
+
const localName = spec.local.name ?? spec.local.value;
|
|
629
|
+
if (exportedName && localName) {
|
|
630
|
+
aliases[exportedName] = localName;
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
return aliases;
|
|
637
|
+
}
|
|
638
|
+
resolveExportAlias(functionQuery, aliases) {
|
|
639
|
+
if (!("isExportAlias" in functionQuery) || !functionQuery.isExportAlias) {
|
|
640
|
+
return functionQuery;
|
|
641
|
+
}
|
|
642
|
+
if ("className" in functionQuery && aliases[functionQuery.className]) {
|
|
643
|
+
return {
|
|
644
|
+
...functionQuery,
|
|
645
|
+
className: aliases[functionQuery.className]
|
|
646
|
+
};
|
|
647
|
+
}
|
|
648
|
+
if ("functionName" in functionQuery && aliases[functionQuery.functionName]) {
|
|
649
|
+
return {
|
|
650
|
+
...functionQuery,
|
|
651
|
+
functionName: aliases[functionQuery.functionName]
|
|
652
|
+
};
|
|
653
|
+
}
|
|
654
|
+
return functionQuery;
|
|
655
|
+
}
|
|
656
|
+
functionQueryLabel(functionQuery) {
|
|
657
|
+
if ("methodName" in functionQuery) {
|
|
658
|
+
return functionQuery.methodName;
|
|
659
|
+
}
|
|
660
|
+
if ("privateMethodName" in functionQuery) {
|
|
661
|
+
return functionQuery.privateMethodName;
|
|
662
|
+
}
|
|
663
|
+
if ("functionName" in functionQuery) {
|
|
664
|
+
return functionQuery.functionName;
|
|
665
|
+
}
|
|
666
|
+
if ("propertyName" in functionQuery) {
|
|
667
|
+
return functionQuery.propertyName;
|
|
668
|
+
}
|
|
669
|
+
return "unknown";
|
|
670
|
+
}
|
|
671
|
+
fromFunctionQuery(functionQuery) {
|
|
672
|
+
const queries = [];
|
|
673
|
+
if ("className" in functionQuery) {
|
|
674
|
+
const { className } = functionQuery;
|
|
675
|
+
const methodName = this.functionQueryLabel(functionQuery);
|
|
676
|
+
const keyType = "privateMethodName" in functionQuery ? "PrivateIdentifier" : "Identifier";
|
|
677
|
+
queries.push(
|
|
678
|
+
`[id.name="${className}"]`,
|
|
679
|
+
`[id.name="${className}"] > ClassExpression`,
|
|
680
|
+
`[id.name="${className}"] > ClassBody > [key.name="${methodName}"][key.type=${keyType}] > [async]`,
|
|
681
|
+
`[id.name="${className}"] > ClassExpression > ClassBody > [key.name="${methodName}"][key.type=${keyType}] > [async]`
|
|
682
|
+
);
|
|
683
|
+
} else if ("methodName" in functionQuery) {
|
|
684
|
+
const { methodName } = functionQuery;
|
|
685
|
+
queries.push(
|
|
686
|
+
`ClassBody > [key.name="${methodName}"][key.type=Identifier] > [async]`,
|
|
687
|
+
`Property[key.name="${methodName}"][key.type=Identifier] > [async]`
|
|
688
|
+
);
|
|
689
|
+
}
|
|
690
|
+
if ("functionName" in functionQuery) {
|
|
691
|
+
const { functionName } = functionQuery;
|
|
692
|
+
queries.push(`FunctionDeclaration[id.name="${functionName}"][async]`);
|
|
693
|
+
}
|
|
694
|
+
if ("objectName" in functionQuery) {
|
|
695
|
+
const { objectName, propertyName } = functionQuery;
|
|
696
|
+
const objectSelector = objectName === "this" ? "left.object.type=ThisExpression" : `left.object.name="${objectName}"`;
|
|
697
|
+
queries.push(
|
|
698
|
+
`AssignmentExpression[${objectSelector}][left.property.name="${propertyName}"] > [async]`
|
|
699
|
+
);
|
|
700
|
+
}
|
|
701
|
+
return queries.join(", ");
|
|
702
|
+
}
|
|
703
|
+
};
|
|
704
|
+
|
|
705
|
+
// src/auto-instrumentations/orchestrion-js/matcher.ts
|
|
706
|
+
var InstrumentationMatcher = class {
|
|
707
|
+
configs = [];
|
|
708
|
+
dcModule;
|
|
709
|
+
transformers = {};
|
|
710
|
+
constructor(configs, dcModule) {
|
|
711
|
+
this.configs = configs;
|
|
712
|
+
this.dcModule = dcModule || "diagnostics_channel";
|
|
713
|
+
}
|
|
714
|
+
/**
|
|
715
|
+
* Returns a Transformer for the given module/file/version, or undefined if no
|
|
716
|
+
* registered config matches.
|
|
717
|
+
*/
|
|
718
|
+
getTransformer(moduleName, version, filePath) {
|
|
719
|
+
filePath = filePath.replace(/\\/g, "/");
|
|
720
|
+
const id = `${moduleName}/${filePath}@${version}`;
|
|
721
|
+
if (this.transformers[id]) {
|
|
722
|
+
return this.transformers[id];
|
|
723
|
+
}
|
|
724
|
+
const configs = this.configs.filter(
|
|
725
|
+
({ module: mod }) => mod.name === moduleName && mod.filePath === filePath && semifies(version, mod.versionRange)
|
|
726
|
+
);
|
|
727
|
+
if (configs.length === 0) {
|
|
728
|
+
return void 0;
|
|
729
|
+
}
|
|
730
|
+
this.transformers[id] = new Transformer(
|
|
731
|
+
moduleName,
|
|
732
|
+
version,
|
|
733
|
+
filePath,
|
|
734
|
+
configs,
|
|
735
|
+
this.dcModule
|
|
736
|
+
);
|
|
737
|
+
return this.transformers[id];
|
|
738
|
+
}
|
|
739
|
+
free() {
|
|
740
|
+
this.transformers = {};
|
|
741
|
+
}
|
|
742
|
+
};
|
|
743
|
+
|
|
744
|
+
// src/auto-instrumentations/orchestrion-js/index.ts
|
|
745
|
+
function create(configs, dcModule) {
|
|
746
|
+
return new InstrumentationMatcher(configs, dcModule);
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
// src/auto-instrumentations/loader/get-package-version.ts
|
|
750
|
+
import { readFileSync, realpathSync } from "fs";
|
|
751
|
+
import { join } from "path";
|
|
752
|
+
var packageVersions = /* @__PURE__ */ new Map();
|
|
753
|
+
var packageNames = /* @__PURE__ */ new Map();
|
|
754
|
+
function readPackageJson(baseDir) {
|
|
755
|
+
try {
|
|
756
|
+
const packageJsonPath = join(baseDir, "package.json");
|
|
757
|
+
const jsonFile = readFileSync(packageJsonPath, "utf8");
|
|
758
|
+
return JSON.parse(jsonFile);
|
|
759
|
+
} catch {
|
|
760
|
+
return void 0;
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
function resolvePackageBaseDir(baseDir) {
|
|
764
|
+
try {
|
|
765
|
+
return realpathSync(baseDir);
|
|
766
|
+
} catch {
|
|
767
|
+
return baseDir;
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
function readPackageJsonWithFallback(baseDir) {
|
|
771
|
+
const packageJson = readPackageJson(baseDir);
|
|
772
|
+
if (packageJson) {
|
|
773
|
+
return packageJson;
|
|
774
|
+
}
|
|
775
|
+
const resolvedBaseDir = resolvePackageBaseDir(baseDir);
|
|
776
|
+
if (resolvedBaseDir === baseDir) {
|
|
777
|
+
return void 0;
|
|
778
|
+
}
|
|
779
|
+
return readPackageJson(resolvedBaseDir);
|
|
780
|
+
}
|
|
781
|
+
function getPackageVersion(baseDir) {
|
|
782
|
+
if (packageVersions.has(baseDir)) {
|
|
783
|
+
return packageVersions.get(baseDir);
|
|
784
|
+
}
|
|
785
|
+
const packageJson = readPackageJsonWithFallback(baseDir);
|
|
786
|
+
if (typeof packageJson?.version === "string") {
|
|
787
|
+
packageVersions.set(baseDir, packageJson.version);
|
|
788
|
+
return packageJson.version;
|
|
789
|
+
}
|
|
790
|
+
return process.version.slice(1);
|
|
791
|
+
}
|
|
792
|
+
function getPackageName(baseDir) {
|
|
793
|
+
if (packageNames.has(baseDir)) {
|
|
794
|
+
return packageNames.get(baseDir);
|
|
795
|
+
}
|
|
796
|
+
const packageJson = readPackageJsonWithFallback(baseDir);
|
|
797
|
+
if (typeof packageJson?.name === "string") {
|
|
798
|
+
packageNames.set(baseDir, packageJson.name);
|
|
799
|
+
return packageJson.name;
|
|
800
|
+
}
|
|
801
|
+
return void 0;
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
// src/auto-instrumentations/loader/openai-api-promise-patch.ts
|
|
805
|
+
var OPENAI_API_PROMISE_PATCH = `
|
|
806
|
+
;(function __btPatchAPIPromise() {
|
|
807
|
+
if (typeof APIPromise === "undefined" || APIPromise.prototype.__btParsePatched) return;
|
|
808
|
+
APIPromise.prototype.__btParsePatched = true;
|
|
809
|
+
var _origThen = APIPromise.prototype.then;
|
|
810
|
+
APIPromise.prototype.then = function __btThen(onfulfilled, onrejected) {
|
|
811
|
+
if (!this.__btParseWrapped && Object.prototype.hasOwnProperty.call(this, "parseResponse")) {
|
|
812
|
+
this.__btParseWrapped = true;
|
|
813
|
+
var _origParse = this.parseResponse;
|
|
814
|
+
var _cached;
|
|
815
|
+
this.parseResponse = function() {
|
|
816
|
+
if (!_cached) _cached = _origParse.apply(this, arguments);
|
|
817
|
+
return _cached;
|
|
818
|
+
};
|
|
819
|
+
}
|
|
820
|
+
return _origThen.call(this, onfulfilled, onrejected);
|
|
821
|
+
};
|
|
822
|
+
})();
|
|
823
|
+
`;
|
|
824
|
+
|
|
825
|
+
// src/auto-instrumentations/loader/special-case-patches.ts
|
|
826
|
+
function applySpecialCasePatch(input) {
|
|
827
|
+
if (input.packageName === "openai" && input.modulePath.includes("api-promise")) {
|
|
828
|
+
return input.source + OPENAI_API_PROMISE_PATCH;
|
|
829
|
+
}
|
|
830
|
+
const mastraTarget = classifyMastraTarget(
|
|
831
|
+
input.packageName,
|
|
832
|
+
input.modulePath
|
|
833
|
+
);
|
|
834
|
+
if (mastraTarget) {
|
|
835
|
+
return patchMastraSource(input.source, mastraTarget, input.format);
|
|
836
|
+
}
|
|
837
|
+
return null;
|
|
838
|
+
}
|
|
839
|
+
function isSpecialCaseTarget(packageName, modulePath) {
|
|
840
|
+
if (packageName === "openai" && modulePath.includes("api-promise")) {
|
|
841
|
+
return true;
|
|
842
|
+
}
|
|
843
|
+
if (classifyMastraTarget(packageName, modulePath) !== null) {
|
|
844
|
+
return true;
|
|
845
|
+
}
|
|
846
|
+
return false;
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
export {
|
|
850
|
+
installMastraExporterFactory,
|
|
851
|
+
create,
|
|
852
|
+
getPackageVersion,
|
|
853
|
+
getPackageName,
|
|
854
|
+
applySpecialCasePatch,
|
|
855
|
+
isSpecialCaseTarget
|
|
856
|
+
};
|