首页 >Java >java教程 >如何将仪表数据添加到 Spring Boot Actuator 指标端点中

如何将仪表数据添加到 Spring Boot Actuator 指标端点中

Mary-Kate Olsen
Mary-Kate Olsen原创
2024-11-01 20:16:02393浏览

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

首先,添加以下依赖

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>

然后通过 Web 访问启用端点

src/main/resources/application.properties

management.endpoints.web.exposure.include=prometheus,metrics

添加单例组件

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

}

最后,可以通过

访问指标数据

/actuator/metrics/mymetrics.counter

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

/actuator/metrics/mymetrics.gauge

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

/执行器/普罗米修斯

# 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

豆子

端点

端点用org.springframework.boot.actuate.endpoint.annotation.Endpoint注释,它们是bean并由Spring的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.metrics.MetricsEndpoint

org.springframework.boot.actuate.autoconfigure.metrics.MetricsEndpointAutoConfiguration#metricsEndpoint

普罗米修斯 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 类

这是由 org.springframework.boot.actuate.autoconfigure.endpoint.web.servlet.WebMvcEndpointManagementContextConfiguration#webEndpointServletHandlerMapping

创建的。端点信息由

org.springframework.boot.actuate.endpoint.web.annotation.WebEndpointDiscoverer. 提供 路径和端点之间的映射将在 org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping#initHandlerMethods

.

中注册

它实现了

org.springframework.web.servlet.HandlerMapping 接口,因此实例在实例构建过程中会被注册到 DispatcherServlet
中。

spring.jmx.enabled=true
将端点公开给 JMX

只需在src/main/resources/application.properties

中添加一行 端点与 MBeanServer 的关联是通过 org.springframework.boot.actuate.autoconfigure.endpoint.jmx.JmxEndpointAutoConfiguration.

以上是如何将仪表数据添加到 Spring Boot Actuator 指标端点中的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn