@mastra/observability 0.0.0-main-test-05-11-2025-2-20251106053353 → 0.0.0-main-test-2-20251127210604
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/CHANGELOG.md +71 -3
- package/dist/config.d.ts +107 -5
- package/dist/config.d.ts.map +1 -1
- package/dist/exporters/index.d.ts +1 -0
- package/dist/exporters/index.d.ts.map +1 -1
- package/dist/exporters/test.d.ts +13 -0
- package/dist/exporters/test.d.ts.map +1 -0
- package/dist/index.cjs +151 -27
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +150 -28
- package/dist/index.js.map +1 -1
- package/dist/instances/base.d.ts +9 -5
- package/dist/instances/base.d.ts.map +1 -1
- package/dist/spans/base.d.ts +38 -0
- package/dist/spans/base.d.ts.map +1 -1
- package/dist/spans/default.d.ts.map +1 -1
- package/package.json +11 -9
package/dist/index.cjs
CHANGED
|
@@ -37,18 +37,38 @@ var observabilityInstanceConfigSchema = zod.z.object({
|
|
|
37
37
|
serviceName: zod.z.string().min(1, "Service name is required"),
|
|
38
38
|
sampling: samplingStrategySchema.optional(),
|
|
39
39
|
exporters: zod.z.array(zod.z.any()).optional(),
|
|
40
|
+
bridge: zod.z.any().optional(),
|
|
40
41
|
spanOutputProcessors: zod.z.array(zod.z.any()).optional(),
|
|
41
42
|
includeInternalSpans: zod.z.boolean().optional(),
|
|
42
43
|
requestContextKeys: zod.z.array(zod.z.string()).optional()
|
|
43
|
-
})
|
|
44
|
+
}).refine(
|
|
45
|
+
(data) => {
|
|
46
|
+
const hasExporters = data.exporters && data.exporters.length > 0;
|
|
47
|
+
const hasBridge = !!data.bridge;
|
|
48
|
+
return hasExporters || hasBridge;
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
message: "At least one exporter or a bridge is required"
|
|
52
|
+
}
|
|
53
|
+
);
|
|
44
54
|
var observabilityConfigValueSchema = zod.z.object({
|
|
45
55
|
serviceName: zod.z.string().min(1, "Service name is required"),
|
|
46
56
|
sampling: samplingStrategySchema.optional(),
|
|
47
57
|
exporters: zod.z.array(zod.z.any()).optional(),
|
|
58
|
+
bridge: zod.z.any().optional(),
|
|
48
59
|
spanOutputProcessors: zod.z.array(zod.z.any()).optional(),
|
|
49
60
|
includeInternalSpans: zod.z.boolean().optional(),
|
|
50
61
|
requestContextKeys: zod.z.array(zod.z.string()).optional()
|
|
51
|
-
})
|
|
62
|
+
}).refine(
|
|
63
|
+
(data) => {
|
|
64
|
+
const hasExporters = data.exporters && data.exporters.length > 0;
|
|
65
|
+
const hasBridge = !!data.bridge;
|
|
66
|
+
return hasExporters || hasBridge;
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
message: "At least one exporter or a bridge is required"
|
|
70
|
+
}
|
|
71
|
+
);
|
|
52
72
|
var observabilityRegistryConfigSchema = zod.z.object({
|
|
53
73
|
default: zod.z.object({
|
|
54
74
|
enabled: zod.z.boolean().optional()
|
|
@@ -75,6 +95,18 @@ var observabilityRegistryConfigSchema = zod.z.object({
|
|
|
75
95
|
{
|
|
76
96
|
message: 'A "configSelector" function is required when multiple configs are specified to determine which config to use.'
|
|
77
97
|
}
|
|
98
|
+
).refine(
|
|
99
|
+
(data) => {
|
|
100
|
+
if (data.configSelector) {
|
|
101
|
+
const isDefaultEnabled = data.default?.enabled === true;
|
|
102
|
+
const hasConfigs = data.configs && typeof data.configs === "object" && !Array.isArray(data.configs) ? Object.keys(data.configs).length > 0 : false;
|
|
103
|
+
return isDefaultEnabled || hasConfigs;
|
|
104
|
+
}
|
|
105
|
+
return true;
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
message: 'A "configSelector" requires at least one config or default observability to be configured.'
|
|
109
|
+
}
|
|
78
110
|
);
|
|
79
111
|
var BaseExporter = class {
|
|
80
112
|
/** Mastra logger instance */
|
|
@@ -153,7 +185,7 @@ var CloudExporter = class extends BaseExporter {
|
|
|
153
185
|
const accessToken = config.accessToken ?? process.env.MASTRA_CLOUD_ACCESS_TOKEN;
|
|
154
186
|
if (!accessToken) {
|
|
155
187
|
this.setDisabled(
|
|
156
|
-
"MASTRA_CLOUD_ACCESS_TOKEN environment variable not set
|
|
188
|
+
"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."
|
|
157
189
|
);
|
|
158
190
|
}
|
|
159
191
|
const endpoint = config.endpoint ?? process.env.MASTRA_CLOUD_TRACES_ENDPOINT ?? "https://api.mastra.ai/ai/spans/publish";
|
|
@@ -895,6 +927,27 @@ var DefaultExporter = class extends BaseExporter {
|
|
|
895
927
|
this.logger.info("DefaultExporter shutdown complete");
|
|
896
928
|
}
|
|
897
929
|
};
|
|
930
|
+
|
|
931
|
+
// src/exporters/test.ts
|
|
932
|
+
var TestExporter = class extends BaseExporter {
|
|
933
|
+
name = "tracing-test-exporter";
|
|
934
|
+
#events = [];
|
|
935
|
+
constructor(config = {}) {
|
|
936
|
+
super(config);
|
|
937
|
+
}
|
|
938
|
+
async _exportTracingEvent(event) {
|
|
939
|
+
this.#events.push(event);
|
|
940
|
+
}
|
|
941
|
+
clearEvents() {
|
|
942
|
+
this.#events = [];
|
|
943
|
+
}
|
|
944
|
+
get events() {
|
|
945
|
+
return this.#events;
|
|
946
|
+
}
|
|
947
|
+
async shutdown() {
|
|
948
|
+
this.logger.info("TestExporter shutdown");
|
|
949
|
+
}
|
|
950
|
+
};
|
|
898
951
|
var ModelSpanTracker = class {
|
|
899
952
|
#modelSpan;
|
|
900
953
|
#currentStepSpan;
|
|
@@ -1226,6 +1279,16 @@ function isSpanInternal(spanType, flags) {
|
|
|
1226
1279
|
return false;
|
|
1227
1280
|
}
|
|
1228
1281
|
}
|
|
1282
|
+
function getExternalParentId(options) {
|
|
1283
|
+
if (!options.parent) {
|
|
1284
|
+
return void 0;
|
|
1285
|
+
}
|
|
1286
|
+
if (options.parent.isInternal) {
|
|
1287
|
+
return options.parent.getParentSpanId(false);
|
|
1288
|
+
} else {
|
|
1289
|
+
return options.parent.id;
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1229
1292
|
var BaseSpan = class {
|
|
1230
1293
|
name;
|
|
1231
1294
|
type;
|
|
@@ -1322,6 +1385,28 @@ var BaseSpan = class {
|
|
|
1322
1385
|
get externalTraceId() {
|
|
1323
1386
|
return this.isValid ? this.traceId : void 0;
|
|
1324
1387
|
}
|
|
1388
|
+
/**
|
|
1389
|
+
* Execute an async function within this span's tracing context.
|
|
1390
|
+
* Delegates to the bridge if available.
|
|
1391
|
+
*/
|
|
1392
|
+
async executeInContext(fn) {
|
|
1393
|
+
const bridge = this.observabilityInstance.getBridge();
|
|
1394
|
+
if (bridge?.executeInContext) {
|
|
1395
|
+
return bridge.executeInContext(this.id, fn);
|
|
1396
|
+
}
|
|
1397
|
+
return fn();
|
|
1398
|
+
}
|
|
1399
|
+
/**
|
|
1400
|
+
* Execute a synchronous function within this span's tracing context.
|
|
1401
|
+
* Delegates to the bridge if available.
|
|
1402
|
+
*/
|
|
1403
|
+
executeInContextSync(fn) {
|
|
1404
|
+
const bridge = this.observabilityInstance.getBridge();
|
|
1405
|
+
if (bridge?.executeInContextSync) {
|
|
1406
|
+
return bridge.executeInContextSync(this.id, fn);
|
|
1407
|
+
}
|
|
1408
|
+
return fn();
|
|
1409
|
+
}
|
|
1325
1410
|
};
|
|
1326
1411
|
var DEFAULT_KEYS_TO_STRIP = /* @__PURE__ */ new Set([
|
|
1327
1412
|
"logger",
|
|
@@ -1368,27 +1453,30 @@ var DefaultSpan = class extends BaseSpan {
|
|
|
1368
1453
|
traceId;
|
|
1369
1454
|
constructor(options, observabilityInstance) {
|
|
1370
1455
|
super(options, observabilityInstance);
|
|
1371
|
-
|
|
1456
|
+
const bridge = observabilityInstance.getBridge();
|
|
1457
|
+
if (bridge && !this.isInternal) {
|
|
1458
|
+
const bridgeIds = bridge.createSpan(options);
|
|
1459
|
+
if (bridgeIds) {
|
|
1460
|
+
this.id = bridgeIds.spanId;
|
|
1461
|
+
this.traceId = bridgeIds.traceId;
|
|
1462
|
+
this.parentSpanId = bridgeIds.parentSpanId;
|
|
1463
|
+
return;
|
|
1464
|
+
}
|
|
1465
|
+
}
|
|
1372
1466
|
if (options.parent) {
|
|
1373
1467
|
this.traceId = options.parent.traceId;
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
} else {
|
|
1378
|
-
console.error(
|
|
1379
|
-
`[Mastra Tracing] Invalid traceId: must be 1-32 hexadecimal characters, got "${options.traceId}". Generating new trace ID.`
|
|
1380
|
-
);
|
|
1381
|
-
this.traceId = generateTraceId();
|
|
1382
|
-
}
|
|
1383
|
-
} else {
|
|
1384
|
-
this.traceId = generateTraceId();
|
|
1468
|
+
this.parentSpanId = options.parent.id;
|
|
1469
|
+
this.id = generateSpanId();
|
|
1470
|
+
return;
|
|
1385
1471
|
}
|
|
1386
|
-
|
|
1472
|
+
this.traceId = getOrCreateTraceId(options);
|
|
1473
|
+
this.id = generateSpanId();
|
|
1474
|
+
if (options.parentSpanId) {
|
|
1387
1475
|
if (isValidSpanId(options.parentSpanId)) {
|
|
1388
1476
|
this.parentSpanId = options.parentSpanId;
|
|
1389
1477
|
} else {
|
|
1390
1478
|
console.error(
|
|
1391
|
-
`[Mastra Tracing] Invalid parentSpanId: must be 1-16 hexadecimal characters, got "${options.parentSpanId}". Ignoring
|
|
1479
|
+
`[Mastra Tracing] Invalid parentSpanId: must be 1-16 hexadecimal characters, got "${options.parentSpanId}". Ignoring.`
|
|
1392
1480
|
);
|
|
1393
1481
|
}
|
|
1394
1482
|
}
|
|
@@ -1493,6 +1581,18 @@ function isValidTraceId(traceId) {
|
|
|
1493
1581
|
function isValidSpanId(spanId) {
|
|
1494
1582
|
return /^[0-9a-f]{1,16}$/i.test(spanId);
|
|
1495
1583
|
}
|
|
1584
|
+
function getOrCreateTraceId(options) {
|
|
1585
|
+
if (options.traceId) {
|
|
1586
|
+
if (isValidTraceId(options.traceId)) {
|
|
1587
|
+
return options.traceId;
|
|
1588
|
+
} else {
|
|
1589
|
+
console.error(
|
|
1590
|
+
`[Mastra Tracing] Invalid traceId: must be 1-32 hexadecimal characters, got "${options.traceId}". Generating new trace ID.`
|
|
1591
|
+
);
|
|
1592
|
+
}
|
|
1593
|
+
}
|
|
1594
|
+
return generateTraceId();
|
|
1595
|
+
}
|
|
1496
1596
|
|
|
1497
1597
|
// src/spans/no-op.ts
|
|
1498
1598
|
var NoOpSpan = class extends BaseSpan {
|
|
@@ -1525,13 +1625,17 @@ var BaseObservabilityInstance = class extends base.MastraBase {
|
|
|
1525
1625
|
sampling: config.sampling ?? { type: "always" /* ALWAYS */ },
|
|
1526
1626
|
exporters: config.exporters ?? [],
|
|
1527
1627
|
spanOutputProcessors: config.spanOutputProcessors ?? [],
|
|
1628
|
+
bridge: config.bridge ?? void 0,
|
|
1528
1629
|
includeInternalSpans: config.includeInternalSpans ?? false,
|
|
1529
1630
|
requestContextKeys: config.requestContextKeys ?? []
|
|
1530
1631
|
};
|
|
1632
|
+
if (this.config.bridge?.init) {
|
|
1633
|
+
this.config.bridge.init({ config: this.config });
|
|
1634
|
+
}
|
|
1531
1635
|
}
|
|
1532
1636
|
/**
|
|
1533
1637
|
* Override setLogger to add Observability specific initialization log
|
|
1534
|
-
* and propagate logger to exporters
|
|
1638
|
+
* and propagate logger to exporters and bridge
|
|
1535
1639
|
*/
|
|
1536
1640
|
__setLogger(logger) {
|
|
1537
1641
|
super.__setLogger(logger);
|
|
@@ -1540,8 +1644,11 @@ var BaseObservabilityInstance = class extends base.MastraBase {
|
|
|
1540
1644
|
exporter.__setLogger(logger);
|
|
1541
1645
|
}
|
|
1542
1646
|
});
|
|
1647
|
+
if (this.config.bridge?.__setLogger) {
|
|
1648
|
+
this.config.bridge.__setLogger(logger);
|
|
1649
|
+
}
|
|
1543
1650
|
this.logger.debug(
|
|
1544
|
-
`[Observability] Initialized [service=${this.config.serviceName}] [instance=${this.config.name}] [sampling=${this.config.sampling
|
|
1651
|
+
`[Observability] Initialized [service=${this.config.serviceName}] [instance=${this.config.name}] [sampling=${this.config.sampling?.type}] [bridge=${!!this.config.bridge}]`
|
|
1545
1652
|
);
|
|
1546
1653
|
}
|
|
1547
1654
|
// ============================================================================
|
|
@@ -1608,6 +1715,12 @@ var BaseObservabilityInstance = class extends base.MastraBase {
|
|
|
1608
1715
|
getSpanOutputProcessors() {
|
|
1609
1716
|
return [...this.spanOutputProcessors];
|
|
1610
1717
|
}
|
|
1718
|
+
/**
|
|
1719
|
+
* Get the bridge instance if configured
|
|
1720
|
+
*/
|
|
1721
|
+
getBridge() {
|
|
1722
|
+
return this.config.bridge;
|
|
1723
|
+
}
|
|
1611
1724
|
/**
|
|
1612
1725
|
* Get the logger instance (for exporters and other components)
|
|
1613
1726
|
*/
|
|
@@ -1652,7 +1765,9 @@ var BaseObservabilityInstance = class extends base.MastraBase {
|
|
|
1652
1765
|
*/
|
|
1653
1766
|
shouldSample(options) {
|
|
1654
1767
|
const { sampling } = this.config;
|
|
1655
|
-
switch (sampling
|
|
1768
|
+
switch (sampling?.type) {
|
|
1769
|
+
case void 0:
|
|
1770
|
+
return true;
|
|
1656
1771
|
case "always" /* ALWAYS */:
|
|
1657
1772
|
return true;
|
|
1658
1773
|
case "never" /* NEVER */:
|
|
@@ -1784,17 +1899,21 @@ var BaseObservabilityInstance = class extends base.MastraBase {
|
|
|
1784
1899
|
}
|
|
1785
1900
|
}
|
|
1786
1901
|
/**
|
|
1787
|
-
* Export tracing event through all exporters (realtime mode)
|
|
1902
|
+
* Export tracing event through all exporters and bridge (realtime mode)
|
|
1788
1903
|
*/
|
|
1789
1904
|
async exportTracingEvent(event) {
|
|
1790
|
-
const
|
|
1905
|
+
const targets = [
|
|
1906
|
+
...this.exporters
|
|
1907
|
+
];
|
|
1908
|
+
if (this.config.bridge) {
|
|
1909
|
+
targets.push(this.config.bridge);
|
|
1910
|
+
}
|
|
1911
|
+
const exportPromises = targets.map(async (target) => {
|
|
1791
1912
|
try {
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
this.logger.debug(`[Observability] Event exported [exporter=${exporter.name}] [type=${event.type}]`);
|
|
1795
|
-
}
|
|
1913
|
+
await target.exportTracingEvent(event);
|
|
1914
|
+
this.logger.debug(`[Observability] Event exported [target=${target.name}] [type=${event.type}]`);
|
|
1796
1915
|
} catch (error) {
|
|
1797
|
-
this.logger.error(`[Observability] Export error [
|
|
1916
|
+
this.logger.error(`[Observability] Export error [target=${target.name}]`, error);
|
|
1798
1917
|
}
|
|
1799
1918
|
});
|
|
1800
1919
|
await Promise.allSettled(exportPromises);
|
|
@@ -1818,6 +1937,9 @@ var BaseObservabilityInstance = class extends base.MastraBase {
|
|
|
1818
1937
|
...this.exporters.map((e) => e.shutdown()),
|
|
1819
1938
|
...this.spanOutputProcessors.map((p) => p.shutdown())
|
|
1820
1939
|
];
|
|
1940
|
+
if (this.config.bridge) {
|
|
1941
|
+
shutdownPromises.push(this.config.bridge.shutdown());
|
|
1942
|
+
}
|
|
1821
1943
|
await Promise.allSettled(shutdownPromises);
|
|
1822
1944
|
this.logger.info(`[Observability] Shutdown completed [name=${this.name}]`);
|
|
1823
1945
|
}
|
|
@@ -2184,7 +2306,9 @@ exports.NoOpSpan = NoOpSpan;
|
|
|
2184
2306
|
exports.Observability = Observability;
|
|
2185
2307
|
exports.SamplingStrategyType = SamplingStrategyType;
|
|
2186
2308
|
exports.SensitiveDataFilter = SensitiveDataFilter;
|
|
2309
|
+
exports.TestExporter = TestExporter;
|
|
2187
2310
|
exports.deepClean = deepClean;
|
|
2311
|
+
exports.getExternalParentId = getExternalParentId;
|
|
2188
2312
|
exports.observabilityConfigValueSchema = observabilityConfigValueSchema;
|
|
2189
2313
|
exports.observabilityInstanceConfigSchema = observabilityInstanceConfigSchema;
|
|
2190
2314
|
exports.observabilityRegistryConfigSchema = observabilityRegistryConfigSchema;
|