Introduction
This article explains how metrics can be pushed from a Spring Boot application to New Relic One platform. One of my client wanted to use New Relic One, since I was not familiar with it, I did this small work in order to see how to push my application metrics there.
Versions
- Java 11
- Spring Boot 2.6.6
New Relic One
Go to https://newrelic.com/platform
and sign in. The storage of my data will happen in the US (I don’t know if choosing Europe will have any impact).
You will need an insert key (license key) so your metrics can be pushed to New Relic One platform, this key will be used as a value of this argument -Dnewrelic.apiKey
in our example later on.
Once you are logged to the New Relic One website, no need to configure anything, type the https://newrelic.com
address in your browser so you can access your license key.
Copy the license key (take the INGEST — LICENSE) from below:
Read more about license keys: https://docs.newrelic.com/docs/apis/intro-apis/new-relic-api-keys/
Code
Find the code here, maven is needed for the compilation:
Clone the project then run:
cd springboot-newrelic
mvn clean install
Then run:
java -Dnewrelic.enabled=true -Dnewrelic.apiKey=<your api key here> -Dnewrelic.serviceName=MySpringBootApp -jar ./target/newrelic-0.0.1-SNAPSHOT.jar
Make sure you already have the required api key.
Metrics example
The NewRelicRegistry
configuration is done in the class NewrelicMicrometerConfig
class, this is where we define the registry that will collect our metrics and push them to the New Relic One platform.
We defined three types of metrics:
demo.counter
that keeps increasing, defined inDemoMicrometer
.demo.gauge
that increases / decreases and stays in the [0,500[ interval, defined inDemoMicrometer
.greetings
that is related to the REST endpoint defined in theDemoController
class.
Those metrics are defined with micrometer.
Note that I left the logging level to TRACE in the file logback-spring.xml, this will show all the interactions between the application and New Relic One:
<logger name="com.newrelic.telemetry" level="TRACE" />
Interesting links
- https://newrelic.com/blog/how-to-relic/how-to-monitor-spring-boot-applications-using-micrometer-metrics
- https://discuss.newrelic.com
- https://github.com/arpan-banerjee7/springboot-micrometer-newrelic-demo
- https://stackoverflow.com/questions/69694779/integrate-new-relic-with-spring-boot-using-micrometer
- https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#actuator.metrics.supported.timed-annotation
- https://ordina-jworks.github.io/microservices/2017/09/17/monitoring-your-microservices-with-micrometer.html
Conclusion
Note that I did not use any agent here, some of the documentation related to New Relic One will describe how to setup the agent.
Also I used the following dependency in my pom.xml in order to bridge New Relic and Micrometer:
<dependency>
<groupId>com.newrelic.telemetry</groupId>
<artifactId>micrometer-registry-new-relic</artifactId>
<version>0.7.0</version>
</dependency>
You might find documentation / tutorials using other dependencies in order to push the metrics to New Relic One. I could try that in a near future.
Enjoy !