#35 springboot: custom metric - gauge
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
package hello;
|
package hello;
|
||||||
|
|
||||||
|
import hello.order.gauge.StockConfigV1;
|
||||||
|
import hello.order.gauge.StockConfigV2;
|
||||||
import hello.order.v0.OrderConfigV0;
|
import hello.order.v0.OrderConfigV0;
|
||||||
import hello.order.v1.OrderConfigV1;
|
import hello.order.v1.OrderConfigV1;
|
||||||
import hello.order.v2.OrderConfigV2;
|
import hello.order.v2.OrderConfigV2;
|
||||||
@@ -15,7 +17,9 @@ import org.springframework.context.annotation.Import;
|
|||||||
//@Import(OrderConfigV1.class)
|
//@Import(OrderConfigV1.class)
|
||||||
//@Import(OrderConfigV2.class)
|
//@Import(OrderConfigV2.class)
|
||||||
//@Import(OrderConfigV3.class)
|
//@Import(OrderConfigV3.class)
|
||||||
@Import(OrderConfigV4.class)
|
//@Import(OrderConfigV4.class)
|
||||||
|
//@Import({OrderConfigV4.class, StockConfigV1.class})
|
||||||
|
@Import({OrderConfigV4.class, StockConfigV2.class})
|
||||||
@SpringBootApplication(scanBasePackages = "hello.controller")
|
@SpringBootApplication(scanBasePackages = "hello.controller")
|
||||||
public class ActuatorApplication {
|
public class ActuatorApplication {
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package hello.order.gauge;
|
||||||
|
|
||||||
|
import hello.order.OrderService;
|
||||||
|
import io.micrometer.core.instrument.Gauge;
|
||||||
|
import io.micrometer.core.instrument.MeterRegistry;
|
||||||
|
import jakarta.annotation.PostConstruct;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class StockConfigV1 {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public MyStockMetric myStockMetric(OrderService orderService, MeterRegistry registry) {
|
||||||
|
return new MyStockMetric(orderService, registry);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
static class MyStockMetric {
|
||||||
|
|
||||||
|
private final OrderService orderService;
|
||||||
|
private final MeterRegistry registry;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
Gauge.builder("my.stock", orderService, service -> {
|
||||||
|
log.info("stock gauge call");
|
||||||
|
return service.getStock().get();
|
||||||
|
}).register(registry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package hello.order.gauge;
|
||||||
|
|
||||||
|
import hello.order.OrderService;
|
||||||
|
import io.micrometer.core.instrument.Gauge;
|
||||||
|
import io.micrometer.core.instrument.binder.MeterBinder;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Configuration
|
||||||
|
public class StockConfigV2 {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public MeterBinder stockSize(OrderService orderService) {
|
||||||
|
return registry -> Gauge.builder("my.stock", orderService, service -> {
|
||||||
|
log.info("stock gauge call");
|
||||||
|
return service.getStock().get();
|
||||||
|
}).register(registry);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user