diff --git a/js/core/src/tracing/instrumentation.ts b/js/core/src/tracing/instrumentation.ts index 7d000d8861..d5a5c4a29b 100644 --- a/js/core/src/tracing/instrumentation.ts +++ b/js/core/src/tracing/instrumentation.ts @@ -89,8 +89,11 @@ export async function runInNewSpan( opts.metadata.state = 'error'; otSpan.setStatus({ code: SpanStatusCode.ERROR, - message: formatError(e), + message: getErrorMessage(e), }); + if (e instanceof Error) { + otSpan.recordException(e); + } throw e; } finally { otSpan.setAttributes(metadataToAttributes(opts.metadata)); @@ -100,9 +103,9 @@ export async function runInNewSpan( ); } -function formatError(e: any): string { +function getErrorMessage(e: any): string { if (e instanceof Error) { - return `${e.message}\n${e.stack}`; + return e.message; } return `${e}`; } diff --git a/js/flow/src/flow.ts b/js/flow/src/flow.ts index 1fb5a6bd25..f6a037a1b5 100644 --- a/js/flow/src/flow.ts +++ b/js/flow/src/flow.ts @@ -16,29 +16,29 @@ import { Action, - action, FlowError, FlowState, FlowStateSchema, FlowStateStore, + Operation, + StreamingCallback, + action, getStreamingCallback, config as globalConfig, isDevEnv, - Operation, - StreamingCallback, } from '@genkit-ai/core'; import { logger } from '@genkit-ai/core/logging'; import { registerAction } from '@genkit-ai/core/registry'; import { toJsonSchema } from '@genkit-ai/core/schema'; import { + SPAN_TYPE_ATTR, newTrace, setCustomMetadataAttribute, setCustomMetadataAttributes, - SPAN_TYPE_ATTR, } from '@genkit-ai/core/tracing'; import { SpanStatusCode } from '@opentelemetry/api'; import * as bodyParser from 'body-parser'; -import { default as cors, CorsOptions } from 'cors'; +import { CorsOptions, default as cors } from 'cors'; import express from 'express'; import { performance } from 'node:perf_hooks'; import * as z from 'zod'; @@ -46,9 +46,9 @@ import { Context } from './context.js'; import { FlowExecutionError, FlowStillRunningError, + InterruptError, getErrorMessage, getErrorStack, - InterruptError, } from './errors.js'; import * as telemetry from './telemetry.js'; import { @@ -464,8 +464,11 @@ export class Flow< metadata.state = 'error'; rootSpan.setStatus({ code: SpanStatusCode.ERROR, - message: formatError(e), + message: getErrorMessage(e), }); + if (e instanceof Error) { + rootSpan.recordException(e); + } setCustomMetadataAttribute(metadataPrefix('state'), 'error'); ctx.state.operation.done = true; @@ -856,10 +859,3 @@ export function startFlowsServer(params?: { console.log(`Flows server listening on port ${port}`); }); } - -function formatError(e: any): string { - if (e instanceof Error) { - return `${e.message}\n${e.stack}`; - } - return `${e}`; -}