4.6. MicroProfile 지표 개발

4.6.1. MicroProfile Metrics 애플리케이션 생성

애플리케이션에 대한 요청 수를 반환하는 애플리케이션을 생성합니다.

프로세스

  1. 다음 내용으로 클래스 파일 HelloService.java 를 생성합니다.

    package com.example.microprofile.metrics;
    
    public class HelloService {
        String createHelloMessage(String name){
            return "Hello" + name;
        }
    }
  2. 다음 내용으로 클래스 파일 HelloWorld.java 를 생성합니다.

    package com.example.microprofile.metrics;
    
    import javax.inject.Inject;
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import org.eclipse.microprofile.metrics.annotation.Counted;
    
    @Path("/")
    public class HelloWorld {
    @Inject
        HelloService helloService;
    
    @GET
    @Path("/json")
        @Produces({ "application/json" })
        @Counted(name = "requestCount",
      		 absolute = true,
    description = "Number of times the getHelloWorldJSON was requested")
        public String getHelloWorldJSON() {
            return "{\"result\":\"" + helloService.createHelloMessage("World") + "\"}";
        }
    }
  3. pom.xml 파일을 업데이트하여 다음 종속성을 포함합니다.

    <dependency>
        <groupId>org.eclipse.microprofile.metrics</groupId>
        <artifactId>microprofile-metrics-api</artifactId>
        <scope>provided</scope>
    </dependency>
  4. 다음 Maven 명령을 사용하여 애플리케이션을 빌드합니다.

    $ mvn clean install wildfly:deploy
  5. 메트릭을 테스트합니다.

    1. CLI에서 다음 명령을 실행합니다.

      $ curl -v http://localhost:9990/metrics |  grep request_count | grep helloworld-rs-metrics

      예상 출력:

      jboss_undertow_request_count_total{deployment="helloworld-rs-metrics.war",servlet="org.jboss.as.quickstarts.rshelloworld.JAXActivator",subdeployment="helloworld-rs-metrics.war",microprofile_scope="vendor"} 0.0
    2. 브라우저에서 URL http://localhost:8080/helloworld-rs/rest/json으로 이동합니다.
    3. CLI에서 다음 명령을 다시 실행합니다.

      $ curl -v http://localhost:9990/metrics |  grep request_count | grep helloworld-rs-metrics

      예상 출력:

      jboss_undertow_request_count_total{deployment="helloworld-rs-metrics.war",servlet="org.jboss.as.quickstarts.rshelloworld.JAXActivator",subdeployment="helloworld-rs-metrics.war",microprofile_scope="vendor"} 1.0