2015-02-05 00:08:52 -05:00
|
|
|
require_dependency 'middleware/anonymous_cache'
|
|
|
|
|
|
|
|
class Middleware::RequestTracker
|
|
|
|
|
|
|
|
def initialize(app, settings={})
|
|
|
|
@app = app
|
|
|
|
end
|
|
|
|
|
2015-02-10 01:05:24 -05:00
|
|
|
def self.log_request_on_site(data,host)
|
2015-02-05 00:08:52 -05:00
|
|
|
RailsMultisite::ConnectionManagement.with_hostname(host) do
|
2015-02-10 01:03:33 -05:00
|
|
|
log_request(data)
|
2015-02-05 00:08:52 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-10 01:03:33 -05:00
|
|
|
def self.log_request(data)
|
|
|
|
status = data[:status]
|
|
|
|
track_view = data[:track_view]
|
2015-02-05 00:08:52 -05:00
|
|
|
|
2015-02-10 01:03:33 -05:00
|
|
|
if track_view
|
|
|
|
if data[:is_crawler]
|
2015-02-05 22:39:04 -05:00
|
|
|
ApplicationRequest.increment!(:page_view_crawler)
|
2015-02-10 01:03:33 -05:00
|
|
|
elsif data[:has_auth_cookie]
|
2015-02-05 22:39:04 -05:00
|
|
|
ApplicationRequest.increment!(:page_view_logged_in)
|
2015-02-05 00:08:52 -05:00
|
|
|
else
|
2015-02-05 22:39:04 -05:00
|
|
|
ApplicationRequest.increment!(:page_view_anon)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
ApplicationRequest.increment!(:http_total)
|
|
|
|
|
|
|
|
if status >= 500
|
|
|
|
ApplicationRequest.increment!(:http_5xx)
|
|
|
|
elsif status >= 400
|
|
|
|
ApplicationRequest.increment!(:http_4xx)
|
|
|
|
elsif status >= 300
|
|
|
|
ApplicationRequest.increment!(:http_3xx)
|
|
|
|
else
|
2015-02-10 01:03:33 -05:00
|
|
|
if data[:is_background]
|
2015-02-05 22:39:04 -05:00
|
|
|
ApplicationRequest.increment!(:http_background)
|
|
|
|
elsif status >= 200 && status < 300
|
|
|
|
ApplicationRequest.increment!(:http_2xx)
|
2015-02-05 00:08:52 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-02-10 01:03:33 -05:00
|
|
|
TRACK_VIEW = "HTTP_DISCOURSE_TRACK_VIEW".freeze
|
|
|
|
CONTENT_TYPE = "Content-Type".freeze
|
|
|
|
def self.get_data(env,result)
|
|
|
|
|
|
|
|
status,headers = result
|
|
|
|
status = status.to_i
|
|
|
|
|
|
|
|
helper = Middleware::AnonymousCache::Helper.new(env)
|
|
|
|
request = Rack::Request.new(env)
|
|
|
|
{
|
|
|
|
status: status,
|
|
|
|
is_crawler: helper.is_crawler?,
|
|
|
|
has_auth_cookie: helper.has_auth_cookie?,
|
|
|
|
is_background: request.path =~ /^\/message-bus\// || request.path == /\/topics\/timings/,
|
|
|
|
track_view: (env[TRACK_VIEW] || (request.get? && !request.xhr? && headers[CONTENT_TYPE] =~ /text\/html/)) && status == 200
|
|
|
|
}
|
|
|
|
end
|
2015-02-05 00:08:52 -05:00
|
|
|
|
|
|
|
def call(env)
|
|
|
|
result = @app.call(env)
|
|
|
|
ensure
|
2015-02-10 01:03:33 -05:00
|
|
|
|
|
|
|
# we got to skip this on error ... its just logging
|
|
|
|
data = self.class.get_data(env,result) rescue nil
|
2015-02-10 01:05:24 -05:00
|
|
|
host = RailsMultisite::ConnectionManagement.host(env)
|
2015-02-10 01:03:33 -05:00
|
|
|
|
|
|
|
if data
|
|
|
|
Scheduler::Defer.later("Track view", _db=nil) do
|
2015-02-10 01:05:24 -05:00
|
|
|
self.class.log_request_on_site(data,host)
|
2015-02-10 01:03:33 -05:00
|
|
|
end
|
2015-02-05 00:08:52 -05:00
|
|
|
end
|
2015-02-10 01:03:33 -05:00
|
|
|
|
2015-02-05 00:08:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|