@mastra/otel-exporter 0.0.0-main-test-2-20251127211532 → 0.0.0-mastra-auto-detect-server-20260108233416
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 +332 -12
- package/README.md +188 -68
- package/dist/gen-ai-messages.d.ts +26 -0
- package/dist/gen-ai-messages.d.ts.map +1 -0
- package/dist/gen-ai-semantics.d.ts +40 -0
- package/dist/gen-ai-semantics.d.ts.map +1 -0
- package/dist/index.cjs +491 -391
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +493 -392
- package/dist/index.js.map +1 -1
- package/dist/span-converter.d.ts +26 -35
- package/dist/span-converter.d.ts.map +1 -1
- package/dist/tracing.d.ts +2 -2
- package/dist/tracing.d.ts.map +1 -1
- package/package.json +9 -10
- package/dist/mastra-span.d.ts +0 -38
- package/dist/mastra-span.d.ts.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { SpanType, TracingEventType } from '@mastra/core/observability';
|
|
2
2
|
import { BaseExporter } from '@mastra/observability';
|
|
3
|
-
import {
|
|
4
|
-
import { resourceFromAttributes } from '@opentelemetry/resources';
|
|
3
|
+
import { TraceFlags, SpanKind, SpanStatusCode, diag, DiagConsoleLogger, DiagLogLevel } from '@opentelemetry/api';
|
|
5
4
|
import { BatchSpanProcessor } from '@opentelemetry/sdk-trace-base';
|
|
5
|
+
import { readFileSync } from 'fs';
|
|
6
|
+
import { fileURLToPath } from 'url';
|
|
7
|
+
import { resourceFromAttributes } from '@opentelemetry/resources';
|
|
6
8
|
import { ATTR_TELEMETRY_SDK_LANGUAGE, ATTR_TELEMETRY_SDK_VERSION, ATTR_TELEMETRY_SDK_NAME, ATTR_SERVICE_VERSION, ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
|
|
9
|
+
import { ATTR_GEN_AI_OPERATION_NAME, ATTR_GEN_AI_INPUT_MESSAGES, ATTR_GEN_AI_OUTPUT_MESSAGES, ATTR_GEN_AI_REQUEST_MODEL, ATTR_GEN_AI_PROVIDER_NAME, ATTR_GEN_AI_AGENT_ID, ATTR_GEN_AI_AGENT_NAME, ATTR_GEN_AI_REQUEST_TEMPERATURE, ATTR_GEN_AI_REQUEST_MAX_TOKENS, ATTR_GEN_AI_REQUEST_TOP_P, ATTR_GEN_AI_REQUEST_TOP_K, ATTR_GEN_AI_REQUEST_PRESENCE_PENALTY, ATTR_GEN_AI_REQUEST_FREQUENCY_PENALTY, ATTR_GEN_AI_REQUEST_STOP_SEQUENCES, ATTR_GEN_AI_REQUEST_SEED, ATTR_GEN_AI_RESPONSE_FINISH_REASONS, ATTR_GEN_AI_RESPONSE_MODEL, ATTR_GEN_AI_RESPONSE_ID, ATTR_SERVER_ADDRESS, ATTR_SERVER_PORT, ATTR_GEN_AI_TOOL_NAME, ATTR_GEN_AI_TOOL_DESCRIPTION, ATTR_GEN_AI_CONVERSATION_ID, ATTR_GEN_AI_SYSTEM_INSTRUCTIONS, ATTR_ERROR_TYPE, ATTR_ERROR_MESSAGE, ATTR_GEN_AI_USAGE_INPUT_TOKENS, ATTR_GEN_AI_USAGE_OUTPUT_TOKENS } from '@opentelemetry/semantic-conventions/incubating';
|
|
7
10
|
|
|
8
11
|
// src/tracing.ts
|
|
9
12
|
|
|
@@ -101,24 +104,31 @@ function resolveProviderConfig(config) {
|
|
|
101
104
|
}
|
|
102
105
|
}
|
|
103
106
|
function resolveDash0Config(config) {
|
|
104
|
-
|
|
105
|
-
|
|
107
|
+
const apiKey = config.apiKey ?? process.env.DASH0_API_KEY;
|
|
108
|
+
const configEndpoint = config.endpoint ?? process.env.DASH0_ENDPOINT;
|
|
109
|
+
const dataset = config.dataset ?? process.env.DASH0_DATASET;
|
|
110
|
+
if (!apiKey) {
|
|
111
|
+
console.error(
|
|
112
|
+
"[OtelExporter] Dash0 configuration requires apiKey. Set DASH0_API_KEY environment variable or pass it in config. Tracing will be disabled."
|
|
113
|
+
);
|
|
106
114
|
return null;
|
|
107
115
|
}
|
|
108
|
-
if (!
|
|
109
|
-
console.error(
|
|
116
|
+
if (!configEndpoint) {
|
|
117
|
+
console.error(
|
|
118
|
+
"[OtelExporter] Dash0 configuration requires endpoint. Set DASH0_ENDPOINT environment variable or pass it in config. Tracing will be disabled."
|
|
119
|
+
);
|
|
110
120
|
return null;
|
|
111
121
|
}
|
|
112
|
-
let endpoint =
|
|
122
|
+
let endpoint = configEndpoint;
|
|
113
123
|
if (!endpoint.includes("/v1/traces")) {
|
|
114
124
|
endpoint = `${endpoint}/v1/traces`;
|
|
115
125
|
}
|
|
116
126
|
const headers = {
|
|
117
|
-
authorization: `Bearer ${
|
|
127
|
+
authorization: `Bearer ${apiKey}`
|
|
118
128
|
// lowercase for gRPC metadata
|
|
119
129
|
};
|
|
120
|
-
if (
|
|
121
|
-
headers["dash0-dataset"] =
|
|
130
|
+
if (dataset) {
|
|
131
|
+
headers["dash0-dataset"] = dataset;
|
|
122
132
|
}
|
|
123
133
|
return {
|
|
124
134
|
endpoint,
|
|
@@ -128,44 +138,58 @@ function resolveDash0Config(config) {
|
|
|
128
138
|
};
|
|
129
139
|
}
|
|
130
140
|
function resolveSignozConfig(config) {
|
|
131
|
-
|
|
132
|
-
|
|
141
|
+
const apiKey = config.apiKey ?? process.env.SIGNOZ_API_KEY;
|
|
142
|
+
const region = config.region ?? process.env.SIGNOZ_REGION;
|
|
143
|
+
const configEndpoint = config.endpoint ?? process.env.SIGNOZ_ENDPOINT;
|
|
144
|
+
if (!apiKey) {
|
|
145
|
+
console.error(
|
|
146
|
+
"[OtelExporter] SigNoz configuration requires apiKey. Set SIGNOZ_API_KEY environment variable or pass it in config. Tracing will be disabled."
|
|
147
|
+
);
|
|
133
148
|
return null;
|
|
134
149
|
}
|
|
135
|
-
const endpoint =
|
|
150
|
+
const endpoint = configEndpoint || `https://ingest.${region || "us"}.signoz.cloud:443/v1/traces`;
|
|
136
151
|
return {
|
|
137
152
|
endpoint,
|
|
138
153
|
headers: {
|
|
139
|
-
"signoz-ingestion-key":
|
|
154
|
+
"signoz-ingestion-key": apiKey
|
|
140
155
|
},
|
|
141
156
|
protocol: "http/protobuf"
|
|
142
157
|
};
|
|
143
158
|
}
|
|
144
159
|
function resolveNewRelicConfig(config) {
|
|
145
|
-
|
|
146
|
-
|
|
160
|
+
const apiKey = config.apiKey ?? process.env.NEW_RELIC_LICENSE_KEY;
|
|
161
|
+
const configEndpoint = config.endpoint ?? process.env.NEW_RELIC_ENDPOINT;
|
|
162
|
+
if (!apiKey) {
|
|
163
|
+
console.error(
|
|
164
|
+
"[OtelExporter] New Relic configuration requires apiKey (license key). Set NEW_RELIC_LICENSE_KEY environment variable or pass it in config. Tracing will be disabled."
|
|
165
|
+
);
|
|
147
166
|
return null;
|
|
148
167
|
}
|
|
149
|
-
const endpoint =
|
|
168
|
+
const endpoint = configEndpoint || "https://otlp.nr-data.net:443/v1/traces";
|
|
150
169
|
return {
|
|
151
170
|
endpoint,
|
|
152
171
|
headers: {
|
|
153
|
-
"api-key":
|
|
172
|
+
"api-key": apiKey
|
|
154
173
|
},
|
|
155
174
|
protocol: "http/protobuf"
|
|
156
175
|
};
|
|
157
176
|
}
|
|
158
177
|
function resolveTraceloopConfig(config) {
|
|
159
|
-
|
|
160
|
-
|
|
178
|
+
const apiKey = config.apiKey ?? process.env.TRACELOOP_API_KEY;
|
|
179
|
+
const destinationId = config.destinationId ?? process.env.TRACELOOP_DESTINATION_ID;
|
|
180
|
+
const configEndpoint = config.endpoint ?? process.env.TRACELOOP_ENDPOINT;
|
|
181
|
+
if (!apiKey) {
|
|
182
|
+
console.error(
|
|
183
|
+
"[OtelExporter] Traceloop configuration requires apiKey. Set TRACELOOP_API_KEY environment variable or pass it in config. Tracing will be disabled."
|
|
184
|
+
);
|
|
161
185
|
return null;
|
|
162
186
|
}
|
|
163
|
-
const endpoint =
|
|
187
|
+
const endpoint = configEndpoint || "https://api.traceloop.com/v1/traces";
|
|
164
188
|
const headers = {
|
|
165
|
-
Authorization: `Bearer ${
|
|
189
|
+
Authorization: `Bearer ${apiKey}`
|
|
166
190
|
};
|
|
167
|
-
if (
|
|
168
|
-
headers["x-traceloop-destination-id"] =
|
|
191
|
+
if (destinationId) {
|
|
192
|
+
headers["x-traceloop-destination-id"] = destinationId;
|
|
169
193
|
}
|
|
170
194
|
return {
|
|
171
195
|
endpoint,
|
|
@@ -174,16 +198,21 @@ function resolveTraceloopConfig(config) {
|
|
|
174
198
|
};
|
|
175
199
|
}
|
|
176
200
|
function resolveLaminarConfig(config) {
|
|
177
|
-
|
|
178
|
-
|
|
201
|
+
const apiKey = config.apiKey ?? process.env.LMNR_PROJECT_API_KEY;
|
|
202
|
+
const teamId = config.teamId ?? process.env.LAMINAR_TEAM_ID;
|
|
203
|
+
const configEndpoint = config.endpoint ?? process.env.LAMINAR_ENDPOINT;
|
|
204
|
+
if (!apiKey) {
|
|
205
|
+
console.error(
|
|
206
|
+
"[OtelExporter] Laminar configuration requires apiKey. Set LMNR_PROJECT_API_KEY environment variable or pass it in config. Tracing will be disabled."
|
|
207
|
+
);
|
|
179
208
|
return null;
|
|
180
209
|
}
|
|
181
|
-
const endpoint =
|
|
210
|
+
const endpoint = configEndpoint || "https://api.lmnr.ai/v1/traces";
|
|
182
211
|
const headers = {
|
|
183
|
-
Authorization: `Bearer ${
|
|
212
|
+
Authorization: `Bearer ${apiKey}`
|
|
184
213
|
};
|
|
185
|
-
if (
|
|
186
|
-
headers["x-laminar-team-id"] =
|
|
214
|
+
if (teamId) {
|
|
215
|
+
headers["x-laminar-team-id"] = teamId;
|
|
187
216
|
}
|
|
188
217
|
return {
|
|
189
218
|
endpoint,
|
|
@@ -203,396 +232,478 @@ function resolveCustomConfig(config) {
|
|
|
203
232
|
protocol: config.protocol || "http/json"
|
|
204
233
|
};
|
|
205
234
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
ended;
|
|
220
|
-
resource;
|
|
221
|
-
instrumentationLibrary;
|
|
222
|
-
instrumentationScope;
|
|
223
|
-
droppedAttributesCount = 0;
|
|
224
|
-
droppedEventsCount = 0;
|
|
225
|
-
droppedLinksCount = 0;
|
|
226
|
-
constructor(span, attributes, kind, parentSpanId, resource, instrumentationLibrary) {
|
|
227
|
-
this.name = span.name;
|
|
228
|
-
this.kind = kind;
|
|
229
|
-
this.attributes = attributes;
|
|
230
|
-
this.parentSpanId = parentSpanId;
|
|
231
|
-
this.links = [];
|
|
232
|
-
this.events = [];
|
|
233
|
-
this.startTime = this.dateToHrTime(span.startTime);
|
|
234
|
-
this.endTime = span.endTime ? this.dateToHrTime(span.endTime) : this.startTime;
|
|
235
|
-
this.ended = !!span.endTime;
|
|
236
|
-
if (span.endTime) {
|
|
237
|
-
const durationMs = span.endTime.getTime() - span.startTime.getTime();
|
|
238
|
-
this.duration = [Math.floor(durationMs / 1e3), durationMs % 1e3 * 1e6];
|
|
239
|
-
} else {
|
|
240
|
-
this.duration = [0, 0];
|
|
235
|
+
|
|
236
|
+
// src/gen-ai-messages.ts
|
|
237
|
+
var isMastraMessagePart = (p) => {
|
|
238
|
+
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);
|
|
239
|
+
};
|
|
240
|
+
var isMastraMessage = (m) => {
|
|
241
|
+
return typeof m === "object" && m != null && "role" in m && "content" in m && (typeof m.content === "string" || Array.isArray(m.content) && m.content.every(isMastraMessagePart));
|
|
242
|
+
};
|
|
243
|
+
var convertMastraMessagesToGenAIMessages = (inputOutputString) => {
|
|
244
|
+
try {
|
|
245
|
+
const parsedIO = JSON.parse(inputOutputString);
|
|
246
|
+
if (typeof parsedIO !== "object" || parsedIO == null || !("messages" in parsedIO) && !("text" in parsedIO)) {
|
|
247
|
+
return inputOutputString;
|
|
241
248
|
}
|
|
242
|
-
if (
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
attributes: {
|
|
250
|
-
"exception.message": span.errorInfo.message,
|
|
251
|
-
"exception.type": "Error",
|
|
252
|
-
...span.errorInfo.details?.stack && {
|
|
253
|
-
"exception.stacktrace": span.errorInfo.details.stack
|
|
254
|
-
}
|
|
255
|
-
},
|
|
256
|
-
time: this.startTime,
|
|
257
|
-
droppedAttributesCount: 0
|
|
258
|
-
});
|
|
259
|
-
} else if (span.endTime) {
|
|
260
|
-
this.status = { code: SpanStatusCode.OK };
|
|
261
|
-
} else {
|
|
262
|
-
this.status = { code: SpanStatusCode.UNSET };
|
|
263
|
-
}
|
|
264
|
-
if (span.isEvent) {
|
|
265
|
-
this.events.push({
|
|
266
|
-
name: "instant_event",
|
|
267
|
-
attributes: {},
|
|
268
|
-
time: this.startTime,
|
|
269
|
-
droppedAttributesCount: 0
|
|
270
|
-
});
|
|
249
|
+
if ("text" in parsedIO) {
|
|
250
|
+
return JSON.stringify([
|
|
251
|
+
{
|
|
252
|
+
role: "assistant",
|
|
253
|
+
parts: [{ type: "text", content: parsedIO.text }]
|
|
254
|
+
}
|
|
255
|
+
]);
|
|
271
256
|
}
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
257
|
+
if (Array.isArray(parsedIO.messages)) {
|
|
258
|
+
return JSON.stringify(
|
|
259
|
+
parsedIO.messages.map((m) => {
|
|
260
|
+
if (!isMastraMessage(m)) {
|
|
261
|
+
return m;
|
|
262
|
+
}
|
|
263
|
+
const role = m.role;
|
|
264
|
+
let parts = [];
|
|
265
|
+
if (Array.isArray(m.content)) {
|
|
266
|
+
parts = m.content.map((c) => {
|
|
267
|
+
switch (c.type) {
|
|
268
|
+
case "text":
|
|
269
|
+
return {
|
|
270
|
+
type: "text",
|
|
271
|
+
content: c.text
|
|
272
|
+
};
|
|
273
|
+
case "tool-call":
|
|
274
|
+
return {
|
|
275
|
+
type: "tool_call",
|
|
276
|
+
id: c.toolCallId,
|
|
277
|
+
name: c.toolName,
|
|
278
|
+
arguments: JSON.stringify(c.input)
|
|
279
|
+
};
|
|
280
|
+
case "tool-result":
|
|
281
|
+
return {
|
|
282
|
+
type: "tool_call_response",
|
|
283
|
+
id: c.toolCallId,
|
|
284
|
+
name: c.toolName,
|
|
285
|
+
response: JSON.stringify(c.output.value)
|
|
286
|
+
};
|
|
287
|
+
default:
|
|
288
|
+
return c;
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
} else {
|
|
292
|
+
parts = [
|
|
293
|
+
{
|
|
294
|
+
type: "text",
|
|
295
|
+
content: m.content
|
|
296
|
+
}
|
|
297
|
+
];
|
|
298
|
+
}
|
|
299
|
+
return {
|
|
300
|
+
role,
|
|
301
|
+
parts
|
|
302
|
+
};
|
|
303
|
+
})
|
|
304
|
+
);
|
|
285
305
|
}
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
version: "1.0.0"
|
|
290
|
-
};
|
|
291
|
-
this.instrumentationScope = this.instrumentationLibrary;
|
|
292
|
-
}
|
|
293
|
-
/**
|
|
294
|
-
* Convert JavaScript Date to hrtime format
|
|
295
|
-
*/
|
|
296
|
-
dateToHrTime(date) {
|
|
297
|
-
const ms = date.getTime();
|
|
298
|
-
const seconds = Math.floor(ms / 1e3);
|
|
299
|
-
const nanoseconds = ms % 1e3 * 1e6;
|
|
300
|
-
return [seconds, nanoseconds];
|
|
306
|
+
return inputOutputString;
|
|
307
|
+
} catch {
|
|
308
|
+
return inputOutputString;
|
|
301
309
|
}
|
|
302
310
|
};
|
|
303
311
|
|
|
304
|
-
// src/
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
312
|
+
// src/gen-ai-semantics.ts
|
|
313
|
+
function formatUsageMetrics(usage) {
|
|
314
|
+
if (!usage) return {};
|
|
315
|
+
const metrics = {};
|
|
316
|
+
if (usage.inputTokens !== void 0) {
|
|
317
|
+
metrics[ATTR_GEN_AI_USAGE_INPUT_TOKENS] = usage.inputTokens;
|
|
318
|
+
}
|
|
319
|
+
if (usage.outputTokens !== void 0) {
|
|
320
|
+
metrics[ATTR_GEN_AI_USAGE_OUTPUT_TOKENS] = usage.outputTokens;
|
|
321
|
+
}
|
|
322
|
+
if (usage.outputDetails?.reasoning !== void 0) {
|
|
323
|
+
metrics["gen_ai.usage.reasoning_tokens"] = usage.outputDetails.reasoning;
|
|
324
|
+
}
|
|
325
|
+
if (usage.inputDetails?.cacheRead !== void 0) {
|
|
326
|
+
metrics["gen_ai.usage.cached_input_tokens"] = usage.inputDetails.cacheRead;
|
|
327
|
+
}
|
|
328
|
+
if (usage.inputDetails?.cacheWrite !== void 0) {
|
|
329
|
+
metrics["gen_ai.usage.cache_write_tokens"] = usage.inputDetails.cacheWrite;
|
|
330
|
+
}
|
|
331
|
+
if (usage.inputDetails?.audio !== void 0) {
|
|
332
|
+
metrics["gen_ai.usage.audio_input_tokens"] = usage.inputDetails.audio;
|
|
333
|
+
}
|
|
334
|
+
if (usage.outputDetails?.audio !== void 0) {
|
|
335
|
+
metrics["gen_ai.usage.audio_output_tokens"] = usage.outputDetails.audio;
|
|
336
|
+
}
|
|
337
|
+
return metrics;
|
|
338
|
+
}
|
|
339
|
+
function getOperationName(span) {
|
|
340
|
+
switch (span.type) {
|
|
341
|
+
case SpanType.MODEL_GENERATION:
|
|
342
|
+
return "chat";
|
|
343
|
+
case SpanType.TOOL_CALL:
|
|
344
|
+
case SpanType.MCP_TOOL_CALL:
|
|
345
|
+
return "execute_tool";
|
|
346
|
+
case SpanType.AGENT_RUN:
|
|
347
|
+
return "invoke_agent";
|
|
348
|
+
case SpanType.WORKFLOW_RUN:
|
|
349
|
+
return "invoke_workflow";
|
|
350
|
+
default:
|
|
351
|
+
return span.type.toLowerCase();
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
function sanitizeSpanName(name) {
|
|
355
|
+
return name.replace(/[^\p{L}\p{N}._ -]/gu, "");
|
|
356
|
+
}
|
|
357
|
+
function getSpanIdentifier(span) {
|
|
358
|
+
switch (span.type) {
|
|
359
|
+
case SpanType.MODEL_GENERATION: {
|
|
360
|
+
const attrs = span.attributes;
|
|
361
|
+
return attrs?.model ?? "unknown";
|
|
319
362
|
}
|
|
363
|
+
default:
|
|
364
|
+
return span.entityName ?? span.entityId ?? "unknown";
|
|
320
365
|
}
|
|
321
|
-
return SPAN_KIND_MAPPING[type] || SpanKind.INTERNAL;
|
|
322
366
|
}
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
this.instrumentationLibrary = {
|
|
329
|
-
name: "@mastra/otel-exporter",
|
|
330
|
-
version: "1.0.0"
|
|
331
|
-
};
|
|
367
|
+
function getSpanName(span) {
|
|
368
|
+
const identifier = getSpanIdentifier(span);
|
|
369
|
+
if (identifier) {
|
|
370
|
+
const operation = getOperationName(span);
|
|
371
|
+
return `${operation} ${identifier}`;
|
|
332
372
|
}
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
const
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
this.instrumentationLibrary
|
|
350
|
-
);
|
|
373
|
+
return sanitizeSpanName(span.name);
|
|
374
|
+
}
|
|
375
|
+
function getAttributes(span) {
|
|
376
|
+
const attributes = {};
|
|
377
|
+
const spanType = span.type.toLowerCase();
|
|
378
|
+
attributes[ATTR_GEN_AI_OPERATION_NAME] = getOperationName(span);
|
|
379
|
+
attributes["mastra.span.type"] = span.type;
|
|
380
|
+
if (span.input !== void 0) {
|
|
381
|
+
const inputStr = typeof span.input === "string" ? span.input : JSON.stringify(span.input);
|
|
382
|
+
if (span.type === SpanType.MODEL_GENERATION) {
|
|
383
|
+
attributes[ATTR_GEN_AI_INPUT_MESSAGES] = convertMastraMessagesToGenAIMessages(inputStr);
|
|
384
|
+
} else if (span.type === SpanType.TOOL_CALL || span.type === SpanType.MCP_TOOL_CALL) {
|
|
385
|
+
attributes["gen_ai.tool.call.arguments"] = inputStr;
|
|
386
|
+
} else {
|
|
387
|
+
attributes[`mastra.${spanType}.input`] = inputStr;
|
|
388
|
+
}
|
|
351
389
|
}
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
const model = attrs?.model || "unknown";
|
|
361
|
-
return `${operation} ${model}`;
|
|
362
|
-
}
|
|
363
|
-
case SpanType.TOOL_CALL:
|
|
364
|
-
case SpanType.MCP_TOOL_CALL: {
|
|
365
|
-
const toolAttrs = Span.attributes;
|
|
366
|
-
const toolName = toolAttrs?.toolId || "unknown";
|
|
367
|
-
return `tool.execute ${toolName}`;
|
|
368
|
-
}
|
|
369
|
-
case SpanType.AGENT_RUN: {
|
|
370
|
-
const agentAttrs = Span.attributes;
|
|
371
|
-
const agentId = agentAttrs?.agentId || "unknown";
|
|
372
|
-
return `agent.${agentId}`;
|
|
373
|
-
}
|
|
374
|
-
case SpanType.WORKFLOW_RUN: {
|
|
375
|
-
const workflowAttrs = Span.attributes;
|
|
376
|
-
const workflowId = workflowAttrs?.workflowId || "unknown";
|
|
377
|
-
return `workflow.${workflowId}`;
|
|
378
|
-
}
|
|
379
|
-
case SpanType.WORKFLOW_STEP:
|
|
380
|
-
return Span.name;
|
|
381
|
-
default:
|
|
382
|
-
return Span.name;
|
|
390
|
+
if (span.output !== void 0) {
|
|
391
|
+
const outputStr = typeof span.output === "string" ? span.output : JSON.stringify(span.output);
|
|
392
|
+
if (span.type === SpanType.MODEL_GENERATION) {
|
|
393
|
+
attributes[ATTR_GEN_AI_OUTPUT_MESSAGES] = convertMastraMessagesToGenAIMessages(outputStr);
|
|
394
|
+
} else if (span.type === SpanType.TOOL_CALL || span.type === SpanType.MCP_TOOL_CALL) {
|
|
395
|
+
attributes["gen_ai.tool.call.result"] = outputStr;
|
|
396
|
+
} else {
|
|
397
|
+
attributes[`mastra.${spanType}.output`] = outputStr;
|
|
383
398
|
}
|
|
384
399
|
}
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
buildAttributes(Span) {
|
|
390
|
-
const attributes = {};
|
|
391
|
-
attributes["gen_ai.operation.name"] = this.getOperationName(Span);
|
|
392
|
-
attributes["span.kind"] = this.getSpanKindString(Span);
|
|
393
|
-
attributes["mastra.span.type"] = Span.type;
|
|
394
|
-
attributes["mastra.trace_id"] = Span.traceId;
|
|
395
|
-
attributes["mastra.span_id"] = Span.id;
|
|
396
|
-
if (Span.parentSpanId) {
|
|
397
|
-
attributes["mastra.parent_span_id"] = Span.parentSpanId;
|
|
398
|
-
}
|
|
399
|
-
if (Span.input !== void 0) {
|
|
400
|
-
const inputStr = typeof Span.input === "string" ? Span.input : JSON.stringify(Span.input);
|
|
401
|
-
attributes["input"] = inputStr;
|
|
402
|
-
if (Span.type === SpanType.MODEL_GENERATION) {
|
|
403
|
-
attributes["gen_ai.prompt"] = inputStr;
|
|
404
|
-
} else if (Span.type === SpanType.TOOL_CALL || Span.type === SpanType.MCP_TOOL_CALL) {
|
|
405
|
-
attributes["gen_ai.tool.input"] = inputStr;
|
|
406
|
-
}
|
|
400
|
+
if (span.type === SpanType.MODEL_GENERATION && span.attributes) {
|
|
401
|
+
const modelAttrs = span.attributes;
|
|
402
|
+
if (modelAttrs.model) {
|
|
403
|
+
attributes[ATTR_GEN_AI_REQUEST_MODEL] = modelAttrs.model;
|
|
407
404
|
}
|
|
408
|
-
if (
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
405
|
+
if (modelAttrs.provider) {
|
|
406
|
+
attributes[ATTR_GEN_AI_PROVIDER_NAME] = normalizeProvider(modelAttrs.provider);
|
|
407
|
+
}
|
|
408
|
+
if (span.entityId) {
|
|
409
|
+
attributes[ATTR_GEN_AI_AGENT_ID] = span.entityId;
|
|
410
|
+
}
|
|
411
|
+
if (span.entityName) {
|
|
412
|
+
attributes[ATTR_GEN_AI_AGENT_NAME] = span.entityName;
|
|
416
413
|
}
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
if (modelAttrs.
|
|
420
|
-
attributes[
|
|
414
|
+
Object.assign(attributes, formatUsageMetrics(modelAttrs.usage));
|
|
415
|
+
if (modelAttrs.parameters) {
|
|
416
|
+
if (modelAttrs.parameters.temperature !== void 0) {
|
|
417
|
+
attributes[ATTR_GEN_AI_REQUEST_TEMPERATURE] = modelAttrs.parameters.temperature;
|
|
421
418
|
}
|
|
422
|
-
if (modelAttrs.
|
|
423
|
-
attributes[
|
|
419
|
+
if (modelAttrs.parameters.maxOutputTokens !== void 0) {
|
|
420
|
+
attributes[ATTR_GEN_AI_REQUEST_MAX_TOKENS] = modelAttrs.parameters.maxOutputTokens;
|
|
424
421
|
}
|
|
425
|
-
if (modelAttrs.
|
|
426
|
-
|
|
427
|
-
const outputTokens = modelAttrs.usage.outputTokens ?? modelAttrs.usage.completionTokens;
|
|
428
|
-
if (inputTokens !== void 0) {
|
|
429
|
-
attributes["gen_ai.usage.input_tokens"] = inputTokens;
|
|
430
|
-
}
|
|
431
|
-
if (outputTokens !== void 0) {
|
|
432
|
-
attributes["gen_ai.usage.output_tokens"] = outputTokens;
|
|
433
|
-
}
|
|
434
|
-
if (modelAttrs.usage.totalTokens !== void 0) {
|
|
435
|
-
attributes["gen_ai.usage.total_tokens"] = modelAttrs.usage.totalTokens;
|
|
436
|
-
}
|
|
437
|
-
if (modelAttrs.usage.reasoningTokens !== void 0) {
|
|
438
|
-
attributes["gen_ai.usage.reasoning_tokens"] = modelAttrs.usage.reasoningTokens;
|
|
439
|
-
}
|
|
440
|
-
if (modelAttrs.usage.cachedInputTokens !== void 0) {
|
|
441
|
-
attributes["gen_ai.usage.cached_input_tokens"] = modelAttrs.usage.cachedInputTokens;
|
|
442
|
-
}
|
|
422
|
+
if (modelAttrs.parameters.topP !== void 0) {
|
|
423
|
+
attributes[ATTR_GEN_AI_REQUEST_TOP_P] = modelAttrs.parameters.topP;
|
|
443
424
|
}
|
|
444
|
-
if (modelAttrs.parameters) {
|
|
445
|
-
|
|
446
|
-
attributes["gen_ai.request.temperature"] = modelAttrs.parameters.temperature;
|
|
447
|
-
}
|
|
448
|
-
if (modelAttrs.parameters.maxOutputTokens !== void 0) {
|
|
449
|
-
attributes["gen_ai.request.max_tokens"] = modelAttrs.parameters.maxOutputTokens;
|
|
450
|
-
}
|
|
451
|
-
if (modelAttrs.parameters.topP !== void 0) {
|
|
452
|
-
attributes["gen_ai.request.top_p"] = modelAttrs.parameters.topP;
|
|
453
|
-
}
|
|
454
|
-
if (modelAttrs.parameters.topK !== void 0) {
|
|
455
|
-
attributes["gen_ai.request.top_k"] = modelAttrs.parameters.topK;
|
|
456
|
-
}
|
|
457
|
-
if (modelAttrs.parameters.presencePenalty !== void 0) {
|
|
458
|
-
attributes["gen_ai.request.presence_penalty"] = modelAttrs.parameters.presencePenalty;
|
|
459
|
-
}
|
|
460
|
-
if (modelAttrs.parameters.frequencyPenalty !== void 0) {
|
|
461
|
-
attributes["gen_ai.request.frequency_penalty"] = modelAttrs.parameters.frequencyPenalty;
|
|
462
|
-
}
|
|
463
|
-
if (modelAttrs.parameters.stopSequences) {
|
|
464
|
-
attributes["gen_ai.request.stop_sequences"] = JSON.stringify(modelAttrs.parameters.stopSequences);
|
|
465
|
-
}
|
|
425
|
+
if (modelAttrs.parameters.topK !== void 0) {
|
|
426
|
+
attributes[ATTR_GEN_AI_REQUEST_TOP_K] = modelAttrs.parameters.topK;
|
|
466
427
|
}
|
|
467
|
-
if (modelAttrs.
|
|
468
|
-
attributes[
|
|
428
|
+
if (modelAttrs.parameters.presencePenalty !== void 0) {
|
|
429
|
+
attributes[ATTR_GEN_AI_REQUEST_PRESENCE_PENALTY] = modelAttrs.parameters.presencePenalty;
|
|
469
430
|
}
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
const toolAttrs = Span.attributes;
|
|
473
|
-
if (toolAttrs.toolId) {
|
|
474
|
-
attributes["gen_ai.tool.name"] = toolAttrs.toolId;
|
|
431
|
+
if (modelAttrs.parameters.frequencyPenalty !== void 0) {
|
|
432
|
+
attributes[ATTR_GEN_AI_REQUEST_FREQUENCY_PENALTY] = modelAttrs.parameters.frequencyPenalty;
|
|
475
433
|
}
|
|
476
|
-
if (
|
|
477
|
-
|
|
478
|
-
if (mcpAttrs.mcpServer) {
|
|
479
|
-
attributes["mcp.server"] = mcpAttrs.mcpServer;
|
|
480
|
-
}
|
|
481
|
-
if (mcpAttrs.serverVersion) {
|
|
482
|
-
attributes["mcp.server.version"] = mcpAttrs.serverVersion;
|
|
483
|
-
}
|
|
484
|
-
} else {
|
|
485
|
-
if (toolAttrs.toolDescription) {
|
|
486
|
-
attributes["gen_ai.tool.description"] = toolAttrs.toolDescription;
|
|
487
|
-
}
|
|
434
|
+
if (modelAttrs.parameters.stopSequences) {
|
|
435
|
+
attributes[ATTR_GEN_AI_REQUEST_STOP_SEQUENCES] = JSON.stringify(modelAttrs.parameters.stopSequences);
|
|
488
436
|
}
|
|
489
|
-
if (
|
|
490
|
-
attributes[
|
|
437
|
+
if (modelAttrs.parameters.seed) {
|
|
438
|
+
attributes[ATTR_GEN_AI_REQUEST_SEED] = modelAttrs.parameters.seed;
|
|
491
439
|
}
|
|
492
440
|
}
|
|
493
|
-
if (
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
441
|
+
if (modelAttrs.finishReason) {
|
|
442
|
+
attributes[ATTR_GEN_AI_RESPONSE_FINISH_REASONS] = JSON.stringify([modelAttrs.finishReason]);
|
|
443
|
+
}
|
|
444
|
+
if (modelAttrs.responseModel) {
|
|
445
|
+
attributes[ATTR_GEN_AI_RESPONSE_MODEL] = modelAttrs.responseModel;
|
|
446
|
+
}
|
|
447
|
+
if (modelAttrs.responseId) {
|
|
448
|
+
attributes[ATTR_GEN_AI_RESPONSE_ID] = modelAttrs.responseId;
|
|
449
|
+
}
|
|
450
|
+
if (modelAttrs.serverAddress) {
|
|
451
|
+
attributes[ATTR_SERVER_ADDRESS] = modelAttrs.serverAddress;
|
|
452
|
+
}
|
|
453
|
+
if (modelAttrs.serverPort !== void 0) {
|
|
454
|
+
attributes[ATTR_SERVER_PORT] = modelAttrs.serverPort;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
if ((span.type === SpanType.TOOL_CALL || span.type === SpanType.MCP_TOOL_CALL) && span.attributes) {
|
|
458
|
+
attributes[ATTR_GEN_AI_TOOL_NAME] = span.entityName ?? span.entityId;
|
|
459
|
+
if (span.type === SpanType.MCP_TOOL_CALL) {
|
|
460
|
+
const mcpAttrs = span.attributes;
|
|
461
|
+
if (mcpAttrs.mcpServer) {
|
|
462
|
+
attributes[ATTR_SERVER_ADDRESS] = mcpAttrs.mcpServer;
|
|
498
463
|
}
|
|
499
|
-
|
|
500
|
-
|
|
464
|
+
} else {
|
|
465
|
+
const toolAttrs = span.attributes;
|
|
466
|
+
if (toolAttrs.toolDescription) {
|
|
467
|
+
attributes[ATTR_GEN_AI_TOOL_DESCRIPTION] = toolAttrs.toolDescription;
|
|
501
468
|
}
|
|
502
|
-
if (
|
|
503
|
-
attributes["
|
|
469
|
+
if (toolAttrs.toolType) {
|
|
470
|
+
attributes["gen_ai.tool.type"] = toolAttrs.toolType;
|
|
504
471
|
}
|
|
505
472
|
}
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
if (workflowAttrs.status) {
|
|
512
|
-
attributes["workflow.status"] = workflowAttrs.status;
|
|
513
|
-
}
|
|
473
|
+
}
|
|
474
|
+
if (span.type === SpanType.AGENT_RUN && span.attributes) {
|
|
475
|
+
const agentAttrs = span.attributes;
|
|
476
|
+
if (span.entityId) {
|
|
477
|
+
attributes[ATTR_GEN_AI_AGENT_ID] = span.entityId;
|
|
514
478
|
}
|
|
515
|
-
if (
|
|
516
|
-
attributes[
|
|
517
|
-
attributes["error.type"] = Span.errorInfo.id || "unknown";
|
|
518
|
-
attributes["error.message"] = Span.errorInfo.message;
|
|
519
|
-
if (Span.errorInfo.domain) {
|
|
520
|
-
attributes["error.domain"] = Span.errorInfo.domain;
|
|
521
|
-
}
|
|
522
|
-
if (Span.errorInfo.category) {
|
|
523
|
-
attributes["error.category"] = Span.errorInfo.category;
|
|
524
|
-
}
|
|
479
|
+
if (span.entityName) {
|
|
480
|
+
attributes[ATTR_GEN_AI_AGENT_NAME] = span.entityName;
|
|
525
481
|
}
|
|
526
|
-
if (
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
482
|
+
if (agentAttrs.conversationId) {
|
|
483
|
+
attributes[ATTR_GEN_AI_CONVERSATION_ID] = agentAttrs.conversationId;
|
|
484
|
+
}
|
|
485
|
+
if (agentAttrs.maxSteps) {
|
|
486
|
+
attributes[`mastra.${spanType}.max_steps`] = agentAttrs.maxSteps;
|
|
487
|
+
}
|
|
488
|
+
if (agentAttrs.availableTools) {
|
|
489
|
+
attributes[`gen_ai.tool.definitions`] = JSON.stringify(agentAttrs.availableTools);
|
|
490
|
+
}
|
|
491
|
+
attributes[ATTR_GEN_AI_SYSTEM_INSTRUCTIONS] = agentAttrs.instructions;
|
|
492
|
+
}
|
|
493
|
+
if (span.errorInfo) {
|
|
494
|
+
attributes[ATTR_ERROR_TYPE] = span.errorInfo.id || "unknown";
|
|
495
|
+
attributes[ATTR_ERROR_MESSAGE] = span.errorInfo.message;
|
|
496
|
+
if (span.errorInfo.domain) {
|
|
497
|
+
attributes["error.domain"] = span.errorInfo.domain;
|
|
539
498
|
}
|
|
540
|
-
if (
|
|
541
|
-
attributes["
|
|
499
|
+
if (span.errorInfo.category) {
|
|
500
|
+
attributes["error.category"] = span.errorInfo.category;
|
|
542
501
|
}
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
502
|
+
}
|
|
503
|
+
return attributes;
|
|
504
|
+
}
|
|
505
|
+
var PROVIDER_ALIASES = {
|
|
506
|
+
anthropic: ["anthropic", "claude"],
|
|
507
|
+
"aws.bedrock": ["awsbedrock", "bedrock", "amazonbedrock"],
|
|
508
|
+
"azure.ai.inference": ["azureaiinference", "azureinference"],
|
|
509
|
+
"azure.ai.openai": ["azureaiopenai", "azureopenai", "msopenai", "microsoftopenai"],
|
|
510
|
+
cohere: ["cohere"],
|
|
511
|
+
deepseek: ["deepseek"],
|
|
512
|
+
"gcp.gemini": ["gcpgemini", "gemini"],
|
|
513
|
+
"gcp.gen_ai": ["gcpgenai", "googlegenai", "googleai"],
|
|
514
|
+
"gcp.vertex_ai": ["gcpvertexai", "vertexai"],
|
|
515
|
+
groq: ["groq"],
|
|
516
|
+
"ibm.watsonx.ai": ["ibmwatsonxai", "watsonx", "watsonxai"],
|
|
517
|
+
mistral_ai: ["mistral", "mistralai"],
|
|
518
|
+
openai: ["openai", "oai"],
|
|
519
|
+
perplexity: ["perplexity", "pplx"],
|
|
520
|
+
x_ai: ["xai", "x-ai", "x_ai", "x.com ai"]
|
|
521
|
+
};
|
|
522
|
+
function normalizeProviderString(input) {
|
|
523
|
+
return input.toLowerCase().replace(/[^a-z0-9]/g, "");
|
|
524
|
+
}
|
|
525
|
+
function normalizeProvider(providerName) {
|
|
526
|
+
const normalized = normalizeProviderString(providerName);
|
|
527
|
+
for (const [canonical, aliases] of Object.entries(PROVIDER_ALIASES)) {
|
|
528
|
+
for (const alias of aliases) {
|
|
529
|
+
if (normalized === alias) {
|
|
530
|
+
return canonical;
|
|
531
|
+
}
|
|
547
532
|
}
|
|
548
|
-
return attributes;
|
|
549
533
|
}
|
|
534
|
+
return providerName.toLowerCase();
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
// src/span-converter.ts
|
|
538
|
+
var SpanConverter = class {
|
|
539
|
+
constructor(params) {
|
|
540
|
+
this.params = params;
|
|
541
|
+
this.format = params.format;
|
|
542
|
+
}
|
|
543
|
+
resource;
|
|
544
|
+
scope;
|
|
545
|
+
initPromise;
|
|
546
|
+
format;
|
|
550
547
|
/**
|
|
551
|
-
*
|
|
548
|
+
* Lazily initialize resource & scope on first use.
|
|
549
|
+
* Subsequent calls reuse the same promise (no races).
|
|
552
550
|
*/
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
const attrs = Span.attributes;
|
|
557
|
-
return attrs?.resultType === "tool_selection" ? "tool_selection" : "chat";
|
|
558
|
-
}
|
|
559
|
-
case SpanType.TOOL_CALL:
|
|
560
|
-
case SpanType.MCP_TOOL_CALL:
|
|
561
|
-
return "tool.execute";
|
|
562
|
-
case SpanType.AGENT_RUN:
|
|
563
|
-
return "agent.run";
|
|
564
|
-
case SpanType.WORKFLOW_RUN:
|
|
565
|
-
return "workflow.run";
|
|
566
|
-
default:
|
|
567
|
-
return Span.type.replace(/_/g, ".");
|
|
551
|
+
async initIfNeeded() {
|
|
552
|
+
if (this.initPromise) {
|
|
553
|
+
return this.initPromise;
|
|
568
554
|
}
|
|
555
|
+
this.initPromise = (async () => {
|
|
556
|
+
const packageVersion = await getPackageVersion(this.params.packageName) ?? "unknown";
|
|
557
|
+
const serviceVersion = await getPackageVersion("@mastra/core") ?? "unknown";
|
|
558
|
+
let resource = resourceFromAttributes({
|
|
559
|
+
[ATTR_SERVICE_NAME]: this.params.serviceName || "mastra-service",
|
|
560
|
+
[ATTR_SERVICE_VERSION]: serviceVersion,
|
|
561
|
+
[ATTR_TELEMETRY_SDK_NAME]: this.params.packageName,
|
|
562
|
+
[ATTR_TELEMETRY_SDK_VERSION]: packageVersion,
|
|
563
|
+
[ATTR_TELEMETRY_SDK_LANGUAGE]: "nodejs"
|
|
564
|
+
});
|
|
565
|
+
if (this.params.config?.resourceAttributes) {
|
|
566
|
+
resource = resource.merge(
|
|
567
|
+
// Duplicate attributes from config will override defaults above
|
|
568
|
+
resourceFromAttributes(this.params.config.resourceAttributes)
|
|
569
|
+
);
|
|
570
|
+
}
|
|
571
|
+
this.resource = resource;
|
|
572
|
+
this.scope = {
|
|
573
|
+
name: this.params.packageName,
|
|
574
|
+
version: packageVersion
|
|
575
|
+
};
|
|
576
|
+
})();
|
|
577
|
+
return this.initPromise;
|
|
569
578
|
}
|
|
570
579
|
/**
|
|
571
|
-
*
|
|
580
|
+
* Convert a Mastra Span to an OpenTelemetry ReadableSpan
|
|
572
581
|
*/
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
582
|
+
async convertSpan(span) {
|
|
583
|
+
await this.initIfNeeded();
|
|
584
|
+
if (!this.resource || !this.scope) {
|
|
585
|
+
throw new Error("SpanConverter not initialized correctly");
|
|
586
|
+
}
|
|
587
|
+
const name = getSpanName(span);
|
|
588
|
+
const kind = getSpanKind(span.type);
|
|
589
|
+
const attributes = getAttributes(span);
|
|
590
|
+
if (span.metadata) {
|
|
591
|
+
for (const [k, v] of Object.entries(span.metadata)) {
|
|
592
|
+
if (v === null || v === void 0) {
|
|
593
|
+
continue;
|
|
594
|
+
}
|
|
595
|
+
attributes[`mastra.metadata.${k}`] = typeof v === "object" ? JSON.stringify(v) : v;
|
|
596
|
+
}
|
|
588
597
|
}
|
|
598
|
+
if (span.isRootSpan && span.tags?.length) {
|
|
599
|
+
attributes["mastra.tags"] = JSON.stringify(span.tags);
|
|
600
|
+
}
|
|
601
|
+
const startTime = dateToHrTime(span.startTime);
|
|
602
|
+
const endTime = span.endTime ? dateToHrTime(span.endTime) : startTime;
|
|
603
|
+
const duration = computeDuration(span.startTime, span.endTime);
|
|
604
|
+
const { status, events } = buildStatusAndEvents(span, startTime);
|
|
605
|
+
const spanContext = {
|
|
606
|
+
traceId: span.traceId,
|
|
607
|
+
spanId: span.id,
|
|
608
|
+
traceFlags: TraceFlags.SAMPLED,
|
|
609
|
+
isRemote: false
|
|
610
|
+
};
|
|
611
|
+
const parentSpanContext = span.parentSpanId ? {
|
|
612
|
+
traceId: span.traceId,
|
|
613
|
+
spanId: span.parentSpanId,
|
|
614
|
+
traceFlags: TraceFlags.SAMPLED,
|
|
615
|
+
isRemote: false
|
|
616
|
+
} : void 0;
|
|
617
|
+
const links = [];
|
|
618
|
+
const readable = {
|
|
619
|
+
name,
|
|
620
|
+
kind,
|
|
621
|
+
spanContext: () => spanContext,
|
|
622
|
+
parentSpanContext,
|
|
623
|
+
startTime,
|
|
624
|
+
endTime,
|
|
625
|
+
status,
|
|
626
|
+
attributes,
|
|
627
|
+
links,
|
|
628
|
+
events,
|
|
629
|
+
duration,
|
|
630
|
+
ended: !!span.endTime,
|
|
631
|
+
resource: this.resource,
|
|
632
|
+
instrumentationScope: this.scope,
|
|
633
|
+
droppedAttributesCount: 0,
|
|
634
|
+
droppedEventsCount: 0,
|
|
635
|
+
droppedLinksCount: 0
|
|
636
|
+
};
|
|
637
|
+
return readable;
|
|
589
638
|
}
|
|
590
639
|
};
|
|
640
|
+
async function getPackageVersion(pkgName) {
|
|
641
|
+
try {
|
|
642
|
+
const manifestUrl = new URL(await import.meta.resolve(`${pkgName}/package.json`));
|
|
643
|
+
const path = fileURLToPath(manifestUrl);
|
|
644
|
+
const pkgJson = JSON.parse(readFileSync(path, "utf8"));
|
|
645
|
+
return pkgJson.version;
|
|
646
|
+
} catch {
|
|
647
|
+
return void 0;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
function getSpanKind(type) {
|
|
651
|
+
switch (type) {
|
|
652
|
+
case SpanType.MODEL_GENERATION:
|
|
653
|
+
case SpanType.MCP_TOOL_CALL:
|
|
654
|
+
return SpanKind.CLIENT;
|
|
655
|
+
default:
|
|
656
|
+
return SpanKind.INTERNAL;
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
function dateToHrTime(date) {
|
|
660
|
+
const ms = date.getTime();
|
|
661
|
+
const seconds = Math.floor(ms / 1e3);
|
|
662
|
+
const nanoseconds = ms % 1e3 * 1e6;
|
|
663
|
+
return [seconds, nanoseconds];
|
|
664
|
+
}
|
|
665
|
+
function computeDuration(start, end) {
|
|
666
|
+
if (!end) return [0, 0];
|
|
667
|
+
const diffMs = end.getTime() - start.getTime();
|
|
668
|
+
return [Math.floor(diffMs / 1e3), diffMs % 1e3 * 1e6];
|
|
669
|
+
}
|
|
670
|
+
function buildStatusAndEvents(span, defaultTime) {
|
|
671
|
+
const events = [];
|
|
672
|
+
if (span.errorInfo) {
|
|
673
|
+
const status = {
|
|
674
|
+
code: SpanStatusCode.ERROR,
|
|
675
|
+
message: span.errorInfo.message
|
|
676
|
+
};
|
|
677
|
+
events.push({
|
|
678
|
+
name: "exception",
|
|
679
|
+
attributes: {
|
|
680
|
+
"exception.message": span.errorInfo.message,
|
|
681
|
+
"exception.type": "Error",
|
|
682
|
+
...span.errorInfo.details?.stack && {
|
|
683
|
+
"exception.stacktrace": span.errorInfo.details.stack
|
|
684
|
+
}
|
|
685
|
+
},
|
|
686
|
+
time: defaultTime,
|
|
687
|
+
droppedAttributesCount: 0
|
|
688
|
+
});
|
|
689
|
+
return { status, events };
|
|
690
|
+
}
|
|
691
|
+
if (span.endTime) {
|
|
692
|
+
return {
|
|
693
|
+
status: { code: SpanStatusCode.OK },
|
|
694
|
+
events
|
|
695
|
+
};
|
|
696
|
+
}
|
|
697
|
+
return {
|
|
698
|
+
status: { code: SpanStatusCode.UNSET },
|
|
699
|
+
events
|
|
700
|
+
};
|
|
701
|
+
}
|
|
591
702
|
|
|
592
703
|
// src/tracing.ts
|
|
593
704
|
var OtelExporter = class extends BaseExporter {
|
|
594
705
|
config;
|
|
595
|
-
|
|
706
|
+
observabilityConfig;
|
|
596
707
|
spanConverter;
|
|
597
708
|
processor;
|
|
598
709
|
exporter;
|
|
@@ -601,7 +712,6 @@ var OtelExporter = class extends BaseExporter {
|
|
|
601
712
|
constructor(config) {
|
|
602
713
|
super(config);
|
|
603
714
|
this.config = config;
|
|
604
|
-
this.spanConverter = new SpanConverter();
|
|
605
715
|
if (config.logLevel === "debug") {
|
|
606
716
|
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);
|
|
607
717
|
}
|
|
@@ -610,7 +720,7 @@ var OtelExporter = class extends BaseExporter {
|
|
|
610
720
|
* Initialize with tracing configuration
|
|
611
721
|
*/
|
|
612
722
|
init(options) {
|
|
613
|
-
this.
|
|
723
|
+
this.observabilityConfig = options.config;
|
|
614
724
|
}
|
|
615
725
|
async setupExporter() {
|
|
616
726
|
if (this.isSetup || this.exporter) return;
|
|
@@ -688,21 +798,12 @@ var OtelExporter = class extends BaseExporter {
|
|
|
688
798
|
}
|
|
689
799
|
async setupProcessor() {
|
|
690
800
|
if (this.processor || this.isSetup) return;
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
[ATTR_TELEMETRY_SDK_VERSION]: "1.0.0",
|
|
697
|
-
[ATTR_TELEMETRY_SDK_LANGUAGE]: "nodejs"
|
|
801
|
+
this.spanConverter = new SpanConverter({
|
|
802
|
+
packageName: "@mastra/otel-exporter",
|
|
803
|
+
serviceName: this.observabilityConfig?.serviceName,
|
|
804
|
+
config: this.config,
|
|
805
|
+
format: "GenAI_v1_38_0"
|
|
698
806
|
});
|
|
699
|
-
if (this.config.resourceAttributes) {
|
|
700
|
-
resource = resource.merge(
|
|
701
|
-
// Duplicate attributes from config will override defaults above
|
|
702
|
-
resourceFromAttributes(this.config.resourceAttributes)
|
|
703
|
-
);
|
|
704
|
-
}
|
|
705
|
-
this.spanConverter = new SpanConverter(resource);
|
|
706
807
|
this.processor = new BatchSpanProcessor(this.exporter, {
|
|
707
808
|
maxExportBatchSize: this.config.batchSize || 512,
|
|
708
809
|
// Default batch size
|
|
@@ -738,9 +839,9 @@ var OtelExporter = class extends BaseExporter {
|
|
|
738
839
|
return;
|
|
739
840
|
}
|
|
740
841
|
try {
|
|
741
|
-
const
|
|
842
|
+
const otelSpan = await this.spanConverter.convertSpan(span);
|
|
742
843
|
await new Promise((resolve) => {
|
|
743
|
-
this.processor.onEnd(
|
|
844
|
+
this.processor.onEnd(otelSpan);
|
|
744
845
|
resolve();
|
|
745
846
|
});
|
|
746
847
|
this.logger.debug(
|
|
@@ -757,6 +858,6 @@ var OtelExporter = class extends BaseExporter {
|
|
|
757
858
|
}
|
|
758
859
|
};
|
|
759
860
|
|
|
760
|
-
export {
|
|
861
|
+
export { OtelExporter, SpanConverter, getSpanKind };
|
|
761
862
|
//# sourceMappingURL=index.js.map
|
|
762
863
|
//# sourceMappingURL=index.js.map
|