首頁  >  文章  >  Java  >  如何將儀表資料新增至 Spring Boot Actuator 指標端點中

如何將儀表資料新增至 Spring Boot Actuator 指標端點中

Mary-Kate Olsen
Mary-Kate Olsen原創
2024-11-01 20:16:02286瀏覽

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