๐Ÿ“š ์Šคํ”„๋ง ์•ก์ถ”์—์ดํ„ฐ(Spring Actuator)

CodeByHanยท2025๋…„ 4์›” 6์ผ

์Šคํ”„๋ง

๋ชฉ๋ก ๋ณด๊ธฐ
18/33

  • ์Šคํ”„๋ง ๋ถ€ํŠธ๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๊ฐ•๋ ฅํ•œ ๋ชจ๋‹ˆํ„ฐ๋ง ๋ฐ ๊ด€๋ฆฌ ๋„๊ตฌ
  • ๋งˆ์ดํฌ๋กœ๋ฏธํ„ฐ, ํ”„๋กœ๋ฉ”ํ…Œ์šฐ์Šค, ๊ทธ๋ผํŒŒ๋‚˜ ๊ฐ™์€ ๋ชจ๋‹ˆํ„ฐ๋ง ์‹œ์Šคํ…œ๊ณผ ๋งค์šฐ ์‰ฝ๊ฒŒ ์—ฐ๋™ ๊ฐ€๋Šฅ

์˜์กด์„ฑ ์ฃผ์ž…

implementation 'org.springframework.boot:spring-boot-starter-actuator'

๐Ÿ“Œ ๋™์ž‘ ํ™•์ธ

  • ์˜์กด์„ฑ์„ ์ฃผ์ž…ํ•˜๋ฉด http://localhost:8080/actuator ๋ฅผ ์‹คํ–‰ํ–ˆ์„ ๋•Œ ์ด๋Ÿฌํ•œ ์ •๋ณด๋“ค์ด ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.
{
  "_links": {
    "self": {
      "href": "http://localhost:8080/actuator",
        "templated": false
   },
     "health-path": {
       "href":
       "http://localhost:8080/actuator/health/{*path}",
         "templated": true
   },
     "health": {
       "href": "http://localhost:8080/actuator/health",
         "templated": false
   }
}

ํ™”๋ฉด์— ๋ณด์ด๋Š” http://localhost:8080/actuator/health๋ฅผ ์‹คํ–‰ํ•ด๋ณด๋ฉด

{"status": "UP"}

๋ฅผ ํ™•์ธ ํ•  ์ˆ˜์žˆ๋Š” ๋ฐ ์ด ์ •๋ณด๋Š” ์„œ๋ฒ„๊ฐ€ ์ž˜ ๋™์ž‘ํ•˜๊ณ  ์žˆ๋Š”์ง€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ํ—ฌ์Šค ์ƒํƒœ๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.
ํ•˜์ง€๋งŒ ์ด๊ฑด ํ—ฌ์Šค ์ƒํƒœ๋งŒ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ์ด๊ณ 

management:
 endpoints:
  web:
   exposure:
    include: "*"

์„ ์„ค์ •ํ•ด์ฃผ๊ณ (์ฐธ๊ณ ๋กœ shutdown์„ ์ œ์™ธํ•œ ๋ชจ๋“  ์—”๋“œํฌ์ธํŠธ๋ฅผ ์›น์— ๋…ธ์ถœํ•˜๋Š” ์„ค์ •์ด๋‹ค.) http://localhost:8080/actuator๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด

{
  "_links": {
    "self": {
      "href": "http://localhost:8080/actuator",
      "templated": false
    },
    "beans": {
      "href": "http://localhost:8080/actuator/beans",
      "templated": false
    },
    "caches": {
      "href": "http://localhost:8080/actuator/caches",
      "templated": false
    },
    "caches-cache": {
      "href": "http://localhost:8080/actuator/caches/{cache}",
      "templated": true
    },
    "health-path": {
      "href": "http://localhost:8080/actuator/health/{*path}",
      "templated": true
    },
    "health": {
      "href": "http://localhost:8080/actuator/health",
      "templated": false
    },
    "info": {
      "href": "http://localhost:8080/actuator/info",
      "templated": false
    },
    "conditions": {
      "href": "http://localhost:8080/actuator/conditions",
      "templated": false
    },
    "configprops-prefix": {
      "href": "http://localhost:8080/actuator/configprops/{prefix}",
      "templated": true
    },
    "configprops": {
      "href": "http://localhost:8080/actuator/configprops",
      "templated": false
    },
    "env": {
      "href": "http://localhost:8080/actuator/env",
      "templated": false
    },
    "env-toMatch": {
      "href": "http://localhost:8080/actuator/env/{toMatch}",
      "templated": true
    },
    "loggers": {
      "href": "http://localhost:8080/actuator/loggers",
      "templated": false
    },
    "loggers-name": {
      "href": "http://localhost:8080/actuator/loggers/{name}",
      "templated": true
    },
    "heapdump": {
      "href": "http://localhost:8080/actuator/heapdump",
      "templated": false
    },
    "threaddump": {
      "href": "http://localhost:8080/actuator/threaddump",
      "templated": false
    },
    "metrics": {
      "href": "http://localhost:8080/actuator/metrics",
      "templated": false
    },
    "metrics-requiredMetricName": {
      "href": "http://localhost:8080/actuator/metrics/{requiredMetricName}",
      "templated": true
    },
    "scheduledtasks": {
      "href": "http://localhost:8080/actuator/scheduledtasks",
      "templated": false
    },
    "mappings": {
      "href": "http://localhost:8080/actuator/mappings",
      "templated": false
    }
  }
}

์ด๋ ‡๊ฒŒ ๋งŽ์€ ๊ธฐ๋Šฅ๋“ค์„ ํ™•์ธ ๊ฐ€๋Šฅํ•˜๋‹ค.

์—”๋“œํฌ์ธํŠธ(EndPoint)

  • ์•ก์ถ”์—์ดํ„ฐ๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ ํ•˜๋‚˜ํ•˜๋‚˜๋ฅผ ์—”๋“œํฌ์ธํŠธ(EndPoint)๋ผ๊ณ  ํ•œ๋‹ค.
  • ๊ฐ๊ฐ์˜ ์—”๋“œํฌ์ธํŠธ(EndPoint)๋Š” /actuator/{์—”๋“œํฌ์ธํŠธ๋ช…}์œผ๋กœ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
    ์˜ˆ์‹œ) http://localhost:8080/actuator/health

๐Ÿ“Œ ์—”๋“œํฌ์ธํŠธ ์„ค์ •

๋Œ€๋ถ€๋ถ„์˜ ์—”๋“œํฌ์ธํŠธ(EndPoint)๋Š” ํ™œ์„ฑํ™” ๋˜์–ด์žˆ์ง€๋งŒ(shutdown ์ œ์™ธ) ์„ค์ •์œผ๋กœ ์–ด๋–ค ์—”๋“œํฌ์ธํŠธ๋ฅผ ๋…ธ์ถœํ• ์ง€ ์„ ํƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

ํŠน์ • ์—”๋“œํฌ์ธํŠธ ์„ค์ •

management:
 endpoint:
  shutdown:
   enabled: true
  • ํŠน์ • ์—”๋“œํฌ์ธํŠธ๋ฅผ ํ™œ์„ฑํ™” ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” management.endpoint.{์—”๋“œํฌ์ธํŠธ๋ช…}.enabled=true ์ ์šฉ
  • shutdown์„ ํ™œ์„ฑํ™” ์‹œํ‚ค๊ณ  http://localhost:8080/actuator/shutdown POST ์š”์ฒญํ•˜๋ฉด ์„œ๋ฒ„๊ฐ€ ์ข…๋ฃŒ

๐Ÿ“Œ ๋‹ค์–‘ํ•œ ์—”๋“œํฌ์ธํŠธ

  • beans: ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ๋“ฑ๋ก๋œ ์Šคํ”„๋ง ๋นˆ์„ ๋ณด์—ฌ์ค€๋‹ค.
  • conditions: ๋นˆ์„ ๋“ฑ๋กํ•  ๋•Œ ํ‰๊ฐ€ ์กฐ๊ฑด๊ณผ ์ผ์น˜ํ•˜๊ฑฐ๋‚˜ ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ์ด์œ ๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.
  • configprops: @ConfigurationProperties ์ •๋ณด๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.
  • env: ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ํ™˜๊ฒฝ(Environment) ์ •๋ณด๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.
  • health: ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ํ—ฌ์Šค ์ƒํƒœ๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.
  • httpexchanges: HTTP ํ˜ธ์ถœ ๋ฐ ์‘๋‹ต ์ •๋ณด๋ฅผ ์ œ๊ณตํ•œ๋‹ค. (HttpExchangeRepository๋ฅผ ๊ตฌํ˜„ํ•œ ๋นˆ ํ•„์š”)
  • info: ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ •๋ณด๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.
  • loggers: ๋กœ๊ฑฐ ์„ค์ •์„ ํ™•์ธํ•˜๊ณ  ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.
  • metrics: ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋ฉ”ํŠธ๋ฆญ ์ •๋ณด๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.
  • mappings: @RequestMapping ์ •๋ณด๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.
  • threaddump: ์“ฐ๋ ˆ๋“œ ๋คํ”„๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ๋ณด์—ฌ์ค€๋‹ค.
  • shutdown: ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์ข…๋ฃŒํ•œ๋‹ค. (๊ธฐ๋ณธ์ ์œผ๋กœ ๋น„ํ™œ์„ฑํ™”๋˜์–ด ์žˆ์Œ)

Endpoints

1) health

http://localhost:8080/actuator/health

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ๋น ๋ฅด๊ฒŒ ๋ฌธ์ œ ํŒŒ์•… ๊ฐ€๋Šฅ

์ข€ ๋” ์ž์„ธํ•˜๊ฒŒ ๋ณด๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์„ค์ • ํ•„์š”

management:
 endpoint:
  health:
   show-details: always

๊ฐ๊ฐ์˜ ํ•ญ๋ชฉ์ด ์•„์ฃผ ์ž์„ธํ•˜๊ฒŒ ๋…ธ์ถœ๋˜๋Š”๊ฒƒ์ด ๋ถ€๋‹ด์Šค๋Ÿฌ์šฐ๋ฉด

management:
 endpoint:
  health:
   show-components: always

show-details ์ œ๊ฑฐ

ํ—ฌ์Šค ์ด์ƒ ์ƒํƒœ

{
  "status": "DOWN",
  "components": {
    "db": {
      "status": "DOWN"
    },
    "diskSpace": {
      "status": "UP"
    },
    "ping": {
      "status": "UP"
    }
  }
}
  • ํ—ฌ์Šค ์ปดํฌ๋„ŒํŠธ์ค‘์— ํ•˜๋‚˜๋ผ๋„ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์ „์ฒด ์ƒํƒœ๋Š” Down

์ถ”๊ฐ€์ ์œผ๋กœ db,mongo,redis,diskspace,ping ๊ฐ™์€ ์ˆ˜ ๋งŽ์€ ํ—ฌ์Šค ๊ธฐ๋Šฅ ์ œ๊ณต

2) Info

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ธฐ๋ณธ ์ •๋ณด๋ฅผ ๋…ธ์ถœ

  • java : ์ž๋ฐ” ๋Ÿฐํƒ€์ž„ ์ •๋ณด
    os : OS ์ •๋ณด
  • env : Environment ์—์„œ info.
    ๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ •๋ณด
  • build : ๋นŒ๋“œ ์ •๋ณด, META-INF/build-info.properties ํŒŒ์ผ์ด ํ•„์š”ํ•˜๋‹ค.
  • git : git ์ •๋ณด, git.properties ํŒŒ์ผ์ด ํ•„์š”ํ•˜๋‹ค.

์ฒ˜์Œ ์‹คํ–‰ํ•˜๋ฉด ์ •๋ณด๊ฐ€ ๋ณด์ด์ง€ ์•Š๋Š”๋‹ค. ๋”ฐ๋ผ์„œ ์„ค์ •ํ•ด์ค€๋‹ค.

java,OS ์ •๋ณด ํ™•์ธ

application.yml

management:
 info:
  java:
   enabled: true
os:
 enabled: true
  • management.info.<id>.enabeld ๊ฐ’ true์ง€์ •

http://localhost:8080/actuator/info ์‹คํ–‰

env

application.yml

management:
 info:
  env:
   enabled: true
info:
 app:
  name: hello-actuator
   company: yh

build

๋นŒ๋“œ ์ •๋ณด ๋…ธ์ถœ์„ ์œ„ํ•ด์„œ๋Š” META-INF/build-info.properties ์„ ์ƒ์„ฑ

build.gradle - ๋นŒ๋“œ ์ •๋ณด ์ถ”๊ฐ€

springBoot {
  buildInfo()
}
{
  "build": {
    "artifact": "actuator",
    "name": "actuator",
    "time": "2023-01-01T00:00:00.000Z",
    "version": "0.0.1-SNAPSHOT",
    "group": "hello"
  }
}

loggers

loggers ์—”๋“œํฌ์ธํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋กœ๊น…๊ณผ ๊ด€๋ จ๋œ ์ •๋ณด๋ฅผ ํ™•์ธํ•˜๊ณ , ๋˜ ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ

package yuhan.actuatorstart;

@Slf4j
@RestController
public class LogController{

    @GetMapping("/log")
    public String log(){
        log.trace("trace log");
        log.debug("debug log");
        log.info("info log");
        log.warn("warn log");
        log.error("error log");
        return "ok";
    }

}

application.yml ์„ค์ •

logging:
  level:
    yuhan.actuatorstart: debug
  • LogController ํด๋ž˜์Šค๋„ debug ๋ ˆ๋ฒจ์˜ ์˜ํ–ฅ์„ ๋ฐ›๋Š”๋‹ค.

http://localhost:8080/actuator/loggers ์„ ์‹คํ–‰ํ•ด๋ณด๋ฉด

{
  "levels": [
    "OFF",
    "ERROR",
    "WARN",
    "INFO",
    "DEBUG",
    "TRACE"
  ],
  "loggers": {
    "ROOT": {
      "configuredLevel": "INFO",
      "effectiveLevel": "INFO"
    },
    "_org.springframework": {
      "effectiveLevel": "INFO"
    },
    "yuhan": {
      "effectiveLevel": "INFO"
    },
    "yuhan.ActuatorApplication": {
      "effectiveLevel": "INFO"
    },
    "yuhan.actuatorstart": {
      "configuredLevel": "DEBUG",
      "effectiveLevel": "DEBUG"
    },
    " yuhan.actuatorstart.LogController": {
      "effectiveLevel": "DEBUG"
    }
  }
}

์ด๋ ‡๊ฒŒ ๋กœ๊ทธ ์ •๋ณด๋“ค์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿค”

  • ๋กœ๊ทธ๋ฅผ ๋ณ„๋„๋กœ ์„ค์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ์Šคํ”„๋ง ๋ถ€ํŠธ๋Š” ๊ธฐ๋ณธ์œผ๋กœ INFO๋ฅผ ์‚ฌ์šฉ
    ์šฐ๋ฆฌ๋Š” yuhan.actuatorstart ๋Š” DEBUG
    ๋กœ ์„ค์ •ํ–ˆ๋‹ค.
  • ๊ทธ๋ž˜์„œ ํ•ด๋‹น ๋ถ€๋ถ„์—์„œ configuredLevel์ด DEBUG๋กœ ์„ค์ •๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ํ•˜์œ„๋„ DEBUG ๋ ˆ๋ฒจ์ด ์ ์šฉ
  • http://localhost:8080/actuator/loggers/yuhan.actuatorstart๋กœ ๋” ์ž์„ธํ•œ ์กฐํšŒ ๊ฐ€๋Šฅ

โ€ผ๏ธ ์‹ค์‹œ๊ฐ„ ๋กœ๊ทธ ๋ ˆ๋ฒจ ๋ณ€๊ฒฝ

์šด์˜ ์„œ๋ฒ„๋Š” ๋ณดํ†ต ์š”์ฒญ๋„ ๋งŽ๊ณ  ๋กœ๊ทธ๋„ ๋„ˆ๋ฌด ๋งŽ์ด ๋‚จ๊ธฐ ๋•Œ๋ฌธ์— DEBUG๋กœ ์ถœ๋ ฅํ•˜๊ฒŒ ๋˜๋ฉด ์„ฑ๋Šฅ์ด๋‚˜ ๋””์Šคํฌ์— ์˜ํ–ฅ์„ ์ค€๋‹ค. ๊ทธ๋ž˜์„œ ์šด์˜ ์„œ๋ฒ„๋Š” ์ค‘์š”ํ•˜๋‹ค๊ณ  ํŒ๋‹จ๋˜๋Š” INFO ๋กœ๊ทธ ๋ ˆ๋ฒจ์„ ์‚ฌ์šฉ

  • loggers ์—”๋“œํฌ์ธํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋‹ค์‹œ ์‹œ์ž‘ํ•˜์ง€ ์•Š๊ณ , ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋กœ๊ทธ ๋ ˆ๋ฒจ์„ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ

http://localhost:8080/actuator/loggers/yuhan.actuatorstar ์—๋‹ค๊ฐ€ POST ๋กœ ์ „์†กํ•˜๊ณ 

{
 "configuredLevel": "TRACE"
}

GET์œผ๋กœ ์š”์ฒญํ•ด์„œ ํ™•์ธํ•ด๋ณด๋ฉด configuredLevel ์ด TRACE
http://localhost:8080/actuator/loggers/yuhan.actuatorstar
๋กœ ๋ณ€๊ฒฝ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

{
 "configuredLevel": "TRACE",
 "effectiveLevel": "TRACE"
}

๐Ÿ“Œ ์•ก์ถ”์—์ดํ„ฐ์™€ ๋ณด์•ˆ

์•ก์ธ„์—์ดํ„ฐ๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ๋“ค์€ ์šฐ๋ฆฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋‚ด๋ถ€ ์ •๋ณด๋ฅผ ๋„ˆ๋ฌด ๋งŽ์ด ๋…ธ์ถœํ•œ๋‹ค. ์•ก์ธ„์—์ดํ„ฐ์˜ ์—”๋“œํฌ์ธํŠธ๋“ค์€ ์™ธ๋ถ€ ์ธํ„ฐ๋„ท์—์„œ ์ ‘๊ทผ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋ง‰๊ณ , ๋‚ด๋ถ€์—์„œ๋งŒ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ ๋‚ด๋ถ€๋ง์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ์ด ์•ˆ์ „ํ•˜๋‹ค.

์•ก์ธ„์—์ดํ„ฐ๋ฅผ ๋‹ค๋ฅธ ํฌํŠธ์—์„œ ์‹คํ–‰

  • ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์™ธ๋ถ€ ์ธํ„ฐ๋„ท ๋ง์„ ํ†ตํ•ด์„œ 8080 ํฌํŠธ์—๋งŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ณ , ๋‹ค๋ฅธ ํฌํŠธ๋Š” ๋‚ด๋ถ€๋ง์—์„œ๋งŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด ์•ก์ถ”์—์ดํ„ฐ์— ๋‹ค๋ฅธ ํฌํŠธ๋ฅผ ์„ค์ •

์•ก์ธ„์—์ดํ„ฐ ํฌํŠธ ์„ค์ •

management.server.port=9292

์ฐธ๊ณ  :

profile
๋…ธ๋ ฅ์€ ๋ฐฐ์‹ ํ•˜์ง€ ์•Š์•„ ๐Ÿ”ฅ

0๊ฐœ์˜ ๋Œ“๊ธ€