An Architect's View

CFML, Clojure, Software Design, Frameworks and more...

An Architect's View

New Relic and CFML

May 14, 2013 ·

A while back I blogged about how we instrumented our Clojure code to show up in New Relic traces and we also did some stuff in CFML to get better data out of New Relic. Just now Joe Swenson asked on Twitter how to get CFML page names to show up in New Relic so I figured I'd blog a quick note about that.

You should already be starting up your servlet container with the -javaagent: option if you're using New Relic for monitoring your web app (and indeed New Relic has documentation about adding the Java agent to ColdFusion). That means you'll have the New Relic JAR on your classpath and so you can instantiate the New Relic agent API class in your CFML code. We do this at application startup:

application.NewRelic = createObject( "java", "com.newrelic.api.agent.NewRelic" );

Then at the start of each request, we do something like this:

if ( structKeyExists( application, "NewRelic" ) ) {
application.NewRelic.setTransactionName( "CFML", CGI.SCRIPT_NAME );
}

The first argument is a 'category' name and is currently ignored by the New Relic API (and you can pass null if you want). The second argument is how you want this web transaction reported in the New Relic traces. We actually just use a fake path to represent groups of requests since we have a lot of internal instrumentation (in Clojure) and that gives us better data about where time is being spent in the code.

Tags: coldfusion

9 responses

  • 1 Chris H // May 15, 2013 at 8:49 AM

    Interesting tool, might have to check it out. Thanks for the info!
  • 2 Oscar Arevalo // May 17, 2013 at 9:29 PM

    A small gotcha to look for when using your own transaction naming is that New Relic expects the transaction names to be URI-like, that means that at least they should start with a "/" character. If not then weird things happen. In my case (Railo 3.x install+tomcat) the request would just time out when calling setTransactionName() method.

    What I ended up doing on my apps is following the convention: /{app name}/{framework event}
  • 3 Manuel Avelar // Mar 3, 2014 at 10:55 AM

    It's working good :) thanks! tested in CF10 and works, I will try Railo 4 later.
  • 4 Manuel Avelar // Mar 3, 2014 at 11:04 AM

    Another tip that could be useful. In my case I'm using FW/1 so "CGI.SCRIPT_NAME" it will print "/index.cfm" on each transaction, I did change to "getSectionAndItem()" FW/1 function instead and looks more useful.
  • 5 Sean Corfield // Mar 3, 2014 at 8:42 PM

    @Manuel, yes, we use the ColdBox event name in our apps instead of the CGI variable but for the blog post I didn't want to muddy the water with mentioning frameworks.

    You can use pretty much anything for the transaction name, but beware that New Relic limits the number of unique transactions an application can generate in a given period, so be careful about using unsanitized user input (through the URL or the browser request itself) since you can be hit by spiders or malicious bots.
  • 6 Manuel Avelar // Mar 4, 2014 at 6:24 AM

    @Sean, I got your point, will work on that. Thanks!
  • 7 Barbara ONeal // Apr 22, 2014 at 12:22 PM

    Manuel, did anyone ever get this working for contexts for Railo 4.0. I have it running on the Main Railo instance but do not know how to get it working on the individual contexts for Railo.

    Thanks.
  • 8 Sean Corfield // May 1, 2014 at 9:04 PM

    Barbara, not sure what multiple contexts would change? Do you mean separate web applications in a single JVM instance (what we do at World Singles) or do you mean separate JVM instances?

    For the former "it just works", for the latter you need the javaagent argument added to each JVM's startup configuration.

    Does that help?
  • 9 Barbara ONeal // May 2, 2014 at 6:32 AM

    Sean, I actually figured it out sort of and then after much deliberation i have decided to test the new version of Fusion Reactor.

    Thank you for responding though.