Skip to content

Commit

Permalink
fix(threshold): enable default threshold customization via envars
Browse files Browse the repository at this point in the history
  • Loading branch information
uladkasach committed Sep 1, 2024
1 parent 4c01eae commit 0b3607e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
45 changes: 45 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
},
"dependencies": {
"@ehmpathy/error-fns": "1.0.2",
"@ehmpathy/uni-time": "1.5.0",
"type-fns": "1.17.0"
},
"devDependencies": {
Expand Down
3 changes: 0 additions & 3 deletions src/logic/asDomainProcedure.ts

This file was deleted.

19 changes: 17 additions & 2 deletions src/logic/withLogTrail.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { UnexpectedCodePathError } from '@ehmpathy/error-fns';
import { toMilliseconds, UniDuration } from '@ehmpathy/uni-time';
import type {
LogLevel,
LogMethod,
Expand All @@ -20,6 +21,11 @@ const pickErrorMessage = (input: Error) => ({
});
const roundToHundredths = (num: number) => Math.round(num * 100) / 100; // https://stackoverflow.com/a/14968691/3068233

const DEFAULT_DURATION_REPORT_THRESHOLD = process.env
.VISUALOGIC_DURATION_THRESHOLD
? parseInt(process.env.VISUALOGIC_DURATION_THRESHOLD)
: toMilliseconds({ seconds: 1 });

/**
* enables input output logging and tracing for a method
*
Expand All @@ -35,8 +41,10 @@ export const withLogTrail = <
logic: (input: TInput, context: TContext) => TOutput,
{
name: declaredName,
durationReportingThresholdInSeconds = 1,
log: logOptions,
duration = {
threshold: { milliseconds: DEFAULT_DURATION_REPORT_THRESHOLD },
},
}: {
/**
* specifies the name of the function, if the function does not have a name assigned already
Expand Down Expand Up @@ -74,7 +82,9 @@ export const withLogTrail = <
/**
* specifies the threshold after which a duration will be included on the output log
*/
durationReportingThresholdInSeconds?: number;
duration?: {
threshold: UniDuration;
};
},
): typeof logic => {
// cache the name of the function per wrapping
Expand All @@ -99,6 +109,11 @@ export const withLogTrail = <
const logOutputMethod = logOptions?.output ?? noOp;
const logErrorMethod = logOptions?.error ?? pickErrorMessage;

// define the duration threshold
const durationReportingThreshold = duration.threshold;
const durationReportingThresholdInSeconds =
toMilliseconds(durationReportingThreshold) / 1000;

// wrap the function
return (
input: ProcedureInput<typeof logic>,
Expand Down

0 comments on commit 0b3607e

Please sign in to comment.