feat(devops): separate public page server timing diagnostics
This commit is contained in:
1 parent
d5091d1a73
commit
baeb54aeb5
15 files changed
+315
-29
No files matched your search
@@ -7,17 +7,28 @@ export type PerformanceMetrics = Record<Dependency, Measurement> & {
|
||||
};
|
||||
const context = new AsyncLocalStorage<PerformanceMetrics>();
|
||||
|
||||
export async function collectPerformance<T>(work: () => Promise<T>) {
|
||||
export async function collectPerformance<T>(
|
||||
work: () => Promise<T>,
|
||||
onComplete?: (metrics: PerformanceMetrics) => void,
|
||||
) {
|
||||
const metrics: PerformanceMetrics = {
|
||||
durationMs: 0,
|
||||
database: { count: 0, durationMs: 0, errors: 0 },
|
||||
external: { count: 0, durationMs: 0, errors: 0 },
|
||||
};
|
||||
const start = performance.now();
|
||||
const value = await context.run(metrics, work);
|
||||
metrics.durationMs = performance.now() - start;
|
||||
// Background work may keep the async context: return a detached snapshot.
|
||||
return { value, metrics: structuredClone(metrics) };
|
||||
try {
|
||||
const value = await context.run(metrics, work);
|
||||
metrics.durationMs = performance.now() - start;
|
||||
return { value, metrics: structuredClone(metrics) };
|
||||
} finally {
|
||||
metrics.durationMs = performance.now() - start;
|
||||
try {
|
||||
onComplete?.(structuredClone(metrics));
|
||||
} catch {
|
||||
/* Diagnostics must not change application outcomes. */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function measureDependency<T>(
|
||||
|
||||
Reference in new issue
Block a user