반응형

들어가며

actuator란?

  • 실행중인 스프링 부트 애플리케이션의 정보를 API 형식으로 확인할 수 있게 해주는 기능이다.
  • 애플리케이션의 실행 여부, 패키지 로깅 레벨, Bean 목록, Metric(CPU, Heap, Thread 등) 등 여러 정보들을 확인할 수 있는 기능을 제공한다.

기본 예제

dependencies

dependencies {
    ...
    implementation 'org.springframework.boot:spring-boot-starter-actuator'    
}

application.properties

management.endpoint.health.show-details=always
management.endpoints.web.exposure.include=*

확인

  • 애플리케이션 실행 후 아래 주소로 접속
  • 요청
  • 응답
    {
        "_links": {
            "self": {
                "href": "http://localhost:8080/actuator",
                "templated": false
            },
            "beans": {
                "href": "http://localhost:8080/actuator/beans",
                "templated": false
            },
            "caches-cache": {
                "href": "http://localhost:8080/actuator/caches/{cache}",
                "templated": true
            },
            "caches": {
                "href": "http://localhost:8080/actuator/caches",
                "templated": false
            },
            "health": {
                "href": "http://localhost:8080/actuator/health",
                "templated": false
            },
            "health-path": {
                "href": "http://localhost:8080/actuator/health/{*path}",
                "templated": true
            },
            "info": {
                "href": "http://localhost:8080/actuator/info",
                "templated": false
            },
            "conditions": {
                "href": "http://localhost:8080/actuator/conditions",
                "templated": false
            },
            "configprops": {
                "href": "http://localhost:8080/actuator/configprops",
                "templated": false
            },
            "configprops-prefix": {
                "href": "http://localhost:8080/actuator/configprops/{prefix}",
                "templated": true
            },
            "env": {
                "href": "http://localhost:8080/actuator/env",
                "templated": false
            },
            "env-toMatch": {
                "href": "http://localhost:8080/actuator/env/{toMatch}",
                "templated": true
            },
            "loggers": {
                "href": "http://localhost:8080/actuator/loggers",
                "templated": false
            },
            "loggers-name": {
                "href": "http://localhost:8080/actuator/loggers/{name}",
                "templated": true
            },
            "heapdump": {
                "href": "http://localhost:8080/actuator/heapdump",
                "templated": false
            },
            "threaddump": {
                "href": "http://localhost:8080/actuator/threaddump",
                "templated": false
            },
            "metrics-requiredMetricName": {
                "href": "http://localhost:8080/actuator/metrics/{requiredMetricName}",
                "templated": true
            },
            "metrics": {
                "href": "http://localhost:8080/actuator/metrics",
                "templated": false
            },
            "scheduledtasks": {
                "href": "http://localhost:8080/actuator/scheduledtasks",
                "templated": false
            },
            "mappings": {
                "href": "http://localhost:8080/actuator/mappings",
                "templated": false
            }
        }
    }
    

노출 항목 지정

application.properties

management.endpoints.web.exposure.include=health, metrics

확인

  • 요청
  • 응답
    {
        "_links": {
            "self": {
                "href": "http://localhost:8080/actuator",
                "templated": false
            },
            "health": {
                "href": "http://localhost:8080/actuator/health",
                "templated": false
            },
            "health-path": {
                "href": "http://localhost:8080/actuator/health/{*path}",
                "templated": true
            },
            "metrics-requiredMetricName": {
                "href": "http://localhost:8080/actuator/metrics/{requiredMetricName}",
                "templated": true
            },
            "metrics": {
                "href": "http://localhost:8080/actuator/metrics",
                "templated": false
            }
        }
    }
    

사용자 정의 Metrics

TransactionCurrentCountInterceptor

public class TransactionCurrentCountInterceptor implements HandlerInterceptor {
    private final Counter counter;

    public TransactionCurrentCountInterceptor(MeterRegistry meterRegistry) {
        this.counter = meterRegistry.counter("transaction.current.count");
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        counter.increment();
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        counter.increment(-1d);
    }
}

WebMvcConfig

@RequiredArgsConstructor
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    private final MeterRegistry meterRegistry;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new TransactionCurrentCountInterceptor(meterRegistry))
            .addPathPatterns("/**")
            .excludePathPatterns("/actuator/**");
    }
}

확인

참고

반응형

'Development > Spring' 카테고리의 다른 글

[Spring] WireMock  (0) 2023.08.18
[Spring] SpEL  (0) 2023.08.09
[Spring] Scheduler Lock  (0) 2021.11.25
[Spring] Replication  (0) 2021.11.15
[Spring] @ConfigurationProperties  (0) 2021.08.02

+ Recent posts