ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Spring-boot-3 Actuator
    서버 모니터링 2024. 3. 26. 00:22

     aws에서 서버를 구매하기 전에 내가 지금 만든 어플리케이션이 얼만큼의 리소스를 사용중인지 확인하고 서버를 구매하고 싶어

    spring boot에서 지원하는 라이브러리 중 Spring boot Actuator는 html과 JMX를 이용하여 서버를 모니터링 하고 관리할 수 있는 기능을 제공한다.

     

    Spring boot Actuator

    Html과 JMX를 이용하여 서버를 모니터링 하고 관리하 수 있는 라이브러리

     

    // spring boot 애플리케이션을 모니터링하고 관리하는 데 도움이 되는 운영 환경용 기능들
    implementation 'org.springframework.boot:spring-boot-starter-actuator'

    build.gradle의 dependencies에 해당 코드를 추가한 후 

    ## Spring Boot 액추에이터의 웹 엔드포인트 중 어떤 것들을 노출할 것인지 지정
    management:
      endpoints:
        web:
          exposure:
            include: '*'

    properties.yml에 해당 내용을 넣어서 http://{server}:{port}/actuator에 접속하면 서버의 상태를 확인 할 수 있게 끔 json형식으로 데이터가 노출 되는 것을 확인 할 수 있다.

     

    spring-boot-starter-actuator 는 내부적으로 micrometer 라이브러리를 사용하고, auto configuration을 위한 라이브러리리(spring-boot-actuator-autoconfigure)를 포함하는걸 알 수 있다.

     

    핵심 라이브러리인 micrometer-core 의 내용을 살펴보면 spring boot 가 구동되는 application 의 health, info, metric 정보들을 수집하는 역할을 하는 binder 클래스들이 아주 많이 구현되어 있는걸 알수 있습니다.

    즉 spring boot 에서 cache를 사용하고 있다면 cache 관련 정보를 수집하는 클래스를 micrometer 라이브러리에서 이미 제공하고 있으며, kafka 를 사용하고 있다면 kafka 관련 정보를 수집하는 클래스를 micrometer 라이브러리에서 이미 제공하고 있습니다. 즉 자주 쓰는 대부분의 기능들에 대해 micrometer 에서 정보를 수집하는 binder 를 이미 제공하고 있습니다.

     

    spring boot 는 자동설정을 지원해주기에 아래처럼 spring-boot-actuator-autoconfigure 라이브러리에서 정보를 수집할수 있도록 bean 으로 등록해주고 있습니다.


    Spring-boot-actuator를 처음 시작해 보면 노출되는 내용이 몇개 없다.

    도움되는 데이터는 "health"데이터지만 행당 링크에 들어가보면 사실 up이라는 글자만 노출되고 다른 데이터는 노출 되지않는다.

    그래서 도움이 되는 데이터를 얻기 위해선 end-point를 설정해 줄 필요가 있다.

    의존성 라이브러리의 소스코드를 봤을때는 엄청 많은 정보들을 지원해주는 것 같았는데, 막상 실행해서 결과를 보면 위와 같이 몇 개 안보여주는것으로 되어 있습니다. actuator 에서는 java version, OS 버전 과 같은 다양한 정보를 제공해주는데 이런 정보는 누구에게나 open 되면 보안상 좋지 않기에, spring boot 에서는 default 설정으로는 위와 같이 2~3개로 제한시켜져 있기 때문입니다.

    이제 설정을 변경해서 더 많은 정보를 보여주도록 해보겠습니다.


    Spring-boot-actuator의 End-Point 설정

     

    우선 spring 이 제공하는 endpoint 가 어떤게 있는지 알아봅시다.

    아래에 주요 endpoint 에 대해 적어봤습니다. 이 보다 더 많은 endpoint가 있으며

    더 자세한것은 아래 링크에서 확인해보세요.

    beans 등록된 bean 목록 제공
    caches cache 사용중이라면 cache 관련 정보 제공
    conditions spring auto configuration 에 의해 bean으로 등록된것과 그렇지 않은 것의 상세 이유를 제공
    health application이 구동중인지, application과 연동되는 다른 서비스(DB, message queue)가 구동중인지 여부 제공
    info application 의 대략적인 정보
    metrics cpu, mem, thread count 등의 모니터링용 메트릭 정보
    logger 로거 설정 확인 실시간 로그 레벨 변경 제공
    quartz quartz 라는 스케줄링 관련 라이브러리를 사용하고 있다면, quartz 관련 정보를 상세히 제공

     

    여기서 내가 현재 내가 필요한 정보는 metrics의 정보가 필요하다.

     

    endpoint 활성화설정, 노출 설정

    먼저 알아야 할것은 각 endpoint 는 enable/disable (활성화 여부)과 expose ( 노출 여부 ) 라는 2가지 설정을 할 수 있으며 2가지 모두 켜진 상태여야 외부로 노출이 됩니다.

    즉 enable/disable 을 통해 spring boot 내부적으로 특정 endpoint 의 정보를 수집하는걸 설정할 수 있으며,

    expose 설정을 통해 최종적으로 web 이나 jmx ( = java 모니터링 관련 프로토콜/스펙) 에 해당 정보가 보이는걸(=노출) 설정할 수 있습니다.

     

    둘 중 하나라도 off 이면 노출이 되지 않습니다. 

     

    include는 노출에 포함할 endpoint,

    exclude 는 노출에 제외할 endpoint 라는 뜻입니다. 

     

    나는 현재 metrics데이터만 필요하므로 metrics를 제외하고는 별다른 데이터를 활설화 시키지도 노출 시키지도 않겠다.

    ## Spring Boot 액추에이터의 웹 엔드포인트 중 어떤 것들을 노출할 것인지 지정
    management:
      endpoints:
        enabled: false
        web:
          exposure:
            include: loggers, metrics

    모든 정보를 활성화/노출 시키고 싶을 때는

    ## Spring Boot 액추에이터의 웹 엔드포인트 중 어떤 것들을 노출할 것인지 지정
    management:
      endpoints:
        web:
          exposure:
            include: '*' 

     

    정리

    enabled : 활성회 (true= 활성화, false= 비활성화)

    include : 노출

    exclude : 비노출

    *actuator를 통해 application의 다양한 정보를 확인할 수 있고, 특정 endpoint에서는 실시간으로 변경도 가능하게 해주지만 보안상의 문제가 있을 수 있으므로 spring security 혹은 이와 유사한 방법으로 보안 위험을 해결 하도록 해야한다.

    ## Spring Boot 액추에이터의 웹 엔드포인트 중 어떤 것들을 노출할 것인지 지정
    management:
      endpoints:
        enabled: false
        web:
          exposure:
            include: loggers, metrics
      endpoint:
        auditevents:
          cache:
            time-to-live: 10s

    cache를 설정해 준다면 정해진 시간동안 정보를 가져오지 않게 cache에 저장된 데이터만 가져온다(새로고침 해도 데이터가 변경안됨)


     

    다음과 같이 설정을 완료한 경우 아래와 같은 데이터를 볼 수 있다.

    {
    "_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-path": {
    "href": "http://localhost:8080/actuator/health/{*path}",
    "templated": true
    },
    "health": {
    "href": "http://localhost:8080/actuator/health",
    "templated": false
    },
    "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": {
    "href": "http://localhost:8080/actuator/metrics",
    "templated": false
    },
    "metrics-requiredMetricName": {
    "href": "http://localhost:8080/actuator/metrics/{requiredMetricName}",
    "templated": true
    },
    "scheduledtasks": {
    "href": "http://localhost:8080/actuator/scheduledtasks",
    "templated": false
    },
    "mappings": {
    "href": "http://localhost:8080/actuator/mappings",
    "templated": false
    }
    }
    }

     

    '서버 모니터링' 카테고리의 다른 글

    SpringBoot - actuator + Prometheus + Grafana  (0) 2024.03.29
    서버 모니터링  (0) 2024.03.21
Designed by Tistory.