require "open-uri" require "cgi" class Monitr def initialize(account_id) @account_id = account_id @monitr_request_start = Time.now.to_f end def log(controller, params = {}) env_fields = %W( HTTP_USER_AGENT REQUEST_PATH CONTROLLER_NAME PATH_INFO REMOTE_ADDR ACCOUNT_ID REQUEST_PROCESS_TIME REQUEST_URI) env_fields.each do |label| params[label] = controller.request.env[label] end # This deals with Heroku passing the load balancers instead of the originating address if controller.request.env["HTTP_X_REAL_IP"] != nil params["REMOTE_ADDR"] = controller.request.env["HTTP_X_REAL_IP"] end if @monitr_request_start params["REQUEST_PROCESS_TIME"] = Time.now.to_f - @monitr_request_start end params["CONTROLLER_NAME"] = controller.controller_name params["ACTION_NAME"] = controller.action_name params["ACCOUNT_ID"] = @account_id begin params = data_to_cgi(params) url = "http://events.monitr.com/cgi-bin/monitr?" + params fp = open(url) data = fp.read fp.close rescue end end private def data_to_cgi(data) params = "" data.each do |label, value| params += "#{CGI.escape(label.to_s)}=#{CGI.escape(value.to_s)}&" end params end end