EventLogger.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* eslint-disable
  2. camelcase,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS102: Remove unnecessary code created because of implicit returns
  9. * DS205: Consider reworking code to avoid use of IIFEs
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. let EventLogger
  14. const logger = require('logger-sharelatex')
  15. const metrics = require('metrics-sharelatex')
  16. const settings = require('settings-sharelatex')
  17. // keep track of message counters to detect duplicate and out of order events
  18. // messsage ids have the format "UNIQUEHOSTKEY-COUNTER"
  19. const EVENT_LOG_COUNTER = {}
  20. const EVENT_LOG_TIMESTAMP = {}
  21. let EVENT_LAST_CLEAN_TIMESTAMP = 0
  22. // counter for debug logs
  23. let COUNTER = 0
  24. module.exports = EventLogger = {
  25. MAX_STALE_TIME_IN_MS: 3600 * 1000,
  26. debugEvent(channel, message) {
  27. if (settings.debugEvents > 0) {
  28. logger.log({ channel, message, counter: COUNTER++ }, 'logging event')
  29. return settings.debugEvents--
  30. }
  31. },
  32. checkEventOrder(channel, message_id, message) {
  33. let result
  34. if (typeof message_id !== 'string') {
  35. return
  36. }
  37. if (!(result = message_id.match(/^(.*)-(\d+)$/))) {
  38. return
  39. }
  40. const key = result[1]
  41. const count = parseInt(result[2], 0)
  42. if (!(count >= 0)) {
  43. // ignore checks if counter is not present
  44. return
  45. }
  46. // store the last count in a hash for each host
  47. const previous = EventLogger._storeEventCount(key, count)
  48. if (previous == null || count === previous + 1) {
  49. metrics.inc(`event.${channel}.valid`, 0.001) // downsample high rate docupdater events
  50. return // order is ok
  51. }
  52. if (count === previous) {
  53. metrics.inc(`event.${channel}.duplicate`)
  54. logger.warn({ channel, message_id }, 'duplicate event')
  55. return 'duplicate'
  56. } else {
  57. metrics.inc(`event.${channel}.out-of-order`)
  58. logger.warn(
  59. { channel, message_id, key, previous, count },
  60. 'out of order event'
  61. )
  62. return 'out-of-order'
  63. }
  64. },
  65. _storeEventCount(key, count) {
  66. const previous = EVENT_LOG_COUNTER[key]
  67. const now = Date.now()
  68. EVENT_LOG_COUNTER[key] = count
  69. EVENT_LOG_TIMESTAMP[key] = now
  70. // periodically remove old counts
  71. if (now - EVENT_LAST_CLEAN_TIMESTAMP > EventLogger.MAX_STALE_TIME_IN_MS) {
  72. EventLogger._cleanEventStream(now)
  73. EVENT_LAST_CLEAN_TIMESTAMP = now
  74. }
  75. return previous
  76. },
  77. _cleanEventStream(now) {
  78. return (() => {
  79. const result = []
  80. for (const key in EVENT_LOG_TIMESTAMP) {
  81. const timestamp = EVENT_LOG_TIMESTAMP[key]
  82. if (now - timestamp > EventLogger.MAX_STALE_TIME_IN_MS) {
  83. delete EVENT_LOG_COUNTER[key]
  84. result.push(delete EVENT_LOG_TIMESTAMP[key])
  85. } else {
  86. result.push(undefined)
  87. }
  88. }
  89. return result
  90. })()
  91. }
  92. }