bitfab 0.52.5 → 0.52.6

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 (36) hide show
  1. package/dist/autoTrace.cjs +84 -34
  2. package/dist/autoTrace.cjs.map +1 -1
  3. package/dist/autoTrace.d.cts +19 -4
  4. package/dist/autoTrace.d.ts +19 -4
  5. package/dist/autoTrace.js +9 -3
  6. package/dist/{chunk-3QXNM62Y.js → chunk-5KVK733P.js} +3 -3
  7. package/dist/{chunk-6DRFQJ75.js → chunk-DRTHTSHO.js} +2 -2
  8. package/dist/{chunk-ZUD7OFYB.js → chunk-E5BLYL5X.js} +81 -34
  9. package/dist/chunk-E5BLYL5X.js.map +1 -0
  10. package/dist/{chunk-3D7FNJ5R.js → chunk-GJKU7VWG.js} +2 -2
  11. package/dist/{chunk-3D7FNJ5R.js.map → chunk-GJKU7VWG.js.map} +1 -1
  12. package/dist/{chunk-CAE7HE4C.js → chunk-NXC6NYIY.js} +2 -2
  13. package/dist/{chunk-CAE7HE4C.js.map → chunk-NXC6NYIY.js.map} +1 -1
  14. package/dist/{chunk-EMRHO6YW.js → chunk-P26KVTI5.js} +136 -31
  15. package/dist/chunk-P26KVTI5.js.map +1 -0
  16. package/dist/{http-UQT6QVBC.js → http-ER2NMEKI.js} +2 -2
  17. package/dist/{http-Q6UEKZRF.js → http-MGES3ZVX.js} +2 -2
  18. package/dist/index.cjs +182 -48
  19. package/dist/index.cjs.map +1 -1
  20. package/dist/index.d.cts +1 -1
  21. package/dist/index.d.ts +1 -1
  22. package/dist/index.js +4 -4
  23. package/dist/node.cjs +182 -48
  24. package/dist/node.cjs.map +1 -1
  25. package/dist/node.js +4 -4
  26. package/dist/{replay-UVSZFZW7.js → replay-5XES2LBL.js} +3 -3
  27. package/dist/replayCli.js +2 -2
  28. package/dist/seedCli.js +2 -2
  29. package/package.json +1 -1
  30. package/dist/chunk-EMRHO6YW.js.map +0 -1
  31. package/dist/chunk-ZUD7OFYB.js.map +0 -1
  32. /package/dist/{chunk-3QXNM62Y.js.map → chunk-5KVK733P.js.map} +0 -0
  33. /package/dist/{chunk-6DRFQJ75.js.map → chunk-DRTHTSHO.js.map} +0 -0
  34. /package/dist/{http-Q6UEKZRF.js.map → http-ER2NMEKI.js.map} +0 -0
  35. /package/dist/{http-UQT6QVBC.js.map → http-MGES3ZVX.js.map} +0 -0
  36. /package/dist/{replay-UVSZFZW7.js.map → replay-5XES2LBL.js.map} +0 -0
@@ -6,7 +6,7 @@ import {
6
6
  flushTraces,
7
7
  parseRetryAfterMs,
8
8
  serializePayloadBody
9
- } from "./chunk-3D7FNJ5R.js";
9
+ } from "./chunk-GJKU7VWG.js";
10
10
  export {
11
11
  BitfabError,
12
12
  HttpClient,
@@ -16,4 +16,4 @@ export {
16
16
  parseRetryAfterMs,
17
17
  serializePayloadBody
18
18
  };
19
- //# sourceMappingURL=http-UQT6QVBC.js.map
19
+ //# sourceMappingURL=http-ER2NMEKI.js.map
@@ -6,7 +6,7 @@ import {
6
6
  flushTraces,
7
7
  parseRetryAfterMs,
8
8
  serializePayloadBody
9
- } from "./chunk-CAE7HE4C.js";
9
+ } from "./chunk-NXC6NYIY.js";
10
10
  import "./chunk-H6LZRFMN.js";
11
11
  export {
12
12
  BitfabError,
@@ -17,4 +17,4 @@ export {
17
17
  parseRetryAfterMs,
18
18
  serializePayloadBody
19
19
  };
20
- //# sourceMappingURL=http-Q6UEKZRF.js.map
20
+ //# sourceMappingURL=http-MGES3ZVX.js.map
package/dist/index.cjs CHANGED
@@ -51,7 +51,7 @@ var __version__, __packageName__;
51
51
  var init_version_generated = __esm({
52
52
  "src/version.generated.ts"() {
53
53
  "use strict";
54
- __version__ = "0.52.5";
54
+ __version__ = "0.52.6";
55
55
  __packageName__ = "bitfab";
56
56
  }
57
57
  });
@@ -4207,19 +4207,18 @@ init_asyncStorage();
4207
4207
  // src/autoTrace.ts
4208
4208
  init_asyncStorage();
4209
4209
  var autoTraceGlobal = globalThis;
4210
- var autoTraceState = autoTraceGlobal.__bitfabAutoTraceStateV3 ?? {
4210
+ var autoTraceState = autoTraceGlobal.__bitfabAutoTraceStateV4 ?? {
4211
4211
  storage: null,
4212
4212
  browserScope: void 0,
4213
4213
  capturePolicies: /* @__PURE__ */ new WeakMap(),
4214
4214
  activeRoots: 0
4215
4215
  };
4216
- autoTraceGlobal.__bitfabAutoTraceStateV3 = autoTraceState;
4216
+ autoTraceGlobal.__bitfabAutoTraceStateV4 = autoTraceState;
4217
4217
  function initializeAutoTraceStorage() {
4218
4218
  autoTraceState.storage ?? (autoTraceState.storage = createAsyncLocalStorage());
4219
4219
  }
4220
- function runWithAutoTraceContext(context, fn, depth = 0) {
4220
+ function runWithAutoTraceScope(scope, fn) {
4221
4221
  initializeAutoTraceStorage();
4222
- const scope = { context, depth };
4223
4222
  if (autoTraceState.storage) {
4224
4223
  return autoTraceState.storage.run(scope, fn);
4225
4224
  }
@@ -4231,41 +4230,65 @@ function runWithAutoTraceContext(context, fn, depth = 0) {
4231
4230
  autoTraceState.browserScope = previous;
4232
4231
  }
4233
4232
  }
4233
+ function currentAutoTraceEntries() {
4234
+ return currentAutoTraceScope()?.entries ?? [];
4235
+ }
4236
+ function replaceEntry(entries, previous, updated) {
4237
+ return entries.map((entry) => entry === previous ? updated : entry);
4238
+ }
4239
+ function runWithAutoTraceEntry(previous, updated, fn) {
4240
+ const result = runWithAutoTraceScope(
4241
+ { entries: replaceEntry(currentAutoTraceEntries(), previous, updated) },
4242
+ fn
4243
+ );
4244
+ if (isAutoTraceAsyncGenerator(result)) {
4245
+ return wrapAutoTraceEntryAsyncGenerator(previous, updated, result);
4246
+ }
4247
+ return result;
4248
+ }
4249
+ function wrapAutoTraceEntryAsyncGenerator(previous, updated, source) {
4250
+ const step = (method, value) => runWithAutoTraceScope(
4251
+ { entries: replaceEntry(currentAutoTraceEntries(), previous, updated) },
4252
+ () => source[method](value)
4253
+ );
4254
+ const wrapped = {
4255
+ next: (value) => step("next", value),
4256
+ return: (value) => step("return", value),
4257
+ throw: (error) => step("throw", error),
4258
+ [Symbol.asyncIterator]: () => wrapped
4259
+ };
4260
+ return wrapped;
4261
+ }
4262
+ function clearAutoTraceNodeConfiguration(fn) {
4263
+ return runWithAutoTraceScope({ entries: currentAutoTraceEntries() }, fn);
4264
+ }
4234
4265
  function runWithAutoTraceNodeConfiguration(nodeConfiguration, fn) {
4235
4266
  const scope = currentAutoTraceScope();
4236
- if (!scope) {
4267
+ if (!scope || scope.entries.length === 0) {
4237
4268
  return fn();
4238
4269
  }
4239
- const configuredScope = { ...scope, nodeConfiguration };
4240
- let result;
4241
- if (autoTraceState.storage) {
4242
- result = autoTraceState.storage.run(configuredScope, fn);
4243
- } else {
4244
- const previous = autoTraceState.browserScope;
4245
- autoTraceState.browserScope = configuredScope;
4246
- try {
4247
- result = fn();
4248
- } finally {
4249
- autoTraceState.browserScope = previous;
4250
- }
4251
- }
4270
+ const result = runWithAutoTraceScope({ ...scope, nodeConfiguration }, fn);
4252
4271
  if (isAutoTraceAsyncGenerator(result)) {
4253
4272
  return wrapAutoTraceNodeAsyncGenerator(nodeConfiguration, result);
4254
4273
  }
4255
4274
  return result;
4256
4275
  }
4257
- function runWithAutoTraceRootContext(context, fn) {
4276
+ function runWithAutoTraceRootContext(context, spanStack, fn) {
4258
4277
  autoTraceState.activeRoots += 1;
4278
+ const entries = [
4279
+ ...currentAutoTraceEntries(),
4280
+ { context, depth: 0, spanStack }
4281
+ ];
4259
4282
  let result;
4260
4283
  try {
4261
- result = runWithAutoTraceContext(context, fn);
4284
+ result = runWithAutoTraceScope({ entries }, fn);
4262
4285
  } catch (error) {
4263
4286
  autoTraceState.activeRoots -= 1;
4264
4287
  throw error;
4265
4288
  }
4266
4289
  if (isAutoTraceAsyncGenerator(result)) {
4267
4290
  autoTraceState.activeRoots -= 1;
4268
- return wrapAutoTraceAsyncGenerator(context, result);
4291
+ return wrapAutoTraceAsyncGenerator(context, spanStack, result);
4269
4292
  }
4270
4293
  if (result instanceof Promise) {
4271
4294
  return result.finally(() => {
@@ -4275,6 +4298,15 @@ function runWithAutoTraceRootContext(context, fn) {
4275
4298
  autoTraceState.activeRoots -= 1;
4276
4299
  return result;
4277
4300
  }
4301
+ function invokeThroughAutoTraceEntries(entries, step, fn) {
4302
+ let run = fn;
4303
+ for (let index = entries.length - 1; index >= 0; index -= 1) {
4304
+ const entry = entries[index];
4305
+ const next = run;
4306
+ run = () => step(entry, next);
4307
+ }
4308
+ return run();
4309
+ }
4278
4310
  function isAutoTraceAsyncGenerator(value) {
4279
4311
  if (value === null || typeof value !== "object") {
4280
4312
  return false;
@@ -4282,8 +4314,8 @@ function isAutoTraceAsyncGenerator(value) {
4282
4314
  const candidate = value;
4283
4315
  return typeof candidate.next === "function" && typeof candidate.return === "function" && typeof candidate.throw === "function" && typeof candidate[Symbol.asyncIterator] === "function";
4284
4316
  }
4285
- function wrapAutoTraceAsyncGenerator(context, source) {
4286
- const step = (method, value) => runWithAutoTraceRootContext(context, () => source[method](value));
4317
+ function wrapAutoTraceAsyncGenerator(context, spanStack, source) {
4318
+ const step = (method, value) => runWithAutoTraceRootContext(context, spanStack, () => source[method](value));
4287
4319
  const wrapped = {
4288
4320
  next: (value) => step("next", value),
4289
4321
  return: (value) => step("return", value),
@@ -4309,7 +4341,7 @@ function __bitfabAutoTraceActive() {
4309
4341
  if (autoTraceState.activeRoots === 0) {
4310
4342
  return false;
4311
4343
  }
4312
- return currentAutoTraceScope() !== void 0;
4344
+ return currentAutoTraceEntries().length > 0;
4313
4345
  }
4314
4346
  function currentAutoTraceScope() {
4315
4347
  initializeAutoTraceStorage();
@@ -7062,11 +7094,15 @@ var Bitfab = class {
7062
7094
  type: options.type ?? "custom",
7063
7095
  surface: "opt-out"
7064
7096
  };
7065
- const tracedRoot = this.withSpan(
7097
+ const rootName = options.name ?? name;
7098
+ const buildTracedRoot = (spanOptions) => this.withSpan(
7066
7099
  traceFunctionKey,
7067
- rootOptions,
7100
+ spanOptions,
7068
7101
  function(...args) {
7069
- const capturePolicy = getAutoTraceCapturePolicy(self, traceFunctionKey);
7102
+ const capturePolicy = getAutoTraceCapturePolicy(
7103
+ self,
7104
+ traceFunctionKey
7105
+ );
7070
7106
  self.refreshAutoTraceCapturePolicy(traceFunctionKey);
7071
7107
  let spansUsed = 0;
7072
7108
  let truncated = false;
@@ -7087,19 +7123,54 @@ var Bitfab = class {
7087
7123
  `"${traceFunctionKey}" hit an automatic subtree capture limit (maxDepth=${maxDepth}, maxSpans=${maxSpans}); its trace is incomplete. Raise the limits or narrow the subtree with exclude.`
7088
7124
  );
7089
7125
  };
7126
+ const spanStackOf = (entry) => entry.spanStack;
7127
+ const openChild = (entry, childOptions, inputs, isAsync, invokeFn) => {
7128
+ const runInside = () => runWithAutoTraceEntry(
7129
+ entry,
7130
+ {
7131
+ context: autoTraceContext,
7132
+ depth: entry.depth + 1,
7133
+ spanStack: getSpanStack()
7134
+ },
7135
+ invokeFn
7136
+ );
7137
+ return runWithSpanStack(spanStackOf(entry), () => {
7138
+ if (isAsync) {
7139
+ const tracedAsyncChild = self.withSpan(
7140
+ traceFunctionKey,
7141
+ childOptions,
7142
+ async (..._inputs) => await runInside()
7143
+ );
7144
+ return tracedAsyncChild(...inputs);
7145
+ }
7146
+ const tracedChild = self.withSpan(
7147
+ traceFunctionKey,
7148
+ childOptions,
7149
+ (..._inputs) => runInside()
7150
+ );
7151
+ return tracedChild(...inputs);
7152
+ });
7153
+ };
7154
+ const overBudget = (entry) => {
7155
+ if (entry.depth < maxDepth && spansUsed < maxSpans) {
7156
+ return false;
7157
+ }
7158
+ runWithSpanStack(spanStackOf(entry), warnTruncated);
7159
+ return true;
7160
+ };
7090
7161
  const autoTraceContext = {
7091
- invoke(definition, inputs, invokeFn, depth, nodeConfiguration) {
7162
+ traceFunctionKey,
7163
+ invoke(definition, inputs, invokeFn, entry, nodeConfiguration) {
7092
7164
  const nameParts = definition.name.split(".");
7093
7165
  const simpleName = nameParts[nameParts.length - 1];
7094
- const invokeWithoutNode = () => nodeConfiguration === void 0 ? invokeFn() : runWithAutoTraceContext(autoTraceContext, invokeFn, depth);
7166
+ const invokeWithoutNode = () => nodeConfiguration === void 0 ? invokeFn() : clearAutoTraceNodeConfiguration(invokeFn);
7095
7167
  if (excluded.has(definition.name) || simpleName !== void 0 && excluded.has(simpleName) || nodeConfiguration === void 0 && definition.wrapper === true && !includeWrappers) {
7096
7168
  return invokeWithoutNode();
7097
7169
  }
7098
7170
  if (nodeConfiguration?.capture === false) {
7099
- return runWithAutoTraceContext(autoTraceContext, invokeFn, depth);
7171
+ return clearAutoTraceNodeConfiguration(invokeFn);
7100
7172
  }
7101
- if (depth >= maxDepth || spansUsed >= maxSpans) {
7102
- warnTruncated();
7173
+ if (overBudget(entry)) {
7103
7174
  return invokeWithoutNode();
7104
7175
  }
7105
7176
  spansUsed += 1;
@@ -7122,34 +7193,81 @@ var Bitfab = class {
7122
7193
  finalize: nodeConfiguration.finalize
7123
7194
  }
7124
7195
  };
7125
- const invokeWithAutoTraceContext = () => runWithAutoTraceContext(autoTraceContext, invokeFn, depth + 1);
7126
- if (definition.async === true) {
7127
- const tracedAsyncChild = self.withSpan(
7128
- traceFunctionKey,
7129
- childOptions,
7130
- async (..._inputs) => await invokeWithAutoTraceContext()
7131
- );
7132
- return tracedAsyncChild(...inputs);
7133
- }
7134
- const tracedChild = self.withSpan(
7135
- traceFunctionKey,
7196
+ return openChild(
7197
+ entry,
7136
7198
  childOptions,
7137
- (..._inputs) => invokeWithAutoTraceContext()
7199
+ inputs,
7200
+ definition.async === true,
7201
+ invokeFn
7138
7202
  );
7139
- return tracedChild(...inputs);
7203
+ },
7204
+ invokeNestedRoot(nestedName, nestedFunctionName, inputs, invokeFn, entry, link) {
7205
+ if (excluded.has(nestedName) || excluded.has(nestedFunctionName) || overBudget(entry)) {
7206
+ return invokeFn();
7207
+ }
7208
+ spansUsed += 1;
7209
+ const childOptions = {
7210
+ name: nestedName,
7211
+ type: "function",
7212
+ captureWhen: "nested",
7213
+ surface: "opt-out",
7214
+ captureContent: true,
7215
+ ...link !== void 0 && { nestedTrace: link }
7216
+ };
7217
+ return openChild(entry, childOptions, inputs, false, invokeFn);
7140
7218
  }
7141
7219
  };
7142
7220
  return runWithAutoTraceRootContext(
7143
7221
  autoTraceContext,
7222
+ getSpanStack(),
7144
7223
  () => fn.apply(this, args)
7145
7224
  );
7146
7225
  }
7147
7226
  );
7227
+ const tracedRoot = buildTracedRoot(rootOptions);
7148
7228
  const autoTraceRoot = function(...args) {
7149
7229
  if (!self.shouldRecord()) {
7150
7230
  return fn.apply(this, args);
7151
7231
  }
7152
- return tracedRoot.apply(this, args);
7232
+ const enclosing = currentAutoTraceEntries();
7233
+ if (enclosing.length === 0) {
7234
+ return tracedRoot.apply(this, args);
7235
+ }
7236
+ const link = getReplayContext() || inSeedScope() ? void 0 : { traceFunctionKey };
7237
+ return invokeThroughAutoTraceEntries(
7238
+ enclosing,
7239
+ (entry, next) => entry.context.invokeNestedRoot(
7240
+ rootName,
7241
+ name,
7242
+ args,
7243
+ next,
7244
+ entry,
7245
+ link
7246
+ ),
7247
+ () => {
7248
+ if (link === void 0) {
7249
+ return fn.apply(this, args);
7250
+ }
7251
+ const innermost = enclosing[enclosing.length - 1];
7252
+ const innermostNow = currentAutoTraceEntries().find(
7253
+ (entry) => entry.context === innermost.context
7254
+ ) ?? innermost;
7255
+ const innermostStack = innermostNow.spanStack;
7256
+ const copy = innermostStack[innermostStack.length - 1];
7257
+ const detachedRoot = buildTracedRoot({
7258
+ ...rootOptions,
7259
+ ...copy !== void 0 && {
7260
+ enclosingTrace: {
7261
+ traceId: copy.traceId,
7262
+ spanId: copy.spanId,
7263
+ traceFunctionKey: innermost.context.traceFunctionKey,
7264
+ link
7265
+ }
7266
+ }
7267
+ });
7268
+ return runWithSpanStack([], () => detachedRoot.apply(this, args));
7269
+ }
7270
+ );
7153
7271
  };
7154
7272
  Object.defineProperty(autoTraceRoot, "_bitfabTraceFunctionKey", {
7155
7273
  value: traceFunctionKey
@@ -7738,6 +7856,10 @@ var Bitfab = class {
7738
7856
  const spanId = randomUuid();
7739
7857
  const parentSpanId = parentContext?.spanId ?? null;
7740
7858
  const isRootSpan = parentSpanId === null;
7859
+ if (options.enclosingTrace !== void 0) {
7860
+ options.enclosingTrace.link.traceId = traceId;
7861
+ options.enclosingTrace.link.rootSpanId = spanId;
7862
+ }
7741
7863
  const requestedSurface = options.surface ?? DEFAULT_SURFACE;
7742
7864
  const surface = resolveSurface(requestedSurface, parentContext?.surface);
7743
7865
  assertSurfacesCompatible(
@@ -7787,7 +7909,9 @@ var Bitfab = class {
7787
7909
  spanType: options.type ?? "custom",
7788
7910
  functionId: options.functionId,
7789
7911
  captureContent: options.captureContent ?? true,
7790
- autoTraceDefinition: options.autoTraceDefinition
7912
+ autoTraceDefinition: options.autoTraceDefinition,
7913
+ nestedTrace: options.nestedTrace,
7914
+ enclosingTrace: options.enclosingTrace
7791
7915
  };
7792
7916
  const sendSpan = async (params) => {
7793
7917
  const replayCtx = getReplayContext();
@@ -8297,6 +8421,16 @@ var Bitfab = class {
8297
8421
  function_line: params.autoTraceDefinition.line,
8298
8422
  function_column: params.autoTraceDefinition.column
8299
8423
  },
8424
+ ...params.nestedTrace?.traceId !== void 0 && {
8425
+ nested_trace_id: params.nestedTrace.traceId,
8426
+ nested_trace_function_key: params.nestedTrace.traceFunctionKey,
8427
+ nested_root_span_id: params.nestedTrace.rootSpanId
8428
+ },
8429
+ ...params.enclosingTrace !== void 0 && {
8430
+ enclosing_trace_id: params.enclosingTrace.traceId,
8431
+ enclosing_trace_function_key: params.enclosingTrace.traceFunctionKey,
8432
+ enclosing_span_id: params.enclosingTrace.spanId
8433
+ },
8300
8434
  ...serializedInputs !== void 0 && {
8301
8435
  input: serializedInputs.json,
8302
8436
  ...serializedInputs.meta !== void 0 && {