New Relic Agent — Integration With Java Application

Sravan K
3 min readApr 7, 2020

Hello Net-heads!! Lately, I have been working on this highly recommended monitoring tool and have come across an use case to monitor the Java monolithic applications on this tool.

Note: New relic Application Performance Monitoring (APM) is best for Web-based applications i.e. apps that can serve http/https requests. You cannot monitor transaction analytics of non-web applications (listeners) using New relic APM.

In this post, we are going to have a look at the steps involved in integrating or would rather say Installing New Relic Java Agent on Monolithic/Java War Applications to monitor the application performance.

Before you install:

Make sure you have
1. New relic account
2. A Sample Java Application (WAR)
3. Tomcat Server
4. AWS EC2 instance.

Steps to install java agent:

Below steps will illustrate ways to integrate New relic with a Java application running on Tomcat server:

Get the agent:
Complete the following:
1. Login to New Relic
2. From the account dropdown, navigate to account settings .
3. Scroll down to view the most recent java agent and download the zip file.
4. Unzip newrelic-java.zip .

Account Settings Page — New Relic UI

Configure the agent:
The agent requires license_key and app_name at the startup which can be configured in newrelic.yml .

Note: We opt to pass the settings with environment variables via setenv.sh rather than manually (not ideal) updating the newrelic.yml .

Install the agent:
Lets perform the below steps to look up to our application on New Relic APM:
1. SSH to an AWS EC2 instance pre-configured with tomcat server running a Java application.
2. Upload the downloaded files newrelic.jar and newrelic.yml onto the EC2 instance.
3. In the tomcat server directory structure, create a directory for New Relic files.

> cd /usr/share/tomcat
> mkdir newrelic
> chmod 655 newrelic
> chown tomcat:tomcat newrelic
> cp <newrelic-files-location> /usr/share/tomcat/newrelic .

4. Create a bash file setenv.sh in the tomcat server bin directory (example — /usr/share/tomcat/bin)and include the below lines:

# These are environment variables identified by New relic Agent
export NEW_RELIC_APP_NAME=<application name>
export NEW_RELIC_LICENSE_KEY=<license key>
JAVA_OPTS=
"$JAVA_OPTS -javaagent:</path/to/newrelic.jar> -Dnewrelic.environment=<env>"

# Sample Configuration
export NEW_RELIC_APP_NAME=sherlock-holmes
export NEW_RELIC_LICENSE_KEY=7832339472937427394729874932
JAVA_OPTS=
"$JAVA_OPTS -javaagent:/usr/share/tomcat/newrelic/newrelic.jar -Dnewrelic.environment=dev"

5. Restart the tomcat server and post a http request to your Java application to monitor the performance on the New Relic APM.

It worked!!!!

Windup:

New relic is one of the outstanding monitoring tools in the current market. Though it slips a bit in capturing transactions (invoked from the application) for non-web applications ,it features profiling tools/events like thread profiler and error analytics in APM UI to support non-web applications.

Stay tuned for more productionised solutions!!!

Thank you for reading!!

What’s next?

To automate this approach and install Java agent every time an AWS EC2 instance is launched, you can refer to:

--

--

Sravan K
Sravan K

Responses (1)