lib/rorvswild.rb in rorvswild-0.0.3 vs lib/rorvswild.rb in rorvswild-0.0.4
- old
+ new
@@ -7,19 +7,21 @@
class Client
def self.default_config
{
api_url: "http://www.rorvswild.com/api",
- sql_threshold: 500
+ explain_sql_threshold: 500,
+ log_sql_threshold: 100,
}
end
- attr_reader :api_url, :api_key, :app_id, :sql_threshold, :error, :request
+ attr_reader :api_url, :api_key, :app_id, :error, :request, :explain_sql_threshold, :log_sql_threshold
def initialize(config)
config = self.class.default_config.merge(config)
- @sql_threshold = config[:sql_threshold]
+ @explain_sql_threshold = config[:explain_sql_threshold]
+ @log_sql_threshold = config[:log_sql_threshold]
@api_url = config[:api_url]
@api_key = config[:api_key]
@app_id = config[:app_id]
setup_callbacks
end
@@ -51,18 +53,15 @@
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}
+ # I can't figure out the exact location which triggered the query, so at least the SQL is logged.
+ sql, file, line, method = payload[:sql], "Unknow", 0, "Unknow" if !file
+ sql = payload[:sql] if runtime >= log_sql_threshold
+ plan = explain(payload[:sql], payload[:binds]) if runtime >= explain_sql_threshold
+ queries << {file: file, line: line, method: method, sql: sql, plan: plan, runtime: runtime}
rescue => exception
log_error(exception)
end
def after_view_rendering(name, start, finish, id, payload)
@@ -114,18 +113,21 @@
rescue => exception
log_error(exception)
end
def extract_file_and_line_from_call_stack(stack)
- 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
+ return unless location = stack.find { |str| str.include?(Rails.root.to_s) }
file, line, method = location.split(":")
+ method = cleanup_method_name(method)
file.sub!(Rails.root.to_s, "")
+ [file, line, method]
+ end
+
+ def cleanup_method_name(method)
method.sub!("block in ", "")
method.sub!("in `", "")
method.sub!("'", "")
- [file, line, method]
+ method.index("_app_views_") == 0 ? nil : method
end
def compute_duration(start, finish)
((finish - start) * 1000).round
end