Rumah >Java >javaTutorial >Cara menambah data meter ke titik akhir metrik Spring Boot Actuator
Pertama, untuk menambah kebergantungan berikut
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>
Kemudian untuk mendayakan titik akhir melalui akses web
src/main/resources/application.properties
management.endpoints.web.exposure.include=prometheus,metrics
Tambah komponen tunggal
@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(); } }
Akhir sekali, data metrik boleh diakses melalui
/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":[]}
/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
Titik tamat diberi anotasi dengan org.springframework.boot.actuate.endpoint.annotation.Endpoint dan ia adalah kacang dan diuruskan oleh Spring's 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
Ini dicipta oleh org.springframework.boot.actuate.autoconfigure.endpoint.web.servlet.WebMvcEndpointManagementContextConfiguration#webEndpointServletHandlerMapping
. Maklumat titik akhir disediakan olehorg.springframework.boot.actuate.endpoint.web.annotation.WebEndpointDiscoverer. Pemetaan antara laluan dan titik akhir akan didaftarkan dalam org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping#initHandlerMethods
.org.springframework.web.servlet.HandlerMapping, jadi contoh akan didaftarkan ke dalam DispatcherServlet
semasa pembinaan contoh.
spring.jmx.enabled=trueDedahkan titik akhir kepada JMX
Hanya tambah satu baris ke dalam src/main/resources/application.properties
Atas ialah kandungan terperinci Cara menambah data meter ke titik akhir metrik Spring Boot Actuator. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!