New Relic for Spring Boot
Asked Answered
M

3

12

Recently, we convert a tomcat/spring app to spring boot. Everything is working fine apart from new relic. Is there a way I can easily config new relic with spring boot project. I don't want to hard code the location of new relic agent jar path, then run the spring boot project with the path.

edit: Spring boot project is with maven

Murvyn answered 13/11, 2014 at 5:30 Comment(2)
You may find this link useful: jdpgrailsdev.github.io/blog/2014/04/08/… This person is using Gradle instead of Maven, but he seems to have solved a similar problem, that is how to pass the new relic agent jar path to spring boot without hardcoding it.Merl
Thanks Toby_New_Relic. I saw this post before, but still can not figure out how to config with mavenMurvyn
D
12

You can include NewRelic Maven dependency and use maven-dependency-plugin to unpack in into your target/classes directory, which allows Maven to include it into final Jar file. Then you have to add Premain-Class attribute into manifest file and you can use your application jar as -javaagent source. You can find details on my blog post

Delightful answered 31/12, 2014 at 22:37 Comment(3)
I followed the instruction and it works perfectly. Thank you!Mancini
@Jakub Kubrybski, your blog post is out of date. Spring boot now packages class files in BOOT-INF/classes, which can't be loaded by the PreMain-Class ClassLoader.Hbeam
the new relic spring-boot guided install gets you to add stuff to pom file. but it still requires you hard code a path to nerelic.jar in command line. We have no idea what this path is or will be. Could you give some instructions on how to add premain-class to manifest? We dont know what this is unfortunatley.Cooney
A
9

Step by step instructions

  • Extract the files from the newrelic java agent archive.
  • Create a directory named newrelic in the root of your application.
  • Place the newrelic.jar from the archive in the above created newrelic folder
  • Place the newrelic.yml YAML config file in the above created newrelic folder.
  • Update the values in newrelic.yml as below.
    • license_key: 'your license key'
    • app_name: ‘Your application name’
  • Run you application by using the option javaagent
    • java -javaagent:newrelic\newrelic.jar -jar yourapplication.jar

-javaagent option needs to be before the -jar so the agent can start

Aceydeucy answered 2/11, 2016 at 17:52 Comment(2)
my spring app is not creating jar, in that how this we can achieve this. Also how can we use programmatically. any idea or any hint.vLoftis
Where do we get the java agent archive from? It seems to be missing rom all new relic documentation.Cooney
M
2

I was stuck with the same issue, here is what I figured out. I implemented the 2nd way for my applications. There are 3 ways to integrate New Relic with a Spring Boot Application-

  1. Using the Java Agent provided by New Relic
  2. Using New Relic's Micrometer Dependency
  3. Micormeter's New Relic Dependency

1. Configuration using Java Agent Provided By New Relic

  1. Download the Java Agent from this URL- https://docs.newrelic.com/docs/release-notes/agent-release-notes/java-release-notes/
  2. Extract it.
  3. Modify the newrelic.yml file inside the extracted folder to include your license_key: app_name:
  4. Create a SpringBoot application with some REST endpoints.
  5. Build the application.
  6. Navigate to the root path where you have extracted the newrelic java agent.
  7. Enter this command java -javagent:<path to your new relic jar>\newrelic.jar -jar <path to your application jar>\<you rapplication jar name>.jar

To view the application metrics-

  1. Log in to your New Relic account.
  2. Go to Explorer Tab.
  3. Click on Services-APM
  4. You can see the name of your application(which you had mentioned in the newrelic.yml file) listed there.
  5. Click on the application name.
  6. The dashboard should look something like this.

image

Using New Relic's Micrometer Dependency is the preferred way to do it.

2. Configuration using New Relic's Micrometer Dependency

  1. Add this dependency
<dependency>
        <groupId>com.newrelic.telemetry</groupId>
        <artifactId>micrometer-registry-new-relic</artifactId>
        <version>0.7.0</version>
    </dependency>
  1. Modify the MicrometerConfig.java class to add your API Key and Application name.
import java.net.InetAddress;
import java.net.UnknownHostException;

import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.newrelic.telemetry.Attributes;
import com.newrelic.telemetry.micrometer.NewRelicRegistry;
import com.newrelic.telemetry.micrometer.NewRelicRegistryConfig;

import java.time.Duration;
import io.micrometer.core.instrument.config.MeterFilter;
import io.micrometer.core.instrument.util.NamedThreadFactory;

@Configuration
@AutoConfigureBefore({ CompositeMeterRegistryAutoConfiguration.class, SimpleMetricsExportAutoConfiguration.class })
@AutoConfigureAfter(MetricsAutoConfiguration.class)
@ConditionalOnClass(NewRelicRegistry.class)
public class MicrometerConfig {

    @Bean
    public NewRelicRegistryConfig newRelicConfig() {
        return new NewRelicRegistryConfig() {
            @Override
            public String get(String key) {
                return null;
            }

            @Override
            public String apiKey() {
                return "your_api_key"; // for production purposes take it from config file
            }

            @Override
            public Duration step() {
                return Duration.ofSeconds(5);
            }

            @Override
            public String serviceName() {
                return "your_service_name"; // take it from config file
            }

        };
    }

    @Bean
    public NewRelicRegistry newRelicMeterRegistry(NewRelicRegistryConfig config) throws UnknownHostException {
        NewRelicRegistry newRelicRegistry = NewRelicRegistry.builder(config)
                .commonAttributes(new Attributes().put("host", InetAddress.getLocalHost().getHostName())).build();
        newRelicRegistry.config().meterFilter(MeterFilter.ignoreTags("plz_ignore_me"));
        newRelicRegistry.config().meterFilter(MeterFilter.denyNameStartsWith("jvm.threads"));
        newRelicRegistry.start(new NamedThreadFactory("newrelic.micrometer.registry"));
        return newRelicRegistry;
    }
}
  1. Run the application.

To view the Application metrics-

  1. Log in to your New Relic account.
  2. Go to Explorer Tab.
  3. Click on Services-OpenTelemetry
  4. You can see the name of your application(which you had mentioned in the MicrometerConfig file) listed there.
  5. Click on the application name.
  6. The dashboard should look something like this.

image

Here is the link to my original question.

Manxman answered 14/1, 2022 at 5:57 Comment(1)
We have not been able to figure out how to install new relic with spring-boot with the instructions provided by new relic (which say to put entries into pom), as we dont know what the class path is or will be. We have no heard of micrometer, is this something provided by new relic, or a third party?Cooney

© 2022 - 2024 — McMap. All rights reserved.