-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add logs to track data store execution time
- Loading branch information
1 parent
cd7fdfa
commit 584c99c
Showing
5 changed files
with
201 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { LoggerService } from '../module/logger/service/logger.service' | ||
|
||
export const startExecutionTimer = (logger: LoggerService, id: string, startContext?: Record<string, unknown>) => { | ||
const startTime = new Date().getTime() | ||
|
||
logger.log(`Execution time of ${id} - Start`, { | ||
...(startContext ? { ...startContext } : {}), | ||
startTime | ||
}) | ||
|
||
return { | ||
stop: (stopContext?: Record<string, unknown>) => { | ||
const stopTime = new Date().getTime() | ||
const duration = stopTime - startTime | ||
|
||
logger.log(`Execution time of ${id} - Stop`, { | ||
...(startContext ? { ...startContext } : {}), | ||
...(stopContext ? { ...stopContext } : {}), | ||
startTime, | ||
stopTime, | ||
duration | ||
}) | ||
} | ||
} | ||
} | ||
|
||
export const withExecutionTime = <T>({ | ||
logger, | ||
id, | ||
startContext, | ||
thunk | ||
}: { | ||
logger: LoggerService | ||
id: string | ||
thunk: () => T | Promise<T> | ||
startContext?: Record<string, unknown> | ||
}): T | Promise<T> => { | ||
const timer = startExecutionTimer(logger, id, startContext) | ||
const result = thunk() | ||
|
||
if (result instanceof Promise) { | ||
return result.then( | ||
(value) => { | ||
timer.stop() | ||
|
||
return value | ||
}, | ||
(error) => { | ||
timer.stop() | ||
throw error | ||
} | ||
) | ||
} | ||
|
||
timer.stop() | ||
|
||
return result | ||
} |