Enabling New Relic Monitoring for Java apps deployed on JBoss AS 7

The OpenShift forums have been retired.
You can still read and search them, but for help, please post a question on Stack Overflow.

Some days ago, I was looking for a way to enable New Relic Monitoring on OpenShift. Since I haven't found anything straight forward, I decided to compile all the info myself and write this little tutorial to help anyone trying to do the same thing. Please, feel free to suggest improvements if there is a better way to do this.

Contents

Step 1: Registration on New Relic

The first step to enable monitoring on Java apps is to sign up and download the respective jars on New Relic. They support a lot of kinds of applications but since we are going to monitor a Java app, we need to download the correct JARs for it.

Step 2: Adding required files

Next, we need to select some of these resources downloaded previously and add it to your app repository.

unzip newrelic_agent2.17.2.zip
cd newrelic

The files we need are inside the .zip extracted in /newrelic:

  • newrelic.jar
  • newrelic.yml

The newrelic.jar file contains the agent class files for monitoring. The newrelic.yml file contains configuration information for the New Relic agent, including your license key, application name, SSL settings, etc.

On server startup, the agent will search for the newrelic.yml file in the directory containing the newrelic.jar file. The agent log will be written to a sub-directory in the same location named logs.

So, we copy the required files to your application repo.

mkdir /path/to/your/app/repo/newrelic
cp newrelic.jar /path/to/your/app/repo/newrelic
cp newrelic.yml /path/to/your/app/repo/newrelic

Step 3: Changing pre_start hook for JBoss AS 7

Now we need to discover the hash created by OpenShift to identify your application repository. This can be done by checking your repo remote settings:

cat .git/config

The expected output is something like

...
[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = ssh://{hash}@your-account.rhcloud.com/~/git/service.git/
...

where {hash} is a placeholder for the hash we are looking for. This is the same hash you needed to clone your repository for the first time. Save it somewhere else, we are going to need it shortly.

The next step is to include -javaagent to _JAVA_OPTS variable. To do so, navigate to .openshift/action_hooks on your app repo and edit the file pre_start_jbossas-7:

vi .openshift/action_hooks/pre_start_jbossas-7

At the end of the file, we need to add some new _JAVA_OPTS by appending the line:

export _JAVA_OPTIONS="$_JAVA_OPTIONS -javaagent:/var/lib/openshift/{hash}/app-root/repo/newrelic/newrelic.jar"

The {hash} is a placeholder for the same hash we retrieved previously. Replace the placeholder with the saved hash and save the file. Notice that -javaagent switch should always reference the full path to the newrelic.jar file. That's why we had to retrieve the repo hash and replace it here.

Step 4: Push it to OpenShift

Being added to your repo and configured, we need to push the new files to OpenShift.

git add .
git commit -m "Add New Relic Agent files and configure monitoring"
git push

If everything goes as expected, OpenShift will restart your application and in a few minutes your Dashboard on New Relic will be receiving status updates.

References

  1. New Relic - Manual Installation
  2. New Relic - JBoss Installation
  3. How to pass JVM options to JBoss

Wow - thanks a lot for putting this together and sharing it with us.

all work fine, thanks you Guilherme =)

Any chance to enable New Relic for other apps like PHP (Zend, other?)

Not at this time cause modifications are needed to the php interpreter which can't be modified on the fly like java (via javaagent). You might be able to modify https://github.com/Sgoettschkes/openshift-php54 to include the agent.

Is it possible to add ruby gem 'newrelic_rpm' instead of using the above Java installation? I'd like a simpler way to add New Relic support to PHP cartridges, and a way that uses a system for easier updates to the new Relic agent (such as 'gem update').

New Relic gives these instructions for setting up their gem:

""""

Add the gem to your Gemfile: gem 'newrelic_rpm'

Bundle from your application's directory: bundle install

""""

Can I do this for Wordpress, Drupal, and Magento cartridges? If so, would someone please point me to a page to read more on the topic?

The gem is for the Ruby agent. There's a different agent/installation method for each interpreter. It is not possible to install the PHP agent on OpenShift Online today due to the way the agent is installed (requires root access that will never be granted on OpenShift Online). We're still working on a way to install the various different agents.

Yes! It's working very fine for me. Thanks a lot.

Great! Thanks for the confirmation!

Hi,

I added the newrelic directory to my project and commit it and pushed the change to Git, but after Jenking rebuild the project the newrelic directory is not present on my OpenShift app (eventhough the Jenkins changes summary does show them):

JENKINS CHANGES:
Commit e782c02890b94b6219cdd43cf02ff86cf322a79b by gero
Added New Relic configuration.
The file was added  newrelic/newrelic.yml
The file was modified   .openshift/action_hooks/pre_start_jbossas
The file was added  newrelic/newrelic.jar

THIS IS ON OPENSHIFT

[garminconnectsync-vermaas.rhcloud.com e3665a8e525a4dd8a0feae7b184df3f5]\> ls -l ~/app-root/repo/
total 44
-rw-r--r--. 1 e3665a8e525a4dd8a0feae7b184df3f5 e3665a8e525a4dd8a0feae7b184df3f5  7629 Aug 29  2012 README
drwxr-xr-x. 2 e3665a8e525a4dd8a0feae7b184df3f5 e3665a8e525a4dd8a0feae7b184df3f5  4096 Sep  3 16:11 deployments
-rw-r--r--. 1 e3665a8e525a4dd8a0feae7b184df3f5 e3665a8e525a4dd8a0feae7b184df3f5  9598 Aug 29  2012 index.html
-rw-r--r--. 1 e3665a8e525a4dd8a0feae7b184df3f5 e3665a8e525a4dd8a0feae7b184df3f5 10459 Aug 29  2012 pom.xml
drwxr-xr-x. 4 e3665a8e525a4dd8a0feae7b184df3f5 e3665a8e525a4dd8a0feae7b184df3f5  4096 Aug 29  2012 src
drwxr-xr-x. 6 e3665a8e525a4dd8a0feae7b184df3f5 e3665a8e525a4dd8a0feae7b184df3f5  4096 Aug 29  2012 target
[garminconnectsync-vermaas.rhcloud.com e3665a8e525a4dd8a0feae7b184df3f5]\> 

Any ideas wat could be causing this?

Regards, Gero

I believe the workflow is such that Jenkins will not deploy the changes on your target app unless the build passed. Did the build fail? You can also check your bldr gear to see if the changes made it in to that stage of the build process - though you have a 15 minute window after the build completes to check.

It's probably desired to create the below files with the same contents as .openshift/action_hooks/pre_start_jbossas-7 from step 3:

  • .openshift/action_hooks/pre_restart_jbossas-7
  • .openshift/action_hooks/pre_reload_jbossas-7 (possibly)

rhc restart wasn't using the OPTIONS, but the restart hook fixed that. I haven't tested reload, but if it was a problem before, this should fix it.

I'd suggest that rather than searching for the {hash} code into the git repository configurations, would be better to use straight the system variable $OPENSHIFT_APP_UUID as:

  export JAVA_OPTS_EXT=" -javaagent:/var/lib/openshift/$OPENSHIFT_APP_UUID/app-root/repository/newrelic/newrelic.jar "

That should allow to trigger the newrelic agent activation even for scalable applications.