@mastra/observability 0.0.0-main-test-05-11-2025-2-20251106053353 → 0.0.0-netlify-no-bundle-20251127120354

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/dist/index.js CHANGED
@@ -35,18 +35,38 @@ var observabilityInstanceConfigSchema = z.object({
35
35
  serviceName: z.string().min(1, "Service name is required"),
36
36
  sampling: samplingStrategySchema.optional(),
37
37
  exporters: z.array(z.any()).optional(),
38
+ bridge: z.any().optional(),
38
39
  spanOutputProcessors: z.array(z.any()).optional(),
39
40
  includeInternalSpans: z.boolean().optional(),
40
41
  requestContextKeys: z.array(z.string()).optional()
41
- });
42
+ }).refine(
43
+ (data) => {
44
+ const hasExporters = data.exporters && data.exporters.length > 0;
45
+ const hasBridge = !!data.bridge;
46
+ return hasExporters || hasBridge;
47
+ },
48
+ {
49
+ message: "At least one exporter or a bridge is required"
50
+ }
51
+ );
42
52
  var observabilityConfigValueSchema = z.object({
43
53
  serviceName: z.string().min(1, "Service name is required"),
44
54
  sampling: samplingStrategySchema.optional(),
45
55
  exporters: z.array(z.any()).optional(),
56
+ bridge: z.any().optional(),
46
57
  spanOutputProcessors: z.array(z.any()).optional(),
47
58
  includeInternalSpans: z.boolean().optional(),
48
59
  requestContextKeys: z.array(z.string()).optional()
49
- });
60
+ }).refine(
61
+ (data) => {
62
+ const hasExporters = data.exporters && data.exporters.length > 0;
63
+ const hasBridge = !!data.bridge;
64
+ return hasExporters || hasBridge;
65
+ },
66
+ {
67
+ message: "At least one exporter or a bridge is required"
68
+ }
69
+ );
50
70
  var observabilityRegistryConfigSchema = z.object({
51
71
  default: z.object({
52
72
  enabled: z.boolean().optional()
@@ -73,6 +93,18 @@ var observabilityRegistryConfigSchema = z.object({
73
93
  {
74
94
  message: 'A "configSelector" function is required when multiple configs are specified to determine which config to use.'
75
95
  }
96
+ ).refine(
97
+ (data) => {
98
+ if (data.configSelector) {
99
+ const isDefaultEnabled = data.default?.enabled === true;
100
+ const hasConfigs = data.configs && typeof data.configs === "object" && !Array.isArray(data.configs) ? Object.keys(data.configs).length > 0 : false;
101
+ return isDefaultEnabled || hasConfigs;
102
+ }
103
+ return true;
104
+ },
105
+ {
106
+ message: 'A "configSelector" requires at least one config or default observability to be configured.'
107
+ }
76
108
  );
77
109
  var BaseExporter = class {
78
110
  /** Mastra logger instance */
@@ -151,7 +183,7 @@ var CloudExporter = class extends BaseExporter {
151
183
  const accessToken = config.accessToken ?? process.env.MASTRA_CLOUD_ACCESS_TOKEN;
152
184
  if (!accessToken) {
153
185
  this.setDisabled(
154
- "MASTRA_CLOUD_ACCESS_TOKEN environment variable not set. \u{1F680} Sign up for Mastra Cloud at https://cloud.mastra.ai to see your traces online and obtain your access token."
186
+ "MASTRA_CLOUD_ACCESS_TOKEN environment variable not set.\n\u{1F680} Sign up at https://cloud.mastra.ai to see your AI traces online and obtain your access token."
155
187
  );
156
188
  }
157
189
  const endpoint = config.endpoint ?? process.env.MASTRA_CLOUD_TRACES_ENDPOINT ?? "https://api.mastra.ai/ai/spans/publish";
@@ -893,6 +925,27 @@ var DefaultExporter = class extends BaseExporter {
893
925
  this.logger.info("DefaultExporter shutdown complete");
894
926
  }
895
927
  };
928
+
929
+ // src/exporters/test.ts
930
+ var TestExporter = class extends BaseExporter {
931
+ name = "tracing-test-exporter";
932
+ #events = [];
933
+ constructor(config = {}) {
934
+ super(config);
935
+ }
936
+ async _exportTracingEvent(event) {
937
+ this.#events.push(event);
938
+ }
939
+ clearEvents() {
940
+ this.#events = [];
941
+ }
942
+ get events() {
943
+ return this.#events;
944
+ }
945
+ async shutdown() {
946
+ this.logger.info("TestExporter shutdown");
947
+ }
948
+ };
896
949
  var ModelSpanTracker = class {
897
950
  #modelSpan;
898
951
  #currentStepSpan;
@@ -1224,6 +1277,16 @@ function isSpanInternal(spanType, flags) {
1224
1277
  return false;
1225
1278
  }
1226
1279
  }
1280
+ function getExternalParentId(options) {
1281
+ if (!options.parent) {
1282
+ return void 0;
1283
+ }
1284
+ if (options.parent.isInternal) {
1285
+ return options.parent.getParentSpanId(false);
1286
+ } else {
1287
+ return options.parent.id;
1288
+ }
1289
+ }
1227
1290
  var BaseSpan = class {
1228
1291
  name;
1229
1292
  type;
@@ -1320,6 +1383,28 @@ var BaseSpan = class {
1320
1383
  get externalTraceId() {
1321
1384
  return this.isValid ? this.traceId : void 0;
1322
1385
  }
1386
+ /**
1387
+ * Execute an async function within this span's tracing context.
1388
+ * Delegates to the bridge if available.
1389
+ */
1390
+ async executeInContext(fn) {
1391
+ const bridge = this.observabilityInstance.getBridge();
1392
+ if (bridge?.executeInContext) {
1393
+ return bridge.executeInContext(this.id, fn);
1394
+ }
1395
+ return fn();
1396
+ }
1397
+ /**
1398
+ * Execute a synchronous function within this span's tracing context.
1399
+ * Delegates to the bridge if available.
1400
+ */
1401
+ executeInContextSync(fn) {
1402
+ const bridge = this.observabilityInstance.getBridge();
1403
+ if (bridge?.executeInContextSync) {
1404
+ return bridge.executeInContextSync(this.id, fn);
1405
+ }
1406
+ return fn();
1407
+ }
1323
1408
  };
1324
1409
  var DEFAULT_KEYS_TO_STRIP = /* @__PURE__ */ new Set([
1325
1410
  "logger",
@@ -1366,27 +1451,30 @@ var DefaultSpan = class extends BaseSpan {
1366
1451
  traceId;
1367
1452
  constructor(options, observabilityInstance) {
1368
1453
  super(options, observabilityInstance);
1369
- this.id = generateSpanId();
1454
+ const bridge = observabilityInstance.getBridge();
1455
+ if (bridge && !this.isInternal) {
1456
+ const bridgeIds = bridge.createSpan(options);
1457
+ if (bridgeIds) {
1458
+ this.id = bridgeIds.spanId;
1459
+ this.traceId = bridgeIds.traceId;
1460
+ this.parentSpanId = bridgeIds.parentSpanId;
1461
+ return;
1462
+ }
1463
+ }
1370
1464
  if (options.parent) {
1371
1465
  this.traceId = options.parent.traceId;
1372
- } else if (options.traceId) {
1373
- if (isValidTraceId(options.traceId)) {
1374
- this.traceId = options.traceId;
1375
- } else {
1376
- console.error(
1377
- `[Mastra Tracing] Invalid traceId: must be 1-32 hexadecimal characters, got "${options.traceId}". Generating new trace ID.`
1378
- );
1379
- this.traceId = generateTraceId();
1380
- }
1381
- } else {
1382
- this.traceId = generateTraceId();
1466
+ this.parentSpanId = options.parent.id;
1467
+ this.id = generateSpanId();
1468
+ return;
1383
1469
  }
1384
- if (!options.parent && options.parentSpanId) {
1470
+ this.traceId = getOrCreateTraceId(options);
1471
+ this.id = generateSpanId();
1472
+ if (options.parentSpanId) {
1385
1473
  if (isValidSpanId(options.parentSpanId)) {
1386
1474
  this.parentSpanId = options.parentSpanId;
1387
1475
  } else {
1388
1476
  console.error(
1389
- `[Mastra Tracing] Invalid parentSpanId: must be 1-16 hexadecimal characters, got "${options.parentSpanId}". Ignoring parent span ID.`
1477
+ `[Mastra Tracing] Invalid parentSpanId: must be 1-16 hexadecimal characters, got "${options.parentSpanId}". Ignoring.`
1390
1478
  );
1391
1479
  }
1392
1480
  }
@@ -1491,6 +1579,18 @@ function isValidTraceId(traceId) {
1491
1579
  function isValidSpanId(spanId) {
1492
1580
  return /^[0-9a-f]{1,16}$/i.test(spanId);
1493
1581
  }
1582
+ function getOrCreateTraceId(options) {
1583
+ if (options.traceId) {
1584
+ if (isValidTraceId(options.traceId)) {
1585
+ return options.traceId;
1586
+ } else {
1587
+ console.error(
1588
+ `[Mastra Tracing] Invalid traceId: must be 1-32 hexadecimal characters, got "${options.traceId}". Generating new trace ID.`
1589
+ );
1590
+ }
1591
+ }
1592
+ return generateTraceId();
1593
+ }
1494
1594
 
1495
1595
  // src/spans/no-op.ts
1496
1596
  var NoOpSpan = class extends BaseSpan {
@@ -1523,13 +1623,17 @@ var BaseObservabilityInstance = class extends MastraBase {
1523
1623
  sampling: config.sampling ?? { type: "always" /* ALWAYS */ },
1524
1624
  exporters: config.exporters ?? [],
1525
1625
  spanOutputProcessors: config.spanOutputProcessors ?? [],
1626
+ bridge: config.bridge ?? void 0,
1526
1627
  includeInternalSpans: config.includeInternalSpans ?? false,
1527
1628
  requestContextKeys: config.requestContextKeys ?? []
1528
1629
  };
1630
+ if (this.config.bridge?.init) {
1631
+ this.config.bridge.init({ config: this.config });
1632
+ }
1529
1633
  }
1530
1634
  /**
1531
1635
  * Override setLogger to add Observability specific initialization log
1532
- * and propagate logger to exporters
1636
+ * and propagate logger to exporters and bridge
1533
1637
  */
1534
1638
  __setLogger(logger) {
1535
1639
  super.__setLogger(logger);
@@ -1538,8 +1642,11 @@ var BaseObservabilityInstance = class extends MastraBase {
1538
1642
  exporter.__setLogger(logger);
1539
1643
  }
1540
1644
  });
1645
+ if (this.config.bridge?.__setLogger) {
1646
+ this.config.bridge.__setLogger(logger);
1647
+ }
1541
1648
  this.logger.debug(
1542
- `[Observability] Initialized [service=${this.config.serviceName}] [instance=${this.config.name}] [sampling=${this.config.sampling.type}]`
1649
+ `[Observability] Initialized [service=${this.config.serviceName}] [instance=${this.config.name}] [sampling=${this.config.sampling?.type}] [bridge=${!!this.config.bridge}]`
1543
1650
  );
1544
1651
  }
1545
1652
  // ============================================================================
@@ -1606,6 +1713,12 @@ var BaseObservabilityInstance = class extends MastraBase {
1606
1713
  getSpanOutputProcessors() {
1607
1714
  return [...this.spanOutputProcessors];
1608
1715
  }
1716
+ /**
1717
+ * Get the bridge instance if configured
1718
+ */
1719
+ getBridge() {
1720
+ return this.config.bridge;
1721
+ }
1609
1722
  /**
1610
1723
  * Get the logger instance (for exporters and other components)
1611
1724
  */
@@ -1650,7 +1763,9 @@ var BaseObservabilityInstance = class extends MastraBase {
1650
1763
  */
1651
1764
  shouldSample(options) {
1652
1765
  const { sampling } = this.config;
1653
- switch (sampling.type) {
1766
+ switch (sampling?.type) {
1767
+ case void 0:
1768
+ return true;
1654
1769
  case "always" /* ALWAYS */:
1655
1770
  return true;
1656
1771
  case "never" /* NEVER */:
@@ -1782,17 +1897,21 @@ var BaseObservabilityInstance = class extends MastraBase {
1782
1897
  }
1783
1898
  }
1784
1899
  /**
1785
- * Export tracing event through all exporters (realtime mode)
1900
+ * Export tracing event through all exporters and bridge (realtime mode)
1786
1901
  */
1787
1902
  async exportTracingEvent(event) {
1788
- const exportPromises = this.exporters.map(async (exporter) => {
1903
+ const targets = [
1904
+ ...this.exporters
1905
+ ];
1906
+ if (this.config.bridge) {
1907
+ targets.push(this.config.bridge);
1908
+ }
1909
+ const exportPromises = targets.map(async (target) => {
1789
1910
  try {
1790
- if (exporter.exportTracingEvent) {
1791
- await exporter.exportTracingEvent(event);
1792
- this.logger.debug(`[Observability] Event exported [exporter=${exporter.name}] [type=${event.type}]`);
1793
- }
1911
+ await target.exportTracingEvent(event);
1912
+ this.logger.debug(`[Observability] Event exported [target=${target.name}] [type=${event.type}]`);
1794
1913
  } catch (error) {
1795
- this.logger.error(`[Observability] Export error [exporter=${exporter.name}]`, error);
1914
+ this.logger.error(`[Observability] Export error [target=${target.name}]`, error);
1796
1915
  }
1797
1916
  });
1798
1917
  await Promise.allSettled(exportPromises);
@@ -1816,6 +1935,9 @@ var BaseObservabilityInstance = class extends MastraBase {
1816
1935
  ...this.exporters.map((e) => e.shutdown()),
1817
1936
  ...this.spanOutputProcessors.map((p) => p.shutdown())
1818
1937
  ];
1938
+ if (this.config.bridge) {
1939
+ shutdownPromises.push(this.config.bridge.shutdown());
1940
+ }
1819
1941
  await Promise.allSettled(shutdownPromises);
1820
1942
  this.logger.info(`[Observability] Shutdown completed [name=${this.name}]`);
1821
1943
  }
@@ -2169,6 +2291,6 @@ var Observability = class extends MastraBase {
2169
2291
  }
2170
2292
  };
2171
2293
 
2172
- export { BaseExporter, BaseObservabilityInstance, BaseSpan, CloudExporter, ConsoleExporter, DefaultExporter, DefaultObservabilityInstance, DefaultSpan, ModelSpanTracker, NoOpSpan, Observability, SamplingStrategyType, SensitiveDataFilter, deepClean, observabilityConfigValueSchema, observabilityInstanceConfigSchema, observabilityRegistryConfigSchema, samplingStrategySchema };
2294
+ export { BaseExporter, BaseObservabilityInstance, BaseSpan, CloudExporter, ConsoleExporter, DefaultExporter, DefaultObservabilityInstance, DefaultSpan, ModelSpanTracker, NoOpSpan, Observability, SamplingStrategyType, SensitiveDataFilter, TestExporter, deepClean, getExternalParentId, observabilityConfigValueSchema, observabilityInstanceConfigSchema, observabilityRegistryConfigSchema, samplingStrategySchema };
2173
2295
  //# sourceMappingURL=index.js.map
2174
2296
  //# sourceMappingURL=index.js.map