February 25, 2015

Using New Relic to monitor non web based Clojure applications.

Recently I wanted to use New Relic to monitor and profile a Clojure application. This specific application was a "sentiment agent" running as a daemon and calculating the sentiment of various URLS.

The rest of this post assumes that you've already created a New Relic account and installed the New Relic agent for your platform of choice.

In order to record basic information in New Relic all I needed to do was start my application using newrelic.jar as the -javaagent. While not perfect I decided that I'd bundle newrelic.jar and newrelic.yml with my application, so I commited them to BitBucket.

java $JVM_OPTS -javaagent:newrelic/newrelic.jar -cp newrelic/newrelic.jar -jar target/sentimizer.jar

This is enough to get basic metrics like memory and CPU but I'm used to using New Relic for web applications and exploring the data it captures for "Transactions".

This blog post provides instructions for adding custom instrumentation to Java applications. All we really need to do is make sure newrelic.jar is on the class path and that we have set enable_custom_tracing equal to true. Then we can import com.newrelic.api.agent.Trace; and decorate the method we wish to consider a transaction with the @Trace(dispatcher=true) annotation. The dispatcher=true part is what tells New Relic that we should consider this to be a new transaction.

As luck would have it, Clojure also has support for annotations. As far as I understand to be able to use annotations as we require, we need to use a record or type in Clojure. I just came up with the simple record below.

This allows us to use a Profiler to wrap any function and have its execution recorded as a New Relic transaction. Like so:

Instrumenting our function like this gives us much more detail. We will be able to see throughput and timings as well as a whole host of information relating to external resources, like databases, HTTP apis etc.

As an added bonus we can also uses New Relics "Thread Profiler" to profile and find hot spots in our Clojure application.

Thanks to Sean Corfield for documenting his efforts.

Tags: clojure new relic