The Open Framework for Building Agents
feat(eve): record gen_ai.execute_tool.duration on execute_tool spans (#4268)
Rui3b1fe5c3c505
diff --git a/.changeset/tool-span-duration.md b/.changeset/tool-span-duration.mdnew file mode 100644index 000000000..bcc84c8f9--- /dev/null+++ b/.changeset/tool-span-duration.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Record `gen_ai.execute_tool.duration` on `execute_tool` spans, as `@ai-sdk/otel` does. Instrumentation providers also receive the tool's run time as `durationMs` on `tool.call.completed`.diff --git a/packages/eve/src/instrumentation/ai-sdk-hook-bridge.test.ts b/packages/eve/src/instrumentation/ai-sdk-hook-bridge.test.tsindex 30e861648..d372281a8 100644--- a/packages/eve/src/instrumentation/ai-sdk-hook-bridge.test.ts+++ b/packages/eve/src/instrumentation/ai-sdk-hook-bridge.test.ts@@ -674,6 +674,7 @@ describe("createAiSdkHookBridge", () => {);expect(after).toHaveBeenCalledExactlyOnceWith({+ durationMs: 1,idempotencyKey: `tool:${scope.attemptId}:tool-1:0`,output: expected,scope,diff --git a/packages/eve/src/instrumentation/ai-sdk-hook-bridge.ts b/packages/eve/src/instrumentation/ai-sdk-hook-bridge.tsindex b9c6390c4..b46ce76ac 100644--- a/packages/eve/src/instrumentation/ai-sdk-hook-bridge.ts+++ b/packages/eve/src/instrumentation/ai-sdk-hook-bridge.ts@@ -318,6 +318,7 @@ function toToolCallCompleted(source: TelemetryEvent<"onToolExecutionEnd">,): InstrumentationToolCallCompletedEvent {return Object.freeze({+ durationMs: source.toolExecutionMs,idempotencyKey,output: toToolOutput(source.toolOutput, state.capturesOutputs),scope: state.scope,diff --git a/packages/eve/src/instrumentation/lifecycle.ts b/packages/eve/src/instrumentation/lifecycle.tsindex a48cddc5e..0ae9d6ad4 100644--- a/packages/eve/src/instrumentation/lifecycle.ts+++ b/packages/eve/src/instrumentation/lifecycle.ts@@ -474,6 +474,8 @@ export interface InstrumentationToolCallStartedEvent {export interface InstrumentationToolCallCompletedEvent {readonly type: "tool.call.completed";+ /** How long the tool's `execute` ran, in milliseconds, when the publisher measured it. */+ readonly durationMs?: number;readonly idempotencyKey: string;readonly output: InstrumentationToolOutput;readonly scope: InstrumentationAttemptScope;diff --git a/packages/eve/src/tracing/agent-otel-provider.test.ts b/packages/eve/src/tracing/agent-otel-provider.test.tsindex 90ebb1ce4..57eaed98f 100644--- a/packages/eve/src/tracing/agent-otel-provider.test.ts+++ b/packages/eve/src/tracing/agent-otel-provider.test.ts@@ -2023,6 +2023,7 @@ describe("createAgentOtelInstrumentation", () => {type: "tool.call.started",});await runtime.hooks.publish({+ durationMs: 1500,idempotencyKey: toolKey,output: { output: "sunny", type: "result" },scope,@@ -2071,6 +2072,9 @@ describe("createAgentOtelInstrumentation", () => {const tool = byName(spans, "execute_tool weather")[0]!;const uncorrelatedTool = byName(spans, "execute_tool final_output")[0]!;expect(tool.parentSpanContext?.spanId).toBe(action.spanContext().spanId);+ // Seconds, as `@ai-sdk/otel` records it; a publisher that measured nothing sets none.+ expect(tool.attributes["gen_ai.execute_tool.duration"]).toBe(1.5);+ expect(uncorrelatedTool.attributes["gen_ai.execute_tool.duration"]).toBeUndefined();expect(uncorrelatedTool.parentSpanContext?.spanId).toBe(byName(spans, "agent.step")[0]!.spanContext().spanId,);diff --git a/packages/eve/src/tracing/agent-tool-instrumentation.ts b/packages/eve/src/tracing/agent-tool-instrumentation.tsindex baac9ccb0..ce81914be 100644--- a/packages/eve/src/tracing/agent-tool-instrumentation.ts+++ b/packages/eve/src/tracing/agent-tool-instrumentation.ts@@ -219,6 +219,10 @@ export function createAgentToolInstrumentation(input: {if (span === undefined || state.finished === true) return;state.finished = true;const terminal = state.terminal;+ // As `@ai-sdk/otel` records it: execute's own run time, in seconds, success or error.+ if (terminal?.type === "tool.call.completed" && terminal.durationMs !== undefined) {+ span.setAttribute("gen_ai.execute_tool.duration", terminal.durationMs / 1000);+ }if (failure !== undefined) {recordError(span, failure.error);} else if (terminal?.type === "tool.call.failed") {