6

在我的 Rails 应用程序的 New Relic 事务部分中,我可以看到这个特定的方法调用 ( Grape::Middleware::Formatter#call ) 是最耗时的一个(占用几乎 90% 的时间):

Middleware  Grape::Middleware::Formatter#call   89.2 % (Time)   824 ms (Avg Time)

但是在 New Relic 的事务分解表中没有提供足够的信息来说明实际导致性能问题的原因。因此,我想为此方法添加Ruby 自定义检测,以便 New Relic 可以通过显示调用 Grape::Middleware::Formatter#call方法时发生的所有事件来为我提供有关该问题的更多信息。

查看用于添加自定义检测的 New Relic文档,我创建了一个名为config/initializers/rpm_instrumentation.rb的新文件,内容如下:

require 'new_relic/agent/method_tracer'

Grape::Middleware::Formatter.class_eval do
  include ::NewRelic::Agent::MethodTracer

  add_method_tracer :call
end

但是在我的开发模式下,New Relic 在这个调用下没有显示任何额外的信息:Grape::Middleware::Formatter/call。仅摘要,显示在添加此自定义跟踪器之前存在的 SQL 调用,这意味着我的自定义跟踪器未按预期工作。

所以我的问题是我在这里遗漏了什么吗?New Relic 是否支持这种类型的方法检测,它们不是 Rails 应用程序的直接部分,而是来自 Rails 应用程序正在使用的 gem?(在我的例子中,Grape::Middleware::Formatter#call方法是' grape ' gem 的一部分)。

4

1 回答 1

2

New Relic does not fully support grape at this time. What you're seeing by default is that we do time the entire grape transaction, starting in middleware and ending at the grape endpoint. However, the Ruby agent just lists that entire time under one transaction name right now (the Grape::Middleware::Formatter#call that you're seeing). To break apart that grape transaction into the various endpoints, you should use set_transaction_name to provide a unique name for the transaction for each endpoint.

To get more details on the methods within call, you'll need to use add_method_tracer, but not by adding a method tracer to call itself, rather by using add_method_tracer on other methods within call. You'll generally want to set this up in an initializer or other setup phase of your application, before any transactions happen, while set_transaction_name is called during the transaction (e.g. in the controller).

于 2014-10-23T21:53:54.753 回答