#35 springboot: custom metric - counter

This commit is contained in:
haerong22
2023-03-26 01:08:13 +09:00
parent 4c83abd161
commit b4144f9a56
4 changed files with 69 additions and 2 deletions

View File

@@ -1,13 +1,15 @@
package hello;
import hello.order.v0.OrderConfigV0;
import hello.order.v1.OrderConfigV1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.web.exchanges.InMemoryHttpExchangeRepository;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
@Import(OrderConfigV0.class)
//@Import(OrderConfigV0.class)
@Import(OrderConfigV1.class)
@SpringBootApplication(scanBasePackages = "hello.controller")
public class ActuatorApplication {

View File

@@ -19,7 +19,7 @@ public class OrderServiceV0 implements OrderService {
@Override
public void cancel() {
log.info("취소");
stock.decrementAndGet();
stock.incrementAndGet();
}
@Override

View File

@@ -0,0 +1,16 @@
package hello.order.v1;
import hello.order.OrderService;
import hello.order.v0.OrderServiceV0;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class OrderConfigV1 {
@Bean
OrderService orderService(MeterRegistry registry) {
return new OrderServiceV1(registry);
}
}

View File

@@ -0,0 +1,49 @@
package hello.order.v1;
import hello.order.OrderService;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.atomic.AtomicInteger;
@Slf4j
@RequiredArgsConstructor
public class OrderServiceV1 implements OrderService {
private final MeterRegistry registry;
private final AtomicInteger stock = new AtomicInteger(100);
@Override
public void order() {
log.info("주문");
stock.decrementAndGet();
Counter.builder("my.order")
.tag("class", this.getClass().getName())
.tag("method", "order")
.description("order")
.register(registry)
.increment();
}
@Override
public void cancel() {
log.info("취소");
stock.incrementAndGet();
Counter.builder("my.order")
.tag("class", this.getClass().getName())
.tag("method", "cancel")
.description("order")
.register(registry)
.increment();
}
@Override
public AtomicInteger getStock() {
return stock;
}
}