【问题标题】:Rails: How can I log all requests which take more than 4s to execute?Rails:如何记录所有执行时间超过 4 秒的请求?
【发布时间】:2011-02-28 09:55:34
【问题描述】:

我有一个托管在云环境中的网络应用程序,它可以扩展到多个网络节点以提供更高的负载。

当我们收到越来越多的 HTTP 请求(资产被远程存储)时,我需要做的是捕捉这种情况。我该怎么做?

从这个角度来看,我看到的问题是,如果我们的请求比 mongrel 集群可以处理的多,那么队列就会增长。而在我们的 Rails 应用程序中,我们只能在 mongrel 收到来自平衡器的请求后才能计数。

有什么建议吗?

【问题讨论】:

    标签: ruby-on-rails performance monitoring scalability


    【解决方案1】:

    我会以around_filter 开头,可能类似于:

    around_filter :time_it
    
    private
      def time_it
        now = DateTime.now
        yield # this is where the request gets made.
        time_span = DateTime.now - now
    
        # convert to double for better accuracy.
        # (i.e. 3.5 => 4 so you might not want to
        # work with integers here.
        if time_span * 86400.0 > 4.0  
          logger.debug "something is taking a little longer then expected here!"
        end
      end
    

    以此为起点。希望对您有所帮助。

    编辑:将此代码放入 ApplicationController 中,以便每个 Controller 都可以使用它。

    【讨论】:

    • 这很好用,我认为这个问题一般来说是你必须用DateTime.now 为每个请求计时,这样非常高效,但它确实是完成工作的唯一方法。
    • 是的,可能有一个很棒的 gem 可以做得更好(metric_fu? 或某种分析 gem),但不知道有什么可以确定他正在寻找的东西。他可以运行几次,找出瓶颈所在,然后将此代码放入有问题的特定控制器中。
    【解决方案2】:

    除了@DJTripleThreat,您还应该查看NewRelic RPM,以更深入地了解您的代码性能。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-16
      • 1970-01-01
      • 1970-01-01
      • 2020-04-29
      • 2013-04-01
      • 1970-01-01
      相关资源
      最近更新 更多