首先,加入以下依賴
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.autoconfigure.metrics.export.prometheus.PrometheusMetricsExportAutoConfiguration.PrometheusScrapeEndpointConfiguration#prometheusEndpoint
這是由 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
以上是如何將儀表資料新增至 Spring Boot Actuator 指標端點中的詳細內容。更多資訊請關注PHP中文網其他相關文章!