How to configure Java with New Relic At Platform.sh

Goal

This tutorial will explain how to configure New Relic into a Java application at Platform.sh.

Assumptions

  • You either have a Java application, and you want to run at Platform.sh or you already have a Java application running at Platform.sh

  • A text editor of your choice.

Problems

New Relic is a technology company that develops cloud-based software to help website and application owners track the performance of their services. That might be useful because you can track everything from performance issues to tiny errors within your code. Every minute the agent posts metric time slice and event data to the New Relic user interface, where the owner of that data can sign in and use the data to see how their website is performing.

Steps

To set up new-relic in the Java project, we have two ways:

  • Using the maven project
  • Download the code through application.app.yaml.

Maven

This section explains how to configure Maven to download and unzip the newrelic-java.zip file, which contains all New Relic Java agent components.

To set up the application with New Relic, you have two options:

To set up the application with New Relic, you have two options:

  1. Configuring and download from Maven
  2. Downloading on your own

Configure your pom.xml to download newrelic-java.zip. For example:

<dependency>
     <groupId>com.newrelic.agent.java</groupId>
     <artifactId>newrelic-java</artifactId>
      <version>JAVA_AGENT_VERSION</version>
     <scope>provided</scope>
     <type>zip</type>
</dependency>

Replace JAVA_AGENT_VERSION with the latest Java agent version.

Unzip newrelic-java.zip by configuring maven-dependency-plugin in your pom.xml. For example:

   <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>3.1.1</version>
        <executions>
          <execution>
            <id>unpack-newrelic</id>
            <phase>package</phase>
            <goals>
              <goal>unpack-dependencies</goal>
            </goals>
            <configuration>
              <includeGroupIds>com.newrelic.agent.java</includeGroupIds>
              <includeArtifactIds>newrelic-java</includeArtifactIds>
              <excludes>**/newrelic.yml</excludes>
              <overWriteReleases>false</overWriteReleases>
              <overWriteSnapshots>false</overWriteSnapshots>
              <overWriteIfNewer>true</overWriteIfNewer>
              <outputDirectory>${project.build.directory}</outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>

The next step is to configure the .platform.app.yaml file to:

  1. Set the agent in the JVM parameters
  2. Overwrite the application file with the proper license key and application name. You can also do it using the API or the web UI. Therefore this configuration will work outside the code very useful when the application is on a public repository.
name: app
type: 'java:8'
disk: 1024
hooks:
    build: |
        mvn clean package
        rm -rf newrelic/
        mv target/newrelic/ newrelic/

mounts:
    'server/':
        source: local
        source_path: server_source

variables:
    env:
        NEW_RELIC_LICENSE_KEY: <LICENSE_KEY>
        NEW_RELIC_APP_NAME: <NAME_APPLICATION>

web:
    commands:
        start: |
            cp target/dependency/webapp-runner.jar server/webapp-runner.jar
            cp target/tomcat.war server/tomcat.war
             java -jar \
            -Xmx$(jq .info.limits.memory /run/config.json)m -XX:+ExitOnOutOfMemoryError \
            -javaagent:/app/newrelic/newrelic.jar //the other parameters here
            

Manual installation

To use this installation it is only required that you modify .platform.app.yaml , which will download and set the New Relic Java agent for you.

name: app
type: 'java:8'
disk: 1024


variables:
    env:
        NEW_RELIC_URL: https://download.newrelic.com/newrelic/java-agent/newrelic-agent/current/newrelic-java.zip
        NEW_RELIC_LICENSE_KEY: <LICENSE_KEY>
        NEW_RELIC_APP_NAME: <NAME_APPLICATION>

hooks:
    build: |
      mvn clean package
      rm -rf newrelic
      curl -O $NEW_RELIC_URL
      unzip newrelic-java.zip


web:
    commands:
        start: |
            java -jar \
            -Xmx$(jq .info.limits.memory /run/config.json)m \
            -XX:+ExitOnOutOfMemoryError \
            -javaagent:/app/newrelic/newrelic.jar //the left of the commands here

References