1. Spring Boot Actuator
-
์ผ๋จ.. ์์ํActuator ์ด๋??
An actuator is a manufacturing term that refers to a mechanical device for moving or controlling something. Actuators can generate a large amount of motion from a small change.
๋ฌผ๊ฑด์ ์์ง์ด๊ฑฐ๋ ์ปจํธ๋กคํ๊ธฐ ์ํ ์ฅ์น๋ฅผ ์๋ฏธํ๋ ์ ์กฐ ์ฉ์ด๋ก, ์์ ๋ณํ๋ก ํฐ ๋ณํ๋ฅผ ์ผ์ผํฌ ์ ์๋ค.
-
์ฑ์ ๋ชจ๋ํฐ๋ง ํ๊ฑฐ๋ ๋ฉํธ๋ฆญ์ ์์งํ๊ฑฐ๋ ํธ๋ํฝ ํน์ ๋ฐ์ดํฐ ๋ฒ ์ด์ค ์ํ๋ฅผ ์ดํดํ๋ ๊ฒ์ ์ฝ๊ฒ ํ๋ ๊ฒ
-
์ค๋ช ๋ง ๋ด์ ์๋ฏ๋ง๋ฏํ๊ฒ ๋ชจ๋ฅด๊ฒ ๊ณ , ์ ๋ฆฌ๋ฅผ ํ๋ ์์ ์๋ ๋ฑ ์ด๊ฑฐ๋ค! ํ๊ณ ๋ ์ค๋ฅด์ง ์๋ค์.
-
๋ชจ๋ ๋ด์ฉ์ spring boot2.x ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์์ฑํ์ต๋๋ค. 1.x ํ๊ณ ๋ ์ฝ๊ฐ ๋ฌ๋ผ์
2. ์์กด์ฑ ์ถ๊ฐ
- maven ์ผ ๊ฒฝ์ฐ
|
|
- Gradle ์ผ ๊ฒฝ์ฐ
|
|
3. Endpoints
-
์๋ํฌ์ธํธ๋ฅผ ์ฌ์ฉํ๋ฉด ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ชจ๋ํฐ๋งํ๊ณ ์ํธ ์์ฉํ ์ ์์
-
spring boot 2.x ์์๋
/health
,/info
2๊ฐ๋ง ๊ธฐ๋ณธ์ ์ผ๋ก ์ ๊ณต -
๋ชจ๋ ๊ธฐ๋ฅ์ ์ฌ์ฉํ๋ ค๋ฉด ํ๋กํผํฐ์ ์ถ๊ฐ
management.endpoints.web.exposure.include = *
-
๊ธฐ๋ณธ์ ์ธ /health ๋ฅผ ํ๋ฉด
1 2 3
{ "status": "UP" }
๋ง ๋์ค๋ฉฐ healthIndicator ์ ๊ตฌํํ์ฌ ์ถ๊ฐ์ ์ผ๋ก ์ปค์คํ ๋ ๊ฐ๋ฅ
-
ex)
1 2 3 4 5 6 7 8 9 10 11 12 13
@Component public class MyHealthIndicator implements HealthIndicator { @Override public Health health() { int errorCode = check(); // perform some specific health check if (errorCode != 0) { return Health.down().withDetail("Error Code", errorCode).build(); } return Health.up().build(); } }
4. Metrics
-
Micrometer ์ง์, ์ง์ ์ํธ์์ฉ
-
MeterRegistry ์ ํ์ Bean ์ด ์๋ ๊ตฌ์ฑ
-
์ด๋ฒ ๊ธ์ ์ฐ๊ฒ ๋ ์ด์ -
/metrics ๋ฅผ ํ๋ฉด
1 2 3 4 5 6 7 8 9 10 11 12 13
{ "names": [ "http.server.requests", "jdbc.connections.idle", "tomcat.sessions.rejected", "hikaricp.connections.max", "hikaricp.connections.min", "process.cpu.usage", "jvm.memory.max", "jvm.threads.states", //.... ] }
์ด๋ฐ ์์ผ๋ก ๋จ๊ณ , ์ค์ ๊ฐ์ ์ป์ผ๋ ค๋ฉด ์ํ๋ ๋ฉํธ๋ฆญ (/actuator/metrics/jvm.memory.max) ์ผ๋ก ์ด๋ํด์ผ ๋จ
-
/actuator/metrics/jvm.memory.max ์ ๊ฒฝ์ฐ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
{ "name": "jvm.memory.max", "description": "The maximum amount of memory in bytes that can be used for memory management", "baseUnit": "bytes", "measurements": [ { "statistic": "VALUE", "value": 9860284415 } ], "availableTags": [ { "tag": "area", "values": [ "heap" //... ] } ] }
-
๋น์ฐํ๊ฒ๋ ์ปค์คํ ๋ฉํธ๋ฆญ ๋ํ ์ถ๊ฐ ๊ฐ๋ฅ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
@Component public class SampleBean { private final Counter counter; private Tag tag = Tag.of("status", "ok") public SampleBean(MeterRegistry registry) { this.counter = registry.counter("received.message", "status", "all"); } public void handleMessage(String message) { this.counter.increment(); // handle message implementation } }
-
metrics ์ ๋ฑ๋ก๋ ๊ฒ ํ์ธ
1 2 3 4 5 6 7 8 9
{ "names": [ "http.server.requests", "jdbc.connections.idle", "tomcat.sessions.rejected", //.... "received.message" ] }
-
/actuator/metrics/received.message ์์ ๋ด์ฉ์ ํ์ธํด๋ณด๋ฉด
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
{ "name": "received.message", "description": null, "baseUnit": null, "measurements": [ { "statistic": "COUNT", "value": 1 // ํธ์ถ ํ์ } ], "availableTags": [ { "tag": "server_name", "values": [ "sample-api" ] }, { "tag": "profile", "values": [ "local" ] }, { "tag": "status", "values": [ "all" ] } ] }
์ด๋ฐ ์์ผ๋ก ์ ๋ณด๋ฅผ ์ ์ ์์
5. Custom Endpoint ์์ฑ
-
์ปค์คํ ์ค๋ ํฌ์ธํธ๋ฅผ ์์ฑํ๋ ๊ฒ์ด ๊ฐ๋ฅ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
@Component @Endpoint(id = "features") public class FeaturesEndpoint { private Map<String, Feature> features = new ConcurrentHashMap<>(); @ReadOperation public Map<String, Feature> features() { return features; } @ReadOperation public Feature feature(@Selector String name) { return features.get(name); } @WriteOperation public void configureFeature(@Selector String name, Feature feature) { features.put(name, feature); } @DeleteOperation public void deleteFeature(@Selector String name) { features.remove(name); } public static class Feature { private Boolean enabled; // [...] getters and setters } }
-
์์ ๊ฒฝ์ฐ๋ /actuator/features ๋ก ์ ์ ๊ฐ๋ฅ
- @ReadOperation โ HTTP GET์ ๋งคํ
- @WriteOperation โ HTTP POST์ ๋งคํ
- @DeleteOperation โ HTTP DELETE์ ๋งคํ
-
์ด ์ธ์๋ actuator ์ ๋ํ ๋ง์ ๊ธฐ๋ฅ๋ค์ด ์์ต๋๋ค. ์๋ํฌ์ธํธ๋ฅผ ํ์ฅํ๋ค๊ฑฐ๋ logger ์ ๊ด๋ จ๋ ๊ฒ์ด๋ผ๋๊ฐ, ๋ณด์๊ณผ ๊ด๋ จ๋ ๊ธฐ๋ฅ์ด๋ผ๋๊ฐ…
-
๋ฉํธ๋ฆญ ์ค์ ์ ์ถ๊ฐํ๋ฉด์ ๋ง๋ณด๊ธฐ๋ก ์์๋ณธ ๊ฒ๋ค์ api ๋ฌธ์ ์ฐธ๊ณ ํ๋ฉด์ ์์ฑํ๊ธฐ ๋๋ฌธ์ ๋น์ฝํ๊ฑฐ๋, ์๋ชป๋ ๋ด์ฉ์ด ์์ ์ ์์ต๋๋ค.
์ฐธ๊ณ