rate limit on logging v1.5.6

This commit is contained in:
Brian Gough
2017-03-23 13:54:44 +00:00
parent c930f6c680
commit 013c2a693c
2 changed files with 16 additions and 5 deletions
+15 -4
View File
@@ -11,6 +11,7 @@ module.exports = Logger =
raven = require "raven"
@raven = new raven.Client(sentry_dsn, options)
@lastErrorTimeStamp = 0 # for rate limiting on sentry reporting
@lastErrorCount = 0
captureException: (attributes, message, level) ->
# handle case of logger.error "message"
@@ -59,12 +60,22 @@ module.exports = Logger =
error: (attributes, message, args...)->
@logger.error(attributes, message, args...)
if @raven?
MAX_ERRORS = 5 # maximum number of errors in 1 minute
now = new Date()
rateLimited = (now - @lastErrorTimeStamp) < 30 * 1000
# only report one error every thirty seconds to avoid overload
if not rateLimited
@captureException(attributes, message, "error")
# have we recently reported an error?
recentSentryReport = (now - @lastErrorTimeStamp) < 60 * 1000
# if so, increment the error count
if recentSentryReport
@lastErrorCount++
else
@lastErrorCount = 0
@lastErrorTimeStamp = now
# only report 5 errors every minute to avoid overload
if @lastErrorCount <= MAX_ERRORS
# add a note if the rate limit has been hit
note = if @lastErrorCount is MAX_ERRORS then "(rate limited)" else ""
# report the exception
@captureException(attributes, message, "error#{note}")
err: () ->
@error.apply(this, arguments)
warn: ()->
+1 -1
View File
@@ -6,7 +6,7 @@
"type": "git",
"url": "http://github.com/sharelatex/logger-sharelatex.git"
},
"version": "1.5.5",
"version": "1.5.6",
"dependencies": {
"bunyan": "1.5.1",
"coffee-script": "1.12.4",