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

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Metrics 2.X in Spring Boot 2.X
  • Spring Boot Metrics with Dynamic Tag Values
  • Spring Boot Monitoring via Prometheus -Grafana
  • [CSF] Using Metrics In Spring Boot Services With Prometheus, Graphana, Instana, and Google cAdvisor

Trending

  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  • How To Introduce a New API Quickly Using Quarkus and ChatGPT
  • Docker Base Images Demystified: A Practical Guide
  • Why Database Migrations Take Months and How to Speed Them Up
  1. DZone
  2. Coding
  3. Frameworks
  4. Spring Boot 2: Migrating From Dropwizard Metrics to Micrometer

Spring Boot 2: Migrating From Dropwizard Metrics to Micrometer

Goodbye Dropwizard, hello Micrometer! Let's take a sneak peek at how to move your metrics to Micrometer for Spring Boot 2 projects.

By 
Tomasz Nurkiewicz user avatar
Tomasz Nurkiewicz
DZone Core CORE ·
Jan. 24, 18 · Tutorial
Likes (26)
Comment
Save
Tweet
Share
50.1K Views

Join the DZone community and get the full member experience.

Join For Free

Spring Boot 2 is around the corner. One of the minor changes is the replacement of Dropwizard Metrics with Micrometer. The migration path is fairly straightforward, and Micrometer actually provides a cleaner API. With Metrics, you have to inject MetricRegistry wherever you need some metrics (see: Monitoring and measuring reactive application with Dropwizard Metrics). This has many drawbacks:

  • We are mixing business and technical dependencies in our components.
  • Therefore, I am sometimes reluctant to add new metrics because it requires me to inject MetricRegistry
  • Also, MetricRegistry must be stubbed in unit tests.

Micrometer's tagline is:

Think SLF4J, but for metrics

It's actually quite accurate. Whenever I need a Logger, I don't inject LoggerFactory. Instead, I simply use static methods available everywhere. The same goes for Micrometer — I simply use static factory methods on the globally available Metrics class:

private final Timer indexTimer = Metrics.timer("es.timer");
private final LongAdder concurrent = Metrics.gauge("es.concurrent", new LongAdder());
private final Counter successes = Metrics.counter("es.index", "result", "success");
private final Counter failures = Metrics.counter("es.index", "result", "failure");


That's it! You can put metrics anywhere you like without polluting your constructor with MetricRegistry. The API is very similar, e.g.:

concurrent.increment()


One major difference is gauges vs. counters. In Dropwizard Metrics, counters can go up and down, whereas in Micrometer, counters must increase monotonically. I thought it was a bug... Counter is used in simple scenarios like counting how many requests succeeded. So how can we measure things like the number of concurrent requests or queue length? With gauges, but it's slightly convoluted.

Did you notice how Metrics.gauge() takes a new LongAdder() as an argument? And returns it? This way, we create a gauge that tracks (by periodically polling for value) any instance of the Number class (e.g. AtomicLong or LongAdder). We can modify the returned LongAdder, and its current value will be reflected by the gauge. Neat! Moreover, there are helper methods like gaugeCollectionSize() and gaugeMapSize() that take any Collection or Map, respectively — and are quite self-explanatory.

Micrometer also has a bunch of built-in system and JVM metrics, for example:

  • LogbackMetrics - number of log messages per each log level. You can watch e.g. error rate
  • ProcessorMetrics- average system load
  • JvmMemoryMetrics - memory usage, split by area
  • JvmThreadMetrics - number of threads (live, daemon, peak...)
  • JvmGcMetrics - GC promotion rate, etc.

Most of these are registered by default by Spring Boot. The last two of them will be available as well. By having all these metrics, we can actually enhance our dashboard quite a bit:

The dashboard definition for Grafana is available on GitHub. Notice how this application uses about 100 MiB of RAM while sustaining almost two thousand concurrent connections(!)? Also, there are fewer than 45 live threads compared to thousands of concurrent connections — impressive.

It's worth mentioning that the setup of Micrometer in Spring Boot is really simple. First, add the appropriate dependency:

compile 'io.micrometer:micrometer-registry-graphite:1.0.0-rc.5'


And a bunch of configuration parameters. Zero code:

spring.metrics.export.graphite:
  host: graphite
  port: 2003
  protocol: Plaintext
  step: PT1S


In the last part of this short series, we will wrap everything together in a Spring Web Flux application.

Spring Framework Metric (unit) Spring Boot

Published at DZone with permission of Tomasz Nurkiewicz, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Metrics 2.X in Spring Boot 2.X
  • Spring Boot Metrics with Dynamic Tag Values
  • Spring Boot Monitoring via Prometheus -Grafana
  • [CSF] Using Metrics In Spring Boot Services With Prometheus, Graphana, Instana, and Google cAdvisor

Partner Resources

×

Comments

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: