mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-07-08 02:11:35 +00:00
fix: wire browse request timing hooks
This commit is contained in:
parent
c55361c552
commit
da490605b7
3 changed files with 146 additions and 13 deletions
|
|
@ -4,15 +4,40 @@ const Logger = require('../Logger')
|
|||
|
||||
const histograms = new Map()
|
||||
|
||||
function createRequestScopedFindOptions(findOptions, funcName) {
|
||||
if (!findOptions || typeof findOptions !== 'object' || Array.isArray(findOptions)) {
|
||||
return findOptions
|
||||
function callTimingHook(requestTiming, hookName, ...args) {
|
||||
if (!requestTiming || typeof requestTiming[hookName] !== 'function') return
|
||||
requestTiming[hookName](...args)
|
||||
}
|
||||
|
||||
function createRequestScopedLogging(funcName, logging, requestTiming) {
|
||||
const logger = (...args) => {
|
||||
Logger.info(`[${funcName}] ${args[0]} Elapsed time: ${args[1]}ms`)
|
||||
if (typeof logging === 'function') {
|
||||
logging(...args)
|
||||
}
|
||||
callTimingHook(requestTiming, 'onQuery', ...args)
|
||||
}
|
||||
|
||||
return logger
|
||||
}
|
||||
|
||||
function createRequestScopedFindOptions(findOptions, funcName) {
|
||||
if (!findOptions || typeof findOptions !== 'object' || Array.isArray(findOptions)) {
|
||||
return {
|
||||
findOptions,
|
||||
requestTiming: null
|
||||
}
|
||||
}
|
||||
|
||||
const requestTiming = findOptions.requestTiming || null
|
||||
|
||||
return {
|
||||
...findOptions,
|
||||
logging: (query, time) => Logger.info(`[${funcName}] ${query} Elapsed time: ${time}ms`),
|
||||
benchmark: true
|
||||
requestTiming,
|
||||
findOptions: {
|
||||
...findOptions,
|
||||
logging: createRequestScopedLogging(funcName, findOptions.logging, requestTiming),
|
||||
benchmark: true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -25,22 +50,26 @@ function profile(asyncFunc, isFindQuery = true, funcName = asyncFunc.name) {
|
|||
const histogram = histograms.get(funcName)
|
||||
|
||||
return async (...args) => {
|
||||
const requestArgs = isFindQuery ? [createRequestScopedFindOptions(args[0], funcName), ...args.slice(1)] : args
|
||||
const requestScoped = isFindQuery ? createRequestScopedFindOptions(args[0], funcName) : { findOptions: args[0], requestTiming: null }
|
||||
const requestArgs = isFindQuery ? [requestScoped.findOptions, ...args.slice(1)] : args
|
||||
|
||||
if (isFindQuery) {
|
||||
const findOptions = requestArgs[0]
|
||||
Logger.info(`[${funcName}] findOptions:`, util.inspect(findOptions, { depth: null }))
|
||||
}
|
||||
const start = performance.now()
|
||||
callTimingHook(requestScoped.requestTiming, 'onStart', requestArgs[0])
|
||||
try {
|
||||
const result = await asyncFunc(...requestArgs)
|
||||
callTimingHook(requestScoped.requestTiming, 'onFinish', result)
|
||||
return result
|
||||
} catch (error) {
|
||||
Logger.error(`[${funcName}] failed`)
|
||||
callTimingHook(requestScoped.requestTiming, 'onError', error)
|
||||
throw error
|
||||
} finally {
|
||||
const end = performance.now()
|
||||
const duration = Math.round(end - start)
|
||||
const duration = Math.max(1, Math.round(end - start))
|
||||
histogram.record(duration)
|
||||
histogram.values.push(duration)
|
||||
Logger.info(`[${funcName}] duration: ${duration}ms`)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue