Skip to content

Commit f4ca6cb

Browse files
committed
More tracing for replication-related functions
1 parent 665870a commit f4ca6cb

2 files changed

Lines changed: 39 additions & 12 deletions

File tree

apps/backend/src/prisma-client.tsx

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import { isPromise } from "util/types";
1919
import { runMigrationNeeded } from "./auto-migrations";
2020
import { registerPgPool } from "./lib/dev-perf-stats";
2121
import { Tenancy } from "./lib/tenancies";
22-
import { drainInFlightPromises } from "./utils/background-tasks";
2322
import { 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
2626
ensurePolyfilled();
@@ -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}`);

packages/stack-shared/src/utils/promises.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { KnownError } from "..";
22
import { StackAssertionError, captureError, concatStacktraces, errorToNiceString } from "./errors";
33
import { DependenciesMap } from "./maps";
44
import { Result } from "./results";
5+
import { traceSpan } from "./telemetry";
56
import { generateUuid } from "./uuids";
67

78
export type ReactPromise<T> = Promise<T> & (
@@ -264,7 +265,9 @@ export async function wait(ms: number) {
264265
if (ms >= 2**31) {
265266
throw new StackAssertionError("The maximum timeout for wait() is 2147483647ms (2**31 - 1). (found: ${ms}ms)");
266267
}
267-
return await new Promise<void>(resolve => setTimeout(resolve, ms));
268+
return await traceSpan({ description: 'wait(...)', attributes: { 'stack.wait.ms': ms } }, async (span) => {
269+
return await new Promise<void>(resolve => setTimeout(resolve, ms));
270+
});
268271
}
269272
import.meta.vitest?.test("wait", async ({ expect }) => {
270273
// Test with valid input

0 commit comments

Comments
 (0)