Tags

๐Ÿง‘โ€๐Ÿ’ป ์Šคํ”„๋ง๋ถ€ํŠธ์™€ ๋งˆ์ดํฌ๋กœ๋ฏธํ„ฐ, ์—˜๋ผ์Šคํ‹ฑ์„œ์น˜๋ฅผ ์ด์šฉํ•œ ๋งคํŠธ๋ฆญ ๋ชจ๋‹ˆํ„ฐ๋ง ์‹œ์Šคํ…œ ๊ตฌ์ถ•ํ•˜๊ธฐ

namjug-kim โ€ข 2020๋…„ 2์›” 03์ผ
spring
monitoring

์†Œ๊ฐœ

์ด ํฌ์ŠคํŠธ๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งคํŠธ๋ฆญ ํˆด์ธ micometer์™€ Elastic Search, Kibana๋ฅผ ์ด์šฉํ•œ ๋Œ€์‹œ๋ณด๋“œ ๊ตฌ์ถ•์— ๋Œ€ํ•œ ์†Œ๊ฐœ๋ฅผ ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

Micrometer๋ž€?

micrometer๋Š” Spring 5๋ถ€ํ„ฐ ๋ฉ”ํŠธ๋ฆญ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ํฌํ•จ๋œ ํ”„๋ ˆ์ž„์›Œํฌ์ž…๋‹ˆ๋‹ค. Think SLF4J, but for metrics ๋ผ๋Š” ์„ค๋ช…๊ณผ ๊ฐ™์ด slf4j์™€ ๊ฐ™์ด ๋ฐด๋” ์ค‘๋ฆฝ์  API๋ฅผ ์ œ๊ณตํ•ด์„œ ์‹ค์ œ ๋งคํŠธ๋ฆญ ์ง‘๊ณ„๋ฅผ ์œ„ํ•œ ๋„๊ตฌ์™€ ๊ด€๊ณ„ ์—†์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์•„๋ž˜์™€ ๊ฐ™์€ API๋ฅผ ํ†ตํ•ด ๋ฐด๋” ์ค‘๋ฆฝ์ ์ธ Metrics ์ง‘๊ณ„ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑ ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ค๋‹ˆ๋‹ค.

public interface MeterBinder {
    void bindTo(@NonNull MeterRegistry registry);
}

๊ธฐ๋ณธ์ ์œผ๋กœ Jvm, JDBC, db, disk, logback, webflux, tomcat, web mvc๋“ฑ์„ ํฌํ•จํ•˜๊ณ  ์žˆ์œผ๋ฉฐ resillience4j, armeria๋“ฑ๊ณผ ๊ฐ™์ด spring ์ƒํƒœ๊ณ„์—์„œ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๊ฒฝ์šฐ์—๋Š” MeterBinder๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์œผ๋ฉฐ ์—†๋”๋ผ๋„ ์†์‰ฝ๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์–ด ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์‰ฝ๊ฒŒ ๋ชจ๋‹ˆํ„ฐ๋งํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์™œ ElasticSearch๋ฅผ ์‚ฌ์šฉํ–ˆ๋Š”๊ฐ€?

์ตœ๊ทผ ํด๋ผ์šฐ๋“œํ™˜๊ฒฝ ๋ชจ๋‹ˆํ„ฐ๋ง์—์„œ ๋งŽ์ด ์‚ฌ์šฉ๋˜๊ณ ์žˆ๋Š” prometheus์™€ grafana์˜ ์กฐํ•ฉ์ด ์žˆ๊ณ  ๋งŽ์€ ์žฅ์ ์„ ๊ฐ€์ง€๊ณ  ์žˆ์ง€๋งŒ ํŒ€์˜ ๊ทœ๋ชจ๊ฐ€ ํฌ์ง€ ์•Š๊ณ  ํŒ€๋‚ด์—์„œ ์ถ”๊ฐ€์ ์ธ ์ธํ”„๋ผ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š”๋Œ€ ๋“ค์–ด๊ฐ€๋Š” ๋ถ€๋‹ด๊ฐ์œผ๋กœ ๊ธฐ์กด์— ํŒ€๋‚ด์—์„œ ์šด์˜์ค‘์ด๋˜ ElasticSearch๋ฅผ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค.

Micrometer ElasticSearch Registry ์ถ”๊ฐ€ํ•˜๊ธฐ

dependencies {
    // web
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web'

    // actuator
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator'
    runtimeOnly group: 'io.micrometer', name: 'micrometer-registry-elastic', version: "$micrometerVersion"
}

์œ„ ์ฝ”๋“œ์™€ ๊ฐ™์ด micrometer-registry-elastic ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.

management:
  metrics:
    export:
      elastic:
        enabled: true
        index: metrics
        host: http://{elastic-search-host}

application.yml ํŒŒ์ผ์— elastic export๋ฅผ enabled true๋กœ ์„ค์ •ํ•˜๊ณ  ์ธ๋ฑ์Šค์™€ ํ˜ธ์ŠคํŠธ๋ฅผ ์„ค์ • ํ•ด์ค๋‹ˆ๋‹ค.

image ์„ค์ •๊นŒ์ง€ ๋๋งˆ์ณค๋‹ค๋ฉด kibana์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฌธ์„œ๊ฐ€ ์ƒ์„ฑ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ตฌ๋ถ„์„ ์œ„ํ•œ ํƒœ๊ทธ ์ถ”๊ฐ€ํ•˜๊ธฐ

๋Œ€๋žต์ ์œผ๋กœ ๋ชจ๋“  ์„ค์ •์„ ๋๋งˆ์ณค์ง€๋งŒ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‚ฌ์šฉํ•˜๋Š” ํ™˜๊ฒฝ์—์„œ๋Š” ๊ฐ๊ฐ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•œ ํƒœ๊ทธ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

management.metrics.tags.{tagName} ์„ค์ •์„ ํ†ตํ•ด์„œ ๊ฐ metric์— ์ถ”๊ฐ€์ ์ธ ํƒœ๊ทธ๋ฅผ ์„ค์ • ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์ธ์Šคํ„ด์Šค ๊ตฌ๋ถ„์„ ์œ„ํ•œ host์™€ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ตฌ๋ถ„์„ ์œ„ํ•œ application ํƒœ๊ทธ๋ฅผ ์ถ”๊ฐ€ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

์„ค์ •์„ ํ•˜๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๊ฒ ์ง€๋งŒ ์ €ํฌ๋Š” aws ec2์œ„์— docker ๋ฐฐํฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ๋•Œ๋ฌธ์— -Dmanagement.metrics.tags.host=${EC2_AGENT_ID} -Dmanagement.metrics.tags.application=${APPLICATION_NAME}์™€ ๊ฐ™์ด ์™ธ๋ถ€ ์„ค์ •์„ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค.

ํ‚ค๋ฐ”๋‚˜ ๋Œ€์‹œ๋ณด๋“œ ์ถ”๊ฐ€ํ•˜๊ธฐ

๋‹ค์–‘ํ•œ ํ…œํ”Œ๋ฆฟ์ด ์กด์žฌํ•˜๋Š” grafana์™€ ๋‹ฌ๋ฆฌ spring boot์™€ micrometer ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด์—ฌ์ฃผ๊ธฐ์œ„ํ•œ kibana ๋Œ€์‹œ๋ณด๋“œ ๋ฐ์ดํ„ฐ๋Š” ๊ทนํžˆ ์ ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ์ค‘์—์„œ ๊ฐ€์žฅ ๋ณด๊ธฐ์ข‹๊ฒŒ ๊ตฌ์„ฑ๋œ https://github.com/acroquest/micrometer-kibana-dashboard ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋Œ€์‹œ๋ณด๋“œ๋ฅผ ๊ตฌ์ถ•ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

kibana๊ฐ€ ์„ธํŒ…๋˜์–ด์žˆ๋‹ค๋ฉด ํ•ด๋‹น ๋ ˆํฌ์ง€ํ† ๋ฆฌ์˜ kibana_setup.sh๋ฅผ ์ด์šฉํ•˜์—ฌ ์†์‰ฝ๊ฒŒ ๊ธฐ๋ณธ์ ์ธ ๋Œ€์‹œ๋ณด๋“œ๋ฅผ ๊ตฌ์ถ• ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

image