Jelajahi Sumber

rate limit on logging v1.5.6

Brian Gough 9 tahun lalu
induk
melakukan
013c2a693c
2 mengubah file dengan 16 tambahan dan 5 penghapusan
  1. 15 4
      libraries/logger/logging-manager.coffee
  2. 1 1
      libraries/logger/package.json

+ 15 - 4
libraries/logger/logging-manager.coffee

@@ -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
libraries/logger/package.json

@@ -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",