@mastra/otel-exporter 1.0.0-beta.0 → 1.0.0-beta.10

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.cjs CHANGED
@@ -1,11 +1,14 @@
1
1
  'use strict';
2
2
 
3
- var observability$1 = require('@mastra/core/observability');
4
- var observability = require('@mastra/observability');
3
+ var observability = require('@mastra/core/observability');
4
+ var observability$1 = require('@mastra/observability');
5
5
  var api = require('@opentelemetry/api');
6
- var resources = require('@opentelemetry/resources');
7
6
  var sdkTraceBase = require('@opentelemetry/sdk-trace-base');
7
+ var fs = require('fs');
8
+ var url = require('url');
9
+ var resources = require('@opentelemetry/resources');
8
10
  var semanticConventions = require('@opentelemetry/semantic-conventions');
11
+ var incubating = require('@opentelemetry/semantic-conventions/incubating');
9
12
 
10
13
  // src/tracing.ts
11
14
 
@@ -205,399 +208,478 @@ function resolveCustomConfig(config) {
205
208
  protocol: config.protocol || "http/json"
206
209
  };
207
210
  }
208
- var MastraReadableSpan = class {
209
- name;
210
- kind;
211
- spanContext;
212
- parentSpanContext;
213
- parentSpanId;
214
- startTime;
215
- endTime;
216
- status;
217
- attributes;
218
- links;
219
- events;
220
- duration;
221
- ended;
222
- resource;
223
- instrumentationLibrary;
224
- instrumentationScope;
225
- droppedAttributesCount = 0;
226
- droppedEventsCount = 0;
227
- droppedLinksCount = 0;
228
- constructor(span, attributes, kind, parentSpanId, resource, instrumentationLibrary) {
229
- this.name = span.name;
230
- this.kind = kind;
231
- this.attributes = attributes;
232
- this.parentSpanId = parentSpanId;
233
- this.links = [];
234
- this.events = [];
235
- this.startTime = this.dateToHrTime(span.startTime);
236
- this.endTime = span.endTime ? this.dateToHrTime(span.endTime) : this.startTime;
237
- this.ended = !!span.endTime;
238
- if (span.endTime) {
239
- const durationMs = span.endTime.getTime() - span.startTime.getTime();
240
- this.duration = [Math.floor(durationMs / 1e3), durationMs % 1e3 * 1e6];
241
- } else {
242
- this.duration = [0, 0];
211
+
212
+ // src/gen-ai-messages.ts
213
+ var isMastraMessagePart = (p) => {
214
+ return typeof p === "object" && p != null && "type" in p && (p.type === "text" || p.type === "tool-call" || p.type === "tool-result") && (p.type === "text" && "text" in p || p.type === "tool-call" && "toolCallId" in p && "toolName" in p && "input" in p || p.type === "tool-result" && "toolCallId" in p && "toolName" in p && "output" in p);
215
+ };
216
+ var isMastraMessage = (m) => {
217
+ return typeof m === "object" && m != null && "role" in m && "content" in m && (typeof m.content === "string" || Array.isArray(m.content) && m.content.every(isMastraMessagePart));
218
+ };
219
+ var convertMastraMessagesToGenAIMessages = (inputOutputString) => {
220
+ try {
221
+ const parsedIO = JSON.parse(inputOutputString);
222
+ if (typeof parsedIO !== "object" || parsedIO == null || !("messages" in parsedIO) && !("text" in parsedIO)) {
223
+ return inputOutputString;
243
224
  }
244
- if (span.errorInfo) {
245
- this.status = {
246
- code: api.SpanStatusCode.ERROR,
247
- message: span.errorInfo.message
248
- };
249
- this.events.push({
250
- name: "exception",
251
- attributes: {
252
- "exception.message": span.errorInfo.message,
253
- "exception.type": "Error",
254
- ...span.errorInfo.details?.stack && {
255
- "exception.stacktrace": span.errorInfo.details.stack
256
- }
257
- },
258
- time: this.startTime,
259
- droppedAttributesCount: 0
260
- });
261
- } else if (span.endTime) {
262
- this.status = { code: api.SpanStatusCode.OK };
263
- } else {
264
- this.status = { code: api.SpanStatusCode.UNSET };
265
- }
266
- if (span.isEvent) {
267
- this.events.push({
268
- name: "instant_event",
269
- attributes: {},
270
- time: this.startTime,
271
- droppedAttributesCount: 0
272
- });
225
+ if ("text" in parsedIO) {
226
+ return JSON.stringify([
227
+ {
228
+ role: "assistant",
229
+ parts: [{ type: "text", content: parsedIO.text }]
230
+ }
231
+ ]);
273
232
  }
274
- this.spanContext = () => ({
275
- traceId: span.traceId,
276
- spanId: span.id,
277
- traceFlags: api.TraceFlags.SAMPLED,
278
- isRemote: false
279
- });
280
- if (parentSpanId) {
281
- this.parentSpanContext = {
282
- traceId: span.traceId,
283
- spanId: parentSpanId,
284
- traceFlags: api.TraceFlags.SAMPLED,
285
- isRemote: false
286
- };
233
+ if (Array.isArray(parsedIO.messages)) {
234
+ return JSON.stringify(
235
+ parsedIO.messages.map((m) => {
236
+ if (!isMastraMessage(m)) {
237
+ return m;
238
+ }
239
+ const role = m.role;
240
+ let parts = [];
241
+ if (Array.isArray(m.content)) {
242
+ parts = m.content.map((c) => {
243
+ switch (c.type) {
244
+ case "text":
245
+ return {
246
+ type: "text",
247
+ content: c.text
248
+ };
249
+ case "tool-call":
250
+ return {
251
+ type: "tool_call",
252
+ id: c.toolCallId,
253
+ name: c.toolName,
254
+ arguments: JSON.stringify(c.input)
255
+ };
256
+ case "tool-result":
257
+ return {
258
+ type: "tool_call_response",
259
+ id: c.toolCallId,
260
+ name: c.toolName,
261
+ response: JSON.stringify(c.output.value)
262
+ };
263
+ default:
264
+ return c;
265
+ }
266
+ });
267
+ } else {
268
+ parts = [
269
+ {
270
+ type: "text",
271
+ content: m.content
272
+ }
273
+ ];
274
+ }
275
+ return {
276
+ role,
277
+ parts
278
+ };
279
+ })
280
+ );
287
281
  }
288
- this.resource = resource || {};
289
- this.instrumentationLibrary = instrumentationLibrary || {
290
- name: "@mastra/otel",
291
- version: "1.0.0"
292
- };
293
- this.instrumentationScope = this.instrumentationLibrary;
294
- }
295
- /**
296
- * Convert JavaScript Date to hrtime format
297
- */
298
- dateToHrTime(date) {
299
- const ms = date.getTime();
300
- const seconds = Math.floor(ms / 1e3);
301
- const nanoseconds = ms % 1e3 * 1e6;
302
- return [seconds, nanoseconds];
282
+ return inputOutputString;
283
+ } catch {
284
+ return inputOutputString;
303
285
  }
304
286
  };
305
287
 
306
- // src/span-converter.ts
307
- var SPAN_KIND_MAPPING = {
308
- // Model operations are CLIENT spans (calling external AI services)
309
- [observability$1.SpanType.MODEL_GENERATION]: api.SpanKind.CLIENT,
310
- [observability$1.SpanType.MODEL_CHUNK]: api.SpanKind.CLIENT,
311
- // MCP tool calls are CLIENT (external service calls)
312
- [observability$1.SpanType.MCP_TOOL_CALL]: api.SpanKind.CLIENT,
313
- // Root spans for agent/workflow are SERVER (entry points)
314
- [observability$1.SpanType.AGENT_RUN]: api.SpanKind.SERVER,
315
- [observability$1.SpanType.WORKFLOW_RUN]: api.SpanKind.SERVER
316
- };
317
- var SpanConverter = class {
318
- resource;
319
- instrumentationLibrary;
320
- constructor(resource) {
321
- this.resource = resource;
322
- this.instrumentationLibrary = {
323
- name: "@mastra/otel",
324
- version: "1.0.0"
325
- };
288
+ // src/gen-ai-semantics.ts
289
+ function formatUsageMetrics(usage) {
290
+ if (!usage) return {};
291
+ const metrics = {};
292
+ if (usage.inputTokens !== void 0) {
293
+ metrics[incubating.ATTR_GEN_AI_USAGE_INPUT_TOKENS] = usage.inputTokens;
326
294
  }
327
- /**
328
- * Convert a Mastra Span to an OpenTelemetry ReadableSpan
329
- * This preserves Mastra's trace and span IDs
330
- */
331
- convertSpan(Span) {
332
- const spanKind = this.getSpanKind(Span);
333
- const attributes = this.buildAttributes(Span);
334
- const spanName = this.buildSpanName(Span);
335
- const otelSpan = { ...Span, name: spanName };
336
- return new MastraReadableSpan(
337
- otelSpan,
338
- attributes,
339
- spanKind,
340
- Span.parentSpanId,
341
- // Use the parentSpanId from the Mastra span directly
342
- this.resource,
343
- this.instrumentationLibrary
344
- );
295
+ if (usage.outputTokens !== void 0) {
296
+ metrics[incubating.ATTR_GEN_AI_USAGE_OUTPUT_TOKENS] = usage.outputTokens;
345
297
  }
346
- /**
347
- * Get the appropriate SpanKind based on span type and context
348
- */
349
- getSpanKind(Span) {
350
- if (Span.isRootSpan) {
351
- if (Span.type === observability$1.SpanType.AGENT_RUN || Span.type === observability$1.SpanType.WORKFLOW_RUN) {
352
- return api.SpanKind.SERVER;
353
- }
298
+ if (usage.outputDetails?.reasoning !== void 0) {
299
+ metrics["gen_ai.usage.reasoning_tokens"] = usage.outputDetails.reasoning;
300
+ }
301
+ if (usage.inputDetails?.cacheRead !== void 0) {
302
+ metrics["gen_ai.usage.cached_input_tokens"] = usage.inputDetails.cacheRead;
303
+ }
304
+ if (usage.inputDetails?.cacheWrite !== void 0) {
305
+ metrics["gen_ai.usage.cache_write_tokens"] = usage.inputDetails.cacheWrite;
306
+ }
307
+ if (usage.inputDetails?.audio !== void 0) {
308
+ metrics["gen_ai.usage.audio_input_tokens"] = usage.inputDetails.audio;
309
+ }
310
+ if (usage.outputDetails?.audio !== void 0) {
311
+ metrics["gen_ai.usage.audio_output_tokens"] = usage.outputDetails.audio;
312
+ }
313
+ return metrics;
314
+ }
315
+ function getOperationName(span) {
316
+ switch (span.type) {
317
+ case observability.SpanType.MODEL_GENERATION:
318
+ return "chat";
319
+ case observability.SpanType.TOOL_CALL:
320
+ case observability.SpanType.MCP_TOOL_CALL:
321
+ return "execute_tool";
322
+ case observability.SpanType.AGENT_RUN:
323
+ return "invoke_agent";
324
+ case observability.SpanType.WORKFLOW_RUN:
325
+ return "invoke_workflow";
326
+ default:
327
+ return span.type.toLowerCase();
328
+ }
329
+ }
330
+ function sanitizeSpanName(name) {
331
+ return name.replace(/[^\p{L}\p{N}._ -]/gu, "");
332
+ }
333
+ function getSpanIdentifier(span) {
334
+ switch (span.type) {
335
+ case observability.SpanType.MODEL_GENERATION: {
336
+ const attrs = span.attributes;
337
+ return attrs?.model ?? "unknown";
354
338
  }
355
- return SPAN_KIND_MAPPING[Span.type] || api.SpanKind.INTERNAL;
339
+ default:
340
+ return span.entityName ?? span.entityId ?? "unknown";
356
341
  }
357
- /**
358
- * Build OTEL-compliant span name based on span type and attributes
359
- */
360
- buildSpanName(Span) {
361
- switch (Span.type) {
362
- case observability$1.SpanType.MODEL_GENERATION: {
363
- const attrs = Span.attributes;
364
- const operation = attrs?.resultType === "tool_selection" ? "tool_selection" : "chat";
365
- const model = attrs?.model || "unknown";
366
- return `${operation} ${model}`;
367
- }
368
- case observability$1.SpanType.TOOL_CALL:
369
- case observability$1.SpanType.MCP_TOOL_CALL: {
370
- const toolAttrs = Span.attributes;
371
- const toolName = toolAttrs?.toolId || "unknown";
372
- return `tool.execute ${toolName}`;
373
- }
374
- case observability$1.SpanType.AGENT_RUN: {
375
- const agentAttrs = Span.attributes;
376
- const agentId = agentAttrs?.agentId || "unknown";
377
- return `agent.${agentId}`;
378
- }
379
- case observability$1.SpanType.WORKFLOW_RUN: {
380
- const workflowAttrs = Span.attributes;
381
- const workflowId = workflowAttrs?.workflowId || "unknown";
382
- return `workflow.${workflowId}`;
383
- }
384
- case observability$1.SpanType.WORKFLOW_STEP:
385
- return Span.name;
386
- default:
387
- return Span.name;
342
+ }
343
+ function getSpanName(span) {
344
+ const identifier = getSpanIdentifier(span);
345
+ if (identifier) {
346
+ const operation = getOperationName(span);
347
+ return `${operation} ${identifier}`;
348
+ }
349
+ return sanitizeSpanName(span.name);
350
+ }
351
+ function getAttributes(span) {
352
+ const attributes = {};
353
+ const spanType = span.type.toLowerCase();
354
+ attributes[incubating.ATTR_GEN_AI_OPERATION_NAME] = getOperationName(span);
355
+ attributes["mastra.span.type"] = span.type;
356
+ if (span.input !== void 0) {
357
+ const inputStr = typeof span.input === "string" ? span.input : JSON.stringify(span.input);
358
+ if (span.type === observability.SpanType.MODEL_GENERATION) {
359
+ attributes[incubating.ATTR_GEN_AI_INPUT_MESSAGES] = convertMastraMessagesToGenAIMessages(inputStr);
360
+ } else if (span.type === observability.SpanType.TOOL_CALL || span.type === observability.SpanType.MCP_TOOL_CALL) {
361
+ attributes["gen_ai.tool.call.arguments"] = inputStr;
362
+ } else {
363
+ attributes[`mastra.${spanType}.input`] = inputStr;
388
364
  }
389
365
  }
390
- /**
391
- * Build OpenTelemetry attributes from Mastra Span
392
- * Following OTEL Semantic Conventions for GenAI
393
- */
394
- buildAttributes(Span) {
395
- const attributes = {};
396
- attributes["gen_ai.operation.name"] = this.getOperationName(Span);
397
- attributes["span.kind"] = this.getSpanKindString(Span);
398
- attributes["mastra.span.type"] = Span.type;
399
- attributes["mastra.trace_id"] = Span.traceId;
400
- attributes["mastra.span_id"] = Span.id;
401
- if (Span.parentSpanId) {
402
- attributes["mastra.parent_span_id"] = Span.parentSpanId;
403
- }
404
- if (Span.input !== void 0) {
405
- const inputStr = typeof Span.input === "string" ? Span.input : JSON.stringify(Span.input);
406
- attributes["input"] = inputStr;
407
- if (Span.type === observability$1.SpanType.MODEL_GENERATION) {
408
- attributes["gen_ai.prompt"] = inputStr;
409
- } else if (Span.type === observability$1.SpanType.TOOL_CALL || Span.type === observability$1.SpanType.MCP_TOOL_CALL) {
410
- attributes["gen_ai.tool.input"] = inputStr;
411
- }
366
+ if (span.output !== void 0) {
367
+ const outputStr = typeof span.output === "string" ? span.output : JSON.stringify(span.output);
368
+ if (span.type === observability.SpanType.MODEL_GENERATION) {
369
+ attributes[incubating.ATTR_GEN_AI_OUTPUT_MESSAGES] = convertMastraMessagesToGenAIMessages(outputStr);
370
+ } else if (span.type === observability.SpanType.TOOL_CALL || span.type === observability.SpanType.MCP_TOOL_CALL) {
371
+ attributes["gen_ai.tool.call.result"] = outputStr;
372
+ } else {
373
+ attributes[`mastra.${spanType}.output`] = outputStr;
412
374
  }
413
- if (Span.output !== void 0) {
414
- const outputStr = typeof Span.output === "string" ? Span.output : JSON.stringify(Span.output);
415
- attributes["output"] = outputStr;
416
- if (Span.type === observability$1.SpanType.MODEL_GENERATION) {
417
- attributes["gen_ai.completion"] = outputStr;
418
- } else if (Span.type === observability$1.SpanType.TOOL_CALL || Span.type === observability$1.SpanType.MCP_TOOL_CALL) {
419
- attributes["gen_ai.tool.output"] = outputStr;
420
- }
375
+ }
376
+ if (span.type === observability.SpanType.MODEL_GENERATION && span.attributes) {
377
+ const modelAttrs = span.attributes;
378
+ if (modelAttrs.model) {
379
+ attributes[incubating.ATTR_GEN_AI_REQUEST_MODEL] = modelAttrs.model;
380
+ }
381
+ if (modelAttrs.provider) {
382
+ attributes[incubating.ATTR_GEN_AI_PROVIDER_NAME] = normalizeProvider(modelAttrs.provider);
383
+ }
384
+ if (span.entityId) {
385
+ attributes[incubating.ATTR_GEN_AI_AGENT_ID] = span.entityId;
421
386
  }
422
- if (Span.type === observability$1.SpanType.MODEL_GENERATION && Span.attributes) {
423
- const modelAttrs = Span.attributes;
424
- if (modelAttrs.model) {
425
- attributes["gen_ai.request.model"] = modelAttrs.model;
387
+ if (span.entityName) {
388
+ attributes[incubating.ATTR_GEN_AI_AGENT_NAME] = span.entityName;
389
+ }
390
+ Object.assign(attributes, formatUsageMetrics(modelAttrs.usage));
391
+ if (modelAttrs.parameters) {
392
+ if (modelAttrs.parameters.temperature !== void 0) {
393
+ attributes[incubating.ATTR_GEN_AI_REQUEST_TEMPERATURE] = modelAttrs.parameters.temperature;
426
394
  }
427
- if (modelAttrs.provider) {
428
- attributes["gen_ai.system"] = modelAttrs.provider;
395
+ if (modelAttrs.parameters.maxOutputTokens !== void 0) {
396
+ attributes[incubating.ATTR_GEN_AI_REQUEST_MAX_TOKENS] = modelAttrs.parameters.maxOutputTokens;
429
397
  }
430
- if (modelAttrs.usage) {
431
- const inputTokens = modelAttrs.usage.inputTokens ?? modelAttrs.usage.promptTokens;
432
- const outputTokens = modelAttrs.usage.outputTokens ?? modelAttrs.usage.completionTokens;
433
- if (inputTokens !== void 0) {
434
- attributes["gen_ai.usage.input_tokens"] = inputTokens;
435
- }
436
- if (outputTokens !== void 0) {
437
- attributes["gen_ai.usage.output_tokens"] = outputTokens;
438
- }
439
- if (modelAttrs.usage.totalTokens !== void 0) {
440
- attributes["gen_ai.usage.total_tokens"] = modelAttrs.usage.totalTokens;
441
- }
442
- if (modelAttrs.usage.reasoningTokens !== void 0) {
443
- attributes["gen_ai.usage.reasoning_tokens"] = modelAttrs.usage.reasoningTokens;
444
- }
445
- if (modelAttrs.usage.cachedInputTokens !== void 0) {
446
- attributes["gen_ai.usage.cached_input_tokens"] = modelAttrs.usage.cachedInputTokens;
447
- }
398
+ if (modelAttrs.parameters.topP !== void 0) {
399
+ attributes[incubating.ATTR_GEN_AI_REQUEST_TOP_P] = modelAttrs.parameters.topP;
448
400
  }
449
- if (modelAttrs.parameters) {
450
- if (modelAttrs.parameters.temperature !== void 0) {
451
- attributes["gen_ai.request.temperature"] = modelAttrs.parameters.temperature;
452
- }
453
- if (modelAttrs.parameters.maxOutputTokens !== void 0) {
454
- attributes["gen_ai.request.max_tokens"] = modelAttrs.parameters.maxOutputTokens;
455
- }
456
- if (modelAttrs.parameters.topP !== void 0) {
457
- attributes["gen_ai.request.top_p"] = modelAttrs.parameters.topP;
458
- }
459
- if (modelAttrs.parameters.topK !== void 0) {
460
- attributes["gen_ai.request.top_k"] = modelAttrs.parameters.topK;
461
- }
462
- if (modelAttrs.parameters.presencePenalty !== void 0) {
463
- attributes["gen_ai.request.presence_penalty"] = modelAttrs.parameters.presencePenalty;
464
- }
465
- if (modelAttrs.parameters.frequencyPenalty !== void 0) {
466
- attributes["gen_ai.request.frequency_penalty"] = modelAttrs.parameters.frequencyPenalty;
467
- }
468
- if (modelAttrs.parameters.stopSequences) {
469
- attributes["gen_ai.request.stop_sequences"] = JSON.stringify(modelAttrs.parameters.stopSequences);
470
- }
401
+ if (modelAttrs.parameters.topK !== void 0) {
402
+ attributes[incubating.ATTR_GEN_AI_REQUEST_TOP_K] = modelAttrs.parameters.topK;
471
403
  }
472
- if (modelAttrs.finishReason) {
473
- attributes["gen_ai.response.finish_reasons"] = modelAttrs.finishReason;
404
+ if (modelAttrs.parameters.presencePenalty !== void 0) {
405
+ attributes[incubating.ATTR_GEN_AI_REQUEST_PRESENCE_PENALTY] = modelAttrs.parameters.presencePenalty;
474
406
  }
475
- }
476
- if ((Span.type === observability$1.SpanType.TOOL_CALL || Span.type === observability$1.SpanType.MCP_TOOL_CALL) && Span.attributes) {
477
- const toolAttrs = Span.attributes;
478
- if (toolAttrs.toolId) {
479
- attributes["gen_ai.tool.name"] = toolAttrs.toolId;
407
+ if (modelAttrs.parameters.frequencyPenalty !== void 0) {
408
+ attributes[incubating.ATTR_GEN_AI_REQUEST_FREQUENCY_PENALTY] = modelAttrs.parameters.frequencyPenalty;
480
409
  }
481
- if (Span.type === observability$1.SpanType.MCP_TOOL_CALL) {
482
- const mcpAttrs = toolAttrs;
483
- if (mcpAttrs.mcpServer) {
484
- attributes["mcp.server"] = mcpAttrs.mcpServer;
485
- }
486
- if (mcpAttrs.serverVersion) {
487
- attributes["mcp.server.version"] = mcpAttrs.serverVersion;
488
- }
489
- } else {
490
- if (toolAttrs.toolDescription) {
491
- attributes["gen_ai.tool.description"] = toolAttrs.toolDescription;
492
- }
410
+ if (modelAttrs.parameters.stopSequences) {
411
+ attributes[incubating.ATTR_GEN_AI_REQUEST_STOP_SEQUENCES] = JSON.stringify(modelAttrs.parameters.stopSequences);
493
412
  }
494
- if (toolAttrs.success !== void 0) {
495
- attributes["gen_ai.tool.success"] = toolAttrs.success;
413
+ if (modelAttrs.parameters.seed) {
414
+ attributes[incubating.ATTR_GEN_AI_REQUEST_SEED] = modelAttrs.parameters.seed;
496
415
  }
497
416
  }
498
- if (Span.type === observability$1.SpanType.AGENT_RUN && Span.attributes) {
499
- const agentAttrs = Span.attributes;
500
- if (agentAttrs.agentId) {
501
- attributes["agent.id"] = agentAttrs.agentId;
502
- attributes["gen_ai.agent.id"] = agentAttrs.agentId;
417
+ if (modelAttrs.finishReason) {
418
+ attributes[incubating.ATTR_GEN_AI_RESPONSE_FINISH_REASONS] = JSON.stringify([modelAttrs.finishReason]);
419
+ }
420
+ if (modelAttrs.responseModel) {
421
+ attributes[incubating.ATTR_GEN_AI_RESPONSE_MODEL] = modelAttrs.responseModel;
422
+ }
423
+ if (modelAttrs.responseId) {
424
+ attributes[incubating.ATTR_GEN_AI_RESPONSE_ID] = modelAttrs.responseId;
425
+ }
426
+ if (modelAttrs.serverAddress) {
427
+ attributes[incubating.ATTR_SERVER_ADDRESS] = modelAttrs.serverAddress;
428
+ }
429
+ if (modelAttrs.serverPort !== void 0) {
430
+ attributes[incubating.ATTR_SERVER_PORT] = modelAttrs.serverPort;
431
+ }
432
+ }
433
+ if ((span.type === observability.SpanType.TOOL_CALL || span.type === observability.SpanType.MCP_TOOL_CALL) && span.attributes) {
434
+ attributes[incubating.ATTR_GEN_AI_TOOL_NAME] = span.entityName ?? span.entityId;
435
+ if (span.type === observability.SpanType.MCP_TOOL_CALL) {
436
+ const mcpAttrs = span.attributes;
437
+ if (mcpAttrs.mcpServer) {
438
+ attributes[incubating.ATTR_SERVER_ADDRESS] = mcpAttrs.mcpServer;
503
439
  }
504
- if (agentAttrs.maxSteps) {
505
- attributes["agent.max_steps"] = agentAttrs.maxSteps;
440
+ } else {
441
+ const toolAttrs = span.attributes;
442
+ if (toolAttrs.toolDescription) {
443
+ attributes[incubating.ATTR_GEN_AI_TOOL_DESCRIPTION] = toolAttrs.toolDescription;
506
444
  }
507
- if (agentAttrs.availableTools) {
508
- attributes["agent.available_tools"] = JSON.stringify(agentAttrs.availableTools);
445
+ if (toolAttrs.toolType) {
446
+ attributes["gen_ai.tool.type"] = toolAttrs.toolType;
509
447
  }
510
448
  }
511
- if (Span.type === observability$1.SpanType.WORKFLOW_RUN && Span.attributes) {
512
- const workflowAttrs = Span.attributes;
513
- if (workflowAttrs.workflowId) {
514
- attributes["workflow.id"] = workflowAttrs.workflowId;
515
- }
516
- if (workflowAttrs.status) {
517
- attributes["workflow.status"] = workflowAttrs.status;
518
- }
449
+ }
450
+ if (span.type === observability.SpanType.AGENT_RUN && span.attributes) {
451
+ const agentAttrs = span.attributes;
452
+ if (span.entityId) {
453
+ attributes[incubating.ATTR_GEN_AI_AGENT_ID] = span.entityId;
519
454
  }
520
- if (Span.errorInfo) {
521
- attributes["error"] = true;
522
- attributes["error.type"] = Span.errorInfo.id || "unknown";
523
- attributes["error.message"] = Span.errorInfo.message;
524
- if (Span.errorInfo.domain) {
525
- attributes["error.domain"] = Span.errorInfo.domain;
526
- }
527
- if (Span.errorInfo.category) {
528
- attributes["error.category"] = Span.errorInfo.category;
529
- }
455
+ if (span.entityName) {
456
+ attributes[incubating.ATTR_GEN_AI_AGENT_NAME] = span.entityName;
530
457
  }
531
- if (Span.metadata) {
532
- Object.entries(Span.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
- });
458
+ if (agentAttrs.conversationId) {
459
+ attributes[incubating.ATTR_GEN_AI_CONVERSATION_ID] = agentAttrs.conversationId;
460
+ }
461
+ if (agentAttrs.maxSteps) {
462
+ attributes[`mastra.${spanType}.max_steps`] = agentAttrs.maxSteps;
544
463
  }
545
- if (Span.startTime) {
546
- attributes["mastra.start_time"] = Span.startTime.toISOString();
464
+ if (agentAttrs.availableTools) {
465
+ attributes[`gen_ai.tool.definitions`] = JSON.stringify(agentAttrs.availableTools);
547
466
  }
548
- if (Span.endTime) {
549
- attributes["mastra.end_time"] = Span.endTime.toISOString();
550
- const duration = Span.endTime.getTime() - Span.startTime.getTime();
551
- attributes["mastra.duration_ms"] = duration;
467
+ attributes[incubating.ATTR_GEN_AI_SYSTEM_INSTRUCTIONS] = agentAttrs.instructions;
468
+ }
469
+ if (span.errorInfo) {
470
+ attributes[incubating.ATTR_ERROR_TYPE] = span.errorInfo.id || "unknown";
471
+ attributes[incubating.ATTR_ERROR_MESSAGE] = span.errorInfo.message;
472
+ if (span.errorInfo.domain) {
473
+ attributes["error.domain"] = span.errorInfo.domain;
552
474
  }
553
- return attributes;
475
+ if (span.errorInfo.category) {
476
+ attributes["error.category"] = span.errorInfo.category;
477
+ }
478
+ }
479
+ return attributes;
480
+ }
481
+ var PROVIDER_ALIASES = {
482
+ anthropic: ["anthropic", "claude"],
483
+ "aws.bedrock": ["awsbedrock", "bedrock", "amazonbedrock"],
484
+ "azure.ai.inference": ["azureaiinference", "azureinference"],
485
+ "azure.ai.openai": ["azureaiopenai", "azureopenai", "msopenai", "microsoftopenai"],
486
+ cohere: ["cohere"],
487
+ deepseek: ["deepseek"],
488
+ "gcp.gemini": ["gcpgemini", "gemini"],
489
+ "gcp.gen_ai": ["gcpgenai", "googlegenai", "googleai"],
490
+ "gcp.vertex_ai": ["gcpvertexai", "vertexai"],
491
+ groq: ["groq"],
492
+ "ibm.watsonx.ai": ["ibmwatsonxai", "watsonx", "watsonxai"],
493
+ mistral_ai: ["mistral", "mistralai"],
494
+ openai: ["openai", "oai"],
495
+ perplexity: ["perplexity", "pplx"],
496
+ x_ai: ["xai", "x-ai", "x_ai", "x.com ai"]
497
+ };
498
+ function normalizeProviderString(input) {
499
+ return input.toLowerCase().replace(/[^a-z0-9]/g, "");
500
+ }
501
+ function normalizeProvider(providerName) {
502
+ const normalized = normalizeProviderString(providerName);
503
+ for (const [canonical, aliases] of Object.entries(PROVIDER_ALIASES)) {
504
+ for (const alias of aliases) {
505
+ if (normalized === alias) {
506
+ return canonical;
507
+ }
508
+ }
509
+ }
510
+ return providerName.toLowerCase();
511
+ }
512
+
513
+ // src/span-converter.ts
514
+ var SpanConverter = class {
515
+ constructor(params) {
516
+ this.params = params;
517
+ this.format = params.format;
554
518
  }
519
+ resource;
520
+ scope;
521
+ initPromise;
522
+ format;
555
523
  /**
556
- * Get the operation name based on span type for gen_ai.operation.name
524
+ * Lazily initialize resource & scope on first use.
525
+ * Subsequent calls reuse the same promise (no races).
557
526
  */
558
- getOperationName(Span) {
559
- switch (Span.type) {
560
- case observability$1.SpanType.MODEL_GENERATION: {
561
- const attrs = Span.attributes;
562
- return attrs?.resultType === "tool_selection" ? "tool_selection" : "chat";
563
- }
564
- case observability$1.SpanType.TOOL_CALL:
565
- case observability$1.SpanType.MCP_TOOL_CALL:
566
- return "tool.execute";
567
- case observability$1.SpanType.AGENT_RUN:
568
- return "agent.run";
569
- case observability$1.SpanType.WORKFLOW_RUN:
570
- return "workflow.run";
571
- default:
572
- return Span.type.replace(/_/g, ".");
527
+ async initIfNeeded() {
528
+ if (this.initPromise) {
529
+ return this.initPromise;
573
530
  }
531
+ this.initPromise = (async () => {
532
+ const packageVersion = await getPackageVersion(this.params.packageName) ?? "unknown";
533
+ const serviceVersion = await getPackageVersion("@mastra/core") ?? "unknown";
534
+ let resource = resources.resourceFromAttributes({
535
+ [semanticConventions.ATTR_SERVICE_NAME]: this.params.serviceName || "mastra-service",
536
+ [semanticConventions.ATTR_SERVICE_VERSION]: serviceVersion,
537
+ [semanticConventions.ATTR_TELEMETRY_SDK_NAME]: this.params.packageName,
538
+ [semanticConventions.ATTR_TELEMETRY_SDK_VERSION]: packageVersion,
539
+ [semanticConventions.ATTR_TELEMETRY_SDK_LANGUAGE]: "nodejs"
540
+ });
541
+ if (this.params.config?.resourceAttributes) {
542
+ resource = resource.merge(
543
+ // Duplicate attributes from config will override defaults above
544
+ resources.resourceFromAttributes(this.params.config.resourceAttributes)
545
+ );
546
+ }
547
+ this.resource = resource;
548
+ this.scope = {
549
+ name: this.params.packageName,
550
+ version: packageVersion
551
+ };
552
+ })();
553
+ return this.initPromise;
574
554
  }
575
555
  /**
576
- * Get span kind as string for attribute
556
+ * Convert a Mastra Span to an OpenTelemetry ReadableSpan
577
557
  */
578
- getSpanKindString(Span) {
579
- const kind = this.getSpanKind(Span);
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";
558
+ async convertSpan(span) {
559
+ await this.initIfNeeded();
560
+ if (!this.resource || !this.scope) {
561
+ throw new Error("SpanConverter not initialized correctly");
562
+ }
563
+ const name = getSpanName(span);
564
+ const kind = getSpanKind(span.type);
565
+ const attributes = getAttributes(span);
566
+ if (span.metadata) {
567
+ for (const [k, v] of Object.entries(span.metadata)) {
568
+ if (v === null || v === void 0) {
569
+ continue;
570
+ }
571
+ attributes[`mastra.metadata.${k}`] = typeof v === "object" ? JSON.stringify(v) : v;
572
+ }
593
573
  }
574
+ if (span.isRootSpan && span.tags?.length) {
575
+ attributes["mastra.tags"] = JSON.stringify(span.tags);
576
+ }
577
+ const startTime = dateToHrTime(span.startTime);
578
+ const endTime = span.endTime ? dateToHrTime(span.endTime) : startTime;
579
+ const duration = computeDuration(span.startTime, span.endTime);
580
+ const { status, events } = buildStatusAndEvents(span, startTime);
581
+ const spanContext = {
582
+ traceId: span.traceId,
583
+ spanId: span.id,
584
+ traceFlags: api.TraceFlags.SAMPLED,
585
+ isRemote: false
586
+ };
587
+ const parentSpanContext = span.parentSpanId ? {
588
+ traceId: span.traceId,
589
+ spanId: span.parentSpanId,
590
+ traceFlags: api.TraceFlags.SAMPLED,
591
+ isRemote: false
592
+ } : void 0;
593
+ const links = [];
594
+ const readable = {
595
+ name,
596
+ kind,
597
+ spanContext: () => spanContext,
598
+ parentSpanContext,
599
+ startTime,
600
+ endTime,
601
+ status,
602
+ attributes,
603
+ links,
604
+ events,
605
+ duration,
606
+ ended: !!span.endTime,
607
+ resource: this.resource,
608
+ instrumentationScope: this.scope,
609
+ droppedAttributesCount: 0,
610
+ droppedEventsCount: 0,
611
+ droppedLinksCount: 0
612
+ };
613
+ return readable;
594
614
  }
595
615
  };
616
+ async function getPackageVersion(pkgName) {
617
+ try {
618
+ const manifestUrl = new URL(await undefined(`${pkgName}/package.json`));
619
+ const path = url.fileURLToPath(manifestUrl);
620
+ const pkgJson = JSON.parse(fs.readFileSync(path, "utf8"));
621
+ return pkgJson.version;
622
+ } catch {
623
+ return void 0;
624
+ }
625
+ }
626
+ function getSpanKind(type) {
627
+ switch (type) {
628
+ case observability.SpanType.MODEL_GENERATION:
629
+ case observability.SpanType.MCP_TOOL_CALL:
630
+ return api.SpanKind.CLIENT;
631
+ default:
632
+ return api.SpanKind.INTERNAL;
633
+ }
634
+ }
635
+ function dateToHrTime(date) {
636
+ const ms = date.getTime();
637
+ const seconds = Math.floor(ms / 1e3);
638
+ const nanoseconds = ms % 1e3 * 1e6;
639
+ return [seconds, nanoseconds];
640
+ }
641
+ function computeDuration(start, end) {
642
+ if (!end) return [0, 0];
643
+ const diffMs = end.getTime() - start.getTime();
644
+ return [Math.floor(diffMs / 1e3), diffMs % 1e3 * 1e6];
645
+ }
646
+ function buildStatusAndEvents(span, defaultTime) {
647
+ const events = [];
648
+ if (span.errorInfo) {
649
+ const status = {
650
+ code: api.SpanStatusCode.ERROR,
651
+ message: span.errorInfo.message
652
+ };
653
+ events.push({
654
+ name: "exception",
655
+ attributes: {
656
+ "exception.message": span.errorInfo.message,
657
+ "exception.type": "Error",
658
+ ...span.errorInfo.details?.stack && {
659
+ "exception.stacktrace": span.errorInfo.details.stack
660
+ }
661
+ },
662
+ time: defaultTime,
663
+ droppedAttributesCount: 0
664
+ });
665
+ return { status, events };
666
+ }
667
+ if (span.endTime) {
668
+ return {
669
+ status: { code: api.SpanStatusCode.OK },
670
+ events
671
+ };
672
+ }
673
+ return {
674
+ status: { code: api.SpanStatusCode.UNSET },
675
+ events
676
+ };
677
+ }
596
678
 
597
679
  // src/tracing.ts
598
- var OtelExporter = class extends observability.BaseExporter {
680
+ var OtelExporter = class extends observability$1.BaseExporter {
599
681
  config;
600
- tracingConfig;
682
+ observabilityConfig;
601
683
  spanConverter;
602
684
  processor;
603
685
  exporter;
@@ -606,7 +688,6 @@ var OtelExporter = class extends observability.BaseExporter {
606
688
  constructor(config) {
607
689
  super(config);
608
690
  this.config = config;
609
- this.spanConverter = new SpanConverter();
610
691
  if (config.logLevel === "debug") {
611
692
  api.diag.setLogger(new api.DiagConsoleLogger(), api.DiagLogLevel.DEBUG);
612
693
  }
@@ -615,7 +696,7 @@ var OtelExporter = class extends observability.BaseExporter {
615
696
  * Initialize with tracing configuration
616
697
  */
617
698
  init(options) {
618
- this.tracingConfig = options.config;
699
+ this.observabilityConfig = options.config;
619
700
  }
620
701
  async setupExporter() {
621
702
  if (this.isSetup || this.exporter) return;
@@ -693,21 +774,12 @@ var OtelExporter = class extends observability.BaseExporter {
693
774
  }
694
775
  async setupProcessor() {
695
776
  if (this.processor || this.isSetup) return;
696
- let resource = resources.resourceFromAttributes({
697
- [semanticConventions.ATTR_SERVICE_NAME]: this.tracingConfig?.serviceName || "mastra-service",
698
- [semanticConventions.ATTR_SERVICE_VERSION]: "1.0.0",
699
- // Add telemetry SDK information
700
- [semanticConventions.ATTR_TELEMETRY_SDK_NAME]: "@mastra/otel-exporter",
701
- [semanticConventions.ATTR_TELEMETRY_SDK_VERSION]: "1.0.0",
702
- [semanticConventions.ATTR_TELEMETRY_SDK_LANGUAGE]: "nodejs"
777
+ this.spanConverter = new SpanConverter({
778
+ packageName: "@mastra/otel-exporter",
779
+ serviceName: this.observabilityConfig?.serviceName,
780
+ config: this.config,
781
+ format: "GenAI_v1_38_0"
703
782
  });
704
- if (this.config.resourceAttributes) {
705
- resource = resource.merge(
706
- // Duplicate attributes from config will override defaults above
707
- resources.resourceFromAttributes(this.config.resourceAttributes)
708
- );
709
- }
710
- this.spanConverter = new SpanConverter(resource);
711
783
  this.processor = new sdkTraceBase.BatchSpanProcessor(this.exporter, {
712
784
  maxExportBatchSize: this.config.batchSize || 512,
713
785
  // Default batch size
@@ -729,7 +801,7 @@ var OtelExporter = class extends observability.BaseExporter {
729
801
  this.isSetup = true;
730
802
  }
731
803
  async _exportTracingEvent(event) {
732
- if (event.type !== observability$1.TracingEventType.SPAN_ENDED) {
804
+ if (event.type !== observability.TracingEventType.SPAN_ENDED) {
733
805
  return;
734
806
  }
735
807
  const span = event.exportedSpan;
@@ -743,9 +815,9 @@ var OtelExporter = class extends observability.BaseExporter {
743
815
  return;
744
816
  }
745
817
  try {
746
- const readableSpan = this.spanConverter.convertSpan(span);
818
+ const otelSpan = await this.spanConverter.convertSpan(span);
747
819
  await new Promise((resolve) => {
748
- this.processor.onEnd(readableSpan);
820
+ this.processor.onEnd(otelSpan);
749
821
  resolve();
750
822
  });
751
823
  this.logger.debug(
@@ -763,5 +835,7 @@ var OtelExporter = class extends observability.BaseExporter {
763
835
  };
764
836
 
765
837
  exports.OtelExporter = OtelExporter;
838
+ exports.SpanConverter = SpanConverter;
839
+ exports.getSpanKind = getSpanKind;
766
840
  //# sourceMappingURL=index.cjs.map
767
841
  //# sourceMappingURL=index.cjs.map