Created
July 23, 2023 20:59
-
-
Save stfsy/4c3b8713a7d9f2645404778ad2c6d72a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { getTraceIdFromLocalStorage } from "./local-storage.js" | |
export default async (name, callback) => { | |
const now = Date.now() | |
const result = callback.apply() | |
if (result && typeof result.finally === 'function') { | |
return result.finally(logExecutionTime(now, name)) | |
} else { | |
logExecutionTime(now, name) | |
return result | |
} | |
} | |
function logExecutionTime(now, name) { | |
return () => { | |
const then = Date.now() | |
const duration = then - now | |
const message = `Finished ${name} execution in ${duration}ms with id ${getTraceIdFromLocalStorage()}` | |
if (duration > 1_000) { | |
console.warn(message) | |
} else if (duration > 5_000) { | |
console.error(message) | |
} else { | |
console.info(message) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment