77 type MessageUpdate ,
88 type PgoutputMessage ,
99} from "@internal/replication" ;
10- import { Span , startSpan , trace , type Tracer } from "@internal/tracing" ;
10+ import { startSpan , trace , type Tracer } from "@internal/tracing" ;
1111import { Logger , LogLevel } from "@trigger.dev/core/logger" ;
1212import { tryCatch } from "@trigger.dev/core/utils" ;
1313import { parsePacket } from "@trigger.dev/core/v3/utils/ioSerialization" ;
@@ -23,6 +23,7 @@ interface TransactionEvent<T = any> {
2323}
2424
2525interface Transaction < T = any > {
26+ beginStartTimestamp : number ;
2627 commitLsn : string | null ;
2728 commitEndLsn : string | null ;
2829 xid : number ;
@@ -70,7 +71,6 @@ export class RunsReplicationService {
7071 private _isShuttingDown = false ;
7172 private _isShutDownComplete = false ;
7273 private _tracer : Tracer ;
73- private _currentSpan : Span | null = null ;
7474 private _currentParseDurationMs : number | null = null ;
7575 private _lastAcknowledgedAt : number | null = null ;
7676 private _acknowledgeTimeoutMs : number ;
@@ -208,17 +208,12 @@ export class RunsReplicationService {
208208 }
209209
210210 this . _currentTransaction = {
211+ beginStartTimestamp : Date . now ( ) ,
211212 commitLsn : message . commitLsn ,
212213 xid : message . xid ,
213214 events : [ ] ,
214215 } ;
215216
216- this . _currentSpan = this . _tracer . startSpan ( "handle_transaction" , {
217- attributes : {
218- "transaction.xid" : message . xid ,
219- } ,
220- } ) ;
221-
222217 this . _currentParseDurationMs = Number ( parseDuration ) / 1_000_000 ;
223218
224219 break ;
@@ -283,11 +278,6 @@ export class RunsReplicationService {
283278 if ( this . _currentParseDurationMs ) {
284279 this . _currentParseDurationMs =
285280 this . _currentParseDurationMs + Number ( parseDuration ) / 1_000_000 ;
286-
287- this . _currentSpan ?. setAttribute (
288- "transaction.parse_duration_ms" ,
289- this . _currentParseDurationMs
290- ) ;
291281 }
292282
293283 const replicationLagMs = Date . now ( ) - Number ( message . commitTime / 1000n ) ;
@@ -303,6 +293,11 @@ export class RunsReplicationService {
303293 this . #handleTransaction( transaction ) ;
304294 break ;
305295 }
296+ default : {
297+ this . logger . debug ( "Unknown message tag" , {
298+ pgMessage : message ,
299+ } ) ;
300+ }
306301 }
307302 }
308303
@@ -315,19 +310,8 @@ export class RunsReplicationService {
315310 } ) ;
316311 }
317312
318- this . _currentSpan ?. setAttribute ( "transaction.replication_lag_ms" , transaction . replicationLagMs ) ;
319- this . _currentSpan ?. setAttribute ( "transaction.xid" , transaction . xid ) ;
320-
321- if ( transaction . commitEndLsn ) {
322- this . _currentSpan ?. setAttribute ( "transaction.commit_end_lsn" , transaction . commitEndLsn ) ;
323- }
324-
325- this . _currentSpan ?. setAttribute ( "transaction.events" , transaction . events . length ) ;
326-
327313 // If there are no events, do nothing
328314 if ( transaction . events . length === 0 ) {
329- this . _currentSpan ?. end ( ) ;
330-
331315 return ;
332316 }
333317
@@ -336,8 +320,6 @@ export class RunsReplicationService {
336320 transaction,
337321 } ) ;
338322
339- this . _currentSpan ?. end ( ) ;
340-
341323 return ;
342324 }
343325
@@ -350,10 +332,7 @@ export class RunsReplicationService {
350332 // If there are events, we need to handle them
351333 const _version = lsnToUInt64 ( transaction . commitEndLsn ) ;
352334
353- this . _currentSpan ?. setAttribute (
354- "transaction.lsn_to_uint64_ms" ,
355- Number ( process . hrtime . bigint ( ) - lsnToUInt64Start ) / 1_000_000
356- ) ;
335+ const lsnToUInt64DurationMs = Number ( process . hrtime . bigint ( ) - lsnToUInt64Start ) / 1_000_000 ;
357336
358337 this . _concurrentFlushScheduler . addToBatch (
359338 transaction . events . map ( ( event ) => ( {
@@ -363,7 +342,20 @@ export class RunsReplicationService {
363342 } ) )
364343 ) ;
365344
366- this . _currentSpan ?. end ( ) ;
345+ const currentSpan = this . _tracer . startSpan ( "handle_transaction" , {
346+ attributes : {
347+ "transaction.xid" : transaction . xid ,
348+ "transaction.replication_lag_ms" : transaction . replicationLagMs ,
349+ "transaction.events" : transaction . events . length ,
350+ "transaction.commit_end_lsn" : transaction . commitEndLsn ,
351+ "transaction.parse_duration_ms" : this . _currentParseDurationMs ?? undefined ,
352+ "transaction.lsn_to_uint64_ms" : lsnToUInt64DurationMs ,
353+ "transaction.version" : _version . toString ( ) ,
354+ } ,
355+ startTime : transaction . beginStartTimestamp ,
356+ } ) ;
357+
358+ currentSpan . end ( ) ;
367359 }
368360
369361 async #acknowledgeLatestTransaction( ) {
0 commit comments