New Relic Browser and the Ruby agent

With the Ruby agent, you can add New Relic Browser instrumentation to your webpages either automatically or manually. To enable Browser in the user interface, use the New Relic Browser settings. Then follow the procedures in this section to set up the Ruby agent.

Requirements

In order to use New Relic Browser with your Ruby agent, make sure you meet these requirements:

  • New Relic Ruby agent (version 3.0.1 or higher)
  • For automatic instrumentation: Rails 2.3 or higher
  • For manual instrumentation: Rails 2.0 to 2.2

Use auto-instrumentation

Automatic instrumentation works with Rack, and therefore requires Rails 2.3 or higher.

Configure the agent to automatically instrument pages with end user monitoring scripts, by adding or editing the following flag in newrelic.yml.

    browser_monitoring:
      auto_instrument: true

This is the simplest way to monitor end users. The Ruby agent examines each page from your application and automatically injects the Browser JavaScript in the header. Auto-instrumentation works for environments that support Rack. For performance reasons the agent scans only the first 50k of your application's response for the header instrumentation point. In cases where a X-UA-Compatible meta tag is present and the <head> tag is longer than 50k, auto-instrumentation will fail gracefully.

Auto-instrumentation will look for an X-UA-Compatible meta tag and insert the JavaScript directly after it. If the auto-instrumentation cannot find an X-UA-Compatible meta tag, it will insert after the opening head tag, and failing that it will insert after the opening body tag. If any of these tags are wrapped in conditionals or comments, the automatic instrumentation will likely fail.

If auto-instrumentation fails to correctly instrument your application’s pages, then you must manually instrument via agent API.

Manually instrument via agent API

If you are using Rails 2.1 to 2.2 or cannot use auto-instrumentation, you can still set up end user monitoring manually by including appropriate scripts in your pages. Use the New Relic Ruby agent's API to generate the script to be inserted into your pages.

For example, to modify your application's template to call the agent, use:

    <head>
    <%= ::NewRelic::Agent.browser_timing_header rescue "" %>
    ... existing template code ...
    </head>

Troubleshooting

If you do not see any Browser data being reported, view the HTML source and confirm two blocks of script similar to this in your HTML head:

<script type="text/javascript">window.NREUM||(NREUM={});...</script>

If these are not present, check the auto-instrumentation settings or the placement of manual instrumentation in your template files.

In versions of the Ruby agent prior to 3.7.0, <%=::NewRelic::Agent.browser_timing_footer rescue "" %> also needed to be called as close to the bottom of the document as possible. With 3.7.0, all the required script is included by browser_timing_header. browser_timing_footer returns an empty string for compatibility.

Disable auto-instrumentation

The default value of auto-instrument is true (even when unspecified). To disable auto-instrumentation, set this flag to false.

To disable auto-instrumentation only for certain controllers or controller actions, use newrelic_ignore_enduser. Server-side instrumentation will be unaffected by this call.

For more help

Additional documentation resources include:

Join the discussion about Ruby in the New Relic Online Technical Community! The Technical Community is a public platform to discuss and troubleshoot your New Relic toolset.

If you need additional help, get support at support.newrelic.com.