Home > Java > javaTutorial > body text

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

Mary-Kate Olsen
Release: 2024-11-01 20:16:02
Original
294 people have browsed it

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>
Copy after login

Then to enable endpoints through web access

src/main/resources/application.properties

management.endpoints.web.exposure.include=prometheus,metrics
Copy after login

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();
    }

}
Copy after login

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":[]}
Copy after login

/actuator/metrics/mymetrics.gauge

{"name":"mymetrics.gauge","description":"MyMetrics gauge","baseUnit":null,"measurements":[{"statistic":"VALUE","value":0.7618330619753056}],"availableTags":[]}
Copy after login

/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
Copy after login

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
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.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.

spring.jmx.enabled=true
Copy after login
Expose endpoints to JMX

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

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

The above is the detailed content of How to add a meter data into Spring Boot Actuator metrics endpoint. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!