DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4
  • How Spring Boot Starters Integrate With Your Project
  • A Practical Guide to Creating a Spring Modulith Project
  • Structured Logging in Spring Boot 3.4 for Improved Logs

Trending

  • Zone-Free Angular: Unlocking High-Performance Change Detection With Signals and Modern Reactivity
  • Content Lakes: Harness Unstructured Data for Enterprise AI Readiness
  • Architecting Petabyte-Scale Hyperspectral Pipelines on AWS
  • Why Your QA Engineer Should Be the Most Stubborn Person on the Team
  1. DZone
  2. Coding
  3. Frameworks
  4. Metrics 2.X in Spring Boot 2.X

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.

By 
Vicky Singh user avatar
Vicky Singh
·
Updated Oct. 27, 21 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
6.2K Views

Join the DZone community and get the full member experience.

Join For Free

Spring 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.

XML
 
<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.

Java
 
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

Java
 
@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.

JSON
 
{
  "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:

  1. /metrics/mockMetricsSomeData
JSON
 
{
  "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.

Spring Framework Metric (unit) Spring Boot

Opinions expressed by DZone contributors are their own.

Related

  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4
  • How Spring Boot Starters Integrate With Your Project
  • A Practical Guide to Creating a Spring Modulith Project
  • Structured Logging in Spring Boot 3.4 for Improved Logs

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook