是否有覆蓋默認異常處理(默認救援),這樣我可以有編寫自定義的邏輯,比如發送堆棧跟蹤我們的時差通道的方式。捕獲異常應用廣泛
基本上,我想做出點什麼呢NewRelic的與他們的寶石:https://github.com/newrelic/rpm,但它更多的本地版本。
感謝
是否有覆蓋默認異常處理(默認救援),這樣我可以有編寫自定義的邏輯,比如發送堆棧跟蹤我們的時差通道的方式。捕獲異常應用廣泛
基本上,我想做出點什麼呢NewRelic的與他們的寶石:https://github.com/newrelic/rpm,但它更多的本地版本。
感謝
你可以做最簡單的事情就是儀器上的所有錯誤使用跟蹤點提高:
trace = TracePoint.new(:raise) do |tp|
myLogger.log [tp.lineno, tp.event, tp.raised_exception]
end
trace.enable
使用痕跡用於導致顯着的代碼減速。我認爲Ruby 2.0中的新API顯着降低了減速。根據我的天真基準:
count = 10_000_000
t = Time.now
count.times do |i|
begin
0/0
rescue
end
end
puts "trial 1: #{Time.now - t}"
t = Time.now
count.times do |i|
"hi"
begin
0/0
rescue
end
end
puts "trial 2: #{Time.now - t}"
trace = TracePoint.new(:raise) do |tp|
"hi"
end
trace.enable
t = Time.now
count.times do |i|
begin
0/0
rescue
end
end
puts "trial 3: #{Time.now - t}"
#=>trial 1: 10.110471094
#=>trial 2: 9.971755759
#=>trial 3: 11.608365399
跟蹤點只增加了1秒(或10%)放緩加薪10,000,000處決。話雖這麼說,跟蹤點依然不被認爲是「值得生產技術」,因爲它不添加開銷,可能很難預測(例如,有在Ruby中有很多模糊的例外)。
如果你想知道新的文物如何管理儀器代碼,而無需額外開銷:
...
class_eval <<-EOC
def #{with_method_name}(*args, &block)
perform_action_with_newrelic_trace(#{argument_list.join(',')}) do
#{without_method_name}(*args, &block)
end
end
EOC
...
它使用幾百行的元編程來捕捉特定的方法,解構他們,並與儀表重新定義它們裏面。這種技術在啓動時需要大量的代碼和(我假設)額外的內存和時間,但是一旦分配了方法,其優點是沒有額外的開銷。
嘗試這樣的事情在application_controller.rb
rescue_from Exception, with: :rescue500 if Rails.env.production?
rescue_from ActiveRecord::RecordNotFound, with: :record_not_found unless Rails.env.development?
def record_not_found
render json: {code: 404, errors: ["Record not found"]}, status: 404
end
def rescue404
render json: {code: 404, errors: ["No round matches"]}, status: 404
end
def rescue500(ex)
Rails.logger.error("\n\nEXCEPTION: #{ex.inspect}\n")
Rails.logger.info(ex)
render json: {code: 500, errors: [ex.message] }, status: 500
end
它做的東西,是的。但它不會捕獲應用程序範圍內的所有異常,例如在rake任務中。不應該有不Rails的設置,設置它的初始化器,或以某種方式的一種方式,但在Ruby中,通過重寫默認的異常行爲 –
如果你重寫它在Ruby中,你將如何能夠升級到Ruby的最新版本? moroever你提到你想要做自定義的東西,更好,我建議你找到所有我提到 – gates
確定可能發生,挽救他們像上面說的例外情況,但NewRelic的寶石某種程度上設法處理錯誤紅寶石範圍,和他們不不要使用rescue_from –