{"id":3660,"date":"2025-12-24T10:01:13","date_gmt":"2025-12-24T15:01:13","guid":{"rendered":"https:\/\/www.mymiller.name\/wordpress\/?p=3660"},"modified":"2025-12-24T10:01:13","modified_gmt":"2025-12-24T15:01:13","slug":"crafting-custom-metrics-for-micrometer-tracing-a-comprehensive-guide","status":"publish","type":"post","link":"https:\/\/www.mymiller.name\/wordpress\/spring_tracing\/crafting-custom-metrics-for-micrometer-tracing-a-comprehensive-guide\/","title":{"rendered":"Crafting Custom Metrics for Micrometer Tracing: A Comprehensive Guide"},"content":{"rendered":"\n<div class=\"wp-block-jetpack-markdown\"><p>Micrometer, a versatile application metrics facade, seamlessly integrates with Spring Boot Actuator to capture and expose vital insights into your application\u2019s performance. While Micrometer offers a plethora of pre-defined metrics, you often need to track application-specific metrics that aren\u2019t covered out of the box. This blog post will explore creating custom metrics for Micrometer tracing, along with the necessary Gradle\/Maven setup and examples of various custom metric types.<\/p>\n<p><strong>1. Gradle\/Maven Setup<\/strong><\/p>\n<p>First, ensure your Spring Boot project includes the required Micrometer dependencies.<\/p>\n<p><strong>Gradle<\/strong><\/p>\n<pre><code class=\"language-gradle\">dependencies {\n    implementation 'io.micrometer:micrometer-core'\n    implementation 'io.micrometer:micrometer-registry-prometheus' \/\/ Or your preferred registry\n}\n<\/code><\/pre>\n<p><strong>Maven<\/strong><\/p>\n<pre><code class=\"language-xml\">&lt;dependencies&gt;\n    &lt;dependency&gt;\n        &lt;groupId&gt;io.micrometer&lt;\/groupId&gt;\n        &lt;artifactId&gt;micrometer-core&lt;\/artifactId&gt;\n    &lt;\/dependency&gt;\n    &lt;dependency&gt;\n        &lt;groupId&gt;io.micrometer&lt;\/groupId&gt;\n        &lt;artifactId&gt;micrometer-registry-prometheus&lt;\/artifactId&gt; \n    &lt;\/dependency&gt;\n&lt;\/dependencies&gt;\n<\/code><\/pre>\n<p><strong>2. Application.yml Configuration<\/strong><\/p>\n<p>While Micrometer typically auto-configures itself, you may want to fine-tune its behavior using <code>application.yml<\/code>:<\/p>\n<pre><code class=\"language-yaml\">management:\n  metrics:\n    export:\n      prometheus:\n        enabled: true  # Enable Prometheus endpoint (if using Prometheus)\n<\/code><\/pre>\n<p><strong>3. The <code>MeterRegistry<\/code><\/strong><\/p>\n<p>The <code>MeterRegistry<\/code> is the core component in Micrometer responsible for managing and collecting metrics. In Spring Boot applications, Micrometer automatically creates and configures a <code>MeterRegistry<\/code> bean for you. You can obtain a reference to this bean using dependency injection:<\/p>\n<pre><code class=\"language-java\">import io.micrometer.core.instrument.MeterRegistry;\n\n@Service\npublic class MyService {\n\n    private final MeterRegistry meterRegistry;\n\n    public MyService(MeterRegistry meterRegistry) {\n        this.meterRegistry = meterRegistry;\n    }\n\n    \/\/ ... your service methods ...\n}\n<\/code><\/pre>\n<p>Once you have a reference to the <code>MeterRegistry<\/code>, you can use it to create and register your custom metrics, as shown in the examples below.<\/p>\n<p><strong>4. Creating Custom Metrics<\/strong><\/p>\n<p>Micrometer provides a rich set of metric types to instrument various aspects of your application.<\/p>\n<p><strong>4.1. Counter<\/strong><\/p>\n<p>Counters are ideal for tracking the number of occurrences of an event.<\/p>\n<pre><code class=\"language-java\">import io.micrometer.core.instrument.Counter;\n\n\/\/ ... (inside your service class)\n\npublic void performOperation() {\n    \/\/ ... your operation logic ...\n    Counter myCounter = Counter.builder(&quot;my.custom.counter&quot;)\n            .description(&quot;Counts the number of times a specific operation is performed&quot;)\n            .register(meterRegistry); \n    myCounter.increment(); \n}\n<\/code><\/pre>\n<p><strong>4.2. Gauge<\/strong><\/p>\n<p>Gauges represent a single numerical value that can go up or down.<\/p>\n<pre><code class=\"language-java\">import io.micrometer.core.instrument.Gauge;\n\n\/\/ ... (inside your service class)\n\nprivate int currentQueueSize = 0;\n\npublic MyService(MeterRegistry registry) {\n    Gauge.builder(&quot;my.queue.size&quot;, this, MyService::getCurrentQueueSize)\n            .description(&quot;Tracks the current size of a queue&quot;)\n            .register(registry);\n}\n\npublic int getCurrentQueueSize() {\n    return currentQueueSize;\n}\n\n\/\/ ... methods to add\/remove items from the queue, updating currentQueueSize\n<\/code><\/pre>\n<p><strong>4.3. Timer<\/strong><\/p>\n<p>Timers measure the duration (and distribution) of an operation.<\/p>\n<pre><code class=\"language-java\">import io.micrometer.core.instrument.Timer;\n\n\/\/ ... (inside your service class)\n\npublic void performOperation() {\n    Timer myTimer = Timer.builder(&quot;my.operation.timer&quot;)\n            .description(&quot;Measures the duration of a specific operation&quot;)\n            .register(meterRegistry);\n\n    myTimer.record(() -&gt; {\n        \/\/ ... your operation logic ...\n    });\n}\n<\/code><\/pre>\n<p><strong>4.4. Distribution Summary<\/strong><\/p>\n<p>Distribution summaries track the distribution of events (e.g., request sizes).<\/p>\n<pre><code class=\"language-java\">import io.micrometer.core.instrument.DistributionSummary;\n\n\/\/ ... (inside your service class)\n\npublic void processRequest(int requestSize) {\n    \/\/ ... your request processing logic ...\n    DistributionSummary requestSizeSummary = DistributionSummary.builder(&quot;my.request.size&quot;)\n            .description(&quot;Tracks the distribution of request sizes&quot;)\n            .register(meterRegistry);\n    requestSizeSummary.record(requestSize); \n}\n<\/code><\/pre>\n<p><strong>4.5. Long Task Timer<\/strong><\/p>\n<p>Long task timers track the duration of long-running operations.<\/p>\n<pre><code class=\"language-java\">import io.micrometer.core.instrument.LongTaskTimer;\n\n\/\/ ... (inside your service class)\n\npublic void performLongTask() {\n    LongTaskTimer longTaskTimer = LongTaskTimer.builder(&quot;my.long.task&quot;)\n            .description(&quot;Tracks the duration of a long-running task&quot;)\n            .register(meterRegistry);\n\n    longTaskTimer.record(() -&gt; {\n        \/\/ ... your long-running task logic ...\n    });\n}\n<\/code><\/pre>\n<p>By creating custom metrics with Micrometer and leveraging the <code>MeterRegistry<\/code>, you gain granular visibility into your Spring Boot application\u2019s behavior. Utilize these metrics to identify bottlenecks, optimize performance, and make informed decisions about your application\u2019s architecture and resource allocation. Remember, effective monitoring is key to building resilient and high-performing applications.<\/p>\n<\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":3661,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[446],"tags":[69,319],"series":[],"class_list":["post-3660","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-spring_tracing","tag-java-2","tag-spring"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/08\/car-dashboard-2667434_1280-jpg.avif","jetpack-related-posts":[{"id":3651,"url":"https:\/\/www.mymiller.name\/wordpress\/spring_tracing\/spring-micrometer-tracing-and-observability-made-easy\/","url_meta":{"origin":3660,"position":0},"title":"Spring Micrometer: Tracing and Observability Made Easy","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"Observability is crucial for modern applications, allowing you to understand their behavior and performance. Spring Boot 3 introduces powerful observability features through Micrometer. Understanding Observability Observability involves collecting metrics, logs, and traces to provide insights into your running application. Micrometer is the core of this in Spring Boot, offering: Metrics:\u2026","rel":"","context":"In &quot;Spring Tracing &amp; Observability&quot;","block_context":{"text":"Spring Tracing &amp; Observability","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring_tracing\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/08\/art-6749527_1280-jpg.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/08\/art-6749527_1280-jpg.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/08\/art-6749527_1280-jpg.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/08\/art-6749527_1280-jpg.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/08\/art-6749527_1280-jpg.avif 3x"},"classes":[]},{"id":3438,"url":"https:\/\/www.mymiller.name\/wordpress\/spring\/architecting-with-spring-and-spring-cloud\/","url_meta":{"origin":3660,"position":1},"title":"Architecting with Spring and Spring Cloud","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"Building a Multi-Service Architecture with Spring 3.1.x and Spring Cloud: Unlocking the Power of Microservices In the ever-evolving landscape of software development, microservices have emerged as a powerful architectural paradigm, enabling organizations to build scalable, resilient, and agile applications. Spring, a widely adopted Java framework, provides a comprehensive suite of\u2026","rel":"","context":"In &quot;Spring&quot;","block_context":{"text":"Spring","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/11\/field-5236879_640.jpg?fit=640%2C360&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/11\/field-5236879_640.jpg?fit=640%2C360&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/11\/field-5236879_640.jpg?fit=640%2C360&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":3912,"url":"https:\/\/www.mymiller.name\/wordpress\/uncategorized\/spring-boot-4-0-whats-next-for-the-modern-java-architect\/","url_meta":{"origin":3660,"position":2},"title":"Spring Boot 4.0: What&#8217;s Next for the Modern Java Architect?","author":"Jeffery Miller","date":"September 24, 2025","format":false,"excerpt":"A Forward-Looking Comparison of Spring Boot 3.x and 4.0 Staying on top of the rapidly evolving Java ecosystem is paramount for any software architect. The shift from Spring Boot 2.x to 3.x brought significant changes, notably the move to Jakarta EE. Now, with the horizon of Spring Boot 4.0 and\u2026","rel":"","context":"Similar post","block_context":{"text":"Similar post","link":""},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/09\/per-2056740_1280.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/09\/per-2056740_1280.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/09\/per-2056740_1280.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/09\/per-2056740_1280.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/09\/per-2056740_1280.avif 3x"},"classes":[]},{"id":3771,"url":"https:\/\/www.mymiller.name\/wordpress\/spring_circuit_breaker\/mastering-circuitbreaker-in-spring-cloud-a-comprehensive-guide\/","url_meta":{"origin":3660,"position":3},"title":"Mastering CircuitBreaker in Spring Cloud: A Comprehensive Guide","author":"Jeffery Miller","date":"December 23, 2025","format":false,"excerpt":"Spring Cloud Circuit Breaker provides an elegant way to handle failures in distributed systems by implementing the circuit breaker pattern. This pattern prevents cascading failures and improves application resilience by isolating failing services and providing fallback mechanisms. This article offers a deep dive into using CircuitBreaker in Spring Cloud, covering\u2026","rel":"","context":"In &quot;Spring Circuit Breaker&quot;","block_context":{"text":"Spring Circuit Breaker","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring_circuit_breaker\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/network-5987786_1280-jpg.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/network-5987786_1280-jpg.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/network-5987786_1280-jpg.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/network-5987786_1280-jpg.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/network-5987786_1280-jpg.avif 3x"},"classes":[]},{"id":3654,"url":"https:\/\/www.mymiller.name\/wordpress\/springboot\/spring-boot-actuator-crafting-custom-endpoints-for-tailored-insights\/","url_meta":{"origin":3660,"position":4},"title":"Spring Boot Actuator: Crafting Custom Endpoints for Tailored Insights","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"Spring Boot Actuator provides a robust set of built-in endpoints for monitoring and managing your applications. However, there are scenarios where you might need to expose application-specific information or metrics beyond what the standard endpoints offer. This is where custom actuator endpoints shine, allowing you to tailor the information you\u2026","rel":"","context":"In &quot;Springboot&quot;","block_context":{"text":"Springboot","link":"https:\/\/www.mymiller.name\/wordpress\/category\/springboot\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/08\/oil-1183699_1280-jpg.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/08\/oil-1183699_1280-jpg.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/08\/oil-1183699_1280-jpg.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/08\/oil-1183699_1280-jpg.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/08\/oil-1183699_1280-jpg.avif 3x"},"classes":[]},{"id":3466,"url":"https:\/\/www.mymiller.name\/wordpress\/spring_shell\/exploring-spring-shell-a-comprehensive-guide\/","url_meta":{"origin":3660,"position":5},"title":"Exploring Spring Shell: A Comprehensive Guide","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"Spring Shell seamlessly integrates command-line applications with the Spring framework, offering a robust and flexible environment for developers. In this article, we'll expand on setting up a Spring Shell project and explore its features, with a detailed focus on parameters, options, and annotations available for crafting powerful commands in Java.\u2026","rel":"","context":"In &quot;Spring Shell&quot;","block_context":{"text":"Spring Shell","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring_shell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2016\/06\/coding-699318_640.jpg?fit=640%2C437&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2016\/06\/coding-699318_640.jpg?fit=640%2C437&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2016\/06\/coding-699318_640.jpg?fit=640%2C437&ssl=1&resize=525%2C300 1.5x"},"classes":[]}],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3660","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/comments?post=3660"}],"version-history":[{"count":1,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3660\/revisions"}],"predecessor-version":[{"id":3662,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3660\/revisions\/3662"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media\/3661"}],"wp:attachment":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media?parent=3660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/categories?post=3660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/tags?post=3660"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/series?post=3660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}