Skip to content

Commit 28ffcf2

Browse files
committed
feat(copilot): attribute tool spans to agents
1 parent af05c8c commit 28ffcf2

3 files changed

Lines changed: 23 additions & 8 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ export async function withCopilotToolSpan<T>(
284284
input: {
285285
toolName: string
286286
toolCallId: string
287+
agentName: string
287288
runId?: string
288289
chatId?: string
289290
argsBytes?: number
@@ -299,6 +300,7 @@ export async function withCopilotToolSpan<T>(
299300
[TraceAttr.ToolName]: input.toolName,
300301
[TraceAttr.ToolCallId]: input.toolCallId,
301302
[TraceAttr.ToolExecutor]: 'sim',
303+
[TraceAttr.GenAiAgentName]: input.agentName,
302304
...(input.runId ? { [TraceAttr.RunId]: input.runId } : {}),
303305
...(input.chatId ? { [TraceAttr.ChatId]: input.chatId } : {}),
304306
...(typeof input.argsBytes === 'number'

apps/sim/lib/copilot/request/tools/executor.test.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,24 @@ import '@sim/testing/mocks/executor'
22

33
import { beforeEach, describe, expect, it, vi } from 'vitest'
44

5-
const { recordSimToolMetric, setAttribute } = vi.hoisted(() => ({
6-
recordSimToolMetric: vi.fn(),
7-
setAttribute: vi.fn(),
8-
}))
5+
const { recordSimToolMetric, setAttribute, withCopilotToolSpan } = vi.hoisted(() => {
6+
const setAttribute = vi.fn()
7+
return {
8+
recordSimToolMetric: vi.fn(),
9+
setAttribute,
10+
withCopilotToolSpan: vi.fn(
11+
(_input: unknown, fn: (span: { setAttribute: typeof setAttribute }) => Promise<unknown>) =>
12+
fn({ setAttribute })
13+
),
14+
}
15+
})
916

1017
vi.mock('@/lib/copilot/request/metrics', () => ({
1118
recordSimToolMetric,
1219
}))
1320

1421
vi.mock('@/lib/copilot/request/otel', () => ({
15-
withCopilotToolSpan: (
16-
_input: unknown,
17-
fn: (span: { setAttribute: typeof setAttribute }) => Promise<unknown>
18-
) => fn({ setAttribute }),
22+
withCopilotToolSpan,
1923
}))
2024

2125
import { TOOL_WATCHDOG_DEFAULT_MS, TOOL_WATCHDOG_LONG_RUNNING_MS } from '@/lib/copilot/constants'
@@ -110,6 +114,10 @@ describe('executeToolAndReport metrics', () => {
110114
MothershipStreamV1ToolOutcome.success,
111115
expect.any(Number)
112116
)
117+
expect(withCopilotToolSpan).toHaveBeenCalledWith(
118+
expect.objectContaining({ agentName: 'workflow' }),
119+
expect.any(Function)
120+
)
113121
})
114122

115123
it.each([
@@ -138,6 +146,10 @@ describe('executeToolAndReport metrics', () => {
138146
MothershipStreamV1ToolOutcome.error,
139147
expect.any(Number)
140148
)
149+
expect(withCopilotToolSpan).toHaveBeenCalledWith(
150+
expect.objectContaining({ agentName: expectedAgentId }),
151+
expect.any(Function)
152+
)
141153
}
142154
)
143155
})

apps/sim/lib/copilot/request/tools/executor.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ export async function executeToolAndReport(
408408
{
409409
toolName: toolCall.name,
410410
toolCallId: toolCall.id,
411+
agentName: toolCall.agentId ?? 'main',
411412
runId: context.runId,
412413
chatId: execContext.chatId,
413414
argsBytes: argsPayload?.length,

0 commit comments

Comments
 (0)