feat: enrich AgentRun dispatch error OTel spans

This commit is contained in:
lyon
2026-06-20 11:17:57 +08:00
parent 5e7224c666
commit 1c4cf29274
+19 -1
View File
@@ -829,10 +829,11 @@ async function submitCodeAgentChatTurn({ params, options, traceId }) {
});
} catch (error) {
if (isCodeAgentResultCanceled(results.get(traceId))) return;
const dispatchErrorAttributes = codeAgentDispatchErrorOtelAttributes(error);
void emitCodeAgentOtelSpan("agentrun_dispatch", traceId, executionOptions.env ?? process.env, {
status: "error",
error,
attributes: { sessionId: safeSessionId(params.sessionId) || null, turnId: codeAgentTurnLifecycleFields(traceId, params).turnId }
attributes: { sessionId: safeSessionId(params.sessionId) || null, turnId: codeAgentTurnLifecycleFields(traceId, params).turnId, ...dispatchErrorAttributes }
});
await settleAdmittedCodeAgentFailure({
base: initial ?? codeAgentAdmittedFailureBasePayload({ params, options, traceId }),
@@ -1028,6 +1029,23 @@ function codeAgentDispatchFailure(error) {
};
}
function codeAgentDispatchErrorOtelAttributes(error) {
const agentRunError = error?.agentRunError && typeof error.agentRunError === "object" ? error.agentRunError : null;
const details = agentRunError?.error?.details && typeof agentRunError.error.details === "object" ? agentRunError.error.details : null;
return {
failureKind: safeOtelText(error?.code ?? agentRunError?.failureKind, 120),
upstreamStatusCode: Number.isInteger(error?.statusCode) ? error.statusCode : null,
upstreamTraceId: safeTraceId(agentRunError?.traceId) || null,
upstreamMessage: safeOtelText(agentRunError?.message ?? error?.message, 300),
upstreamStderrTail: safeOtelText(details?.stderr, 600)
};
}
function safeOtelText(value, limit = 300) {
const text = String(value ?? "").replace(/[\u0000-\u001f\u007f]+/gu, " ").trim();
return text ? text.slice(0, limit) : null;
}
function codeAgentAdmittedFailureBasePayload({ params = {}, options = {}, traceId } = {}) {
const now = new Date().toISOString();
return {