First, to add the following dependency
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>
Then to enable endpoints through web access
src/main/resources/application.properties
management.endpoints.web.exposure.include=prometheus,metrics
Add a singleton component
@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(); } }
Finally, metric data can be accessed through
/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
Endpoints are annotated with org.springframework.boot.actuate.endpoint.annotation.Endpoint, and they are beans and managed by Spring's BeanFactory.
Endpoint | Class | Created by | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
metrics |
|
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 |
This is created by org.springframework.boot.actuate.autoconfigure.endpoint.web.servlet.WebMvcEndpointManagementContextConfiguration#webEndpointServletHandlerMapping
. Endpoints information is provided byorg.springframework.boot.actuate.endpoint.web.annotation.WebEndpointDiscoverer. The mapping between path and endpoints will be registered in org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping#initHandlerMethods
.org.springframework.web.servlet.HandlerMapping interface, so the instance will be registered into DispatcherServlet
during instance construction.
spring.jmx.enabled=true
Just add a line into src/main/resources/application.properties
The above is the detailed content of How to add a meter data into Spring Boot Actuator metrics endpoint. For more information, please follow other related articles on the PHP Chinese website!