Daniel Fitzgerald, a Senior Solutions Consultant at New Relic, contributed to the demo project in this post.

Open source monitoring has made it easier than ever to get metrics from any service. But if you’re not careful, all those metrics can quickly lead to data silos. The good news is that they don’t have to because you can bring all of your data, including telemetry data from open source tools, together in New Relic One. This includes metrics data from services built on the Spring Boot framework. With the New Relic Micrometer registry, you can now use Spring Boot Micrometer metrics in New Relic One to monitor your services.

This post explains how the New Relic Micrometer registry works, and how to install and use it to send metrics to New Relic from an example application.

About Micrometer and registries

Micrometer is a library for collecting metrics from JVM-based applications and services and is included in Spring Boot 2 and backported to Spring Boot 1.3+. If you use Spring Boot to write services, you automatically get metric instrumentation for many Spring Boot component libraries, including Spring MVC and RestTemplate latencies, cache utilization, datastore utilization, and many more.

In Micrometer, you collect metrics about your application using an interface called a Meter. Meters, according to their documentation, are “created from and held in a MeterRegistry.” You can then register one or more MeterRegistry implementations to send metrics to a monitoring system, such as New Relic. This approach decouples your metric instrumentation from the metrics backend you use, and it lets you register multiple MeterRegistry implementations to send the same metrics to multiple backends.

If you’re unfamiliar with Micrometer, their concepts documentation is a great place to learn more about metric naming, tagging, filtering, and other central concepts about gathering metrics with Micrometer.

Micrometer’s New Relic registry vs. New Relic’s Micrometer registry

If you’re familiar with Micrometer, you may have previously used the first version of the New Relic Micrometer registry included in the Micrometer project. That New Relic registry version sent metrics to New Relic Insights as events where you could use dashboards to visualize metrics.

This version of the registry uses New Relic’s new Telemetry platform which offers three essential capabilities:

  1. Native support for New Relic Dimensional metrics: All of the metrics sent via this registry go to New Relic’s metrics system where you can query them using metric-specific features rather than querying Insights events that the previous version created.
  2. Entities created from the Micrometer metrics: The metric payload includes the name of the service from where the metrics were generated. Using that name, New Relic creates a service entity that you can find in the entity explorer.
  3. A default overview that captures key Micrometer metrics: Now that the New Relic platform recognizes Spring Boot Micrometer metrics and creates a service entity for each service, you also get a curated dashboard with the key metrics for that service. You can use this as a starting point to build more complex dashboards that include information specific to your service.

How the New Relic Micrometer registry works

The New Relic Micrometer registry is built on the New Relic Java Telemetry SDK, an open source set of API client libraries that send your metric and trace data to the New Relic platform.

After you register the New Relic Micrometer MeterRegistry implementation in your application’s  registry, it begins sending metrics to New Relic. New Relic observes the incoming metrics and automatically registers a service entity in New Relic’s entity system and creates a default overview page with key performance indicators like response time, throughput, error rate, and system metrics like CPU utilization and other JVM metrics.

You can choose to use the New Relic Micrometer registry as a standalone, lightweight approach to monitoring Spring Boot applications, or you can also use the Micrometer registry alongside the New Relic Java agent.

Getting started with the New Relic Micrometer registry

Setting up the New Relic Micrometer registry to send your Spring Boot Micrometer metrics to New Relic is relatively straightforward. You can find full implementation details in the project’s GitHub README.

You can use the registry within a Spring Boot application or in any application that uses micrometer for recording metrics. Note that to send metrics to New Relic, you’ll need an Insert API key.

  1. To add the New Relic Micrometer dependency to your project, choose one of the following methods:
    • Via Gradle
      implementation 'com.newrelic.telemetry:micrometer-registry-new-relic:0.5.0'
    • Via Maven
      <dependency>
      
          <groupId>com.newrelic.telemetry</groupId>
      
          <artifactId>micrometer-registry-new-relic</artifactId>
      
          <version>0.5.0</version>
      
      </dependency>
  2. Register the io.micrometer.newrelic.NewRelicRegistry. To do so, you’ll need to create an io.micrometer.NewRelicRegistryConfig.See the Spring-Config-Example for full implementation details.

Example: Use the New Relic Micrometer registry with the Spring Pet Clinic Microservices Demo

The Spring Pet Clinic Microservices Demo is a popular demo application that showcases a way to build a containerized Spring Boot microservices application. It includes Prometheus and Grafana for storing and visualizing Micrometer metrics as well as Zipkin to store and view distributed traces.

In the following steps, we’ll add the New Relic Micrometer registry to one of the microservices and see what the experience looks like in New Relic.

Prerequisites

  1. Download or clone the Spring Boot Spring Boot Microservices Demo project. You’ll find the source code and supporting files for seven services that make up the demo application.
  2. You’ll need your Insert API key.
  3. You’ll need Docker on your system.

Note: You can view the source code for the completed project in this repository.

Step 1. Configure the API Gateway service to send metrics to New Relic

  1. Add the New Relic Micrometer registry dependency. Navigate to [project-folder]/spring-petclinic-api-gateway/pom.xml and add the following to the <dependencies> block:
    <!-- New Relic Micrometer metrics -->
    
    <dependency>
    
        <groupId>com.newrelic.telemetry</groupId>
    
        <artifactId>micrometer-registry-new-relic</artifactId>
    
        <version>0.5.0</version>
    
    </dependency>
  2. In [project-folder]/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic, create a new directory named newrelic.
  3. In the newrelic directory, create a new file named MicrometerConfig.java.
  4. Copy the contents of the MicrometerConfig.java from my repo into your version of MicrometerConfig.java.This class uses the Spring Boot auto-configuration annotations to register the New Relic Micrometer registry.

Step 2. Modify the Spring Boot application to scan for our new autoconfigured class

  1. Open and edit the following file: [project-folder]/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/ApiGatewayApplication.java
  2. Find the @SpringBootApplication annotation, and modify it to scan for our class: @SpringBootApplication(scanBasePackages = {"org.springframework.samples.petclinic"})

Step 3. Modify the build to include the Insert API Key and Metric API URI

  1. Modify the top-level pom.xml to include the build arguments we need to pass as environment variables to the running containers. Navigate to [project-folder]/pom.xml, and in the <buildArgs> block add the following new arguments:
    <NR_INSERT_API_KEY>${env.NR_INSERT_API_KEY}</NR_INSERT_API_KEY>
    
    <NR_METRIC_URI>${env.NR_METRIC_URI}</NR_METRIC_URI>
  2. Modify Dockerfile to add the environment variables we need. Navigate to [project-folder]/docker/Dockerfile, and after the line with ENV SPRING_PROFILES_ACTIVE docker command, add the following to make the environment variables available to the running container:
    # Configure New Relic API key and endpoints
    
    ARG NR_INSERT_API_KEY
    
    ENV NR_INSERT_API_KEY=${NR_INSERT_API_KEY}
    
    
    
    ARG NR_METRIC_URI
    
    ENV NR_METRIC_URI=${NR_METRIC_URI}

Step 4. Build and run the project

  1. Set the environment variables and use Maven to build the project.

    Modify the following command to use your own Insert API Key. You can change the Metric URI value to use the EU region URI:

    NR_INSERT_API_KEY=[YOUR INSERT API KEY HERE] \
    
    NR_METRIC_URI=https://metric-api.newrelic.com/metric/v1 \
    
    ./mvnw clean install -PbuildDocker
  2. Run the project using Docker compose:
    docker-compose up

    It’ll take a couple of minutes for all the services to start, and you can ignore the “Problem with dial…” errors. Once the discovery services are up, those errors will cease.

Step 5. View the api-gateway service in New Relic

  1. Generate some traffic on the Pet Clinic website by visiting http://localhost:8080/.
  2. Navigate to New Relic One, and select the entity explorer.The api-gateway service should be listed in the Services section of the entity explorer.The api-gateway service listed in the Services section of the entity explorer.
  3. Click the api-gateway service. New Relic automatically detects the telemetry as Spring Boot Micrometer metrics and creates a default summary page using those metrics. The service summary includes charts for response time, throughput, and error rate, as well as charts showing JVM metrics. You can also group and filter the charts by the dimensions available on the metrics.New Relic automatically detects the telemetry as Spring Boot Micrometer metrics and creates a default summary page using those metrics
  4. Create your own charts.

    Use the data explorer to choose metrics to chart and build your own dashboard with the metrics coming from the api-gateway.Chart showing micrometer metrics

Step 6. Add the New Relic Micrometer registry to the rest of the services

  1. Follow the instructions in Step 1 and Step 2 for the remaining services in the application.
  2. Follow Step 4 to rebuild and run the services.

Making instrumentation ubiquitous

With New Relic Metrics and New Relic Traces, you can send telemetry data to New Relic, so you can query it and create custom dashboards that tie your application-centric data to your business KPIs.

As part of our open instrumentation initiative, we’re participating in the OpenTelemetry project. It aims to make instrumentation ubiquitous, so you can make the most of your telemetry data for easier and more effective observability. We know you need to ingest and make use of telemetry no matter where it’s from—whether it’s New Relic agents, OpenTelemetry, or other popular telemetry sources like Zipkin, Micrometer, and others. In addition to participating in OpenTelemetry, we have built integrations for other popular open source instrumentation tools like Dropwizard and Kamon.How to use Micrometer Metrics

We plan on evolving New Relic’s out-of-the-box support for Spring Boot Micrometer metrics. If you have ideas, feel free to hit the feedback button at the top-right corner in New Relic One, contribute to the project via New Relic Open Source.