lib/rorvswild.rb in rorvswild-0.0.2 vs lib/rorvswild.rb in rorvswild-0.0.3

- old
+ new

@@ -43,24 +43,28 @@ request[:db_runtime] = (payload[:db_runtime] || 0).round request[:view_runtime] = (payload[:view_runtime] || 0).round request[:other_runtime] = compute_duration(start, finish) - request[:db_runtime] - request[:view_runtime] request[:params] = params_filter.filter(payload[:params]) if error Thread.new { post_request } - rescue => ex - Rails.logger.error("[RorVsWild] " + ex.inspect) - Rails.logger.error("[RorVsWild] " + ex.backtrace.join("\n[RorVsWild] ")) + rescue => exception + log_error(exception) end def after_sql_query(name, start, finish, id, payload) return if !queries || payload[:name] == "EXPLAIN".freeze runtime, sql, plan = compute_duration(start, finish), nil, nil file, line, method = extract_file_and_line_from_call_stack(caller) + + if !file # I have no idea of the origin of the query, so at least we log the SQL. + file, line, method = ["Unknow", 0, "Unknow"] + sql = payload[:sql] + end + plan = explain(sql = payload[:sql], payload[:binds]) if runtime > sql_threshold queries << {file: file, line: line, sql: sql, plan: plan, runtime: runtime} - rescue => ex - Rails.logger.error("[RorVsWild] " + ex.inspect) - Rails.logger.error("[RorVsWild] " + ex.backtrace.join("\n[RorVsWild] ")) + rescue => exception + log_error(exception) end def after_view_rendering(name, start, finish, id, payload) views << {file: relative_path(payload[:identifier]), runtime: compute_duration(start, finish)} if views end @@ -105,15 +109,19 @@ rescue => ex end def post_request post("/requests", request: request.merge(queries: slowest_queries, views: slowest_views, error: error)) + rescue => exception + log_error(exception) end def extract_file_and_line_from_call_stack(stack) - file, line, method = (stack.find { |str| str.include?(Rails.root.to_s) } || - stack.find { |str| str.include?("(irb):") }).split(":") + location = stack.find { |str| str.include?(Rails.root.to_s) } || + stack.find { |str| !str.include?("/active_support/notifications/") } # Origin of the call comes from a gem probably + return unless location # Ok I have no idea + file, line, method = location.split(":") file.sub!(Rails.root.to_s, "") method.sub!("block in ", "") method.sub!("in `", "") method.sub!("'", "") [file, line, method] @@ -128,18 +136,27 @@ end def post(path, data) uri = URI(api_url + path) Net::HTTP.start(uri.host, uri.port) do |http| - post = Net::HTTP::Post.new(uri) + post = Net::HTTP::Post.new(uri.path) post.content_type = "application/json" post.basic_auth(app_id, api_key) post.body = data.to_json http.request(post) end end def params_filter @params_filter ||= ActionDispatch::Http::ParameterFilter.new(Rails.application.config.filter_parameters) + end + + def logger + Rails.logger + end + + def log_error(exception) + logger.error("[RorVsWild] " + exception.inspect) + logger.error("[RorVsWild] " + exception.backtrace.join("\n[RorVsWild] ")) end end end