7
7
type MessageUpdate ,
8
8
type PgoutputMessage ,
9
9
} from "@internal/replication" ;
10
- import { Span , startSpan , trace , type Tracer } from "@internal/tracing" ;
10
+ import { startSpan , trace , type Tracer } from "@internal/tracing" ;
11
11
import { Logger , LogLevel } from "@trigger.dev/core/logger" ;
12
12
import { tryCatch } from "@trigger.dev/core/utils" ;
13
13
import { parsePacket } from "@trigger.dev/core/v3/utils/ioSerialization" ;
@@ -23,6 +23,7 @@ interface TransactionEvent<T = any> {
23
23
}
24
24
25
25
interface Transaction < T = any > {
26
+ beginStartTimestamp : number ;
26
27
commitLsn : string | null ;
27
28
commitEndLsn : string | null ;
28
29
xid : number ;
@@ -70,7 +71,6 @@ export class RunsReplicationService {
70
71
private _isShuttingDown = false ;
71
72
private _isShutDownComplete = false ;
72
73
private _tracer : Tracer ;
73
- private _currentSpan : Span | null = null ;
74
74
private _currentParseDurationMs : number | null = null ;
75
75
private _lastAcknowledgedAt : number | null = null ;
76
76
private _acknowledgeTimeoutMs : number ;
@@ -208,17 +208,12 @@ export class RunsReplicationService {
208
208
}
209
209
210
210
this . _currentTransaction = {
211
+ beginStartTimestamp : Date . now ( ) ,
211
212
commitLsn : message . commitLsn ,
212
213
xid : message . xid ,
213
214
events : [ ] ,
214
215
} ;
215
216
216
- this . _currentSpan = this . _tracer . startSpan ( "handle_transaction" , {
217
- attributes : {
218
- "transaction.xid" : message . xid ,
219
- } ,
220
- } ) ;
221
-
222
217
this . _currentParseDurationMs = Number ( parseDuration ) / 1_000_000 ;
223
218
224
219
break ;
@@ -283,11 +278,6 @@ export class RunsReplicationService {
283
278
if ( this . _currentParseDurationMs ) {
284
279
this . _currentParseDurationMs =
285
280
this . _currentParseDurationMs + Number ( parseDuration ) / 1_000_000 ;
286
-
287
- this . _currentSpan ?. setAttribute (
288
- "transaction.parse_duration_ms" ,
289
- this . _currentParseDurationMs
290
- ) ;
291
281
}
292
282
293
283
const replicationLagMs = Date . now ( ) - Number ( message . commitTime / 1000n ) ;
@@ -303,6 +293,11 @@ export class RunsReplicationService {
303
293
this . #handleTransaction( transaction ) ;
304
294
break ;
305
295
}
296
+ default : {
297
+ this . logger . debug ( "Unknown message tag" , {
298
+ pgMessage : message ,
299
+ } ) ;
300
+ }
306
301
}
307
302
}
308
303
@@ -315,19 +310,8 @@ export class RunsReplicationService {
315
310
} ) ;
316
311
}
317
312
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
-
327
313
// If there are no events, do nothing
328
314
if ( transaction . events . length === 0 ) {
329
- this . _currentSpan ?. end ( ) ;
330
-
331
315
return ;
332
316
}
333
317
@@ -336,8 +320,6 @@ export class RunsReplicationService {
336
320
transaction,
337
321
} ) ;
338
322
339
- this . _currentSpan ?. end ( ) ;
340
-
341
323
return ;
342
324
}
343
325
@@ -350,10 +332,7 @@ export class RunsReplicationService {
350
332
// If there are events, we need to handle them
351
333
const _version = lsnToUInt64 ( transaction . commitEndLsn ) ;
352
334
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 ;
357
336
358
337
this . _concurrentFlushScheduler . addToBatch (
359
338
transaction . events . map ( ( event ) => ( {
@@ -363,7 +342,20 @@ export class RunsReplicationService {
363
342
} ) )
364
343
) ;
365
344
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 ( ) ;
367
359
}
368
360
369
361
async #acknowledgeLatestTransaction( ) {
0 commit comments