Automate New Relic Agent Integration with Java

Hey Net-Heads!!
Isn’t it true that the machines have taken over the world. And this post is about the machines that can automate the process by running few lines of code.

Let’s implement scripts to install New relic agent on tomcat server and pass user data to the EC2 instance that can be used to run scripts after the instance starts.

OH, Wait!!! Before We Start

To understand the approach in integrating New Relic Java agent with Monolithic Java Applications, you can refer to:

Scripting…

Keep Calm And Automate

Terraform template file:

Let’s create a user data template file aws-user-data.tpl and include the commands to be executed at the instance launch time.

# Create a directory for New Relic within tomcat directory
cd /usr/share/tomcat/
mkdir newrelic
sudo chown -R tomcat:tomcat newrelic
# Download New Relic files
wget https://download.newrelic.com/newrelic/java-agent/newrelic-agent/current/newrelic-java.zip
# Unzip the newrelic-java.zip
unzip newrelic-java.zip # unzips to newrelic folder
rm -rf newrelic-java.zip
# Copy newrelic.jar and newrelic.yml
cp newrelic/newrelic.jar /usr/share/tomcat/newrelic/
cp newrelic/newrelic.yml /usr/share/tomcat/newrelic/
rm -rf newrelic
# Change mode and owner
sudo chmod 655 /usr/share/tomcat/newrelic/*
sudo chown -R tomcat:tomcat /usr/share/tomcat/newrelic/*
# Get New Relic license from AWS parameter store
NEWRELIC_LICENSE_KEY=$(aws ssm get-parameter --region=eu-west-1 --name=NEWRELIC_LICENSE --with-decryption --query=Parameter.Value --output text)
# Write New Relic ENV variables to setenv.sh
echo "export NEW_RELIC_APP_NAME=sherlock-holmes" >> /usr/share/tomcat/bin/setenv.sh
echo "export NEW_RELIC_LICENSE_KEY=$${NEWRELIC_LICENSE_KEY}" >> /usr/share/tomcat/bin/setenv.sh# Write JAVA_OPTS to setenv.sh
echo -e "JAVA_OPTS=\"\$JAVA_OPTS -javaagent:/usr/share/tomcat/newrelic/newrelic.jar -Dnewrelic.environment=${environment}\"" >> /usr/share/tomcat/bin/setenv.sh
# Change mode and owner
sudo chmod 655 /usr/share/tomcat/bin/setenv.sh
sudo chown -R tomcat:tomcat /usr/share/tomcat/bin/setenv.sh

You can pass the aws-user-data.tpl file as user_data to AWS Launch Configuration resource

resource "aws_launch_configuration" "sherlock_launch_configuration"{
/* setting other variables */
user_data = file("${path.module}/aws-user-data.tpl")
}

Now, the user data will run the script and provision New Relic Java agent, every time the instance is launched.

Thank you for reading!!
Stay tuned for more productionised solutions!!!

Happy Coding!!!

Interested to read more stories, you can find my writings here:

--

--

--

Full Stack Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Don’t OOP on my Cloud with your Class and Inheritance. I’m the Boy With No Dragon Tattoo.

Lights, Camera, Asteroid!

How to Setup MariaDB Master and Slave Replication on Ubuntu 18.04

Capabilities that a good feature toggle tool should have

Git is Hard, We’re Here to Help you GitBetter

API Management and DevOps

Bash — pipes and redirections

The Cost of Poor Contract Management in Revenue & Sales

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sravan Kadium

Sravan Kadium

Full Stack Developer

More from Medium

Java App stops running on AWS EC2 instance after some time!

From zero to hero — Deploy a scalable Java app on AWS App Runner ..

Super-Linter, what is it? and how can I use it in my CI CD process in GitHub

7-Steps to Create Jenkins Maven Project Job

List of different project and you need to choose the Maven project