@@ -19,8 +19,8 @@ import { isPromise } from "util/types";
1919import { runMigrationNeeded } from "./auto-migrations" ;
2020import { registerPgPool } from "./lib/dev-perf-stats" ;
2121import { Tenancy } from "./lib/tenancies" ;
22- import { drainInFlightPromises } from "./utils/background-tasks" ;
2322import { ensurePolyfilled } from "./polyfills" ;
23+ import { drainInFlightPromises } from "./utils/background-tasks" ;
2424
2525// just ensure we're polyfilled because this file relies on envvars being expanded
2626ensurePolyfilled ( ) ;
@@ -215,23 +215,47 @@ async function waitForReplication(replicas: PrismaClient[], target: string, time
215215 throw new StackAssertionError ( `Invalid pg_lsn format: ${ target } ` ) ;
216216 }
217217 checkCaughtUp = async ( replica ) => {
218- const [ { caught_up } ] = await ( replica as any ) . $queryRaw < [ { caught_up : boolean } ] > `
219- SELECT pg_last_wal_replay_lsn() >= ${ target } ::pg_lsn AS caught_up
220- ` ;
221- return caught_up ;
218+ return await traceSpan ( {
219+ description : 'checking replication status from replica' ,
220+ attributes : {
221+ 'stack.db-replication.strategy' : strategy ,
222+ 'stack.db-replication.target' : target ,
223+ 'stack.db-replication.replica-count' : replicas . length ,
224+ 'stack.db-replication.timeout-ms' : timeoutMs ,
225+ } ,
226+ } , async ( span ) => {
227+ const [ { caught_up } ] = await ( replica as any ) . $queryRaw < [ { caught_up : boolean } ] > `
228+ SELECT pg_last_wal_replay_lsn() >= ${ target } ::pg_lsn AS caught_up
229+ ` ;
230+ span . setAttribute ( 'stack.db-replication.caught-up' , caught_up ) ;
231+ return caught_up ;
232+ } ) ;
222233 } ;
223234 } else if ( strategy === "aurora" ) {
224235 if ( ! / ^ \d + $ / . test ( target ) ) {
225236 throw new StackAssertionError ( `Invalid bigint format for Aurora durable_lsn: ${ target } ` ) ;
226237 }
227238 const targetBigInt = BigInt ( target ) ;
228239 checkCaughtUp = async ( replica ) => {
229- const [ { current_lsn } ] = await ( replica as any ) . $queryRaw < [ { current_lsn : bigint | null } ] > `
230- SELECT current_read_lsn AS current_lsn
231- FROM aurora_replica_status()
232- WHERE server_id = aurora_db_instance_identifier()
233- ` ;
234- return current_lsn === null || current_lsn >= targetBigInt ;
240+ return await traceSpan ( {
241+ description : 'checking replication status from replica' ,
242+ attributes : {
243+ 'stack.db-replication.strategy' : strategy ,
244+ 'stack.db-replication.target' : target ,
245+ 'stack.db-replication.replica-count' : replicas . length ,
246+ 'stack.db-replication.timeout-ms' : timeoutMs ,
247+ } ,
248+ } , async ( span ) => {
249+ const replicaStatus = await ( replica as any ) . $queryRaw < [ { current_lsn : bigint | null } ] > `
250+ SELECT *
251+ FROM aurora_replica_status()
252+ WHERE server_id = aurora_db_instance_identifier()
253+ ` ;
254+ span . setAttribute ( 'stack.db-replication.replica-status' , JSON . stringify ( replicaStatus ) ) ;
255+ const currentLsn = replicaStatus [ 0 ] . current_read_lsn ;
256+ span . setAttribute ( 'stack.db-replication.current-lsn' , currentLsn . toString ( ) ) ;
257+ return currentLsn === null || currentLsn >= targetBigInt ;
258+ } ) ;
235259 } ;
236260 } else {
237261 throw new StackAssertionError ( `Unknown replication wait strategy: ${ strategy } ` ) ;
0 commit comments