@@ -440,16 +440,23 @@ func PullCdcRecords[Items model.Items](
440440
441441 logger .Info ("pulling records start" )
442442
443+ var fetchedBytes , totalFetchedBytes , allFetchedBytes atomic.Int64
444+ defer func () {
445+ p .otelManager .Metrics .FetchedBytesCounter .Add (ctx , fetchedBytes .Swap (0 ))
446+ p .otelManager .Metrics .AllFetchedBytesCounter .Add (ctx , allFetchedBytes .Swap (0 ))
447+ }()
443448 shutdown := shared .Interval (ctx , time .Minute , func () {
444- logger .Info ("pulling records" , slog .Int ("records" , cdcRecordsStorage .Len ()))
449+ p .otelManager .Metrics .FetchedBytesCounter .Add (ctx , fetchedBytes .Swap (0 ))
450+ p .otelManager .Metrics .AllFetchedBytesCounter .Add (ctx , allFetchedBytes .Swap (0 ))
451+ logger .Info ("pulling records" , slog .Int ("records" , cdcRecordsStorage .Len ()),
452+ slog .Int64 ("bytes" , totalFetchedBytes .Load ()))
445453 })
446454 defer shutdown ()
447455
448456 nextStandbyMessageDeadline := time .Now ().Add (req .IdleTimeout )
449457
450458 pkmRequiresResponse := false
451459 waitingForCommit := false
452- fetchedBytes := 0
453460
454461 addRecordWithKey := func (key model.TableWithPkey , rec model.Record [Items ]) error {
455462 if err := cdcRecordsStorage .Set (logger , key , rec ); err != nil {
@@ -467,7 +474,7 @@ func PullCdcRecords[Items model.Items](
467474 if cdcRecordsStorage .Len ()% 50000 == 0 {
468475 logger .Info ("pulling records" ,
469476 slog .Int ("records" , cdcRecordsStorage .Len ()),
470- slog .Int ("bytes" , fetchedBytes ),
477+ slog .Int64 ("bytes" , totalFetchedBytes . Load () ),
471478 slog .Int ("channelLen" , records .ChannelLen ()),
472479 slog .Bool ("waitingForCommit" , waitingForCommit ))
473480 }
@@ -497,7 +504,7 @@ func PullCdcRecords[Items model.Items](
497504 if time .Since (standByLastLogged ) > 10 * time .Second {
498505 logger .Info ("Sent Standby status message" ,
499506 slog .Int ("records" , cdcRecordsStorage .Len ()),
500- slog .Int ("bytes" , fetchedBytes ),
507+ slog .Int64 ("bytes" , totalFetchedBytes . Load () ),
501508 slog .Int ("channelLen" , records .ChannelLen ()),
502509 slog .Bool ("waitingForCommit" , waitingForCommit ))
503510 standByLastLogged = time .Now ()
@@ -508,15 +515,15 @@ func PullCdcRecords[Items model.Items](
508515 if cdcRecordsStorage .Len () >= int (req .MaxBatchSize ) {
509516 logger .Info ("batch filled, returning currently accumulated records" ,
510517 slog .Int ("records" , cdcRecordsStorage .Len ()),
511- slog .Int ("bytes" , fetchedBytes ),
518+ slog .Int64 ("bytes" , totalFetchedBytes . Load () ),
512519 slog .Int ("channelLen" , records .ChannelLen ()))
513520 return nil
514521 }
515522
516523 if waitingForCommit {
517524 logger .Info ("commit received, returning currently accumulated records" ,
518525 slog .Int ("records" , cdcRecordsStorage .Len ()),
519- slog .Int ("bytes" , fetchedBytes ),
526+ slog .Int64 ("bytes" , totalFetchedBytes . Load () ),
520527 slog .Int ("channelLen" , records .ChannelLen ()))
521528 return nil
522529 }
@@ -530,13 +537,13 @@ func PullCdcRecords[Items model.Items](
530537 if p .commitLock == nil {
531538 logger .Info ("no commit lock, returning currently accumulated records" ,
532539 slog .Int ("records" , cdcRecordsStorage .Len ()),
533- slog .Int ("bytes" , fetchedBytes ),
540+ slog .Int64 ("bytes" , totalFetchedBytes . Load () ),
534541 slog .Int ("channelLen" , records .ChannelLen ()))
535542 return nil
536543 } else {
537544 logger .Info ("commit lock, waiting for commit to return records" ,
538545 slog .Int ("records" , cdcRecordsStorage .Len ()),
539- slog .Int ("bytes" , fetchedBytes ),
546+ slog .Int64 ("bytes" , totalFetchedBytes . Load () ),
540547 slog .Int ("channelLen" , records .ChannelLen ()))
541548 waitingForCommit = true
542549 }
@@ -568,7 +575,7 @@ func PullCdcRecords[Items model.Items](
568575 if pgconn .Timeout (err ) {
569576 logger .Info ("Stand-by deadline reached, returning currently accumulated records" ,
570577 slog .Int ("records" , cdcRecordsStorage .Len ()),
571- slog .Int ("bytes" , fetchedBytes ),
578+ slog .Int64 ("bytes" , totalFetchedBytes . Load () ),
572579 slog .Int ("channelLen" , records .ChannelLen ()))
573580 return nil
574581 } else {
@@ -580,7 +587,7 @@ func PullCdcRecords[Items model.Items](
580587 case * pgproto3.ErrorResponse :
581588 return shared .LogError (logger , exceptions .NewPostgresWalError (errors .New ("received error response" ), msg ))
582589 case * pgproto3.CopyData :
583- p . otelManager . Metrics . AllFetchedBytesCounter . Add (ctx , int64 (len (msg .Data )))
590+ allFetchedBytes . Add (int64 (len (msg .Data )))
584591 switch msg .Data [0 ] {
585592 case pglogrepl .PrimaryKeepaliveMessageByteID :
586593 pkm , err := pglogrepl .ParsePrimaryKeepaliveMessage (msg .Data [1 :])
@@ -615,8 +622,8 @@ func PullCdcRecords[Items model.Items](
615622 }
616623
617624 if rec != nil {
618- p . otelManager . Metrics . FetchedBytesCounter . Add (ctx , int64 (len (msg .Data )))
619- fetchedBytes += len (msg .Data )
625+ fetchedBytes . Add (int64 (len (msg .Data )))
626+ totalFetchedBytes . Add ( int64 ( len (msg .Data )) )
620627 tableName := rec .GetDestinationTableName ()
621628 switch r := rec .(type ) {
622629 case * model.UpdateRecord [Items ]:
0 commit comments