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.
Files changed (79) hide show
  1. package/NOTICE +6 -0
  2. package/dev/dist/index.d.mts +130 -2
  3. package/dev/dist/index.d.ts +130 -2
  4. package/dev/dist/index.js +4133 -1421
  5. package/dev/dist/index.mjs +3242 -530
  6. package/dist/apply-auto-instrumentation.js +526 -426
  7. package/dist/apply-auto-instrumentation.mjs +329 -229
  8. package/dist/auto-instrumentations/bundler/esbuild.cjs +1198 -364
  9. package/dist/auto-instrumentations/bundler/esbuild.d.mts +4 -4
  10. package/dist/auto-instrumentations/bundler/esbuild.d.ts +4 -4
  11. package/dist/auto-instrumentations/bundler/esbuild.mjs +6 -8
  12. package/dist/auto-instrumentations/bundler/next.cjs +1227 -413
  13. package/dist/auto-instrumentations/bundler/next.mjs +7 -29
  14. package/dist/auto-instrumentations/bundler/rollup.cjs +1198 -364
  15. package/dist/auto-instrumentations/bundler/rollup.d.mts +4 -4
  16. package/dist/auto-instrumentations/bundler/rollup.d.ts +4 -4
  17. package/dist/auto-instrumentations/bundler/rollup.mjs +6 -8
  18. package/dist/auto-instrumentations/bundler/vite.cjs +1249 -387
  19. package/dist/auto-instrumentations/bundler/vite.d.mts +4 -4
  20. package/dist/auto-instrumentations/bundler/vite.d.ts +4 -4
  21. package/dist/auto-instrumentations/bundler/vite.mjs +34 -8
  22. package/dist/auto-instrumentations/bundler/webpack-loader.cjs +1368 -324
  23. package/dist/auto-instrumentations/bundler/webpack-loader.d.ts +20 -12
  24. package/dist/auto-instrumentations/bundler/webpack.cjs +1198 -364
  25. package/dist/auto-instrumentations/bundler/webpack.d.mts +4 -4
  26. package/dist/auto-instrumentations/bundler/webpack.d.ts +4 -4
  27. package/dist/auto-instrumentations/bundler/webpack.mjs +6 -4
  28. package/dist/auto-instrumentations/chunk-2AMDGD65.mjs +1396 -0
  29. package/dist/auto-instrumentations/{chunk-7P6563SW.mjs → chunk-BW33ULMW.mjs} +2 -6
  30. package/dist/auto-instrumentations/{chunk-M6DLIJ2Z.mjs → chunk-I55G56ZL.mjs} +114 -221
  31. package/dist/auto-instrumentations/chunk-JPUFNW7X.mjs +649 -0
  32. package/dist/auto-instrumentations/{chunk-JPVCUKTY.mjs → chunk-XEYKUBLY.mjs} +191 -73
  33. package/dist/auto-instrumentations/{chunk-CZ24KNHT.mjs → chunk-ZNHTSSGI.mjs} +231 -69
  34. package/dist/auto-instrumentations/hook.mjs +406 -183
  35. package/dist/auto-instrumentations/index.cjs +853 -49
  36. package/dist/auto-instrumentations/index.d.mts +10 -2
  37. package/dist/auto-instrumentations/index.d.ts +10 -2
  38. package/dist/auto-instrumentations/index.mjs +10 -1
  39. package/dist/auto-instrumentations/loader/cjs-patch.cjs +566 -221
  40. package/dist/auto-instrumentations/loader/cjs-patch.d.mts +2 -2
  41. package/dist/auto-instrumentations/loader/cjs-patch.d.ts +2 -2
  42. package/dist/auto-instrumentations/loader/cjs-patch.mjs +3 -2
  43. package/dist/auto-instrumentations/loader/esm-hook.mjs +1 -1
  44. package/dist/auto-instrumentations/plugin-B-aZh7EP.d.ts +30 -0
  45. package/dist/auto-instrumentations/plugin-Bnj4E_6B.d.mts +30 -0
  46. package/dist/auto-instrumentations/{types-RNPaKi9o.d.mts → types-C1fVeiCp.d.mts} +1 -1
  47. package/dist/auto-instrumentations/{types-RNPaKi9o.d.ts → types-C1fVeiCp.d.ts} +1 -1
  48. package/dist/browser.d.mts +871 -199
  49. package/dist/browser.d.ts +871 -199
  50. package/dist/browser.js +3737 -549
  51. package/dist/browser.mjs +3737 -549
  52. package/dist/{chunk-ABR2QWDP.mjs → chunk-CZM5JIQL.mjs} +788 -132
  53. package/dist/{chunk-P25IOOU4.js → chunk-MF7NU6BT.js} +795 -139
  54. package/dist/{chunk-GSIDVFE6.js → chunk-QRHGVBKU.js} +3580 -1463
  55. package/dist/{chunk-2SANLSWX.mjs → chunk-YKD22IMR.mjs} +2505 -388
  56. package/dist/cli.js +3244 -533
  57. package/dist/edge-light.d.mts +1 -1
  58. package/dist/edge-light.d.ts +1 -1
  59. package/dist/edge-light.js +3737 -552
  60. package/dist/edge-light.mjs +3737 -552
  61. package/dist/index.d.mts +871 -199
  62. package/dist/index.d.ts +871 -199
  63. package/dist/index.js +893 -446
  64. package/dist/index.mjs +463 -16
  65. package/dist/instrumentation/index.d.mts +138 -6
  66. package/dist/instrumentation/index.d.ts +138 -6
  67. package/dist/instrumentation/index.js +3362 -559
  68. package/dist/instrumentation/index.mjs +3362 -559
  69. package/dist/vitest-evals-reporter.js +45 -43
  70. package/dist/vitest-evals-reporter.mjs +5 -3
  71. package/dist/workerd.d.mts +1 -1
  72. package/dist/workerd.d.ts +1 -1
  73. package/dist/workerd.js +3737 -552
  74. package/dist/workerd.mjs +3737 -552
  75. package/licenses/node-diagnostics-channel/LICENSE +19 -0
  76. package/package.json +34 -35
  77. package/dist/auto-instrumentations/chunk-VT6DDNKM.mjs +0 -856
  78. package/dist/auto-instrumentations/plugin-D0KHwSJv.d.mts +0 -44
  79. package/dist/auto-instrumentations/plugin-G6fgxk1b.d.ts +0 -44
@@ -0,0 +1,649 @@
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 reportError(error) {
12
+ try {
13
+ errorReporter?.(error);
14
+ } catch {
15
+ }
16
+ }
17
+ function setContextValue(context, key, value) {
18
+ try {
19
+ context[key] = value;
20
+ } catch (error) {
21
+ reportError(error);
22
+ }
23
+ }
24
+ function wrapStoreRun(store, message, next, transform) {
25
+ return () => {
26
+ let context;
27
+ try {
28
+ context = transform ? transform(message) : message;
29
+ } catch (error) {
30
+ reportError(error);
31
+ return next();
32
+ }
33
+ let called = false;
34
+ let result;
35
+ let providerError;
36
+ let providerThrew = false;
37
+ const runNext = () => {
38
+ if (called) {
39
+ reportError(
40
+ new Error(
41
+ "Instrumentation store invoked its callback more than once"
42
+ )
43
+ );
44
+ if (providerThrew) {
45
+ throw providerError;
46
+ }
47
+ return result;
48
+ }
49
+ called = true;
50
+ try {
51
+ result = next();
52
+ return result;
53
+ } catch (error) {
54
+ providerThrew = true;
55
+ providerError = error;
56
+ throw error;
57
+ }
58
+ };
59
+ try {
60
+ store.run(context, runNext);
61
+ } catch (error) {
62
+ if (!providerThrew || error !== providerError) {
63
+ reportError(error);
64
+ }
65
+ }
66
+ if (!called) {
67
+ reportError(
68
+ new Error("Instrumentation store did not invoke its callback")
69
+ );
70
+ return runNext();
71
+ }
72
+ if (providerThrew) {
73
+ throw providerError;
74
+ }
75
+ return result;
76
+ };
77
+ }
78
+ var HookChannel = class {
79
+ constructor(name) {
80
+ this.name = name;
81
+ }
82
+ name;
83
+ subscribers = [];
84
+ stores = /* @__PURE__ */ new Map();
85
+ get hasSubscribers() {
86
+ return this.subscribers.length > 0 || this.stores.size > 0;
87
+ }
88
+ subscribe(subscription) {
89
+ if (typeof subscription !== "function") {
90
+ throw new TypeError("subscription must be a function");
91
+ }
92
+ this.subscribers = [...this.subscribers, subscription];
93
+ }
94
+ unsubscribe(subscription) {
95
+ const index = this.subscribers.indexOf(subscription);
96
+ if (index === -1) {
97
+ return false;
98
+ }
99
+ this.subscribers = [
100
+ ...this.subscribers.slice(0, index),
101
+ ...this.subscribers.slice(index + 1)
102
+ ];
103
+ return true;
104
+ }
105
+ bindStore(store, transform) {
106
+ if (!store || typeof store.run !== "function") {
107
+ throw new TypeError("store must have a run method");
108
+ }
109
+ this.stores.set(
110
+ store,
111
+ transform
112
+ );
113
+ }
114
+ unbindStore(store) {
115
+ return this.stores.delete(store);
116
+ }
117
+ publish(message) {
118
+ const subscribers = this.subscribers;
119
+ for (const subscriber of subscribers) {
120
+ try {
121
+ subscriber(message, this.name);
122
+ } catch (error) {
123
+ reportError(error);
124
+ }
125
+ }
126
+ }
127
+ runStores(message, fn, thisArg, ...args) {
128
+ let run = () => {
129
+ this.publish(message);
130
+ return Reflect.apply(fn, thisArg, args);
131
+ };
132
+ for (const [store, transform] of this.stores.entries()) {
133
+ run = wrapStoreRun(store, message, run, transform);
134
+ }
135
+ return run();
136
+ }
137
+ };
138
+ var traceEvents = [
139
+ "start",
140
+ "end",
141
+ "asyncStart",
142
+ "asyncEnd",
143
+ "error"
144
+ ];
145
+ function traceInvocation(hook, operator, target, thisArg, args, additional, callbackIndex = -1) {
146
+ const context = {
147
+ ...additional,
148
+ arguments: args,
149
+ self: thisArg
150
+ };
151
+ const invoke = () => hook.invoke(target, thisArg, args, additional);
152
+ if (operator === "traceCallback") {
153
+ return hook.traceCallback(invoke, callbackIndex, context);
154
+ }
155
+ if (operator === "tracePromise") {
156
+ return hook.tracePromise(invoke, context);
157
+ }
158
+ return hook.traceSync(invoke, context);
159
+ }
160
+ var InvocationHook = class {
161
+ interceptors = [];
162
+ get hasInterceptors() {
163
+ return this.interceptors.length > 0;
164
+ }
165
+ intercept(interceptor) {
166
+ if (typeof interceptor !== "function") {
167
+ throw new TypeError("interceptor must be a function");
168
+ }
169
+ this.interceptors = [...this.interceptors, interceptor];
170
+ let active = true;
171
+ return () => {
172
+ if (!active) {
173
+ return;
174
+ }
175
+ active = false;
176
+ const index = this.interceptors.indexOf(interceptor);
177
+ if (index !== -1) {
178
+ this.interceptors = [
179
+ ...this.interceptors.slice(0, index),
180
+ ...this.interceptors.slice(index + 1)
181
+ ];
182
+ }
183
+ };
184
+ }
185
+ invoke(target, thisArg, args, additional) {
186
+ const interceptors = this.interceptors;
187
+ if (interceptors.length === 0) {
188
+ return Reflect.apply(target, thisArg, args);
189
+ }
190
+ let next = target;
191
+ for (let index = interceptors.length - 1; index >= 0; index -= 1) {
192
+ const interceptor = interceptors[index];
193
+ const downstream = next;
194
+ next = function(...nextArgs) {
195
+ return interceptor(downstream, this, nextArgs, additional);
196
+ };
197
+ }
198
+ return Reflect.apply(next, thisArg, args);
199
+ }
200
+ };
201
+ var TracingHook = class {
202
+ start;
203
+ end;
204
+ asyncStart;
205
+ asyncEnd;
206
+ error;
207
+ invocationHook = new InvocationHook();
208
+ constructor(nameOrChannels) {
209
+ Object.defineProperty(this, hookBrand, {
210
+ configurable: false,
211
+ enumerable: false,
212
+ value: GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION,
213
+ writable: false
214
+ });
215
+ Object.defineProperty(this, invocationHookBrand, {
216
+ configurable: false,
217
+ enumerable: false,
218
+ value: GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION,
219
+ writable: false
220
+ });
221
+ if (typeof nameOrChannels === "string") {
222
+ this.start = new HookChannel(`tracing:${nameOrChannels}:start`);
223
+ this.end = new HookChannel(`tracing:${nameOrChannels}:end`);
224
+ this.asyncStart = new HookChannel(`tracing:${nameOrChannels}:asyncStart`);
225
+ this.asyncEnd = new HookChannel(`tracing:${nameOrChannels}:asyncEnd`);
226
+ this.error = new HookChannel(`tracing:${nameOrChannels}:error`);
227
+ return;
228
+ }
229
+ this.start = nameOrChannels.start ?? new HookChannel("tracing:start");
230
+ this.end = nameOrChannels.end ?? new HookChannel("tracing:end");
231
+ this.asyncStart = nameOrChannels.asyncStart ?? new HookChannel("tracing:asyncStart");
232
+ this.asyncEnd = nameOrChannels.asyncEnd ?? new HookChannel("tracing:asyncEnd");
233
+ this.error = nameOrChannels.error ?? new HookChannel("tracing:error");
234
+ }
235
+ get hasSubscribers() {
236
+ return this.start.hasSubscribers || this.end.hasSubscribers || this.asyncStart.hasSubscribers || this.asyncEnd.hasSubscribers || this.error.hasSubscribers;
237
+ }
238
+ get hasInterceptors() {
239
+ return this.invocationHook.hasInterceptors;
240
+ }
241
+ intercept(interceptor) {
242
+ return this.invocationHook.intercept(interceptor);
243
+ }
244
+ invoke(target, thisArg, args, additional) {
245
+ return this.invocationHook.invoke(target, thisArg, args, additional);
246
+ }
247
+ traceInvocation(operator, target, thisArg, args, additional, callbackIndex = -1) {
248
+ return traceInvocation(
249
+ this,
250
+ operator,
251
+ target,
252
+ thisArg,
253
+ args,
254
+ additional,
255
+ callbackIndex
256
+ );
257
+ }
258
+ subscribe(handlers) {
259
+ for (const eventName of traceEvents) {
260
+ const handler = handlers[eventName];
261
+ if (handler) {
262
+ this[eventName].subscribe(handler);
263
+ }
264
+ }
265
+ }
266
+ unsubscribe(handlers) {
267
+ let done = true;
268
+ for (const eventName of traceEvents) {
269
+ const handler = handlers[eventName];
270
+ if (handler && !this[eventName].unsubscribe(handler)) {
271
+ done = false;
272
+ }
273
+ }
274
+ return done;
275
+ }
276
+ traceSync(fn, message = {}, thisArg, ...args) {
277
+ if (!this.hasSubscribers) {
278
+ return Reflect.apply(fn, thisArg, args);
279
+ }
280
+ const context = message;
281
+ return this.start.runStores(message, () => {
282
+ try {
283
+ const result = Reflect.apply(fn, thisArg, args);
284
+ setContextValue(context, "result", result);
285
+ return result;
286
+ } catch (error) {
287
+ setContextValue(context, "error", error);
288
+ this.error.publish(message);
289
+ throw error;
290
+ } finally {
291
+ this.end.publish(message);
292
+ }
293
+ });
294
+ }
295
+ tracePromise(fn, message = {}, thisArg, ...args) {
296
+ if (!this.hasSubscribers) {
297
+ return Reflect.apply(fn, thisArg, args);
298
+ }
299
+ const context = message;
300
+ return this.start.runStores(message, () => {
301
+ let result;
302
+ try {
303
+ result = Reflect.apply(fn, thisArg, args);
304
+ } catch (error) {
305
+ setContextValue(context, "error", error);
306
+ this.error.publish(message);
307
+ this.end.publish(message);
308
+ throw error;
309
+ }
310
+ this.end.publish(message);
311
+ if (!result || typeof result !== "object" && typeof result !== "function") {
312
+ setContextValue(context, "result", result);
313
+ this.asyncStart.publish(message);
314
+ this.asyncEnd.publish(message);
315
+ return result;
316
+ }
317
+ let terminalPublished = false;
318
+ const finishUnobservedResult = (error) => {
319
+ reportError(error);
320
+ if (!terminalPublished) {
321
+ terminalPublished = true;
322
+ setContextValue(context, "result", result);
323
+ this.asyncStart.publish(message);
324
+ this.asyncEnd.publish(message);
325
+ }
326
+ return result;
327
+ };
328
+ let then;
329
+ try {
330
+ then = result.then;
331
+ } catch (error) {
332
+ return finishUnobservedResult(error);
333
+ }
334
+ if (typeof then !== "function") {
335
+ setContextValue(context, "result", result);
336
+ this.asyncStart.publish(message);
337
+ this.asyncEnd.publish(message);
338
+ return result;
339
+ }
340
+ const resolve = (resolved) => {
341
+ if (!terminalPublished) {
342
+ terminalPublished = true;
343
+ setContextValue(context, "result", resolved);
344
+ this.asyncStart.publish(message);
345
+ this.asyncEnd.publish(message);
346
+ }
347
+ return resolved;
348
+ };
349
+ let rejectionThrown = false;
350
+ let rejectionError;
351
+ const reject = (error) => {
352
+ if (!terminalPublished) {
353
+ terminalPublished = true;
354
+ setContextValue(context, "error", error);
355
+ this.error.publish(message);
356
+ this.asyncStart.publish(message);
357
+ this.asyncEnd.publish(message);
358
+ }
359
+ rejectionThrown = true;
360
+ rejectionError = error;
361
+ throw error;
362
+ };
363
+ let isPlainPromise;
364
+ try {
365
+ isPlainPromise = result instanceof Promise && result.constructor === Promise;
366
+ } catch (error) {
367
+ return finishUnobservedResult(error);
368
+ }
369
+ try {
370
+ if (isPlainPromise) {
371
+ return Reflect.apply(then, result, [resolve, reject]);
372
+ }
373
+ Reflect.apply(then, result, [
374
+ resolve,
375
+ (error) => {
376
+ try {
377
+ reject(error);
378
+ } catch {
379
+ }
380
+ }
381
+ ]);
382
+ } catch (error) {
383
+ if (rejectionThrown && error === rejectionError) {
384
+ return result;
385
+ }
386
+ return finishUnobservedResult(error);
387
+ }
388
+ return result;
389
+ });
390
+ }
391
+ traceCallback(fn, position = -1, message = {}, thisArg, ...args) {
392
+ if (!this.hasSubscribers) {
393
+ return Reflect.apply(fn, thisArg, args);
394
+ }
395
+ const context = message;
396
+ const callArgs = args.length > 0 ? args : context.arguments ?? args;
397
+ const callback = Array.prototype.at.call(callArgs, position);
398
+ if (typeof callback !== "function") {
399
+ return Reflect.apply(fn, thisArg, args);
400
+ }
401
+ const { asyncStart, asyncEnd, error: errorChannel } = this;
402
+ function wrappedCallback(error, result) {
403
+ if (error) {
404
+ setContextValue(context, "error", error);
405
+ errorChannel.publish(message);
406
+ } else {
407
+ setContextValue(context, "result", result);
408
+ }
409
+ return asyncStart.runStores(message, () => {
410
+ try {
411
+ return Reflect.apply(callback, this, arguments);
412
+ } finally {
413
+ asyncEnd.publish(message);
414
+ }
415
+ });
416
+ }
417
+ Array.prototype.splice.call(callArgs, position, 1, wrappedCallback);
418
+ return this.start.runStores(message, () => {
419
+ try {
420
+ return Reflect.apply(fn, thisArg, args);
421
+ } catch (error) {
422
+ setContextValue(context, "error", error);
423
+ this.error.publish(message);
424
+ throw error;
425
+ } finally {
426
+ this.end.publish(message);
427
+ }
428
+ });
429
+ }
430
+ };
431
+ var inertChannel = Object.freeze({
432
+ name: "braintrust:inert",
433
+ hasSubscribers: false,
434
+ subscribe() {
435
+ },
436
+ unsubscribe() {
437
+ return false;
438
+ },
439
+ bindStore() {
440
+ },
441
+ unbindStore() {
442
+ return false;
443
+ },
444
+ publish() {
445
+ },
446
+ runStores(_message, fn, thisArg, ...args) {
447
+ return Reflect.apply(fn, thisArg, args);
448
+ }
449
+ });
450
+ var inertTracingHook = new TracingHook({
451
+ start: inertChannel,
452
+ end: inertChannel,
453
+ asyncStart: inertChannel,
454
+ asyncEnd: inertChannel,
455
+ error: inertChannel
456
+ });
457
+ function isHookChannel(value) {
458
+ if (typeof value !== "object" && typeof value !== "function" || value === null) {
459
+ return false;
460
+ }
461
+ try {
462
+ const channel = value;
463
+ 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";
464
+ } catch {
465
+ return false;
466
+ }
467
+ }
468
+ function hasTracingHookShape(value) {
469
+ if (typeof value !== "object" && typeof value !== "function" || value === null) {
470
+ return false;
471
+ }
472
+ try {
473
+ const hook = value;
474
+ 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);
475
+ } catch {
476
+ return false;
477
+ }
478
+ }
479
+ function hasInvocationHookShape(value) {
480
+ if (typeof value !== "object" && typeof value !== "function" || value === null) {
481
+ return false;
482
+ }
483
+ try {
484
+ const hook = value;
485
+ return value[invocationHookBrand] === GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION && typeof hook.hasInterceptors === "boolean" && typeof hook.intercept === "function" && typeof hook.invoke === "function";
486
+ } catch {
487
+ return false;
488
+ }
489
+ }
490
+ function installInvocationHook(value) {
491
+ const hasInvocationHook = hasInvocationHookShape(value);
492
+ const invocationHook = new InvocationHook();
493
+ try {
494
+ if (!hasInvocationHook) {
495
+ Object.defineProperties(value, {
496
+ [invocationHookBrand]: {
497
+ configurable: false,
498
+ enumerable: false,
499
+ value: GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION,
500
+ writable: false
501
+ },
502
+ hasInterceptors: {
503
+ configurable: false,
504
+ enumerable: false,
505
+ get: () => invocationHook.hasInterceptors
506
+ },
507
+ intercept: {
508
+ configurable: false,
509
+ enumerable: false,
510
+ value: invocationHook.intercept.bind(invocationHook),
511
+ writable: false
512
+ },
513
+ invoke: {
514
+ configurable: false,
515
+ enumerable: false,
516
+ value: invocationHook.invoke.bind(invocationHook),
517
+ writable: false
518
+ }
519
+ });
520
+ }
521
+ if (typeof value.traceInvocation !== "function") {
522
+ Object.defineProperty(value, "traceInvocation", {
523
+ configurable: false,
524
+ enumerable: false,
525
+ value: traceInvocation.bind(void 0, value),
526
+ writable: false
527
+ });
528
+ }
529
+ } catch (error) {
530
+ reportError(error);
531
+ }
532
+ }
533
+ function isCompatibleTracingHook(value) {
534
+ try {
535
+ return value[hookBrand] === GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION && hasTracingHookShape(value);
536
+ } catch {
537
+ return false;
538
+ }
539
+ }
540
+ function isCompatibleHookRegistry(value) {
541
+ try {
542
+ return value instanceof Map && value[registryBrand] === GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION;
543
+ } catch {
544
+ return false;
545
+ }
546
+ }
547
+ function getHookRegistry() {
548
+ let descriptor;
549
+ try {
550
+ descriptor = Object.getOwnPropertyDescriptor(
551
+ globalThis,
552
+ GLOBAL_INSTRUMENTATION_HOOKS_KEY
553
+ );
554
+ } catch (error) {
555
+ reportError(error);
556
+ return void 0;
557
+ }
558
+ if (descriptor && "value" in descriptor && isCompatibleHookRegistry(descriptor.value) && (descriptor.configurable || descriptor.enumerable === false)) {
559
+ if (descriptor.configurable || descriptor.writable) {
560
+ try {
561
+ Object.defineProperty(globalThis, GLOBAL_INSTRUMENTATION_HOOKS_KEY, {
562
+ configurable: false,
563
+ enumerable: false,
564
+ value: descriptor.value,
565
+ writable: false
566
+ });
567
+ } catch (error) {
568
+ reportError(error);
569
+ return void 0;
570
+ }
571
+ }
572
+ return descriptor.value;
573
+ }
574
+ if (descriptor && !descriptor.configurable) {
575
+ reportError(new Error("Incompatible global instrumentation hook registry"));
576
+ return void 0;
577
+ }
578
+ const registry = /* @__PURE__ */ new Map();
579
+ try {
580
+ Object.defineProperty(registry, registryBrand, {
581
+ configurable: false,
582
+ enumerable: false,
583
+ value: GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION,
584
+ writable: false
585
+ });
586
+ } catch (error) {
587
+ reportError(error);
588
+ return void 0;
589
+ }
590
+ try {
591
+ Object.defineProperty(globalThis, GLOBAL_INSTRUMENTATION_HOOKS_KEY, {
592
+ configurable: false,
593
+ enumerable: false,
594
+ value: registry,
595
+ writable: false
596
+ });
597
+ return registry;
598
+ } catch (error) {
599
+ reportError(error);
600
+ return void 0;
601
+ }
602
+ }
603
+ function newGlobalTracingChannel(nameOrChannels) {
604
+ if (typeof nameOrChannels !== "string") {
605
+ return new TracingHook(nameOrChannels);
606
+ }
607
+ const registry = getHookRegistry();
608
+ if (!registry) {
609
+ return inertTracingHook;
610
+ }
611
+ let existing;
612
+ try {
613
+ existing = Map.prototype.get.call(registry, nameOrChannels);
614
+ } catch (error) {
615
+ reportError(error);
616
+ return inertTracingHook;
617
+ }
618
+ if (isCompatibleTracingHook(existing)) {
619
+ installInvocationHook(existing);
620
+ return existing;
621
+ }
622
+ if (existing !== void 0) {
623
+ reportError(
624
+ new Error(`Invalid global instrumentation hook: ${nameOrChannels}`)
625
+ );
626
+ try {
627
+ Map.prototype.delete.call(registry, nameOrChannels);
628
+ } catch (error) {
629
+ reportError(error);
630
+ return inertTracingHook;
631
+ }
632
+ }
633
+ const hook = new TracingHook(nameOrChannels);
634
+ try {
635
+ Map.prototype.set.call(registry, nameOrChannels, hook);
636
+ } catch (error) {
637
+ reportError(error);
638
+ return inertTracingHook;
639
+ }
640
+ return hook;
641
+ }
642
+
643
+ export {
644
+ GLOBAL_INSTRUMENTATION_HOOKS_KEY,
645
+ GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION,
646
+ GLOBAL_INSTRUMENTATION_HOOKS_REGISTRY_BRAND,
647
+ GLOBAL_INSTRUMENTATION_HOOK_BRAND,
648
+ newGlobalTracingChannel
649
+ };