아무거나

[Spring Actuator] Example 4편 - Metrics Tags 본문

Java/Spring

[Spring Actuator] Example 4편 - Metrics Tags

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

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

 

  • Metrics Tags
    • /actuator/metrics/{메트릭명} 에 접속할 경우 availableTags 에 존재하는 tag 의 values 값에 대한 상세 정보를 보고 싶다면
      • 만약 tag: test, value: bong 이라고 가정해볼경우 /actuator/metrics/{메트릭명}?tag=test:bong (즉, tag={key}:{value} Querystring 형태이다)
    • metrics 에 tag 를 직접 넣어보자.
      • 샘플코드
        [TagController.java]
        package com.example.bkjeon.base.services.api.v1.actuator.controller;
        
        import com.example.bkjeon.base.services.api.v1.actuator.service.MyQueueManagerWithTags;
        import lombok.RequiredArgsConstructor;
        import org.springframework.http.HttpStatus;
        import org.springframework.web.bind.annotation.GetMapping;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RestController;
        
        @RestController
        @RequestMapping("v1/tags")
        @RequiredArgsConstructor
        public class TagController {
        
            private final MyQueueManagerWithTags myQueueManagerWithTags;
        
            @GetMapping("push")
            public String push() {
                myQueueManagerWithTags.push();
                return HttpStatus.OK.toString();
            }
        
            @GetMapping("pop")
            public String pop() {
                myQueueManagerWithTags.pop();
                return HttpStatus.OK.toString();
            }
        
        }
        
        [MyQueueManagerWithTags.java]
        package com.example.bkjeon.base.services.api.v1.actuator.service;
        
        import io.micrometer.core.instrument.Counter;
        import io.micrometer.core.instrument.MeterRegistry;
        import lombok.RequiredArgsConstructor;
        import org.springframework.stereotype.Service;
        
        @Service
        @RequiredArgsConstructor
        public class MyQueueManagerWithTags {
        
            private final MeterRegistry meterRegistry;
        
            public void push() {
                Counter.builder("my.queue.counter")
                    .tag("type", "push")
                    .tag("class", this.getClass().toString())    // tag 는 여러개를 넣을 수 있다.
                    .register(meterRegistry).increment();
            }
        
            public void pop() {
                Counter.builder("my.queue.counter")
                    .tag("type", "pop")
                    .tag("class", this.getClass().toString())
                    .register(meterRegistry).increment();
            }
        
        }
        
      • 샘플코드2
        • @Counted 를 사용하여 간소화 시킬 수 있다.
          [TagController.java]
          package com.example.bkjeon.base.services.api.v1.actuator.controller;
          
          import io.micrometer.core.annotation.Counted;
          import lombok.RequiredArgsConstructor;
          import org.springframework.http.HttpStatus;
          import org.springframework.web.bind.annotation.GetMapping;
          import org.springframework.web.bind.annotation.RequestMapping;
          import org.springframework.web.bind.annotation.RestController;
          
          @RestController
          @RequestMapping("v1/tags")
          @RequiredArgsConstructor
          public class TagController {
          
              // private final MyQueueManagerWithTags myQueueManagerWithTags;
          
              // extraTags 는 추가적으로 Tag 가 필요시에 사용하면되고 형태는 key, value, key, value ... 이다
              @Counted(value = "my.counted", extraTags = { "type", "value1", "type2", "value2" })
              @GetMapping("push")
              public String push() {
                  // myQueueManagerWithTags.push();
                  return HttpStatus.OK.toString();
              }
          
              @Counted(value = "my.counted", extraTags = { "type3", "value3", "type4", "value4" })
              @GetMapping("pop")
              public String pop() {
                  // myQueueManagerWithTags.pop();
                  return HttpStatus.OK.toString();
              }
          
          }
          
      • 상기 Counter.builder() 를 확인해보면 Tag 가 다를 시 별개의 카운터가 만들어지며 이름이 같으면 같은걸로 뭉쳐질 수 있다.

참고

  • 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