Metrics 2.X in Spring Boot 2.X
Spring Boot offers a metrics endpoint that you can use diagnostically to analyze the metrics gathered by the application. Keeping track of metrics for Spring Boot 2.x.
Join the DZone community and get the full member experience.
Join For FreeSpring Boot offers a metrics endpoint that you can use diagnostically to analyze the metrics gathered by the application.
Adding the Dependency Inside POM Is the First Step
A meter is an interface for gathering a bunch of estimations (which we separately call measurements) about your application. spring-measurements loads with an upheld set of Meter natives including Timer, Counter, Gauge, DistributionSummary, and LongTaskTimer. Note that diverse meter types bring about an alternate number of measurements. For instance, while there is a solitary metric that addresses a Gauge, a Timer estimates both the number of coordinated occasions and the absolute season of all occasions planned.
<dependency>
<groupId>org.springframework.metrics</groupId>
<artifactId>spring-metrics</artifactId>
<version>${metrics.version}</version>
</dependency>
Create Registry Configuration
A library makes and deals with your application's arrangement of meters. Exporters utilize the meter library to emphasize the arrangement of meters instrumenting your application, and afterward further repeat over each meter's measurements, for the most part bringing about a period series in the measurements backend for every mix of measurements and measurements.
Three meter library executions are given out-of-the-case: SpectatorMeterRegistry, PrometheusMeterRegistry, and SimpleMeterRegistry. The libraries are for the most part made unexpectedly by choosing a backend through @EnableAtlasMetrics, @EnablePrometheusMetrics, and so on.
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import io.micrometer.core.aop.TimedAspect;
import io.micrometer.core.instrument.MeterRegistry;
@configuration
public class RegistryConfig {
@Bean
MeterRegistryCustomizer<MeterRegistry> metricsCommonTags() {
return registry -> registry.config().commonTags("region", "someRegionName");
}
@Bean
TimedAspect timedAspect(MterRegistry registry) {
return new TimedAspect(reegistry);
}
}
Whenever we want to check the metrics of any method we only have to add the @Timed above the method and we can keep track of the method.
Add @Timed to the Method
@Timed("mockMetricsSomeData")
public String getSomeData(){
return "mockData":
}
Call the URL With Metrics
You will get the desired response from the metrics URL and we can keep these records and use them for creating a dashboard for any threshold value.
{
"names": [
"jvm.memory.committed",
"jvm.gc.concurrent.phase.time",
"jvm.buffer.total.capacity",
"logback.events",
"jvm.memory.max",
"jvm.buffer.memory.used",
"jvm.classes.loaded",
"tomcat.threads.config.max",
"http.server.requests",
"metrometric.interval.gc.executiontimemillis",
"jvm.memory.used",
"jvm.classes.unloaded",
"tomcat.sessions.active.current",
"tomcat.global.error",
"metrometric.interval.gc.count",
"tomcat.sessions.alive.max",
"jvm.gc.live.data.size",
"metrometric.heap.max",
"tomcat.threads.current",
"metrometric.heap.used",
"process.files.open",
"jvm.gc.pause",
"tomcat.sessions.active.max",
"metrometric.heap.committed",
"tomcat.threads.busy",
"OemCrn.getCrnPrograms",
"process.start.time",
"process.files.max",
"jvm.gc.memory.promoted",
"metrometric.total.metrics_in_memory.count",
"system.load.average.1m",
"jvm.buffer.count",
"jvm.gc.max.data.size",
"tomcat.servlet.request.max",
"process.cpu.usage"
]
}
And if we deeper inside the KEY We will get the more granular response for example:
- /metrics/mockMetricsSomeData
{
"name": "mockMetricsSomeData",
"description": null,
"baseUnit": "milliseconds",
"measurements": [
{
"statistic": "COUNT",
"value": 0
},
{
"statistic": "TOTAL_TIME",
"value": 0
},
{
"statistic": "MAX",
"value": 0
}
],
"availableTags": [
{
"tag": "servicename",
"values": [
"mockMetrics"
]
},
{
"tag": "containerhost",
"values": [
"3c94ccb43ab6"
]
}
]
}
So we will have all the responses for metrics and we can use these at our convenience. In case of any doubt, feel free to ask in the comment section.
Opinions expressed by DZone contributors are their own.
Comments