Skip to content
Open
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
12 changes: 8 additions & 4 deletions core/src/agents/base_agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {context, trace} from '@opentelemetry/api';
import {createEvent, Event} from '../events/event.js';

import {
getElapsedS,
recordAgentInvocationDuration,
recordAgentRequestSize,
recordAgentResponseSize,
Expand Down Expand Up @@ -283,7 +284,7 @@ export abstract class BaseAgent<
const startTime = performance.now();
const agentName = this.name;
const tally: AgentEventTally = {stepCount: 0};
let error: Error | undefined;
let error: unknown;
try {
yield* runAsyncGeneratorWithOtelContext<BaseAgent, Event>(
ctx,
Expand Down Expand Up @@ -323,12 +324,15 @@ export abstract class BaseAgent<
},
);
} catch (e) {
error = e as Error;
error = e;
throw e;
} finally {
span.end();
const elapsedMs = performance.now() - startTime;
recordAgentInvocationDuration(agentName, elapsedMs, error);
recordAgentInvocationDuration(
agentName,
getElapsedS(span, startTime),
error,
);
recordAgentWorkflowSteps(agentName, tally.stepCount);
recordAgentResponseSize(agentName, tally.lastContent);
}
Expand Down
14 changes: 10 additions & 4 deletions core/src/agents/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import {ToolConfirmation} from '../tools/tool_confirmation.js';
import {logger} from '../utils/logger.js';
import {Context} from './context.js';

import {recordToolExecutionDuration} from '../telemetry/metrics.js';
import {
getElapsedS,
recordToolExecutionDuration,
} from '../telemetry/metrics.js';
import {
traceMergedToolCalls,
tracer,
Expand Down Expand Up @@ -179,8 +182,10 @@ async function callToolAsync(
const startTime = performance.now();
const agentName = toolContext.invocationContext.agent.name;
const toolName = tool.name;
// e.g. FunctionTool, matching the gen_ai.tool.type span attribute.
const toolType = tool.constructor.name;
return tracer.startActiveSpan(`execute_tool ${tool.name}`, async (span) => {
let error: Error | undefined;
let error: unknown;
try {
logger.debug(`callToolAsync ${tool.name}`);
const result = await tool.runAsync({args, toolContext});
Expand All @@ -196,14 +201,15 @@ async function callToolAsync(
});
return result;
} catch (e) {
error = e as Error;
error = e;
throw e;
} finally {
span.end();
recordToolExecutionDuration(
toolName,
toolType,
agentName,
performance.now() - startTime,
getElapsedS(span, startTime),
error,
);
}
Expand Down
22 changes: 13 additions & 9 deletions core/src/agents/llm_agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {logger} from '../utils/logger.js';
import {Context} from './context.js';

import {
getElapsedS,
recordClientOperationDuration,
recordClientTokenUsage,
} from '../telemetry/metrics.js';
Expand Down Expand Up @@ -1093,7 +1094,7 @@ export class LlmAgent extends BaseAgent<LlmAgentConfig> {
invocationContext.incrementLlmCallCount();
const startTime = performance.now();
let lastResponse: LlmResponse | undefined;
let error: Error | undefined;
let error: unknown;
try {
const responsesGenerator = llm.generateContentAsync(
llmRequest,
Expand Down Expand Up @@ -1129,18 +1130,21 @@ export class LlmAgent extends BaseAgent<LlmAgentConfig> {
yield alteredLlmResponse ?? llmResponse;
}
} catch (e) {
error = e as Error;
error = e;
throw e;
} finally {
const elapsedMs = performance.now() - startTime;
recordClientOperationDuration(
this.name,
elapsedMs,
recordClientOperationDuration({
agentName: this.name,
elapsedS: getElapsedS(undefined, startTime),
llmRequest,
lastResponse,
response: lastResponse,
error,
);
recordClientTokenUsage(this.name, llmRequest, lastResponse);
});
recordClientTokenUsage({
agentName: this.name,
llmRequest,
response: lastResponse,
});
}
}
}
Expand Down
1 change: 0 additions & 1 deletion core/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,5 +321,4 @@ export * from './artifacts/base_artifact_service.js';
export * from './features/feature_registry.js';
export * from './memory/base_memory_service.js';
export * from './sessions/base_session_service.js';
export * from './telemetry/metrics.js';
export * from './tools/base_tool.js';
Loading