DEV Community

Salad Lam
Salad Lam

Posted on

How to add a meter data into Spring Boot Actuator metrics endpoint

Step

First, to add the following dependency

pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- for Prometheus endpoint -->
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
Enter fullscreen mode Exit fullscreen mode

Then to enable endpoints through web access

src/main/resources/application.properties

management.endpoints.web.exposure.include=prometheus,metrics
Enter fullscreen mode Exit fullscreen mode

Add a singleton component

@Component
public class MyMetrics implements MeterBinder {

    private final Random random = new Random();
    private final AtomicInteger counter = new AtomicInteger();

    @Override
    public void bindTo(MeterRegistry registry) {
        // can any value
        Gauge.builder("mymetrics.gauge", this, MyMetrics::getRandom)
                .description("MyMetrics gauge")
                .register(registry);

        // must the same or greater than the value last got, or reset to zero on restart
        FunctionCounter.builder("mymetrics.counter", this, MyMetrics::counterGetAndIncrement)
                .description("MyMetrics counter")
                .register(registry);
    }

    public double getRandom() {
        return random.nextDouble();
    }

    public int counterGetAndIncrement() {
        return counter.getAndIncrement();
    }

}
Enter fullscreen mode Exit fullscreen mode

Finally, metric data can be accessed through

/actuator/metrics/mymetrics.counter

{"name":"mymetrics.counter","description":"MyMetrics counter","baseUnit":null,"measurements":[{"statistic":"COUNT","value":9.0}],"availableTags":[]}
Enter fullscreen mode Exit fullscreen mode

/actuator/metrics/mymetrics.gauge

{"name":"mymetrics.gauge","description":"MyMetrics gauge","baseUnit":null,"measurements":[{"statistic":"VALUE","value":0.7618330619753056}],"availableTags":[]}
Enter fullscreen mode Exit fullscreen mode

/actuator/prometheus

# HELP mymetrics_counter_total MyMetrics counter
# TYPE mymetrics_counter_total counter
mymetrics_counter_total 8.0
# HELP mymetrics_gauge MyMetrics gauge
# TYPE mymetrics_gauge gauge
mymetrics_gauge 0.1346348968727723
Enter fullscreen mode Exit fullscreen mode

Beans

Endpoints

Endpoints are annotated with org.springframework.boot.actuate.endpoint.annotation.Endpoint, and they are beans and managed by Spring's BeanFactory.

Endpoint Class Created by
metrics org.springframework.boot.actuate.metrics.MetricsEndpoint org.springframework.boot.actuate.autoconfigure.metrics.MetricsEndpointAutoConfiguration#metricsEndpoint
prometheus org.springframework.boot.actuate.metrics.export.prometheus.PrometheusScrapeEndpoint org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus.PrometheusMetricsExportAutoConfiguration.PrometheusScrapeEndpointConfiguration#prometheusEndpoint

org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping class

This is created by org.springframework.boot.actuate.autoconfigure.endpoint.web.servlet.WebMvcEndpointManagementContextConfiguration#webEndpointServletHandlerMapping. Endpoints information is provided by org.springframework.boot.actuate.endpoint.web.annotation.WebEndpointDiscoverer.

The mapping between path and endpoints will be registered in org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping#initHandlerMethods.

It implements org.springframework.web.servlet.HandlerMapping interface, so the instance will be registered into DispatcherServlet during instance construction.

Expose endpoints to JMX

Just add a line into src/main/resources/application.properties

spring.jmx.enabled=true
Enter fullscreen mode Exit fullscreen mode

Association of the endpoints with MBeanServer is by org.springframework.boot.actuate.autoconfigure.endpoint.jmx.JmxEndpointAutoConfiguration.

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Heroku

This site is powered by Heroku

Heroku was created by developers, for developers. Get started today and find out why Heroku has been the platform of choice for brands like DEV for over a decade.

Sign Up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay