|
| 1 | +import { EventRepository } from "~/v3/eventRepository.server"; |
| 2 | +import { TracedEventSpan, TraceEventConcern, TriggerTaskRequest } from "../types"; |
| 3 | +import { SemanticInternalAttributes } from "@trigger.dev/core/v3/semanticInternalAttributes"; |
| 4 | +import { BatchId } from "@trigger.dev/core/v3/isomorphic"; |
| 5 | +import { TaskRun } from "@trigger.dev/database"; |
| 6 | + |
| 7 | +export class DefaultTraceEventsConcern implements TraceEventConcern { |
| 8 | + private readonly eventRepository: EventRepository; |
| 9 | + |
| 10 | + constructor(eventRepository: EventRepository) { |
| 11 | + this.eventRepository = eventRepository; |
| 12 | + } |
| 13 | + |
| 14 | + async traceRun<T>( |
| 15 | + request: TriggerTaskRequest, |
| 16 | + callback: (span: TracedEventSpan) => Promise<T> |
| 17 | + ): Promise<T> { |
| 18 | + return await this.eventRepository.traceEvent( |
| 19 | + request.taskId, |
| 20 | + { |
| 21 | + context: request.options?.traceContext, |
| 22 | + spanParentAsLink: request.options?.spanParentAsLink, |
| 23 | + parentAsLinkType: request.options?.parentAsLinkType, |
| 24 | + kind: "SERVER", |
| 25 | + environment: request.environment, |
| 26 | + taskSlug: request.taskId, |
| 27 | + attributes: { |
| 28 | + properties: { |
| 29 | + [SemanticInternalAttributes.SHOW_ACTIONS]: true, |
| 30 | + }, |
| 31 | + style: { |
| 32 | + icon: request.options?.customIcon ?? "task", |
| 33 | + }, |
| 34 | + runIsTest: request.body.options?.test ?? false, |
| 35 | + batchId: request.options?.batchId |
| 36 | + ? BatchId.toFriendlyId(request.options.batchId) |
| 37 | + : undefined, |
| 38 | + idempotencyKey: request.options?.idempotencyKey, |
| 39 | + }, |
| 40 | + incomplete: true, |
| 41 | + immediate: true, |
| 42 | + }, |
| 43 | + async (event, traceContext, traceparent) => { |
| 44 | + return await callback({ |
| 45 | + traceId: event.traceId, |
| 46 | + spanId: event.spanId, |
| 47 | + traceContext, |
| 48 | + traceparent, |
| 49 | + setAttribute: (key, value) => event.setAttribute(key as any, value), |
| 50 | + failWithError: event.failWithError.bind(event), |
| 51 | + }); |
| 52 | + } |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + async traceIdempotentRun<T>( |
| 57 | + request: TriggerTaskRequest, |
| 58 | + options: { |
| 59 | + existingRun: TaskRun; |
| 60 | + idempotencyKey: string; |
| 61 | + incomplete: boolean; |
| 62 | + isError: boolean; |
| 63 | + }, |
| 64 | + callback: (span: TracedEventSpan) => Promise<T> |
| 65 | + ): Promise<T> { |
| 66 | + const { existingRun, idempotencyKey, incomplete, isError } = options; |
| 67 | + |
| 68 | + return await this.eventRepository.traceEvent( |
| 69 | + `${request.taskId} (cached)`, |
| 70 | + { |
| 71 | + context: request.options?.traceContext, |
| 72 | + spanParentAsLink: request.options?.spanParentAsLink, |
| 73 | + parentAsLinkType: request.options?.parentAsLinkType, |
| 74 | + kind: "SERVER", |
| 75 | + environment: request.environment, |
| 76 | + taskSlug: request.taskId, |
| 77 | + attributes: { |
| 78 | + properties: { |
| 79 | + [SemanticInternalAttributes.SHOW_ACTIONS]: true, |
| 80 | + [SemanticInternalAttributes.ORIGINAL_RUN_ID]: existingRun.friendlyId, |
| 81 | + }, |
| 82 | + style: { |
| 83 | + icon: "task-cached", |
| 84 | + }, |
| 85 | + runIsTest: request.body.options?.test ?? false, |
| 86 | + batchId: request.options?.batchId |
| 87 | + ? BatchId.toFriendlyId(request.options.batchId) |
| 88 | + : undefined, |
| 89 | + idempotencyKey, |
| 90 | + runId: existingRun.friendlyId, |
| 91 | + }, |
| 92 | + incomplete, |
| 93 | + isError, |
| 94 | + immediate: true, |
| 95 | + }, |
| 96 | + async (event, traceContext, traceparent) => { |
| 97 | + //log a message |
| 98 | + await this.eventRepository.recordEvent( |
| 99 | + `There's an existing run for idempotencyKey: ${idempotencyKey}`, |
| 100 | + { |
| 101 | + taskSlug: request.taskId, |
| 102 | + environment: request.environment, |
| 103 | + attributes: { |
| 104 | + runId: existingRun.friendlyId, |
| 105 | + }, |
| 106 | + context: request.options?.traceContext, |
| 107 | + parentId: event.spanId, |
| 108 | + } |
| 109 | + ); |
| 110 | + |
| 111 | + return await callback({ |
| 112 | + traceId: event.traceId, |
| 113 | + spanId: event.spanId, |
| 114 | + traceContext, |
| 115 | + traceparent, |
| 116 | + setAttribute: (key, value) => event.setAttribute(key as any, value), |
| 117 | + failWithError: event.failWithError.bind(event), |
| 118 | + }); |
| 119 | + } |
| 120 | + ); |
| 121 | + } |
| 122 | +} |
0 commit comments