β 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 Type | Restaurant Analogy | Real-world Example |
|---|---|---|
| Counter | No. of customers entering | Total requests received by a service |
| Timer | Time to serve an order | HTTP request latency |
| Gauge | Number of pending orders | Jobs 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 Tracing | With Micrometer Tracing |
|---|---|
| Difficult to correlate logs | Logs include trace & span identifiers |
| Bottlenecks are hard to detect | Easily identify slow service calls |
| Troubleshooting is slow | Can debug production using trace visuals |
π Spring Boot 2 vs Spring Boot 3
| Feature | Spring Boot 2 | Spring Boot 3 |
|---|---|---|
| Tracing Integration | Not built-in | Integrated using Micrometer Tracing |
| Configuration | Required external libraries (Sleuth) | Auto-configured |
| Supported Bridges | N/A | Brave, OpenTelemetry |
| HTTP Monitoring | Manual | Automatic (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?