<?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>Actioncontroller-Metal on Damian Galarza | Software Engineering &amp; AI Consulting</title><link>https://www.damiangalarza.com/tags/actioncontroller-metal/</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/actioncontroller-metal/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><item><title>AssetPath with ActionController::Metal</title><link>https://www.damiangalarza.com/posts/2012-08-13-assetpath-with-actioncontroller-metal/</link><pubDate>Mon, 13 Aug 2012 02:26:53 -0400</pubDate><author>Damian Galarza</author><guid>https://www.damiangalarza.com/posts/2012-08-13-assetpath-with-actioncontroller-metal/</guid><content:encoded><![CDATA[<p>At QFive we&rsquo;re building an API to serve our iOS application and throughout the site for ajax requests. Since our API only serves JSON we could slim down our controllers being used for the API to speed up requests as much as possible. In order to achieve this we&rsquo;ve built a base controller for our API which inherits from ActionController::Metal instead of ActionController::Base. I was originally introduced to this idea in the book &ldquo;Crafting Rails Applications&rdquo; by Jose Valim.</p>
<p>One issue we&rsquo;ve run into we&rsquo;ve run into is related to the asset pipeline and serving assets from a custom asset host.</p>
<p>For example:</p>
<p><em>config/environments/production.rb</em></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">Example</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">Application</span><span style="color:#89dceb;font-weight:bold">.</span>configure <span style="color:#cba6f7">do</span>
</span></span><span style="display:flex;"><span>	config<span style="color:#89dceb;font-weight:bold">.</span>action_controller<span style="color:#89dceb;font-weight:bold">.</span>asset_host <span style="color:#89dceb;font-weight:bold">=</span> <span style="color:#a6e3a1">&#34;//static.example/com&#34;</span>
</span></span><span style="display:flex;"><span><span style="color:#cba6f7">end</span>
</span></span></code></pre></div><p>Typically the usage behind this in a rails application is masked, whenever you use <em>image_tag</em> the image will be routed to the asset host on its own behind the scenes. However since we&rsquo;re inheriting from ActionController::Base there is some work to be done. We&rsquo;ll need to include a couple of modules and ensure that the load hooks are loaded in our base controller, something that happens behind the scenes when inheriting from ActionController::Base.</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">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">AbstractController</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">AssetPaths</span>
</span></span><span style="display:flex;"><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">Redirecting</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 style="color:#cba6f7">include</span> <span style="color:#f9e2af">ActionController</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">Caching</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">Renderers</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">All</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">ConditionalGet</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">ParamsWrapper</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">MimeResponds</span>
</span></span><span style="display:flex;"><span>	
</span></span><span style="display:flex;"><span>	<span style="color:#f9e2af">ActiveSupport</span><span style="color:#89dceb;font-weight:bold">.</span>run_load_hooks(<span style="color:#a6e3a1">:action_controller</span>, <span style="color:#89dceb">self</span>)
</span></span><span style="display:flex;"><span><span style="color:#cba6f7">end</span>
</span></span></code></pre></div><p>Of course, depending on what your application is using your mileage may vary. Our actual API BaseController looks something more like:</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">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">AbstractController</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">AssetPaths</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">Helpers</span>
</span></span><span style="display:flex;"><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">Redirecting</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 style="color:#cba6f7">include</span> <span style="color:#f9e2af">ActionController</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">Caching</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">Renderers</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">All</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">ConditionalGet</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">ParamsWrapper</span>
</span></span><span style="display:flex;"><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">MimeResponds</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">RequestForgeryProtection</span>
</span></span><span style="display:flex;"><span>	<span style="color:#cba6f7">include</span> <span style="color:#f9e2af">AbstractController</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">Callbacks</span>
</span></span><span style="display:flex;"><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:#f9e2af">Rails</span><span style="color:#89dceb;font-weight:bold">.</span>application<span style="color:#89dceb;font-weight:bold">.</span>routes<span style="color:#89dceb;font-weight:bold">.</span>default_url_options <span style="color:#89dceb;font-weight:bold">=</span> <span style="color:#f9e2af">ActionMailer</span><span style="color:#89dceb;font-weight:bold">::</span><span style="color:#f9e2af">Base</span><span style="color:#89dceb;font-weight:bold">.</span>default_url_options
</span></span><span style="display:flex;"><span>	<span style="color:#cba6f7">include</span> <span style="color:#f9e2af">Rails</span><span style="color:#89dceb;font-weight:bold">.</span>application<span style="color:#89dceb;font-weight:bold">.</span>routes<span style="color:#89dceb;font-weight:bold">.</span>url_helpers
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	append_view_path <span style="color:#a6e3a1">&#34;</span><span style="color:#a6e3a1">#{</span><span style="color:#f9e2af">Rails</span><span style="color:#89dceb;font-weight:bold">.</span>root<span style="color:#a6e3a1">}</span><span style="color:#a6e3a1">/app/views&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	protect_from_forgery
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#f9e2af">ActiveSupport</span><span style="color:#89dceb;font-weight:bold">.</span>run_load_hooks(<span style="color:#a6e3a1">:action_controller</span>, <span style="color:#89dceb">self</span>)
</span></span><span style="display:flex;"><span><span style="color:#cba6f7">end</span>
</span></span></code></pre></div><p>Be sure to check out the rails ActionController::Base to see what else is included by default and tweak as you see fit.</p>
]]></content:encoded></item></channel></rss>