首先,添加以下依赖
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
只需在src/main/resources/application.properties
以上是如何将仪表数据添加到 Spring Boot Actuator 指标端点中的详细内容。更多信息请关注PHP中文网其他相关文章!