#35 springboot: custom metric - timer
This commit is contained in:
@@ -3,6 +3,7 @@ package hello;
|
||||
import hello.order.v0.OrderConfigV0;
|
||||
import hello.order.v1.OrderConfigV1;
|
||||
import hello.order.v2.OrderConfigV2;
|
||||
import hello.order.v3.OrderConfigV3;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.actuate.web.exchanges.InMemoryHttpExchangeRepository;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
@@ -11,7 +12,8 @@ import org.springframework.context.annotation.Import;
|
||||
|
||||
//@Import(OrderConfigV0.class)
|
||||
//@Import(OrderConfigV1.class)
|
||||
@Import(OrderConfigV2.class)
|
||||
//@Import(OrderConfigV2.class)
|
||||
@Import(OrderConfigV3.class)
|
||||
@SpringBootApplication(scanBasePackages = "hello.controller")
|
||||
public class ActuatorApplication {
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package hello.order.v3;
|
||||
|
||||
import hello.order.OrderService;
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class OrderConfigV3 {
|
||||
|
||||
@Bean
|
||||
public OrderService orderService(MeterRegistry registry) {
|
||||
return new OrderServiceV3(registry);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package hello.order.v3;
|
||||
|
||||
import hello.order.OrderService;
|
||||
import io.micrometer.core.annotation.Counted;
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import io.micrometer.core.instrument.Timer;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class OrderServiceV3 implements OrderService {
|
||||
|
||||
private final MeterRegistry registry;
|
||||
private final AtomicInteger stock = new AtomicInteger(100);
|
||||
|
||||
@Override
|
||||
public void order() {
|
||||
Timer timer = Timer.builder("my.order")
|
||||
.tag("class", this.getClass().getName())
|
||||
.tag("method", "order")
|
||||
.description("order")
|
||||
.register(registry);
|
||||
|
||||
timer.record(() -> {
|
||||
log.info("주문");
|
||||
stock.decrementAndGet();
|
||||
sleep(500);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancel() {
|
||||
Timer timer = Timer.builder("my.order")
|
||||
.tag("class", this.getClass().getName())
|
||||
.tag("method", "cancel")
|
||||
.description("order")
|
||||
.register(registry);
|
||||
|
||||
timer.record(() -> {
|
||||
log.info("취소");
|
||||
stock.incrementAndGet();
|
||||
sleep(200);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public AtomicInteger getStock() {
|
||||
return stock;
|
||||
}
|
||||
|
||||
private void sleep(int l) {
|
||||
try {
|
||||
Thread.sleep(l + new Random().nextInt(200));
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user