2023. 5. 22. 16:50ใ์ธํ๋ฐ/์คํ๋ง ๋ถํธ - ํต์ฌ ์๋ฆฌ์ ํ์ฉ
์นด์ดํฐ ๋ฉํธ๋ฆญ ๋ฑ๋ก ๋ฐ ๋ชจ๋ํฐ๋ง (์คํ๋ง, ํ๋ก๋ฉํ ์ฐ์ค, ๊ทธ๋ผํ๋)
์คํ๋ง & ํ๋ก๋ฉํ ์ฐ์ค & ๊ทธ๋ผํ๋ (๊ทธ๋ผํ๋ ์ค์น, ์ฐ๋, ์ฌ์ฉ ๋ฐฉ๋ฒ) (tistory.com) ์คํ๋ง & ํ๋ก๋ฉํ ์ฐ์ค & ๊ทธ๋ผํ๋ (๊ทธ๋ผํ๋ ์ค์น, ์ฐ๋, ์ฌ์ฉ ๋ฐฉ๋ฒ) ์คํ๋ง & ํ๋ก๋ฉํ ์ฐ์ค (์ค์น, ์ค์ , ๊ธฐ๋ณธ ๊ธฐ
hyejin.tistory.com
Timer ๋ฉํธ๋ฆญ ๋ฑ๋ก ๋ฐ ๋ชจ๋ํฐ๋ง (์คํ๋ง, ํ๋ก๋ฉํ ์ฐ์ค, ๊ทธ๋ผํ๋) (tistory.com)
Timer ๋ฉํธ๋ฆญ ๋ฑ๋ก ๋ฐ ๋ชจ๋ํฐ๋ง (์คํ๋ง, ํ๋ก๋ฉํ ์ฐ์ค, ๊ทธ๋ผํ๋)
์นด์ดํฐ ๋ฉํธ๋ฆญ ๋ฑ๋ก ๋ฐ ๋ชจ๋ํฐ๋ง (์คํ๋ง, ํ๋ก๋ฉํ ์ฐ์ค, ๊ทธ๋ผํ๋) (tistory.com) ์นด์ดํฐ ๋ฉํธ๋ฆญ ๋ฑ๋ก ๋ฐ ๋ชจ๋ํฐ๋ง (์คํ๋ง, ํ๋ก๋ฉํ ์ฐ์ค, ๊ทธ๋ผํ๋) ์คํ๋ง & ํ๋ก๋ฉํ ์ฐ์ค & ๊ทธ๋ผํ๋ (๊ทธ๋ผํ๋
hyejin.tistory.com
์์ ๋ ๊ธ์์๋ ์นด์ดํฐ ๊ฐ์ ๋ํด์ ์ง์ ๋ฉํธ๋ฆญ์ ๋ฑ๋กํด์ ๊ทธ๋ผํ๋ ๋์๋ณด๋์ ๋ํ๋์๋ค.
์ด๋ฒ์๋ ๊ฒ์ด์ง๋ฅผ ๋ฑ๋กํด์ ํ์ธํด๋ณผ ๊ฒ์ด๋ค. ๐โ๏ธ
1๏ธโฃ ๋ฉํธ๋ฆญ ๋ฑ๋ก 5 - ๊ฒ์ด์ง
Gauge ๊ฒ์ด์ง
: ๊ฒ์ด์ง๋ ์์๋ก ์ค๋ฅด๋ด๋ฆด ์ ์๋ ๋จ์ผ ์ซ์ ๊ฐ์ ๋ํ๋ด๋ ๋ฉํธ๋ฆญ์ผ๋ก ๊ฐ์ ํ์ฌ ์ํ๋ฅผ ๋ณด๋๋ฐ ์ฌ์ฉํ๋ค.
-> ๋ฐ๋ผ์ ๊ฐ์ด ์ฆ๊ฐํ๊ฑฐ๋ ๊ฐ์ํ ์ ์๋ค.
ex) ์ฐจ๋์ ์๋, CPU ์ฌ์ฉ๋, ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋
๐ฉ๐ป ์ฐธ๊ณ : ๊ฒ์ด์ง์ ์นด์ดํฐ๊ฐ ํท๊ฐ๋ฆฐ๋ค๋ฉด ๊ฐ์ด ๊ฐ์ํ ์ ์๋๊ฐ๋ฅผ ๊ธฐ์ค์ผ๋ก ์๊ฐํด๋ณด๋ฉด ๋๋ค.
StockConfigV1
@Configuration
public class StockConfigV1
{
@Bean
public MyStockMetric myStockMetric(OrderService orderService, MeterRegistry meterRegistry)
{
return new MyStockMetric(orderService, meterRegistry);
}
@Slf4j
static class MyStockMetric
{
private OrderService orderService;
private MeterRegistry meterRegistry;
public MyStockMetric(OrderService orderService, MeterRegistry meterRegistry)
{
this.orderService = orderService;
this.meterRegistry = meterRegistry;
}
@PostConstruct
public void init()
{
Gauge.builder("my.stock", orderService, service -> {
log.info("stock gague call");
return service.getStock().get();
}).register(meterRegistry);
}
}
}
my.stock์ด๋ผ๋ ๊ฒ์ด์ง๋ฅผ ๋ฑ๋กํ๋๋ฐ, ์ด๋ ํจ์๋ฅผ ํจ๊ป ์ ๋ฌํ๋ค. ์ด ํจ์๋ ์ธ๋ถ์์ ๋ฉํธ๋ฆญ์ ํ์ธํ ๋ ๋ง๋ค ํธ์ถ๋๋ ๊ฒ์ผ๋ก ์ด ํจ์์ ๋ฐํ๊ฐ์ด ๊ฒ์ด์ง์ ๊ฐ์ด๋ค.
ActuatorApplication
@Import({OrderConfigV4.class, StockConfigV1.class})
@SpringBootApplication(scanBasePackages = "hello.controller")
public class ActuatorApplication {
public static void main(String[] args) {
SpringApplication.run(ActuatorApplication.class, args);
}
}
* ์คํ
์ ํ๋ฆฌ์ผ์ด์ ์ ์คํํ๋ฉด stock gauge call ๋ก๊ทธ๊ฐ ์ฃผ๊ธฐ์ ์ผ๋ก ๋จ๋ ๊ฒ์ ํ์ธํ ์ ์๋๋ฐ
๊ฒ์ด์ง๋ฅผ ํ์ธํ๋ ํจ์๋ ์ธ๋ถ์์ ๋ฉํธ๋ฆญ์ ํ์ธํ ๋ ํธ์ถ๋๋ค.
-> ์ง๊ธ ํ๋ก๋ฉํ ์ฐ์ค๊ฐ ๋ฉํธ๋ฆญ์ ์ฃผ๊ธฐ์ ์ผ๋ก ํ์ธํ๊ธฐ ๋๋ฌธ์ ๊ณ์ ๋ก๊ทธ๊ฐ ๋จ๋๋ค.
http://localhost:8080/actuator/prometheus
๋ง์ฝ ํ๋ก๋ฉํ ์ฐ์ค ์ข ๋ฃํ๋ฉด ๋์ด์ ๋ก๊ทธ๊ฐ ์ถ๋ ฅ๋์ง ์๋ ๊ฒ์ ์ ์ ์๋ค.
http://localhost:8080/actuator/metrics/my.stock
๊ฒ์ด์ง๋ ๋์ ๊ฐ์ด ํ์์๊ณ ํ์ฌ ๊ฐ๋ง ์์ผ๋ฉด ๋๊ธฐ ๋๋ฌธ์ ํ์ฌ๊ฐ๋ง ๋ฐํํด์ค๋ค.
* ๊ทธ๋ผํ๋ ๋ฑ๋ก
2๏ธโฃ ๊ฒ์ด์ง ๊ฐ๋จ ๋ฑ๋ก
StockConfigV2
@Slf4j
@Configuration
public class StockConfigV2
{
@Bean
public MeterBinder stockSize(OrderService orderService)
{
return registry -> Gauge.builder("my.stock", orderService, service -> {
log.info("stock gague call");
return service.getStock().get();
}).register(registry);
}
}
ActuatorApplication
@Import({OrderConfigV4.class, StockConfigV2.class})
@SpringBootApplication(scanBasePackages = "hello.controller")
public class ActuatorApplication {
public static void main(String[] args) {
SpringApplication.run(ActuatorApplication.class, args);
}
@Bean
public InMemoryHttpExchangeRepository httpExchangeRepository()
{
return new InMemoryHttpExchangeRepository();
}
}