@@ -28,6 +28,7 @@ import {
2828 generateTraceId ,
2929 parseEventsField ,
3030 convertDateToNanoseconds ,
31+ createExceptionPropertiesFromError ,
3132} from "./common.server" ;
3233import { serializeTraceparent } from "@trigger.dev/core/v3/isomorphic" ;
3334import {
@@ -46,6 +47,8 @@ import {
4647import { unflattenAttributes } from "@trigger.dev/core/v3/utils/flattenAttributes" ;
4748import { TaskEventLevel } from "@trigger.dev/database" ;
4849import { originalRunIdCache } from "./originalRunIdCache.server" ;
50+ import { SemanticInternalAttributes } from "@trigger.dev/core/v3/semanticInternalAttributes" ;
51+ import { createJsonErrorObject } from "@trigger.dev/core/v3/errors" ;
4952
5053export type ClickhouseEventRepositoryConfig = {
5154 clickhouse : ClickHouse ;
@@ -500,9 +503,46 @@ export class ClickhouseEventRepository implements IEventRepository {
500503 expires_at : convertDateToClickhouseDateTime ( new Date ( Date . now ( ) + 30 * 24 * 60 * 60 * 1000 ) ) ,
501504 } ;
502505
503- // TODO: Handle failedWithError by creating a SPAN_EVENT
506+ const originalRunId =
507+ options . attributes . properties ?. [ SemanticInternalAttributes . ORIGINAL_RUN_ID ] ;
504508
505- this . _flushScheduler . addToBatch ( [ event ] ) ;
509+ if ( typeof originalRunId === "string" ) {
510+ await originalRunIdCache . set ( traceId , spanId , originalRunId ) ;
511+ }
512+
513+ const events = [ event ] ;
514+
515+ if ( failedWithError ) {
516+ const error = createJsonErrorObject ( failedWithError ) ;
517+
518+ events . push ( {
519+ environment_id : options . environment . id ,
520+ organization_id : options . environment . organizationId ,
521+ project_id : options . environment . projectId ,
522+ task_identifier : options . taskSlug ,
523+ run_id : options . attributes . runId ,
524+ start_time : startTime . toString ( ) ,
525+ duration : String ( options . incomplete ? 0 : duration ) ,
526+ trace_id : traceId ,
527+ span_id : spanId ,
528+ parent_span_id : parentId ?? "" ,
529+ message : "exception" ,
530+ kind : "SPAN_EVENT" ,
531+ status : "ERROR" ,
532+ attributes : {
533+ error,
534+ } ,
535+ metadata : JSON . stringify ( {
536+ exception : createExceptionPropertiesFromError ( failedWithError ) ,
537+ } ) ,
538+ // TODO: make sure configurable and by org
539+ expires_at : convertDateToClickhouseDateTime (
540+ new Date ( Date . now ( ) + 30 * 24 * 60 * 60 * 1000 )
541+ ) ,
542+ } ) ;
543+ }
544+
545+ this . _flushScheduler . addToBatch ( events ) ;
506546
507547 return result ;
508548 }
0 commit comments