From 2210ac93c86f45b5f8a3cecf1530b3e3f6c99978 Mon Sep 17 00:00:00 2001 From: Ulad Kasach Date: Tue, 28 May 2024 06:35:40 -0400 Subject: [PATCH] feat(breach): log breaches before termination for observability --- src/logic/withLogTrail.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/logic/withLogTrail.ts b/src/logic/withLogTrail.ts index a3e0301..42f5f1d 100644 --- a/src/logic/withLogTrail.ts +++ b/src/logic/withLogTrail.ts @@ -88,6 +88,28 @@ export const withLogTrail = any>( // now execute the method const result = logic(...input); + // if the result was a promise, log when that method crosses the reporting threshold, to identify which procedures are slow + if (isAPromise(result)) { + // define how to log the breach, on breach + const onDurationBreach = () => + logMethod( + `${name}.duration.breach: procedure has taken longer than duration report threshold`, + { + input: logInputMethod(...input), + already: { duration: `${durationReportingThresholdInSeconds} sec` }, + }, + ); + + // define a timeout which will trigger on duration threshold + const onBreachTrigger = setTimeout( + onDurationBreach, + durationReportingThresholdInSeconds, + ); + + // remove the timeout when the operation completes, to prevent logging if completes before duration + result.finally(() => clearTimeout(onBreachTrigger)); + } + // define what to do when we have output const logOutput = (output: Awaited>) => { const endTimeInMilliseconds = new Date().getTime();