Posted on: June 24, 2025 Posted by: rahulgite Comments: 0

βœ… Introduction to Micrometer in Spring Boot 3

Micrometer is a metrics and observability library integrated into Spring Boot 3. It helps collect, expose, and export application signals such as:

  • Metrics (numerical indicators)
  • Tracing (request flows)
  • Logs (event data)

It provides a vendor-neutral API, allowing integration with tools like Prometheus, Datadog, New Relic, Zipkin, and more.


🧱 Three Core Pillars of Observability

1. πŸ“Š Metrics

  • Quantitative data that describes system behavior
  • Example: HTTP request count, response times, JVM memory usage

2. πŸ“ Tracing

  • Tracks how requests move through various microservices
  • Enables visualization of service dependencies and latencies

3. πŸ“„ Logs

  • Textual event records that capture errors, transactions, and internal events

🍽️ Metrics with Restaurant Analogy

Metric TypeRestaurant AnalogyReal-world Example
CounterNo. of customers enteringTotal requests received by a service
TimerTime to serve an orderHTTP request latency
GaugeNumber of pending ordersJobs in queue or current memory usage

🧭 Understanding Tracing

πŸ”Ή What is Tracing?

Tracing is the process of tracking a request as it travels through one or more services.

πŸ”‘ Key Terms:

  • Trace ID: Unique identifier for the entire request chain
  • Span ID: Unique for each service/method call
  • Parent Span ID: Shows nesting relationship between spans
  • Context Propagation: Passing trace-related metadata (via headers like X-B3-TraceId) to preserve continuity across services

πŸ” Example:

User calls /placeOrder β†’ Service A calls Service B β†’ Service B calls Service C. All services share a single Trace ID, each with a unique Span ID.


🚨 Importance of Micrometer Tracing

Without TracingWith Micrometer Tracing
Difficult to correlate logsLogs include trace & span identifiers
Bottlenecks are hard to detectEasily identify slow service calls
Troubleshooting is slowCan debug production using trace visuals

πŸ†š Spring Boot 2 vs Spring Boot 3

FeatureSpring Boot 2Spring Boot 3
Tracing IntegrationNot built-inIntegrated using Micrometer Tracing
ConfigurationRequired external libraries (Sleuth)Auto-configured
Supported BridgesN/ABrave, OpenTelemetry
HTTP MonitoringManualAutomatic (for RestTemplate, WebClient)

πŸ—οΈ Micrometer Tracing Architecture

Client β†’ Spring Boot App β†’ Micrometer Bridge (Brave)
      β†’ Exporter (Zipkin/Splunk) β†’ Observability Platform

Components:

  • Micrometer Tracing API: Abstract observability interfaces
  • Bridge: Connects Micrometer to a tracing backend (e.g., Brave)
  • Exporter/Reporter: Sends trace data to platforms (e.g., Zipkin)

πŸ“¦ Required Dependencies (Maven)

<!-- Actuator for metrics -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<!-- Micrometer tracing with Brave -->
<dependency>
  <groupId>io.micrometer</groupId>
  <artifactId>micrometer-tracing-bridge-brave</artifactId>
</dependency>

<!-- Zipkin exporter -->
<dependency>
  <groupId>io.zipkin.reporter2</groupId>
  <artifactId>zipkin-reporter-brave</artifactId>
</dependency>

βš™οΈ Practical Usage & Customization

βœ… 1. Auto-track REST endpoints

Spring Boot automatically traces controller endpoints.

πŸ“ 2. Add Trace ID/Span ID to logs

logging:
  pattern:
    level: "%5p [${spring.application.name},%X{traceId},%X{spanId}]"

⏱️ 3. Create Custom Metrics using @Timed

@RestController
public class OrderController {

    @Timed(value = "order.process.time", description = "Time taken to process orders")
    @GetMapping("/order")
    public String processOrder() {
        return "Order processed";
    }
}

πŸ“ˆ Backend Integrations

Supported tools:

  • Zipkin
  • Jaeger
  • Prometheus (for metrics)
  • Grafana
  • Elastic Stack
  • Datadog, New Relic, Splunk

Configuration (YAML)

management:
  tracing:
    sampling:
      probability: 1.0
  zipkin:
    tracing:
      endpoint: http://localhost:9411/api/v2/spans

❓ Common Interview Questions

Conceptual:

  • What are the differences between metrics, logs, and traces?
  • How does context propagation work in Micrometer?
  • What are Trace ID and Span ID? Why are they important?

Practical:

  • How do you export traces to Zipkin in Spring Boot 3?
  • How do you expose a custom metric using @Timed?
  • What’s the role of Brave in Micrometer tracing?

Leave a Comment