2020-06-23 13:29:44 -04:00
|
|
|
let EventLogger
|
2021-12-14 08:00:35 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2020-11-25 06:57:22 -05:00
|
|
|
const metrics = require('@overleaf/metrics')
|
2021-07-12 12:47:18 -04:00
|
|
|
const settings = require('@overleaf/settings')
|
2020-06-23 13:29:34 -04:00
|
|
|
|
|
|
|
// keep track of message counters to detect duplicate and out of order events
|
|
|
|
// messsage ids have the format "UNIQUEHOSTKEY-COUNTER"
|
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
const EVENT_LOG_COUNTER = {}
|
|
|
|
const EVENT_LOG_TIMESTAMP = {}
|
|
|
|
let EVENT_LAST_CLEAN_TIMESTAMP = 0
|
2020-06-23 13:29:34 -04:00
|
|
|
|
|
|
|
// counter for debug logs
|
2020-06-23 13:29:44 -04:00
|
|
|
let COUNTER = 0
|
2020-06-23 13:29:34 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
module.exports = EventLogger = {
|
|
|
|
MAX_STALE_TIME_IN_MS: 3600 * 1000,
|
2020-06-23 13:29:34 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
debugEvent(channel, message) {
|
|
|
|
if (settings.debugEvents > 0) {
|
2021-09-14 04:36:24 -04:00
|
|
|
logger.info({ channel, message, counter: COUNTER++ }, 'logging event')
|
2020-07-07 06:06:02 -04:00
|
|
|
settings.debugEvents--
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
|
|
|
},
|
2020-06-23 13:29:34 -04:00
|
|
|
|
2023-03-20 10:10:40 -04:00
|
|
|
checkEventOrder(channel, messageId) {
|
|
|
|
if (typeof messageId !== 'string') {
|
2020-06-23 13:29:44 -04:00
|
|
|
return
|
|
|
|
}
|
2020-07-07 06:06:02 -04:00
|
|
|
let result
|
2023-03-20 10:10:40 -04:00
|
|
|
if (!(result = messageId.match(/^(.*)-(\d+)$/))) {
|
2020-06-23 13:29:44 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
const key = result[1]
|
|
|
|
const count = parseInt(result[2], 0)
|
|
|
|
if (!(count >= 0)) {
|
|
|
|
// ignore checks if counter is not present
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// store the last count in a hash for each host
|
|
|
|
const previous = EventLogger._storeEventCount(key, count)
|
2020-07-07 06:06:02 -04:00
|
|
|
if (!previous || count === previous + 1) {
|
2021-07-13 07:40:46 -04:00
|
|
|
metrics.inc(`event.${channel}.valid`)
|
2020-06-23 13:29:44 -04:00
|
|
|
return // order is ok
|
|
|
|
}
|
|
|
|
if (count === previous) {
|
|
|
|
metrics.inc(`event.${channel}.duplicate`)
|
2023-03-20 10:10:40 -04:00
|
|
|
logger.warn({ channel, messageId }, 'duplicate event')
|
2020-06-23 13:29:44 -04:00
|
|
|
return 'duplicate'
|
|
|
|
} else {
|
|
|
|
metrics.inc(`event.${channel}.out-of-order`)
|
|
|
|
logger.warn(
|
2023-03-20 10:10:40 -04:00
|
|
|
{ channel, messageId, key, previous, count },
|
2020-06-23 13:29:44 -04:00
|
|
|
'out of order event'
|
|
|
|
)
|
|
|
|
return 'out-of-order'
|
|
|
|
}
|
|
|
|
},
|
2020-06-23 13:29:34 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
_storeEventCount(key, count) {
|
|
|
|
const previous = EVENT_LOG_COUNTER[key]
|
|
|
|
const now = Date.now()
|
|
|
|
EVENT_LOG_COUNTER[key] = count
|
|
|
|
EVENT_LOG_TIMESTAMP[key] = now
|
|
|
|
// periodically remove old counts
|
|
|
|
if (now - EVENT_LAST_CLEAN_TIMESTAMP > EventLogger.MAX_STALE_TIME_IN_MS) {
|
|
|
|
EventLogger._cleanEventStream(now)
|
|
|
|
EVENT_LAST_CLEAN_TIMESTAMP = now
|
|
|
|
}
|
|
|
|
return previous
|
|
|
|
},
|
2020-06-23 13:29:34 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
_cleanEventStream(now) {
|
2020-07-07 06:06:02 -04:00
|
|
|
Object.entries(EVENT_LOG_TIMESTAMP).forEach(([key, timestamp]) => {
|
|
|
|
if (now - timestamp > EventLogger.MAX_STALE_TIME_IN_MS) {
|
|
|
|
delete EVENT_LOG_COUNTER[key]
|
|
|
|
delete EVENT_LOG_TIMESTAMP[key]
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
2020-07-07 06:06:02 -04:00
|
|
|
})
|
2021-07-13 07:04:45 -04:00
|
|
|
},
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|