Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/server-utils/src/ai/mastra/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import {
startInactiveSpan,
timestampInSeconds,
} from '@sentry/core';
import { GEN_AI_RESPONSE_MODEL, SENTRY_OP, SENTRY_ORIGIN } from '@sentry/conventions/attributes';
import {
GEN_AI_CONVERSATION_ID,
GEN_AI_RESPONSE_MODEL,
SENTRY_OP,
SENTRY_ORIGIN,
} from '@sentry/conventions/attributes';
import { DEBUG_BUILD } from '../../debug-build';
import type { GenAiCallOptions } from '../core/utils';
import { resolveGenAiCallRecording } from '../core/utils';
Expand Down Expand Up @@ -174,6 +179,8 @@ export class SentryMastraExporter implements MastraObservabilityExporter {
parentSpan: parentSpan ?? activeSpan,
attributes: {
...getSpanAttributes(span, recordingOptions),
// Only on start, so the id from `Sentry.setConversationId()`, applied on `spanStart`, is kept.
[GEN_AI_CONVERSATION_ID]: span.attributes?.conversationId ?? span.metadata?.threadId,
[SENTRY_OP]: getOperation(span.type)?.op,
[SENTRY_ORIGIN]: MASTRA_ORIGIN,
},
Expand Down
2 changes: 0 additions & 2 deletions packages/server-utils/src/ai/mastra/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { stringify } from '@sentry/core';
import {
ERROR_TYPE,
GEN_AI_AGENT_NAME,
GEN_AI_CONVERSATION_ID,
GEN_AI_INPUT_MESSAGES,
GEN_AI_OPERATION_NAME,
GEN_AI_OUTPUT_MESSAGES,
Expand Down Expand Up @@ -150,7 +149,6 @@ export function getSpanAttributes(span: MastraExportedSpan, options: AttributeRe

const attributes: SpanAttributes = {
[GEN_AI_OPERATION_NAME]: operationName,
[GEN_AI_CONVERSATION_ID]: span.attributes?.conversationId ?? span.metadata?.threadId,
};

if (AGENT_SPAN_TYPES.has(span.type)) {
Expand Down
12 changes: 12 additions & 0 deletions packages/server-utils/test/ai/lib/tracing/mastra-exporter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ import type { Span } from '@sentry/core';
import {
_INTERNAL_clearAiProviderSkips,
_INTERNAL_shouldSkipAiProviderWrapping,
conversationIdIntegration,
getClient,
getMainCarrier,
setConversationId,
setCurrentClient,
spanToStaticSpanJSON,
} from '@sentry/core';
Expand Down Expand Up @@ -430,6 +433,15 @@ describe('SentryMastraExporter', () => {
expect(spanToStaticSpanJSON(endedSpans[0]!).data[GEN_AI_CONVERSATION_ID]).toBe(expected);
});

it('keeps the conversation id from Sentry.setConversationId when the span updates and ends', async () => {
getClient()!.addIntegration(conversationIdIntegration());
setConversationId('chat-1');
const span = makeSpan({ entityName: 'agent', metadata: { threadId: 'thread-9' } });
await run(started(span), { type: 'span_updated', exportedSpan: span }, ended(span));

expect(spanToStaticSpanJSON(endedSpans[0]!).data[GEN_AI_CONVERSATION_ID]).toBe('chat-1');
});

it('does not use runId as gen_ai.conversation.id', async () => {
const span = makeSpan({ entityName: 'agent', metadata: { runId: 'run-3' } });
await run(started(span), ended(span));
Expand Down
Loading