Skip to content

Commit 560fa26

Browse files
committed
fix(copilot): bound tool span agent labels
1 parent f4703a9 commit 560fa26

3 files changed

Lines changed: 14 additions & 4 deletions

File tree

apps/sim/lib/copilot/request/metrics.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ vi.mock('@opentelemetry/api', () => ({
2121
}))
2222

2323
import { TraceAttr } from '@/lib/copilot/generated/trace-attributes-v1'
24-
import { recordSimToolMetric } from '@/lib/copilot/request/metrics'
24+
import { normalizeToolAgentId, recordSimToolMetric } from '@/lib/copilot/request/metrics'
2525

2626
describe('recordSimToolMetric', () => {
2727
beforeEach(() => {
@@ -66,4 +66,13 @@ describe('recordSimToolMetric', () => {
6666
[TraceAttr.GenAiAgentName]: 'other',
6767
})
6868
})
69+
70+
it.each([
71+
{ agentId: 'main', expected: 'main' },
72+
{ agentId: 'workflow', expected: 'workflow' },
73+
{ agentId: 'tenant-defined-agent', expected: 'other' },
74+
{ agentId: '', expected: 'other' },
75+
])('normalizes $agentId to $expected for every telemetry signal', ({ agentId, expected }) => {
76+
expect(normalizeToolAgentId(agentId)).toBe(expected)
77+
})
6978
})

apps/sim/lib/copilot/request/metrics.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const REGISTERED_AGENT_IDS = new Set([
7373
...Object.values(TOOL_CATALOG).flatMap(({ subagentId }) => (subagentId ? [subagentId] : [])),
7474
])
7575

76-
function cappedAgentId(agentId: string): string {
76+
export function normalizeToolAgentId(agentId: string): string {
7777
return REGISTERED_AGENT_IDS.has(agentId) ? agentId : 'other'
7878
}
7979

@@ -91,7 +91,7 @@ export function recordSimToolMetric(
9191
[TraceAttr.ToolName]: cappedToolName(name),
9292
[TraceAttr.ToolExecutor]: 'sim',
9393
[TraceAttr.ToolOutcome]: outcome,
94-
[TraceAttr.GenAiAgentName]: cappedAgentId(agentId),
94+
[TraceAttr.GenAiAgentName]: normalizeToolAgentId(agentId),
9595
}
9696
toolCalls.add(1, attrs)
9797
if (durationMs >= 0) toolDuration.record(durationMs, attrs)

apps/sim/lib/copilot/request/otel.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
import { TraceAttr } from '@/lib/copilot/generated/trace-attributes-v1'
2323
import { TraceSpan } from '@/lib/copilot/generated/trace-spans-v1'
2424
import { contextFromRequestHeaders } from '@/lib/copilot/request/go/propagation'
25+
import { normalizeToolAgentId } from '@/lib/copilot/request/metrics'
2526
import { isExplicitStopReason } from '@/lib/copilot/request/session/abort-reason'
2627

2728
// OTel GenAI content-capture env var (spec:
@@ -300,7 +301,7 @@ export async function withCopilotToolSpan<T>(
300301
[TraceAttr.ToolName]: input.toolName,
301302
[TraceAttr.ToolCallId]: input.toolCallId,
302303
[TraceAttr.ToolExecutor]: 'sim',
303-
[TraceAttr.GenAiAgentName]: input.agentName,
304+
[TraceAttr.GenAiAgentName]: normalizeToolAgentId(input.agentName),
304305
...(input.runId ? { [TraceAttr.RunId]: input.runId } : {}),
305306
...(input.chatId ? { [TraceAttr.ChatId]: input.chatId } : {}),
306307
...(typeof input.argsBytes === 'number'

0 commit comments

Comments
 (0)