Making today worse so tomorrow seems better.
Using New Relic add_method_tracer with Class methods
At Vocel we use the New Relic plugin to monitor the performance of our production Rails apps.
In addition to the built-in ActiveRecord and Controller metrics, you can define your own custom metrics. The documentation clearly shows how to add a metric to an instance method, but how do you add one to a class method?
I’m using custom metrics to differentiate between time spent in our app and time spent waiting for external components. So we can imagine that we have a class like:
The problem with this is that the New Relic add_method_tracer call expects an instance method. So when we add the custom metric inside the class definition it is unable to find the method that it is supposed to be tracing. It’s looking for get_recommended_for_subscriber not
The key to the solution is that in Ruby, Class methods are just instance methods on the singleton object that defines the class. So instead of dealing with the class methods on the class, we want to operate on the instance methods of the singleton. Luckily we can get access to the singleton definition by using the “class << self” syntax.
You’ll see the “
- About
- Technology, programming, the interwebs and other topics by members of the slackworks community.
- Contributors
- All Posts
-
- Aug 05 2009 Using New Relic add_method_tracer with Class methods
- Jun 30 2009 Estimating Key Collisions (Birthday Problem)
- Jun 29 2009 The Blarg is Back!
- Jul 23 2008 JNDI with Rails
- Jul 06 2008 JRuby Service in Rails
- May 16 2008 Wildcard DNS and Rails
- Jan 30 2008 Sprint Error "911" and the "Download Domain"
- Jan 16 2008 Getting a Good View of Your Couch
- Jan 12 2008 Bring Selenium to the integration party
- Jan 12 2008 Bj Makes Attachment_fu Happy
- Jan 03 2008 Restore Finder magic to a sparsebundle directory
- Dec 18 2007 First Post
0 comments