DZone
Performance Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Performance Zone > How to Enable the Java New Relic Agent on Heroku

How to Enable the Java New Relic Agent on Heroku

Read on to learn how to use enable a Java New Relic agent on Heroku using either the Heroku Maven plugin or a Heroku deploy jar.

Sharath Honnaiah user avatar by
Sharath Honnaiah
·
Dec. 02, 16 · Performance Zone · Tutorial
Like (5)
Save
Tweet
5.20K Views

Join the DZone community and get the full member experience.

Join For Free

Identifying performance bottlenecks for microservices-based applications becomes slightly easier with the help of monitoring tools like New Relic and Dynatrace. This article provides information on how to set up a Java New Relic agent on Heroku with an example.

Getting started, there are few prerequisites needed to run the sample Spring boot example and deploy the same on Heroku. They are as follows:

  • A Heroku account.

  • Heroku CLI.

  • A login to Heroku using CLI.

  • JDK 1.8.

  • Maven 3.3.3.

  • A Heroku app with the New Relic add-on.

Here's a sample Spring boot application project structure diagram:

heroku-samples folder structure

The sample application is a simple rest service built using the Spring boot.

The rest controller code is:

package microservice;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SampleRestController {

@RequestMapping("/ms")
public String echo() {
return "Sample Rest Service";
}
}

The Spring boot application startup code is:

package microservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SampleApplication {

public static void main(String[] args) {
SpringApplication.run(SampleApplication.class, args);
}
}

Test Class code is:

package microservice;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class SampleRestControllerTest {
@Autowired
    private MockMvc mockMvc;

@Test
    public void noParamGreetingShouldReturnDefaultMessage() throws Exception {

        this.mockMvc.perform(get("/ms")).andDo(print()).andExpect(status().isOk())
                .andExpect(jsonPath("$").value("Sample Rest Service"));
    }

}

Project main pom.xml file is:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.sample</groupId>
<artifactId>microservice</artifactId>
<version>1.0.0</version>
<name>Sample Rest Service</name>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <scope>test</scope>
        </dependency>
</dependencies>

<properties>
<java.version>1.8</java.version>
</properties>


<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>

To build the project just, run the below command in terminal:

mvn clean install

Setting Up New Relic

You can download the New Relic jar and YML files from the URL and copy them to project folder heroku-samples/src/deploy/newrelic.

https://download.newrelic.com/newrelic/java-agent/newrelic-agent/3.33.0/

Edit the newrelic.YML file to update the license key. Once the New Relic Heroku add-on has been added, go to the Heroku app settings and copy the config var NEW_RELIC_LICENSE_KEY and specify the application name as shown below. For the rest of the properties, you can leave the default values:

  license_key: '743a3736d6a98ef9f43dba8a903697142fcc1c6a'
  app_name: SAMPLE_MICROSERVICE
development:
  <<: *default_settings
  app_name: SAMPLE_MICROSERVICE

test:
  <<: *default_settings
  app_name: SAMPLE_MICROSERVICE

production:
  <<: *default_settings

staging:
  <<: *default_settings
  app_name: SAMPLE_MICROSERVICE

Login to Heroku via CLI:

heroku login
enter email id and password of your heroku account when prompted

Enabling the Java New Relic Agent on Heroku

There are two ways to enable Java New Relic agent on Heroku.

1. Include the New Relic Agent to an Executable Jar

The profile content is:

web: java $JAVA_OPTS -Dserver.port=$PORT -jar microservice-1.0.0.jar

The herokupush content is shown below. (Here, the Heroku app name is java-heroku-newrelic-sample, so in your case, please change it to your Heroku app name in the herokupush script file.)

#!/bin/bash -x

rm microservice-1.0.0.jar
rm newrelic.jar
rm newrelic.yml
cp -v ../../../target/microservice-1.0.0.jar .
cp -v ../newrelic/newrelic.jar .
cp -v ../newrelic/newrelic.yml .
heroku deploy:jar -j microservice-1.0.0.jar -i newrelic.jar:newrelic.yml --app  java-heroku-newrelic-sample

Follow these steps:

Build the project first from project root dir 
  mvn clean install
cd src/deploy/heroku-executable-deploy
  sh herokupush

Upon successful Heroku deployment, we can see console output as shown below:

Console Output:

rm: newrelic.jar: No such file or directory
rm: newrelic.yml: No such file or directory
../../../target/microservice-1.0.0.jar -> ./microservice-1.0.0.jar
../newrelic/newrelic.jar -> ./newrelic.jar
../newrelic/newrelic.yml -> ./newrelic.yml
Uploading microservice-1.0.0.jar
-----> Packaging application...
       - app: java-heroku-newrelic-sample
       - including: newrelic.jar
       - including: newrelic.yml
       - including: microservice-1.0.0.jar
-----> Creating build...
       - file: slug.tgz
       - size: 19MB
-----> Uploading build...
       - success
-----> Deploying...
remote: 
remote: -----> heroku-deploy app detected
remote: -----> Installing OpenJDK 1.8... done
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote: 
remote: -----> Compressing...
remote:        Done: 67.8M
remote: -----> Launching...
remote:        Released v42
remote:        https://java-heroku-newrelic-sample.herokuapp.com/ deployed to Heroku
remote: 
-----> Done

The application can be accessed using URL as shown below:

https://java-heroku-newrelic-sample.herokuapp.com/ms

here java-heroku-newrelic-sample is the heroku app name so please change it to your heroku app name.
  ex: https://<herokuappname>.herokuapp.com/ms

2. Include the New Relic Agent via the Heroku Maven Plugin

The pom.xml content (heroku-samples/src/deploy/maven-heroku-deploy) is:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.sample</groupId>
  <artifactId>microservice</artifactId>
  <version>1.0.0</version>
  <packaging>jar</packaging>
  <name>Heroku new relic deployment</name>
    <url>http://maven.apache.org</url>
    <properties> 
        <maven.resources.overwrite>true</maven.resources.overwrite> 
</properties>

  <build>
    <plugins>
      <plugin>
        <groupId>com.heroku.sdk</groupId>
        <artifactId>heroku-maven-plugin</artifactId>
        <version>1.0.3</version>
          <configuration>
              <appName>java-heroku-newrelic-sample</appName>
              <jdkVersion>1.8</jdkVersion>
              <processTypes>
                  <web>java $JAVA_OPTS -Dserver.port=$PORT -jar target/microservice-1.0.0.jar</web>
              </processTypes>
              <configVars>
                  <JAVA_OPTS>-javaagent:target/newrelic.jar</JAVA_OPTS>
              </configVars>
              <includes>
  <include>*</include>
  </includes>
          </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>3.0.1</version>
        <executions>
          <execution>
            <id>copy-resources</id>
            <phase>validate</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <outputDirectory>${basedir}/target/</outputDirectory>
              <resources>          
                <resource>
                  <directory>../../../target/</directory>
                    <includes>
             <include>microservice-1.0.0.jar</include>
          </includes>
                </resource>
                <resource>
                  <directory>../newrelic/</directory>
                    <includes>
             <include>newrelic.jar</include>
             <include>newrelic.yml</include>
          </includes>
                </resource>
              </resources>              
            </configuration>            
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Run the Maven command as shown below:

mvn -e clean heroku:deploy

On successful Heroku deployment, you can access the rest URL shown below:

https://java-heroku-newrelic-sample.herokuapp.com/ms

PS Note: replace java-heroku-newrelic-sample with your heroku app name

Try to hit the above url for few number of times so that some metrics is captured by newrelic agent. 

Access the New Relic dashboard to view the performance metrics information by logging into heroku.com, clicking on respective Heroku app, clicking the Resources tab, and then clicking the New Relic link under the add-ons section.

The github code base url is https://github.com/sharathh/heroku-samples

git clone https://github.com/sharathh/heroku-samples

References

  • New Relic 

  • Heroku 

Java (programming language) Spring Framework application Spring Boot app Apache Maven Command-line interface

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Exhaustive JUNIT5 Testing with Combinations, Permutations, and Products
  • JIT Compilation of SQL in NoSQL
  • The Power of Enum: Make Your Code More Readable and Efficient [Video]
  • 8 Must-Have Project Reports You Can Use Today

Comments

Performance Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo