아무거나

[Spring Actuator] Example 2편 - 각 Info Endpoint 설명 및 Custom Info Endpoint 생성 설명 본문

Java/Java

[Spring Actuator] Example 2편 - 각 Info Endpoint 설명 및 Custom Info Endpoint 생성 설명

전봉근 2023. 11. 26. 13:53
반응형

이전글: https://bkjeon1614.tistory.com/782

 

[Spring Actuator] Example 2편 - 각 Info Endpoint 설명 및 Custom Info Endpoint 생성 설명

각 Info Endpoint 설명 및 Custom Info Endpoint 생성 설명 Health Endpoint show-details health 정보에서 components > diskSpace 의 details 필드에 상세 정보를 보여줌 [application.yml] ... management: endpoint: health: show-details: alway

bkjeon1614.tistory.com

 

각 Info Endpoint 설명 및 Custom Info Endpoint 생성 설명

  • Health Endpoint
    • show-details
      • health 정보에서 components > diskSpace 의 details 필드에 상세 정보를 보여줌
        [application.yml]
        ...
        
        management:
          endpoint:
            health:
              show-details: always    
        
        ...
        
      • 이렇게 하면 /actuator/health 접근 시 ping, diskSpace 2가지 밖에 표시가 안되는데 예를들어 "spring-boot-starter-data-jpa" 를 연결한다고하면 해당 라이브러리에서 상태 bean 까지 등록해주므로 이것을 사용할 경우 components 아래 jpa 관련 또 다른 정보가 추가되어 보여진다.
    • health 정보 들은 각 시스템관련 HealthIndicator 클래스에서 정보를 제공한다.
    • Custom HealthIndicator 를 만들어 보자.
      • 샘플코드
        [ApplicationHealthIndicator.java]
        import org.springframework.boot.actuate.health.Health;
        import org.springframework.boot.actuate.health.HealthIndicator;
        
        public class ApplicationHealthIndicator implements HealthIndicator {
        
            @Override
            public Health health() {
                boolean status = getStatus();   // 해당 Status 가 DOWN 이면 최상단의 Status 도 동일하게 DOWN 으로 된다.
                if (status) {
                    // withDetail 는 details 포함여부 기능 제공
                    return Health.up().withDetail("key1", "value2").withDetail("key2", "value2").build();
                }
        
                return Health.down().withDetail("key3", "value3").withDetail("key4", "value4").build();
            }
        
            boolean getStatus() {
                return System.currentTimeMillis() % 2 == 0;
            }
        
        }
        
        [ApplicationLibInfoEndpointConfig.java]
        ...
        
        @Bean
        public ApplicationHealthIndicator applicationHealthIndicator() {
            return new ApplicationHealthIndicator();
        }    
        
      • /actuator/health 에 접근하면 components > application (HealthIndicator 를 제외한 앞부분의 문자로 표시된다.) 객체에 status, details 에 key/value가 추가되어 표시되는걸 확인할 수 있다.
    • STATUS
      • /actuator/health 접근시 status: DOWN 일 때 503 Error 를 표시하는걸 볼 수 있다.
      • STATUS 종류
        • DOWN: 503
        • OUT_OF_SERVICE: 503
        • UP: 200
        • UNKNOWN: 200
  • Info Endpoint
    • 종류
      • build (default enable 이며 /META_INF/build-info.properties 파일이 있어야 된다.)
      • env
      • git (default enable 이며 /META_INF/git.properties 파일이 있어야 된다.)
      • java
      • os
    • 적용
      • Gradle
        [build.gradle]
        ...
        
        group 'com.bkjeon.codebase'
        version '0.0.0'
        sourceCompatibility = 11      
        
        ext {
            projectName = 'base-api'
        }
        
        processResources {
            filesMatching('**/application.yml') {
                expand(project.properties)
            }
        }      
        
        ...
        
        [application.yml]
        ...
        
        management:
        
        ...
        
        info: # /actuator/info 정보가 표시되는것을 볼 수 있다. 각 환경변수들은 build.gradle 정의된 내용을 가져온다. 또한 VM 으로도 주입받을 수 있다. (Ex: -Dinfo.name=bkjeon)
          app-info:
            projectName: ${ext.projectName} // gradle 에서는 ${xxx.xxx} 와 같은 형태인 특수문법을 사용하여 properties 에 해당하는 값을 읽어올 수 있다.
            group: ${group}
            version: ${version}
            javaVersion: ${sourceCompatibility}
        
      • Maven
        [pom.xml]
        ...
        
        <groupId>..</groupId>
        <artifactid>..</artifactid>
        <properties>
          <java.version>11</java.version>
          <custom.value>bkjeon</custom.value>
        </properties>
        
        [application.yml]
        ...
        
        management:
        
        ...
        
        info: # /actuator/info 정보가 표시되는것을 볼 수 있다. 각 환경변수들은 build.gradle 정의된 내용을 가져온다. 또한 VM 으로도 주입받을 수 있다. (Ex: -Dinfo.name=bkjeon)
          app-info:
          group-id: "@project.groupId@" # maven 에서는 @project.groupId@ 와 같은 방식으로 pom.xml 에 지정된 <artifactId>..</artifactId> 값을 가져올 수 있는 특수문법이다.
          artifact-id: "@project.artifactId@"
          custom: "@custom.value@"  # 상기 선언한 <custom.value>..</custom.value> 를 가져올 수 있다.
        
  • Env Endpoint
    • /actuator/env 에서는 모든 환경변수가 들어가 있다.
  • Git Info Endpoint
    • 필수 파일인 git.properties 생성이 필요하다. 해당 파일을 자동으로 생성되게끔 설정을 추가해보자.
    • maven 방법
      [pom.xml]
      ...
      
      <build>
        <plugins>
          <plugin>
            <groupId>io.github.git-commit-id</groupId>
            <artifactId>git-commit-id-maven-plugin</artifactId>
          </plugin>
        </plugins>
      </build>
      
      ...
      
    • gradle plugin 추가
      [build.gradle]
      plugins {
          id "com.gorylenko.gradle-git-properties" version "2.2.3"
      }  
      
      ...  
      
    • 빌드를 완료하면 /build/resources/main/git.properties 파일이 생성된 걸 확인할 수 있다. 실행해보면 git 의 정보들을 볼 수 있다. 그 상태에서 이제 서버를 구동한 후 /actuator/info 에 git 정보가 표시되는걸 확인할 수 있다.
      • 더 많은 정보를 보려면 yml 파일에 추가하자 [application.yml]
        management:
          ...
          info:
            ...
            git:
              enabled: true
              mode: full  # 기본이 simple 모드이므로 full 변경
        
  • Build Info Endpoint
    • 필수 파일인 build-info.properties 생성이 필요하다. 해당 파일을 자동으로 생성되게끔 설정을 추가해보자.
    • 설정 (https://docs.spring.io/spring-boot/docs/3.0.4/reference/html/howto.html#howto.build.generate-info)
      • maven (spring boot 버전하고 플러그인 버전하고 맞춰주자)
        [pom.xml]
        ...
        
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>3.0.4</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>build-info</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
        
        ...
        
      • gradle
        [build.gradle]
        springBoot {
            buildInfo()
        }
        
      • clean 후 빌드를 완료하면 /build/resources/main/META_INF/build-info.properties 파일이 생성된 걸 확인할 수 있다. 그 다음 서버를 구동하면 /actuator/info 에 build 가 추가된 걸 확인할 수 있다.
  • Custom Info Endpoint (하드코딩인경우 .yml 에 선언하는 방식이 더 유용하다. 해당 방식은 로직이 들어가야 될 경우에 사용)

 

 

참고

  • https://semtul79.tistory.com/
  • https://docs.spring.io/
  • https://www.inflearn.com/course/spring-boot-actuator-%ED%8C%8C%ED%97%A4%EC%B9%98%EA%B8%B0/dashboard
반응형
Comments