# frozen_string_literal: true require_dependency 'middleware/anonymous_cache' class Middleware::RequestTracker @@detailed_request_loggers = nil # register callbacks for detailed request loggers called on every request # example: # # Middleware::RequestTracker.detailed_request_logger(->|env, data| do # # do stuff with env and data # end def self.register_detailed_request_logger(callback) unless @patched_instrumentation require_dependency "method_profiler" MethodProfiler.patch(PG::Connection, [ :exec, :async_exec, :exec_prepared, :send_query_prepared, :query ], :sql) MethodProfiler.patch(Redis::Client, [ :call, :call_pipeline ], :redis) @patched_instrumentation = true end (@@detailed_request_loggers ||= []) << callback end def self.unregister_detailed_request_logger(callback) @@detailed_request_loggers.delete callback if @@detailed_request_loggers.length == 0 @detailed_request_loggers = nil end end def initialize(app, settings = {}) @app = app end def self.log_request_on_site(data, host) RailsMultisite::ConnectionManagement.with_hostname(host) do log_request(data) end end def self.log_request(data) status = data[:status] track_view = data[:track_view] if track_view if data[:is_crawler] ApplicationRequest.increment!(:page_view_crawler) elsif data[:has_auth_cookie] ApplicationRequest.increment!(:page_view_logged_in) ApplicationRequest.increment!(:page_view_logged_in_mobile) if data[:is_mobile] else ApplicationRequest.increment!(:page_view_anon) ApplicationRequest.increment!(:page_view_anon_mobile) if data[:is_mobile] end end ApplicationRequest.increment!(:http_total) if status >= 500 ApplicationRequest.increment!(:http_5xx) elsif data[:is_background] ApplicationRequest.increment!(:http_background) elsif status >= 400 ApplicationRequest.increment!(:http_4xx) elsif status >= 300 ApplicationRequest.increment!(:http_3xx) elsif status >= 200 && status < 300 ApplicationRequest.increment!(:http_2xx) end end def self.get_data(env, result, timing) status, headers = result status = status.to_i helper = Middleware::AnonymousCache::Helper.new(env) request = Rack::Request.new(env) env_track_view = env["HTTP_DISCOURSE_TRACK_VIEW"] track_view = status == 200 track_view &&= env_track_view != "0" && env_track_view != "false" track_view &&= env_track_view || (request.get? && !request.xhr? && headers["Content-Type"] =~ /text\/html/) track_view = !!track_view { status: status, is_crawler: helper.is_crawler?, has_auth_cookie: helper.has_auth_cookie?, is_background: request.path =~ /^\/message-bus\// || request.path == /\/topics\/timings/, is_mobile: helper.is_mobile?, track_view: track_view, timing: timing } end def call(env) MethodProfiler.start if @@detailed_request_loggers result = @app.call(env) info = MethodProfiler.stop if @@detailed_request_loggers result ensure # we got to skip this on error ... its just logging data = self.class.get_data(env, result, info) rescue nil host = RailsMultisite::ConnectionManagement.host(env) if data if result && (headers = result[1]) headers["X-Discourse-TrackView"] = "1" if data[:track_view] end if @@detailed_request_loggers @@detailed_request_loggers.each { |logger| logger.call(env, data) } end log_later(data, host) end end def log_later(data, host) Scheduler::Defer.later("Track view", _db = nil) do self.class.log_request_on_site(data, host) end end end