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-
- Using the Java Agent provided by New Relic
- Using New Relic's Micrometer Dependency
- Micormeter's New Relic Dependency
1. Configuration using Java Agent Provided By New Relic
- Download the Java Agent from this URL- https://docs.newrelic.com/docs/release-notes/agent-release-notes/java-release-notes/
- Extract it.
- Modify the newrelic.yml file inside the extracted folder to include your
license_key:
app_name:
- Create a SpringBoot application with some REST endpoints.
- Build the application.
- Navigate to the root path where you have extracted the newrelic java agent.
- 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-
- Log in to your New Relic account.
- Go to Explorer Tab.
- Click on Services-APM
- You can see the name of your application(which you had mentioned in the newrelic.yml file) listed there.
- Click on the application name.
- The dashboard should look something like this.
Using New Relic's Micrometer Dependency is the preferred way to do it.
2. Configuration using New Relic's Micrometer Dependency
- Add this dependency
<dependency>
<groupId>com.newrelic.telemetry</groupId>
<artifactId>micrometer-registry-new-relic</artifactId>
<version>0.7.0</version>
</dependency>
- 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;
}
}
- Run the application.
To view the Application metrics-
- Log in to your New Relic account.
- Go to Explorer Tab.
- Click on Services-OpenTelemetry
- You can see the name of your application(which you had mentioned in the MicrometerConfig file) listed there.
- Click on the application name.
- The dashboard should look something like this.
Here is the link to my original question.