Skip to content

Commit

Permalink
feat(breach): log breaches before termination for observability
Browse files Browse the repository at this point in the history
  • Loading branch information
uladkasach committed May 28, 2024
1 parent 497e712 commit 2210ac9
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/logic/withLogTrail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,28 @@ export const withLogTrail = <T extends (...args: any[]) => 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<ReturnType<T>>) => {
const endTimeInMilliseconds = new Date().getTime();
Expand Down

0 comments on commit 2210ac9

Please sign in to comment.