<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Performance-Monitoring on Damian Galarza | Software Engineering &amp; AI Consulting</title><link>https://www.damiangalarza.com/tags/performance-monitoring/</link><description>Recent posts from Damian Galarza | Software Engineering &amp; AI Consulting</description><generator>Hugo</generator><language>en-us</language><managingEditor>Damian Galarza</managingEditor><atom:link href="https://www.damiangalarza.com/tags/performance-monitoring/feed.xml" rel="self" type="application/rss+xml"/><item><title>NewRelic RPM with ActionController::Metal</title><link>https://www.damiangalarza.com/posts/2012-09-05-newrelic-rpm-with-actioncontroller-metal/</link><pubDate>Wed, 05 Sep 2012 00:00:00 +0000</pubDate><author>Damian Galarza</author><guid>https://www.damiangalarza.com/posts/2012-09-05-newrelic-rpm-with-actioncontroller-metal/</guid><content:encoded><![CDATA[<p>As part of our work with the <a href="http://www.qfive.com">QFive</a> API for internal consumption we&rsquo;ve built it out using Rails&rsquo; ActionController::Metal.
We quickly realized that incoming requests to the API were not being tracked by NewRelic and looked for a way to resolve this. A quick google search will yield <a href="https://newrelic.com/docs/ruby/adding-instrumentation-to-actioncontroller-metal">this documentation from New Relic</a>
which includes the following example:</p>
<div class="highlight"><pre tabindex="0" style="color:#cdd6f4;background-color:#1e1e2e;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-ruby" data-lang="ruby"><span style="display:flex;"><span><span style="color:#cba6f7">class</span> <span style="color:#f9e2af">SteelController</span> <span style="color:#89dceb;font-weight:bold">&lt;</span> <span style="color:#f9e2af">ActionController</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">Metal</span>
</span></span><span style="display:flex;"><span>  <span style="color:#cba6f7">include</span> <span style="color:#f9e2af">ActionController</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">Rendering</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>  <span style="color:#cba6f7">def</span> <span style="color:#89b4fa">show</span>
</span></span><span style="display:flex;"><span>    render <span style="color:#a6e3a1">:text</span> <span style="color:#89dceb;font-weight:bold">=&gt;</span> <span style="color:#a6e3a1">&#34;Here is some text&#34;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#cba6f7">end</span>
</span></span><span style="display:flex;"><span>  <span style="color:#cba6f7">include</span> <span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">NewRelic</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">Agent</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">Instrumentation</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">ControllerInstrumentation</span>
</span></span><span style="display:flex;"><span>  add_transaction_tracer <span style="color:#a6e3a1">:show</span>
</span></span><span style="display:flex;"><span><span style="color:#cba6f7">end</span>
</span></span></code></pre></div><p>While this works, the problem remains that now you will need to add a transaction tracer for each and every controller action you add.</p>
<p>With this in mind I went in search of a better solution which could streamline the solution. Digging around the NewRelic RPM gem internals I was able to find the &lsquo;magic&rsquo; behind the way New Relic integrates into requests. Within <code>lib/new_relic/agent/instrumentation/rails3/action_controller.rb</code> you can find the following:</p>
<div class="highlight"><pre tabindex="0" style="color:#cdd6f4;background-color:#1e1e2e;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-ruby" data-lang="ruby"><span style="display:flex;"><span><span style="color:#f9e2af">DependencyDetection</span><span style="color:#89dceb;font-weight:bold">.</span>defer <span style="color:#cba6f7">do</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f5e0dc">@name</span> <span style="color:#89dceb;font-weight:bold">=</span> <span style="color:#a6e3a1">:rails3_controller</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>  depends_on <span style="color:#cba6f7">do</span>
</span></span><span style="display:flex;"><span>    defined?(<span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">Rails</span>) <span style="color:#89dceb;font-weight:bold">&amp;&amp;</span> <span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">Rails</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">VERSION</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">MAJOR</span><span style="color:#89dceb;font-weight:bold">.</span>to_i <span style="color:#89dceb;font-weight:bold">==</span> <span style="color:#fab387">3</span>
</span></span><span style="display:flex;"><span>  <span style="color:#cba6f7">end</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>  depends_on <span style="color:#cba6f7">do</span>
</span></span><span style="display:flex;"><span>    defined?(<span style="color:#f9e2af">ActionController</span>) <span style="color:#89dceb;font-weight:bold">&amp;&amp;</span> defined?(<span style="color:#f9e2af">ActionController</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">Base</span>)
</span></span><span style="display:flex;"><span>  <span style="color:#cba6f7">end</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>  executes <span style="color:#cba6f7">do</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f9e2af">NewRelic</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">Agent</span><span style="color:#89dceb;font-weight:bold">.</span>logger<span style="color:#89dceb;font-weight:bold">.</span>debug <span style="color:#a6e3a1">&#39;Installing Rails 3 Controller instrumentation&#39;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#cba6f7">end</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>  executes <span style="color:#cba6f7">do</span>
</span></span><span style="display:flex;"><span>    <span style="color:#cba6f7">class</span> <span style="color:#f9e2af">ActionController</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">Base</span>
</span></span><span style="display:flex;"><span>      <span style="color:#cba6f7">include</span> <span style="color:#f9e2af">NewRelic</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">Agent</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">Instrumentation</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">ControllerInstrumentation</span>
</span></span><span style="display:flex;"><span>      <span style="color:#cba6f7">include</span> <span style="color:#f9e2af">NewRelic</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">Agent</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">Instrumentation</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">Rails3</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">ActionController</span>
</span></span><span style="display:flex;"><span>    <span style="color:#cba6f7">end</span>
</span></span><span style="display:flex;"><span>  <span style="color:#cba6f7">end</span>
</span></span><span style="display:flex;"><span><span style="color:#cba6f7">end</span>
</span></span></code></pre></div><p>Which handles including the various instrumentation tools for ActionController. With this in mind we can follow the same logic in our API BaseController:</p>
<div class="highlight"><pre tabindex="0" style="color:#cdd6f4;background-color:#1e1e2e;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-ruby" data-lang="ruby"><span style="display:flex;"><span><span style="color:#89dceb">require</span> <span style="color:#a6e3a1">&#39;new_relic/agent/instrumentation/controller_instrumentation&#39;</span>
</span></span><span style="display:flex;"><span><span style="color:#89dceb">require</span> <span style="color:#a6e3a1">&#39;new_relic/agent/instrumentation/rails3/action_controller&#39;</span>
</span></span><span style="display:flex;"><span><span style="color:#89dceb">require</span> <span style="color:#a6e3a1">&#39;new_relic/agent/instrumentation/rails3/errors&#39;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#cba6f7">class</span> <span style="color:#f9e2af">Api</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">V1</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">BaseController</span> <span style="color:#89dceb;font-weight:bold">&lt;</span> <span style="color:#f9e2af">ActionController</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">Metal</span>
</span></span><span style="display:flex;"><span>	<span style="color:#cba6f7">include</span> <span style="color:#f9e2af">ActionController</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">Instrumentation</span>
</span></span><span style="display:flex;"><span>	<span style="color:#cba6f7">include</span> <span style="color:#f9e2af">ActionController</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">Rescue</span>
</span></span><span style="display:flex;"><span>	
</span></span><span style="display:flex;"><span>	<span style="color:#6c7086;font-style:italic"># ...</span>
</span></span><span style="display:flex;"><span>	
</span></span><span style="display:flex;"><span>	<span style="color:#6c7086;font-style:italic"># In order to enable NewRelic RPM monitoring in the API we&#39;ll need to include it</span>
</span></span><span style="display:flex;"><span>	<span style="color:#cba6f7">include</span> <span style="color:#f9e2af">NewRelic</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">Agent</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">Instrumentation</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">ControllerInstrumentation</span>
</span></span><span style="display:flex;"><span>	<span style="color:#cba6f7">include</span> <span style="color:#f9e2af">NewRelic</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">Agent</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">Instrumentation</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">Rails3</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">ActionController</span>
</span></span><span style="display:flex;"><span>	<span style="color:#cba6f7">include</span> <span style="color:#f9e2af">NewRelic</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">Agent</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">Instrumentation</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">Rails3</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">Errors</span>
</span></span><span style="display:flex;"><span>		
</span></span><span style="display:flex;"><span><span style="color:#cba6f7">end</span>
</span></span></code></pre></div>]]></content:encoded></item></channel></rss>