FEATURE: group warnings about IP level rate limiting

This commit is contained in:
Sam 2018-08-13 13:14:34 +10:00
parent d10c9d7d75
commit 168ffd8384
3 changed files with 38 additions and 3 deletions

View File

@ -184,7 +184,7 @@ GEM
logstash-event (1.2.02) logstash-event (1.2.02)
logstash-logger (0.26.1) logstash-logger (0.26.1)
logstash-event (~> 1.2) logstash-event (~> 1.2)
logster (1.2.9) logster (1.2.11)
loofah (2.2.2) loofah (2.2.2)
crass (~> 1.0.2) crass (~> 1.0.2)
nokogiri (>= 1.5.9) nokogiri (>= 1.5.9)

View File

@ -489,6 +489,41 @@ module Discourse
nil nil
end end
# you can use Discourse.warn when you want to report custom environment
# with the error, this helps with grouping
def self.warn(message, env = nil)
append = env ? (+" ") << env.map { |k, v|"#{k}: #{v}" }.join(" ") : ""
if !(Logster::Logger === Rails.logger)
Rails.logger.warn("#{message}#{append}")
return
end
loggers = [Rails.logger]
if Rails.logger.chained
loggers.concat(Rails.logger.chained)
end
if old_env = Thread.current[Logster::Logger::LOGSTER_ENV]
env = env.merge(old_env)
end
loggers.each do |logger|
if !(Logster::Logger === logger)
logger.warn("#{message} #{append}")
next
end
logger.store.report(
::Logger::Severity::WARN,
"discourse",
message,
env: env
)
end
end
# report a warning maintaining backtrack for logster # report a warning maintaining backtrack for logster
def self.warn_exception(e, message: "", env: nil) def self.warn_exception(e, message: "", env: nil)
if Rails.logger.respond_to? :add_with_opts if Rails.logger.respond_to? :add_with_opts

View File

@ -258,7 +258,7 @@ class Middleware::RequestTracker
if !limiter_assets10.can_perform? if !limiter_assets10.can_perform?
if warn if warn
Rails.logger.warn("Global asset IP rate limit exceeded for #{ip}: 10 second rate limit, uri: #{request.env["REQUEST_URI"]}") Discourse.warn("Global asset IP rate limit exceeded for #{ip}: 10 second rate limit", uri: request.env["REQUEST_URI"])
end end
return !(GlobalSetting.max_reqs_per_ip_mode == "warn") return !(GlobalSetting.max_reqs_per_ip_mode == "warn")
@ -272,7 +272,7 @@ class Middleware::RequestTracker
false false
rescue RateLimiter::LimitExceeded rescue RateLimiter::LimitExceeded
if warn if warn
Rails.logger.warn("Global IP rate limit exceeded for #{ip}: #{type} second rate limit, uri: #{request.env["REQUEST_URI"]}") Discourse.warn("Global IP rate limit exceeded for #{ip}: #{type} second rate limit", uri: request.env["REQUEST_URI"])
!(GlobalSetting.max_reqs_per_ip_mode == "warn") !(GlobalSetting.max_reqs_per_ip_mode == "warn")
else else
true true