@mastra/otel-exporter 0.0.0-suspendRuntimeContextTypeFix-20250930142630
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 +10 -0
- package/LICENSE.md +15 -0
- package/README.md +435 -0
- package/dist/ai-tracing.d.ts +26 -0
- package/dist/ai-tracing.d.ts.map +1 -0
- package/dist/index.cjs +754 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +752 -0
- package/dist/index.js.map +1 -0
- package/dist/loadExporter.d.ts +6 -0
- package/dist/loadExporter.d.ts.map +1 -0
- package/dist/mastra-span.d.ts +37 -0
- package/dist/mastra-span.d.ts.map +1 -0
- package/dist/provider-configs.d.ts +11 -0
- package/dist/provider-configs.d.ts.map +1 -0
- package/dist/span-converter.d.ts +38 -0
- package/dist/span-converter.d.ts.map +1 -0
- package/dist/types.d.ts +63 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +93 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,754 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var aiTracing = require('@mastra/core/ai-tracing');
|
|
4
|
+
var logger = require('@mastra/core/logger');
|
|
5
|
+
var api = require('@opentelemetry/api');
|
|
6
|
+
var resources = require('@opentelemetry/resources');
|
|
7
|
+
var sdkTraceBase = require('@opentelemetry/sdk-trace-base');
|
|
8
|
+
var semanticConventions = require('@opentelemetry/semantic-conventions');
|
|
9
|
+
|
|
10
|
+
// src/ai-tracing.ts
|
|
11
|
+
|
|
12
|
+
// src/loadExporter.ts
|
|
13
|
+
var OTLPHttpExporter;
|
|
14
|
+
var OTLPGrpcExporter;
|
|
15
|
+
var OTLPProtoExporter;
|
|
16
|
+
var ZipkinExporter;
|
|
17
|
+
async function loadExporter(protocol, provider) {
|
|
18
|
+
switch (protocol) {
|
|
19
|
+
case "zipkin":
|
|
20
|
+
if (!ZipkinExporter) {
|
|
21
|
+
try {
|
|
22
|
+
const module = await import('@opentelemetry/exporter-zipkin');
|
|
23
|
+
ZipkinExporter = module.ZipkinExporter;
|
|
24
|
+
} catch {
|
|
25
|
+
console.error(
|
|
26
|
+
`[OtelExporter] Zipkin exporter is not installed.
|
|
27
|
+
To use Zipkin export, install the required package:
|
|
28
|
+
npm install @opentelemetry/exporter-zipkin`
|
|
29
|
+
);
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return ZipkinExporter;
|
|
34
|
+
case "grpc":
|
|
35
|
+
if (!OTLPGrpcExporter) {
|
|
36
|
+
try {
|
|
37
|
+
const module = await import('@opentelemetry/exporter-trace-otlp-grpc');
|
|
38
|
+
OTLPGrpcExporter = module.OTLPTraceExporter;
|
|
39
|
+
} catch {
|
|
40
|
+
const providerInfo = provider ? ` (required for ${provider})` : "";
|
|
41
|
+
console.error(
|
|
42
|
+
`[OtelExporter] gRPC exporter is not installed${providerInfo}.
|
|
43
|
+
To use gRPC export, install the required packages:
|
|
44
|
+
npm install @opentelemetry/exporter-trace-otlp-grpc @grpc/grpc-js`
|
|
45
|
+
);
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return OTLPGrpcExporter;
|
|
50
|
+
case "http/protobuf":
|
|
51
|
+
if (!OTLPProtoExporter) {
|
|
52
|
+
try {
|
|
53
|
+
const module = await import('@opentelemetry/exporter-trace-otlp-proto');
|
|
54
|
+
OTLPProtoExporter = module.OTLPTraceExporter;
|
|
55
|
+
} catch {
|
|
56
|
+
const providerInfo = provider ? ` (required for ${provider})` : "";
|
|
57
|
+
console.error(
|
|
58
|
+
`[OtelExporter] HTTP/Protobuf exporter is not installed${providerInfo}.
|
|
59
|
+
To use HTTP/Protobuf export, install the required package:
|
|
60
|
+
npm install @opentelemetry/exporter-trace-otlp-proto`
|
|
61
|
+
);
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return OTLPProtoExporter;
|
|
66
|
+
case "http/json":
|
|
67
|
+
default:
|
|
68
|
+
if (!OTLPHttpExporter) {
|
|
69
|
+
try {
|
|
70
|
+
const module = await import('@opentelemetry/exporter-trace-otlp-http');
|
|
71
|
+
OTLPHttpExporter = module.OTLPTraceExporter;
|
|
72
|
+
} catch {
|
|
73
|
+
const providerInfo = provider ? ` (required for ${provider})` : "";
|
|
74
|
+
console.error(
|
|
75
|
+
`[OtelExporter] HTTP/JSON exporter is not installed${providerInfo}.
|
|
76
|
+
To use HTTP/JSON export, install the required package:
|
|
77
|
+
npm install @opentelemetry/exporter-trace-otlp-http`
|
|
78
|
+
);
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return OTLPHttpExporter;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// src/provider-configs.ts
|
|
87
|
+
function resolveProviderConfig(config) {
|
|
88
|
+
if ("dash0" in config) {
|
|
89
|
+
return resolveDash0Config(config.dash0);
|
|
90
|
+
} else if ("signoz" in config) {
|
|
91
|
+
return resolveSignozConfig(config.signoz);
|
|
92
|
+
} else if ("newrelic" in config) {
|
|
93
|
+
return resolveNewRelicConfig(config.newrelic);
|
|
94
|
+
} else if ("traceloop" in config) {
|
|
95
|
+
return resolveTraceloopConfig(config.traceloop);
|
|
96
|
+
} else if ("laminar" in config) {
|
|
97
|
+
return resolveLaminarConfig(config.laminar);
|
|
98
|
+
} else if ("custom" in config) {
|
|
99
|
+
return resolveCustomConfig(config.custom);
|
|
100
|
+
} else {
|
|
101
|
+
const _exhaustive = config;
|
|
102
|
+
return _exhaustive;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
function resolveDash0Config(config) {
|
|
106
|
+
if (!config.apiKey) {
|
|
107
|
+
console.error("[OtelExporter] Dash0 configuration requires apiKey. Tracing will be disabled.");
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
if (!config.endpoint) {
|
|
111
|
+
console.error("[OtelExporter] Dash0 configuration requires endpoint. Tracing will be disabled.");
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
let endpoint = config.endpoint;
|
|
115
|
+
if (!endpoint.includes("/v1/traces")) {
|
|
116
|
+
endpoint = `${endpoint}/v1/traces`;
|
|
117
|
+
}
|
|
118
|
+
const headers = {
|
|
119
|
+
authorization: `Bearer ${config.apiKey}`
|
|
120
|
+
// lowercase for gRPC metadata
|
|
121
|
+
};
|
|
122
|
+
if (config.dataset) {
|
|
123
|
+
headers["dash0-dataset"] = config.dataset;
|
|
124
|
+
}
|
|
125
|
+
return {
|
|
126
|
+
endpoint,
|
|
127
|
+
headers,
|
|
128
|
+
protocol: "grpc"
|
|
129
|
+
// Use gRPC for Dash0
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
function resolveSignozConfig(config) {
|
|
133
|
+
if (!config.apiKey) {
|
|
134
|
+
console.error("[OtelExporter] SigNoz configuration requires apiKey. Tracing will be disabled.");
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
const endpoint = config.endpoint || `https://ingest.${config.region || "us"}.signoz.cloud:443/v1/traces`;
|
|
138
|
+
return {
|
|
139
|
+
endpoint,
|
|
140
|
+
headers: {
|
|
141
|
+
"signoz-ingestion-key": config.apiKey
|
|
142
|
+
},
|
|
143
|
+
protocol: "http/protobuf"
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
function resolveNewRelicConfig(config) {
|
|
147
|
+
if (!config.apiKey) {
|
|
148
|
+
console.error("[OtelExporter] New Relic configuration requires apiKey (license key). Tracing will be disabled.");
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
const endpoint = config.endpoint || "https://otlp.nr-data.net:443/v1/traces";
|
|
152
|
+
return {
|
|
153
|
+
endpoint,
|
|
154
|
+
headers: {
|
|
155
|
+
"api-key": config.apiKey
|
|
156
|
+
},
|
|
157
|
+
protocol: "http/protobuf"
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
function resolveTraceloopConfig(config) {
|
|
161
|
+
if (!config.apiKey) {
|
|
162
|
+
console.error("[OtelExporter] Traceloop configuration requires apiKey. Tracing will be disabled.");
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
const endpoint = config.endpoint || "https://api.traceloop.com/v1/traces";
|
|
166
|
+
const headers = {
|
|
167
|
+
Authorization: `Bearer ${config.apiKey}`
|
|
168
|
+
};
|
|
169
|
+
if (config.destinationId) {
|
|
170
|
+
headers["x-traceloop-destination-id"] = config.destinationId;
|
|
171
|
+
}
|
|
172
|
+
return {
|
|
173
|
+
endpoint,
|
|
174
|
+
headers,
|
|
175
|
+
protocol: "http/json"
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
function resolveLaminarConfig(config) {
|
|
179
|
+
if (!config.apiKey) {
|
|
180
|
+
console.error("[OtelExporter] Laminar configuration requires apiKey. Tracing will be disabled.");
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
const endpoint = config.endpoint || "https://api.lmnr.ai/v1/traces";
|
|
184
|
+
const headers = {
|
|
185
|
+
Authorization: `Bearer ${config.apiKey}`
|
|
186
|
+
};
|
|
187
|
+
if (config.teamId) {
|
|
188
|
+
headers["x-laminar-team-id"] = config.teamId;
|
|
189
|
+
}
|
|
190
|
+
return {
|
|
191
|
+
endpoint,
|
|
192
|
+
headers,
|
|
193
|
+
protocol: "http/protobuf"
|
|
194
|
+
// Use HTTP/protobuf instead of gRPC for better compatibility
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
function resolveCustomConfig(config) {
|
|
198
|
+
if (!config.endpoint) {
|
|
199
|
+
console.error("[OtelExporter] Custom configuration requires endpoint. Tracing will be disabled.");
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
return {
|
|
203
|
+
endpoint: config.endpoint,
|
|
204
|
+
headers: config.headers || {},
|
|
205
|
+
protocol: config.protocol || "http/json"
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
var MastraReadableSpan = class {
|
|
209
|
+
name;
|
|
210
|
+
kind;
|
|
211
|
+
spanContext;
|
|
212
|
+
parentSpanId;
|
|
213
|
+
startTime;
|
|
214
|
+
endTime;
|
|
215
|
+
status;
|
|
216
|
+
attributes;
|
|
217
|
+
links;
|
|
218
|
+
events;
|
|
219
|
+
duration;
|
|
220
|
+
ended;
|
|
221
|
+
resource;
|
|
222
|
+
instrumentationLibrary;
|
|
223
|
+
instrumentationScope;
|
|
224
|
+
droppedAttributesCount = 0;
|
|
225
|
+
droppedEventsCount = 0;
|
|
226
|
+
droppedLinksCount = 0;
|
|
227
|
+
constructor(aiSpan, attributes, kind, parentSpanId, resource, instrumentationLibrary) {
|
|
228
|
+
this.name = aiSpan.name;
|
|
229
|
+
this.kind = kind;
|
|
230
|
+
this.attributes = attributes;
|
|
231
|
+
this.parentSpanId = parentSpanId;
|
|
232
|
+
this.links = [];
|
|
233
|
+
this.events = [];
|
|
234
|
+
this.startTime = this.dateToHrTime(aiSpan.startTime);
|
|
235
|
+
this.endTime = aiSpan.endTime ? this.dateToHrTime(aiSpan.endTime) : this.startTime;
|
|
236
|
+
this.ended = !!aiSpan.endTime;
|
|
237
|
+
if (aiSpan.endTime) {
|
|
238
|
+
const durationMs = aiSpan.endTime.getTime() - aiSpan.startTime.getTime();
|
|
239
|
+
this.duration = [Math.floor(durationMs / 1e3), durationMs % 1e3 * 1e6];
|
|
240
|
+
} else {
|
|
241
|
+
this.duration = [0, 0];
|
|
242
|
+
}
|
|
243
|
+
if (aiSpan.errorInfo) {
|
|
244
|
+
this.status = {
|
|
245
|
+
code: api.SpanStatusCode.ERROR,
|
|
246
|
+
message: aiSpan.errorInfo.message
|
|
247
|
+
};
|
|
248
|
+
this.events.push({
|
|
249
|
+
name: "exception",
|
|
250
|
+
attributes: {
|
|
251
|
+
"exception.message": aiSpan.errorInfo.message,
|
|
252
|
+
"exception.type": "Error",
|
|
253
|
+
...aiSpan.errorInfo.details?.stack && {
|
|
254
|
+
"exception.stacktrace": aiSpan.errorInfo.details.stack
|
|
255
|
+
}
|
|
256
|
+
},
|
|
257
|
+
time: this.startTime,
|
|
258
|
+
droppedAttributesCount: 0
|
|
259
|
+
});
|
|
260
|
+
} else if (aiSpan.endTime) {
|
|
261
|
+
this.status = { code: api.SpanStatusCode.OK };
|
|
262
|
+
} else {
|
|
263
|
+
this.status = { code: api.SpanStatusCode.UNSET };
|
|
264
|
+
}
|
|
265
|
+
if (aiSpan.isEvent) {
|
|
266
|
+
this.events.push({
|
|
267
|
+
name: "instant_event",
|
|
268
|
+
attributes: {},
|
|
269
|
+
time: this.startTime,
|
|
270
|
+
droppedAttributesCount: 0
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
this.spanContext = () => ({
|
|
274
|
+
traceId: aiSpan.traceId,
|
|
275
|
+
spanId: aiSpan.id,
|
|
276
|
+
traceFlags: api.TraceFlags.SAMPLED,
|
|
277
|
+
isRemote: false
|
|
278
|
+
});
|
|
279
|
+
this.resource = resource || {};
|
|
280
|
+
this.instrumentationLibrary = instrumentationLibrary || {
|
|
281
|
+
name: "@mastra/otel",
|
|
282
|
+
version: "1.0.0"
|
|
283
|
+
};
|
|
284
|
+
this.instrumentationScope = this.instrumentationLibrary;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Convert JavaScript Date to hrtime format
|
|
288
|
+
*/
|
|
289
|
+
dateToHrTime(date) {
|
|
290
|
+
const ms = date.getTime();
|
|
291
|
+
const seconds = Math.floor(ms / 1e3);
|
|
292
|
+
const nanoseconds = ms % 1e3 * 1e6;
|
|
293
|
+
return [seconds, nanoseconds];
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
// src/span-converter.ts
|
|
298
|
+
var SPAN_KIND_MAPPING = {
|
|
299
|
+
// LLM operations are CLIENT spans (calling external AI services)
|
|
300
|
+
[aiTracing.AISpanType.LLM_GENERATION]: api.SpanKind.CLIENT,
|
|
301
|
+
[aiTracing.AISpanType.LLM_CHUNK]: api.SpanKind.CLIENT,
|
|
302
|
+
// Tool calls can be CLIENT (external) or INTERNAL based on context
|
|
303
|
+
[aiTracing.AISpanType.TOOL_CALL]: api.SpanKind.INTERNAL,
|
|
304
|
+
[aiTracing.AISpanType.MCP_TOOL_CALL]: api.SpanKind.CLIENT,
|
|
305
|
+
// Root spans for agent/workflow are SERVER (entry points)
|
|
306
|
+
[aiTracing.AISpanType.AGENT_RUN]: api.SpanKind.SERVER,
|
|
307
|
+
[aiTracing.AISpanType.WORKFLOW_RUN]: api.SpanKind.SERVER,
|
|
308
|
+
// Internal workflow operations
|
|
309
|
+
[aiTracing.AISpanType.WORKFLOW_STEP]: api.SpanKind.INTERNAL,
|
|
310
|
+
[aiTracing.AISpanType.WORKFLOW_LOOP]: api.SpanKind.INTERNAL,
|
|
311
|
+
[aiTracing.AISpanType.WORKFLOW_PARALLEL]: api.SpanKind.INTERNAL,
|
|
312
|
+
[aiTracing.AISpanType.WORKFLOW_CONDITIONAL]: api.SpanKind.INTERNAL,
|
|
313
|
+
[aiTracing.AISpanType.WORKFLOW_CONDITIONAL_EVAL]: api.SpanKind.INTERNAL,
|
|
314
|
+
[aiTracing.AISpanType.WORKFLOW_SLEEP]: api.SpanKind.INTERNAL,
|
|
315
|
+
[aiTracing.AISpanType.WORKFLOW_WAIT_EVENT]: api.SpanKind.INTERNAL,
|
|
316
|
+
[aiTracing.AISpanType.GENERIC]: api.SpanKind.INTERNAL
|
|
317
|
+
};
|
|
318
|
+
var SpanConverter = class {
|
|
319
|
+
resource;
|
|
320
|
+
instrumentationLibrary;
|
|
321
|
+
constructor(resource) {
|
|
322
|
+
this.resource = resource;
|
|
323
|
+
this.instrumentationLibrary = {
|
|
324
|
+
name: "@mastra/otel",
|
|
325
|
+
version: "1.0.0"
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Convert a Mastra AI span to an OpenTelemetry ReadableSpan
|
|
330
|
+
* This preserves Mastra's trace and span IDs
|
|
331
|
+
*/
|
|
332
|
+
convertSpan(aiSpan) {
|
|
333
|
+
const spanKind = this.getSpanKind(aiSpan);
|
|
334
|
+
const attributes = this.buildAttributes(aiSpan);
|
|
335
|
+
const spanName = this.buildSpanName(aiSpan);
|
|
336
|
+
const otelSpan = { ...aiSpan, name: spanName };
|
|
337
|
+
return new MastraReadableSpan(
|
|
338
|
+
otelSpan,
|
|
339
|
+
attributes,
|
|
340
|
+
spanKind,
|
|
341
|
+
aiSpan.parentSpanId,
|
|
342
|
+
// Use the parentSpanId from the Mastra span directly
|
|
343
|
+
this.resource,
|
|
344
|
+
this.instrumentationLibrary
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Get the appropriate SpanKind based on span type and context
|
|
349
|
+
*/
|
|
350
|
+
getSpanKind(aiSpan) {
|
|
351
|
+
if (aiSpan.isRootSpan) {
|
|
352
|
+
if (aiSpan.type === aiTracing.AISpanType.AGENT_RUN || aiSpan.type === aiTracing.AISpanType.WORKFLOW_RUN) {
|
|
353
|
+
return api.SpanKind.SERVER;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
return SPAN_KIND_MAPPING[aiSpan.type] || api.SpanKind.INTERNAL;
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Build OTEL-compliant span name based on span type and attributes
|
|
360
|
+
*/
|
|
361
|
+
buildSpanName(aiSpan) {
|
|
362
|
+
switch (aiSpan.type) {
|
|
363
|
+
case aiTracing.AISpanType.LLM_GENERATION: {
|
|
364
|
+
const attrs = aiSpan.attributes;
|
|
365
|
+
const operation = attrs?.resultType === "tool_selection" ? "tool_selection" : "chat";
|
|
366
|
+
const model = attrs?.model || "unknown";
|
|
367
|
+
return `${operation} ${model}`;
|
|
368
|
+
}
|
|
369
|
+
case aiTracing.AISpanType.TOOL_CALL:
|
|
370
|
+
case aiTracing.AISpanType.MCP_TOOL_CALL: {
|
|
371
|
+
const toolAttrs = aiSpan.attributes;
|
|
372
|
+
const toolName = toolAttrs?.toolId || "unknown";
|
|
373
|
+
return `tool.execute ${toolName}`;
|
|
374
|
+
}
|
|
375
|
+
case aiTracing.AISpanType.AGENT_RUN: {
|
|
376
|
+
const agentAttrs = aiSpan.attributes;
|
|
377
|
+
const agentId = agentAttrs?.agentId || "unknown";
|
|
378
|
+
return `agent.${agentId}`;
|
|
379
|
+
}
|
|
380
|
+
case aiTracing.AISpanType.WORKFLOW_RUN: {
|
|
381
|
+
const workflowAttrs = aiSpan.attributes;
|
|
382
|
+
const workflowId = workflowAttrs?.workflowId || "unknown";
|
|
383
|
+
return `workflow.${workflowId}`;
|
|
384
|
+
}
|
|
385
|
+
case aiTracing.AISpanType.WORKFLOW_STEP:
|
|
386
|
+
return aiSpan.name;
|
|
387
|
+
default:
|
|
388
|
+
return aiSpan.name;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* Build OpenTelemetry attributes from Mastra AI span
|
|
393
|
+
* Following OTEL Semantic Conventions for GenAI
|
|
394
|
+
*/
|
|
395
|
+
buildAttributes(aiSpan) {
|
|
396
|
+
const attributes = {};
|
|
397
|
+
attributes["gen_ai.operation.name"] = this.getOperationName(aiSpan);
|
|
398
|
+
attributes["span.kind"] = this.getSpanKindString(aiSpan);
|
|
399
|
+
attributes["mastra.span.type"] = aiSpan.type;
|
|
400
|
+
attributes["mastra.trace_id"] = aiSpan.traceId;
|
|
401
|
+
attributes["mastra.span_id"] = aiSpan.id;
|
|
402
|
+
if (aiSpan.parentSpanId) {
|
|
403
|
+
attributes["mastra.parent_span_id"] = aiSpan.parentSpanId;
|
|
404
|
+
}
|
|
405
|
+
if (aiSpan.input !== void 0) {
|
|
406
|
+
const inputStr = typeof aiSpan.input === "string" ? aiSpan.input : JSON.stringify(aiSpan.input);
|
|
407
|
+
attributes["input"] = inputStr;
|
|
408
|
+
if (aiSpan.type === aiTracing.AISpanType.LLM_GENERATION) {
|
|
409
|
+
attributes["gen_ai.prompt"] = inputStr;
|
|
410
|
+
} else if (aiSpan.type === aiTracing.AISpanType.TOOL_CALL || aiSpan.type === aiTracing.AISpanType.MCP_TOOL_CALL) {
|
|
411
|
+
attributes["gen_ai.tool.input"] = inputStr;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
if (aiSpan.output !== void 0) {
|
|
415
|
+
const outputStr = typeof aiSpan.output === "string" ? aiSpan.output : JSON.stringify(aiSpan.output);
|
|
416
|
+
attributes["output"] = outputStr;
|
|
417
|
+
if (aiSpan.type === aiTracing.AISpanType.LLM_GENERATION) {
|
|
418
|
+
attributes["gen_ai.completion"] = outputStr;
|
|
419
|
+
} else if (aiSpan.type === aiTracing.AISpanType.TOOL_CALL || aiSpan.type === aiTracing.AISpanType.MCP_TOOL_CALL) {
|
|
420
|
+
attributes["gen_ai.tool.output"] = outputStr;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
if (aiSpan.type === aiTracing.AISpanType.LLM_GENERATION && aiSpan.attributes) {
|
|
424
|
+
const llmAttrs = aiSpan.attributes;
|
|
425
|
+
if (llmAttrs.model) {
|
|
426
|
+
attributes["gen_ai.request.model"] = llmAttrs.model;
|
|
427
|
+
}
|
|
428
|
+
if (llmAttrs.provider) {
|
|
429
|
+
attributes["gen_ai.system"] = llmAttrs.provider;
|
|
430
|
+
}
|
|
431
|
+
if (llmAttrs.usage) {
|
|
432
|
+
const inputTokens = llmAttrs.usage.inputTokens ?? llmAttrs.usage.promptTokens;
|
|
433
|
+
const outputTokens = llmAttrs.usage.outputTokens ?? llmAttrs.usage.completionTokens;
|
|
434
|
+
if (inputTokens !== void 0) {
|
|
435
|
+
attributes["gen_ai.usage.input_tokens"] = inputTokens;
|
|
436
|
+
}
|
|
437
|
+
if (outputTokens !== void 0) {
|
|
438
|
+
attributes["gen_ai.usage.output_tokens"] = outputTokens;
|
|
439
|
+
}
|
|
440
|
+
if (llmAttrs.usage.totalTokens !== void 0) {
|
|
441
|
+
attributes["gen_ai.usage.total_tokens"] = llmAttrs.usage.totalTokens;
|
|
442
|
+
}
|
|
443
|
+
if (llmAttrs.usage.reasoningTokens !== void 0) {
|
|
444
|
+
attributes["gen_ai.usage.reasoning_tokens"] = llmAttrs.usage.reasoningTokens;
|
|
445
|
+
}
|
|
446
|
+
if (llmAttrs.usage.cachedInputTokens !== void 0) {
|
|
447
|
+
attributes["gen_ai.usage.cached_input_tokens"] = llmAttrs.usage.cachedInputTokens;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
if (llmAttrs.parameters) {
|
|
451
|
+
if (llmAttrs.parameters.temperature !== void 0) {
|
|
452
|
+
attributes["gen_ai.request.temperature"] = llmAttrs.parameters.temperature;
|
|
453
|
+
}
|
|
454
|
+
if (llmAttrs.parameters.maxOutputTokens !== void 0) {
|
|
455
|
+
attributes["gen_ai.request.max_tokens"] = llmAttrs.parameters.maxOutputTokens;
|
|
456
|
+
}
|
|
457
|
+
if (llmAttrs.parameters.topP !== void 0) {
|
|
458
|
+
attributes["gen_ai.request.top_p"] = llmAttrs.parameters.topP;
|
|
459
|
+
}
|
|
460
|
+
if (llmAttrs.parameters.topK !== void 0) {
|
|
461
|
+
attributes["gen_ai.request.top_k"] = llmAttrs.parameters.topK;
|
|
462
|
+
}
|
|
463
|
+
if (llmAttrs.parameters.presencePenalty !== void 0) {
|
|
464
|
+
attributes["gen_ai.request.presence_penalty"] = llmAttrs.parameters.presencePenalty;
|
|
465
|
+
}
|
|
466
|
+
if (llmAttrs.parameters.frequencyPenalty !== void 0) {
|
|
467
|
+
attributes["gen_ai.request.frequency_penalty"] = llmAttrs.parameters.frequencyPenalty;
|
|
468
|
+
}
|
|
469
|
+
if (llmAttrs.parameters.stopSequences) {
|
|
470
|
+
attributes["gen_ai.request.stop_sequences"] = JSON.stringify(llmAttrs.parameters.stopSequences);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
if (llmAttrs.finishReason) {
|
|
474
|
+
attributes["gen_ai.response.finish_reasons"] = llmAttrs.finishReason;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
if ((aiSpan.type === aiTracing.AISpanType.TOOL_CALL || aiSpan.type === aiTracing.AISpanType.MCP_TOOL_CALL) && aiSpan.attributes) {
|
|
478
|
+
const toolAttrs = aiSpan.attributes;
|
|
479
|
+
if (toolAttrs.toolId) {
|
|
480
|
+
attributes["gen_ai.tool.name"] = toolAttrs.toolId;
|
|
481
|
+
}
|
|
482
|
+
if (aiSpan.type === aiTracing.AISpanType.MCP_TOOL_CALL) {
|
|
483
|
+
const mcpAttrs = toolAttrs;
|
|
484
|
+
if (mcpAttrs.mcpServer) {
|
|
485
|
+
attributes["mcp.server"] = mcpAttrs.mcpServer;
|
|
486
|
+
}
|
|
487
|
+
if (mcpAttrs.serverVersion) {
|
|
488
|
+
attributes["mcp.server.version"] = mcpAttrs.serverVersion;
|
|
489
|
+
}
|
|
490
|
+
} else {
|
|
491
|
+
if (toolAttrs.toolDescription) {
|
|
492
|
+
attributes["gen_ai.tool.description"] = toolAttrs.toolDescription;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
if (toolAttrs.success !== void 0) {
|
|
496
|
+
attributes["gen_ai.tool.success"] = toolAttrs.success;
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
if (aiSpan.type === aiTracing.AISpanType.AGENT_RUN && aiSpan.attributes) {
|
|
500
|
+
const agentAttrs = aiSpan.attributes;
|
|
501
|
+
if (agentAttrs.agentId) {
|
|
502
|
+
attributes["agent.id"] = agentAttrs.agentId;
|
|
503
|
+
}
|
|
504
|
+
if (agentAttrs.maxSteps) {
|
|
505
|
+
attributes["agent.max_steps"] = agentAttrs.maxSteps;
|
|
506
|
+
}
|
|
507
|
+
if (agentAttrs.availableTools) {
|
|
508
|
+
attributes["agent.available_tools"] = JSON.stringify(agentAttrs.availableTools);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
if (aiSpan.type === aiTracing.AISpanType.WORKFLOW_RUN && aiSpan.attributes) {
|
|
512
|
+
const workflowAttrs = aiSpan.attributes;
|
|
513
|
+
if (workflowAttrs.workflowId) {
|
|
514
|
+
attributes["workflow.id"] = workflowAttrs.workflowId;
|
|
515
|
+
}
|
|
516
|
+
if (workflowAttrs.status) {
|
|
517
|
+
attributes["workflow.status"] = workflowAttrs.status;
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
if (aiSpan.errorInfo) {
|
|
521
|
+
attributes["error"] = true;
|
|
522
|
+
attributes["error.type"] = aiSpan.errorInfo.id || "unknown";
|
|
523
|
+
attributes["error.message"] = aiSpan.errorInfo.message;
|
|
524
|
+
if (aiSpan.errorInfo.domain) {
|
|
525
|
+
attributes["error.domain"] = aiSpan.errorInfo.domain;
|
|
526
|
+
}
|
|
527
|
+
if (aiSpan.errorInfo.category) {
|
|
528
|
+
attributes["error.category"] = aiSpan.errorInfo.category;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
if (aiSpan.metadata) {
|
|
532
|
+
Object.entries(aiSpan.metadata).forEach(([key, value]) => {
|
|
533
|
+
if (!attributes[key]) {
|
|
534
|
+
if (value === null || value === void 0) {
|
|
535
|
+
return;
|
|
536
|
+
}
|
|
537
|
+
if (typeof value === "object") {
|
|
538
|
+
attributes[key] = JSON.stringify(value);
|
|
539
|
+
} else {
|
|
540
|
+
attributes[key] = value;
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
});
|
|
544
|
+
}
|
|
545
|
+
if (aiSpan.startTime) {
|
|
546
|
+
attributes["mastra.start_time"] = aiSpan.startTime.toISOString();
|
|
547
|
+
}
|
|
548
|
+
if (aiSpan.endTime) {
|
|
549
|
+
attributes["mastra.end_time"] = aiSpan.endTime.toISOString();
|
|
550
|
+
const duration = aiSpan.endTime.getTime() - aiSpan.startTime.getTime();
|
|
551
|
+
attributes["mastra.duration_ms"] = duration;
|
|
552
|
+
}
|
|
553
|
+
return attributes;
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* Get the operation name based on span type for gen_ai.operation.name
|
|
557
|
+
*/
|
|
558
|
+
getOperationName(aiSpan) {
|
|
559
|
+
switch (aiSpan.type) {
|
|
560
|
+
case aiTracing.AISpanType.LLM_GENERATION: {
|
|
561
|
+
const attrs = aiSpan.attributes;
|
|
562
|
+
return attrs?.resultType === "tool_selection" ? "tool_selection" : "chat";
|
|
563
|
+
}
|
|
564
|
+
case aiTracing.AISpanType.TOOL_CALL:
|
|
565
|
+
case aiTracing.AISpanType.MCP_TOOL_CALL:
|
|
566
|
+
return "tool.execute";
|
|
567
|
+
case aiTracing.AISpanType.AGENT_RUN:
|
|
568
|
+
return "agent.run";
|
|
569
|
+
case aiTracing.AISpanType.WORKFLOW_RUN:
|
|
570
|
+
return "workflow.run";
|
|
571
|
+
default:
|
|
572
|
+
return aiSpan.type.replace(/_/g, ".");
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
/**
|
|
576
|
+
* Get span kind as string for attribute
|
|
577
|
+
*/
|
|
578
|
+
getSpanKindString(aiSpan) {
|
|
579
|
+
const kind = this.getSpanKind(aiSpan);
|
|
580
|
+
switch (kind) {
|
|
581
|
+
case api.SpanKind.SERVER:
|
|
582
|
+
return "server";
|
|
583
|
+
case api.SpanKind.CLIENT:
|
|
584
|
+
return "client";
|
|
585
|
+
case api.SpanKind.INTERNAL:
|
|
586
|
+
return "internal";
|
|
587
|
+
case api.SpanKind.PRODUCER:
|
|
588
|
+
return "producer";
|
|
589
|
+
case api.SpanKind.CONSUMER:
|
|
590
|
+
return "consumer";
|
|
591
|
+
default:
|
|
592
|
+
return "internal";
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
};
|
|
596
|
+
|
|
597
|
+
// src/ai-tracing.ts
|
|
598
|
+
var OtelExporter = class {
|
|
599
|
+
config;
|
|
600
|
+
tracingConfig;
|
|
601
|
+
spanConverter;
|
|
602
|
+
processor;
|
|
603
|
+
exporter;
|
|
604
|
+
isSetup = false;
|
|
605
|
+
isDisabled = false;
|
|
606
|
+
logger;
|
|
607
|
+
name = "opentelemetry";
|
|
608
|
+
constructor(config) {
|
|
609
|
+
this.config = config;
|
|
610
|
+
this.spanConverter = new SpanConverter();
|
|
611
|
+
this.logger = new logger.ConsoleLogger({ level: config.logLevel ?? "warn" });
|
|
612
|
+
if (config.logLevel === "debug") {
|
|
613
|
+
api.diag.setLogger(new api.DiagConsoleLogger(), api.DiagLogLevel.DEBUG);
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
/**
|
|
617
|
+
* Initialize with tracing configuration
|
|
618
|
+
*/
|
|
619
|
+
init(config) {
|
|
620
|
+
this.tracingConfig = config;
|
|
621
|
+
}
|
|
622
|
+
async setupExporter() {
|
|
623
|
+
if (this.isSetup) return;
|
|
624
|
+
if (!this.config.provider) {
|
|
625
|
+
this.logger.error(
|
|
626
|
+
'[OtelExporter] Provider configuration is required. Use the "custom" provider for generic endpoints.'
|
|
627
|
+
);
|
|
628
|
+
this.isDisabled = true;
|
|
629
|
+
this.isSetup = true;
|
|
630
|
+
return;
|
|
631
|
+
}
|
|
632
|
+
const resolved = resolveProviderConfig(this.config.provider);
|
|
633
|
+
if (!resolved) {
|
|
634
|
+
this.isDisabled = true;
|
|
635
|
+
this.isSetup = true;
|
|
636
|
+
return;
|
|
637
|
+
}
|
|
638
|
+
const endpoint = resolved.endpoint;
|
|
639
|
+
const headers = resolved.headers;
|
|
640
|
+
const protocol = resolved.protocol;
|
|
641
|
+
const providerName = Object.keys(this.config.provider)[0];
|
|
642
|
+
const ExporterClass = await loadExporter(protocol, providerName);
|
|
643
|
+
if (!ExporterClass) {
|
|
644
|
+
this.isDisabled = true;
|
|
645
|
+
this.isSetup = true;
|
|
646
|
+
return;
|
|
647
|
+
}
|
|
648
|
+
try {
|
|
649
|
+
if (protocol === "zipkin") {
|
|
650
|
+
this.exporter = new ExporterClass({
|
|
651
|
+
url: endpoint,
|
|
652
|
+
headers
|
|
653
|
+
});
|
|
654
|
+
} else if (protocol === "grpc") {
|
|
655
|
+
let metadata;
|
|
656
|
+
try {
|
|
657
|
+
const grpcModule = await import('@grpc/grpc-js');
|
|
658
|
+
metadata = new grpcModule.Metadata();
|
|
659
|
+
Object.entries(headers).forEach(([key, value]) => {
|
|
660
|
+
metadata.set(key, value);
|
|
661
|
+
});
|
|
662
|
+
} catch (grpcError) {
|
|
663
|
+
this.logger.error(
|
|
664
|
+
`[OtelExporter] Failed to load gRPC metadata. Install required packages:
|
|
665
|
+
npm install @opentelemetry/exporter-trace-otlp-grpc @grpc/grpc-js
|
|
666
|
+
`,
|
|
667
|
+
grpcError
|
|
668
|
+
);
|
|
669
|
+
this.isDisabled = true;
|
|
670
|
+
this.isSetup = true;
|
|
671
|
+
return;
|
|
672
|
+
}
|
|
673
|
+
this.exporter = new ExporterClass({
|
|
674
|
+
url: endpoint,
|
|
675
|
+
metadata,
|
|
676
|
+
timeoutMillis: this.config.timeout
|
|
677
|
+
});
|
|
678
|
+
} else {
|
|
679
|
+
this.exporter = new ExporterClass({
|
|
680
|
+
url: endpoint,
|
|
681
|
+
headers,
|
|
682
|
+
timeoutMillis: this.config.timeout
|
|
683
|
+
});
|
|
684
|
+
}
|
|
685
|
+
} catch (error) {
|
|
686
|
+
this.logger.error(`[OtelExporter] Failed to create exporter:`, error);
|
|
687
|
+
this.isDisabled = true;
|
|
688
|
+
this.isSetup = true;
|
|
689
|
+
return;
|
|
690
|
+
}
|
|
691
|
+
const resource = resources.resourceFromAttributes({
|
|
692
|
+
[semanticConventions.ATTR_SERVICE_NAME]: this.tracingConfig?.serviceName || "mastra-service",
|
|
693
|
+
[semanticConventions.ATTR_SERVICE_VERSION]: "1.0.0",
|
|
694
|
+
// Add telemetry SDK information
|
|
695
|
+
[semanticConventions.ATTR_TELEMETRY_SDK_NAME]: "@mastra/otel-exporter",
|
|
696
|
+
[semanticConventions.ATTR_TELEMETRY_SDK_VERSION]: "1.0.0",
|
|
697
|
+
[semanticConventions.ATTR_TELEMETRY_SDK_LANGUAGE]: "nodejs"
|
|
698
|
+
});
|
|
699
|
+
this.spanConverter = new SpanConverter(resource);
|
|
700
|
+
this.processor = new sdkTraceBase.BatchSpanProcessor(this.exporter, {
|
|
701
|
+
maxExportBatchSize: this.config.batchSize || 512,
|
|
702
|
+
// Default batch size
|
|
703
|
+
maxQueueSize: 2048,
|
|
704
|
+
// Maximum spans to queue
|
|
705
|
+
scheduledDelayMillis: 5e3,
|
|
706
|
+
// Export every 5 seconds
|
|
707
|
+
exportTimeoutMillis: this.config.timeout || 3e4
|
|
708
|
+
// Export timeout
|
|
709
|
+
});
|
|
710
|
+
this.logger.debug(
|
|
711
|
+
`[OtelExporter] Using BatchSpanProcessor (batch size: ${this.config.batchSize || 512}, delay: 5s)`
|
|
712
|
+
);
|
|
713
|
+
this.isSetup = true;
|
|
714
|
+
}
|
|
715
|
+
async exportEvent(event) {
|
|
716
|
+
if (this.isDisabled) {
|
|
717
|
+
return;
|
|
718
|
+
}
|
|
719
|
+
if (event.type !== aiTracing.AITracingEventType.SPAN_ENDED) {
|
|
720
|
+
return;
|
|
721
|
+
}
|
|
722
|
+
const span = event.exportedSpan;
|
|
723
|
+
await this.exportSpan(span);
|
|
724
|
+
}
|
|
725
|
+
async exportSpan(span) {
|
|
726
|
+
if (!this.isSetup) {
|
|
727
|
+
await this.setupExporter();
|
|
728
|
+
}
|
|
729
|
+
if (this.isDisabled || !this.processor) {
|
|
730
|
+
return;
|
|
731
|
+
}
|
|
732
|
+
try {
|
|
733
|
+
const readableSpan = this.spanConverter.convertSpan(span);
|
|
734
|
+
await new Promise((resolve) => {
|
|
735
|
+
this.processor.onEnd(readableSpan);
|
|
736
|
+
resolve();
|
|
737
|
+
});
|
|
738
|
+
this.logger.debug(
|
|
739
|
+
`[OtelExporter] Exported span ${span.id} (trace: ${span.traceId}, parent: ${span.parentSpanId || "none"}, type: ${span.type})`
|
|
740
|
+
);
|
|
741
|
+
} catch (error) {
|
|
742
|
+
this.logger.error(`[OtelExporter] Failed to export span ${span.id}:`, error);
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
async shutdown() {
|
|
746
|
+
if (this.processor) {
|
|
747
|
+
await this.processor.shutdown();
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
};
|
|
751
|
+
|
|
752
|
+
exports.OtelExporter = OtelExporter;
|
|
753
|
+
//# sourceMappingURL=index.cjs.map
|
|
754
|
+
//# sourceMappingURL=index.cjs.map
|