New Relic transaction trace links

Introduction

New Relic transaction traces (and any service capable of accepting custom parameters) can show a link to related log messages in Papertrail.

To learn more about collecting custom parameters in New Relic, see custom parameters. This document is based on “Extending New Relic’s Custom Parameters.”

Result

The end result is a link from each transaction trace, like this:

custom_parameters.png

How It Works

New Relic transaction traces measure the performance of a unit of work, typically a web request. Transaction traces can include arbitrary key/value attributes, which are shown alongside the trace.

With just a few lines of code in your app, we’ll generate a URL to Papertrail. The URL will include the system name and the current timestamp. Since Papertrail honors URL query parameters, Papertrail shows the logs which are very likely related to that transaction trace.

This URL is used as the transaction trace log_url custom parameter. No API integration is required other than the New Relic agent, which is already installed.

Setup

RubyJava

Language not listed? This technique applies to New Relic agents in all languages. For help, just ask us.

Ruby

First, define a new Rails method to generate the URL and set the custom parameter. Edit app/controllers/application_controller.rb. Add this method in the existing ApplicationController class:

class ApplicationController < ActionController::Base
  def add_papertrail_new_relic_custom_attribute
    log_url = "https://papertrailapp.com/systems/#{Socket.gethostname}/events?time=#{Time.now.to_i}"
    NewRelic::Agent.add_custom_attributes(:log_url => log_url)
    true

  end
end

The method calls add_custom_attributes, which is part of New Relic’s Ruby Agent (>= 3.12; older versions use add_custom_parameters). The method returns true because it is meant to be called as a controller callback.

Second, ensure that the new method is called during each request (or those which you would like to be linked). Add a before_filter to the top of the ApplicationController class definition, like this:

class ApplicationController < ActionController::Base
  before_filter :add_papertrail_new_relic_custom_attribute
end

Deploy this code change and restart your app. All subsequent transaction traces will include a link to Papertrail.

Java

First, define a new method to generate the URL and set the custom parameter. This can reside anywhere in your servlet or supporting libraries.

public class MyServlet extends HttpServlet {
  private void addPapertrailNewRelicCustomParameter(HttpServletRequest req) {
    String systemName = InetAddress.getLocalHost().getHostName();
    long epochTime = Date.getTime() / 1000L;
    String log_url = "https://papertrailapp.com/systems/" + systemName + "/events?time=" + Long.toString(epochTime);

    NewRelic.addCustomParameter("log_url", log_url);
}
end

New Relic’s Java Agent API Example contains a simple servlet that shows how this works. In the example servlet, the saveNewRelicInfo method is equivalent to our addPapertrailNewRelicCustomParameter method.

The method calls addCustomParameter, which is part of New Relic’s Java Agent.

Second, ensure that the new method is called during each request (or those which you would like to be linked). This depends on your servlet structure, but typically means calling addPapertrailNewRelicCustomParameter from within your servlet’s processRequest method, similar to this:

protected void processRequest(HttpServletRequest req,
                              HttpServletResponse resp)
  throws ServletException, IOException {

    addPapertrailNewRelicCustomParameter(req);
    doRequestWork(req);
    writeResponse(resp);
}

New Relic’s Java Agent API Example contains this method in context.

Deploy this code change and restart your app. All subsequent transaction traces will include a link to Papertrail.