@@ -8,7 +8,13 @@ import {
88 ReplicationAbortedError ,
99 ReplicationAssertionError
1010} from '@powersync/lib-services-framework' ;
11- import { getUuidReplicaIdentityBson , Metrics , SourceEntityDescriptor , storage } from '@powersync/service-core' ;
11+ import {
12+ getUuidReplicaIdentityBson ,
13+ MetricsEngine ,
14+ ReplicationMetricType ,
15+ SourceEntityDescriptor ,
16+ storage
17+ } from '@powersync/service-core' ;
1218import * as pgwire from '@powersync/service-jpgwire' ;
1319import { DatabaseInputRow , SqliteRow , SqlSyncRules , TablePattern , toSyncRulesRow } from '@powersync/service-sync-rules' ;
1420import * as pg_utils from '../utils/pgwire_utils.js' ;
@@ -20,6 +26,7 @@ import { checkSourceConfiguration, getReplicationIdentityColumns } from './repli
2026export interface WalStreamOptions {
2127 connections : PgManager ;
2228 storage : storage . SyncRulesBucketStorage ;
29+ metrics : MetricsEngine ;
2330 abort_signal : AbortSignal ;
2431}
2532
@@ -72,7 +79,7 @@ export class WalStream {
7279 connection_id = 1 ;
7380
7481 private readonly storage : storage . SyncRulesBucketStorage ;
75-
82+ private readonly metrics : MetricsEngine ;
7683 private readonly slot_name : string ;
7784
7885 private connections : PgManager ;
@@ -85,6 +92,7 @@ export class WalStream {
8592
8693 constructor ( options : WalStreamOptions ) {
8794 this . storage = options . storage ;
95+ this . metrics = options . metrics ;
8896 this . sync_rules = options . storage . getParsedSyncRules ( { defaultSchema : POSTGRES_DEFAULT_SCHEMA } ) ;
8997 this . group_id = options . storage . group_id ;
9098 this . slot_name = options . storage . slot_name ;
@@ -471,7 +479,7 @@ WHERE oid = $1::regclass`,
471479 }
472480
473481 at += rows . length ;
474- Metrics . getInstance ( ) . rows_replicated_total . add ( rows . length ) ;
482+ this . metrics . getCounter ( ReplicationMetricType . ROWS_REPLICATED_TOTAL ) . add ( rows . length ) ;
475483
476484 await touch ( ) ;
477485 }
@@ -562,7 +570,7 @@ WHERE oid = $1::regclass`,
562570 }
563571
564572 if ( msg . tag == 'insert' ) {
565- Metrics . getInstance ( ) . rows_replicated_total . add ( 1 ) ;
573+ this . metrics . getCounter ( ReplicationMetricType . ROWS_REPLICATED_TOTAL ) . add ( 1 ) ;
566574 const baseRecord = pg_utils . constructAfterRecord ( msg ) ;
567575 return await batch . save ( {
568576 tag : storage . SaveOperationTag . INSERT ,
@@ -573,7 +581,7 @@ WHERE oid = $1::regclass`,
573581 afterReplicaId : getUuidReplicaIdentityBson ( baseRecord , table . replicaIdColumns )
574582 } ) ;
575583 } else if ( msg . tag == 'update' ) {
576- Metrics . getInstance ( ) . rows_replicated_total . add ( 1 ) ;
584+ this . metrics . getCounter ( ReplicationMetricType . ROWS_REPLICATED_TOTAL ) . add ( 1 ) ;
577585 // "before" may be null if the replica id columns are unchanged
578586 // It's fine to treat that the same as an insert.
579587 const before = pg_utils . constructBeforeRecord ( msg ) ;
@@ -587,7 +595,7 @@ WHERE oid = $1::regclass`,
587595 afterReplicaId : getUuidReplicaIdentityBson ( after , table . replicaIdColumns )
588596 } ) ;
589597 } else if ( msg . tag == 'delete' ) {
590- Metrics . getInstance ( ) . rows_replicated_total . add ( 1 ) ;
598+ this . metrics . getCounter ( ReplicationMetricType . ROWS_REPLICATED_TOTAL ) . add ( 1 ) ;
591599 const before = pg_utils . constructBeforeRecord ( msg ) ! ;
592600
593601 return await batch . save ( {
@@ -697,7 +705,7 @@ WHERE oid = $1::regclass`,
697705 } else if ( msg . tag == 'begin' ) {
698706 inTx = true ;
699707 } else if ( msg . tag == 'commit' ) {
700- Metrics . getInstance ( ) . transactions_replicated_total . add ( 1 ) ;
708+ this . metrics . getCounter ( ReplicationMetricType . TRANSACTIONS_REPLICATED_TOTAL ) . add ( 1 ) ;
701709 inTx = false ;
702710 await batch . commit ( msg . lsn ! , { createEmptyCheckpoints } ) ;
703711 await this . ack ( msg . lsn ! , replicationStream ) ;
@@ -739,7 +747,7 @@ WHERE oid = $1::regclass`,
739747 await this . ack ( chunkLastLsn , replicationStream ) ;
740748 }
741749
742- Metrics . getInstance ( ) . chunks_replicated_total . add ( 1 ) ;
750+ this . metrics . getCounter ( ReplicationMetricType . CHUNKS_REPLICATED_TOTAL ) . add ( 1 ) ;
743751 }
744752 }
745753 ) ;
0 commit comments