현대 백엔드 시스템에서는 단순히 애플리케이션을 "잘 동작하게 만드는 것"만으로는 부족합니다.
운영 환경에서 상태를 관찰하고, 성능을 측정하며, 이상을 감지하는 것이 매우 중요해졌습니다.
바로 그 중심에 있는 라이브러리가 Micrometer입니다.
Micrometer는 벤더 중립적인 애플리케이션 메트릭 수집 라이브러리입니다.
Prometheus, Datadog, New Relic, Graphite 등 다양한 모니터링 백엔드로 지표(Metrics)를 전송할 수 있도록 설계되었습니다.
쉽게 말해, 다양한 모니터링 시스템에 동일한 코드로 메트릭을 전송할 수 있도록 돕는 메트릭 파사드 계층입니다.
Spring Boot 2.x 이후, Actuator는 내부적으로 Micrometer를 사용합니다.
| 구성 요소 | 역할 |
|---|---|
| Spring Boot Actuator | /actuator/metrics 같은 API를 통해 운영 상태를 노출 |
| Micrometer | JVM, HTTP 요청, DB 커넥션 등 실제 메트릭 수집 및 전송 담당 |
즉, Actuator = UI/엔드포인트, Micrometer = 계측/데이터 전송입니다.
Micrometer는 다음과 같은 메트릭을 자동 또는 수동으로 수집할 수 있습니다:
다음 예제는 /api/test 요청의 수, 처리 시간, 활성 세션 수를 측정하는 코드입니다.
@Service
public class CustomMetricsService {
private final Counter requestCounter;
private final Timer requestTimer;
private final CustomGauge customGauge;
public CustomMetricsService(MeterRegistry meterRegistry) {
this.requestCounter = meterRegistry.counter("custom.requests.total", "endpoint", "/api/test");
this.requestTimer = meterRegistry.timer("custom.request.duration", "endpoint", "/api/test");
this.customGauge = new CustomGauge();
Gauge.builder("custom.active.sessions", customGauge, CustomGauge::getActiveSessions)
.tag("region", "us-east")
.register(meterRegistry);
}
public void processRequest(Runnable requestLogic) {
requestCounter.increment(); // 요청 수 증가
requestTimer.record(requestLogic); // 처리 시간 측정
}
public void updateActiveSessions(int activeSessions) {
customGauge.setActiveSessions(activeSessions); // 실시간 상태 반영
}
private static class CustomGauge {
private volatile double activeSessions = 0;
public double getActiveSessions() { return activeSessions; }
public void setActiveSessions(double activeSessions) { this.activeSessions = activeSessions; }
}
}
| 종류 | 설명 | 사용 예 |
|---|---|---|
| Counter | 누적되는 정수값 | 요청 수, 오류 수 등 |
| Timer | 실행 시간 측정 | 응답 시간, DB 처리 시간 |
| Gauge | 현재 상태값 | 동시 접속자 수, 큐 길이 등 |
MeterRegistry는 Micrometer의 핵심 진입점입니다. 모든 메트릭은 이 객체에 등록됩니다.endpoint, status, region 등)@Scheduled, 필터, 서비스 로직 등 원하는 위치에서 활용 가능/actuator/prometheus로 메트릭 노출 가능| 이유 | 설명 |
|---|---|
| 벤더 중립 | Prometheus, Datadog, CloudWatch 등 다양한 시스템과 연동 가능 |
| 통합성 | Spring Boot와 자연스럽게 통합 (Actuator, 설정 자동화) |
| 성능 | 성능 오버헤드 최소화 설계 (비동기 배치 전송 등) |
| 확장성 | 비즈니스 지표까지 직접 정의 가능 |
운영 중인 서비스를 “보이지 않는 블랙박스”로 두지 않으려면,
메트릭 수집과 모니터링은 선택이 아닌 필수입니다.
Micrometer는 단순한 수치 수집 도구가 아니라, 서비스 운영의 눈입니다.