Skip to content

Instantly share code, notes, and snippets.

@stfsy
Created July 23, 2023 20:59
Show Gist options
  • Save stfsy/4c3b8713a7d9f2645404778ad2c6d72a to your computer and use it in GitHub Desktop.
Save stfsy/4c3b8713a7d9f2645404778ad2c6d72a to your computer and use it in GitHub Desktop.
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