본문 바로가기
Framework and Tool/Prometheus

Prometheus - 살펴보기(first steps)

by ocwokocw 2022. 1. 6.

- 출처: https://prometheus.io/docs/introduction/first_steps/

- 개요

Prometheus는 monitor 대상의 metric HTTP endpoint를 통해 정보를 긁어모아서 지표를 수집하는 모니터링 플랫폼이다. 이번 글에서는 prometheus 설치 방법과 환경설정 그리고 모니터에 대해 간단하게 살펴본다. 
 
prometheus를 다운받아서 설치하고 실행시켜보고 host와 서비스에서 시계열 데이터를 노출하는 exporter를 다운받고 설치해본다. 우선 prometheus 자체적으로 제공하는 host 수준의 다양한 지표들(ex - 메모리 사용, GC 등)을 이용하여 exporter를 별도 연동하지 않고 prometheus 자체적인 exporter로 사용해본다.

- Prometheus 설치

OS에 맞는 Prometheus를 https://prometheus.io/download/ 에서 다운로드를 받고 압축을 해제한다. 압축을 해제하면 prometheus(window - prometheus.exe) 단일 binary 실행 파일이 있을것이다. 
 
tar xvfz prometheus-*.tar.gz
cd prometheus-*
 
위와 같이 gzip을 압축해제한 후 prometheus 폴더로 들어간다.
 
user@ prometheus % cd prometheus-2.32.1.darwin-amd64
user@ prometheus-2.32.1.darwin-amd64 % ls -latr
total 385328
-rwxr-xr-x   1 user  staff  102653296 12 18 06:57 prometheus
-rwxr-xr-x   1 user  staff   94611392 12 18 07:00 promtool
drwxr-xr-x   9 user  staff        288 12 18 07:18 consoles
drwxr-xr-x   4 user  staff        128 12 18 07:18 console_libraries
-rw-r--r--   1 user  staff       3646 12 18 07:18 NOTICE
-rw-r--r--   1 user  staff      11357 12 18 07:18 LICENSE
drwxr-xr-x   6 user  staff        192 12 27 20:02 ..
-rw-r--r--   1 user  staff       1182 12 27 20:09 prometheus.yml
drwxr-xr-x  10 user  staff        320 12 27 20:09 .
drwxr-xr-x   5 user  staff        160 12 27 20:12 data
user@ prometheus-2.32.1.darwin-amd64 %
 
prometheus를 --help flag와 함께 실행시키면 아래와 같은 안내문구를 볼 수 있다.
 
user@ prometheus-2.32.1.darwin-amd64 % ./prometheus --help
usage: prometheus [<flags>]

The Prometheus monitoring server

Flags:
  -h, --help                     Show context-sensitive help (also try --help-long and --help-man).
      --version                  Show application version.
      --config.file="prometheus.yml"
                                 Prometheus configuration file path.
      --web.listen-address="0.0.0.0:9090"
 
성공적으로 prometheus를 설치한것 같다.

- Prometheus 환경 설정

prometheus의 환경설정 파일은 YAML 포맷으로 되어있다. 파일을 살펴보면 prometheus.yml 파일이 보일 것이다. 해당 파일의 내용은 아래와 같이 구성되어 있다.
 
global:
  scrape_interval: 15s
  evaluation_interval: 15s

rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

scrape_configs:
  - job_name: "prometheus"
    static_configs:
      - targets: ["localhost:9090"]
 
실제로는 더 많은 내용과 주석이 있지만 간단하게 보기위해 많은 내용을 제거하였다.
 
환경 설정은 크게 3개의 부분(global, rule_files, scrape_configs)으로 구성되어 있다.
 
global 부분에서는 prometheus 서버의 전역 설정을 제어한다. 현재 설정파일에는 2개 옵션만 있다. scrape_interval은 어느 정도의 빈도로 prometheus가 대상들의 지표를 수집할것인지 설정한다. 개별적인 대상을 설정하면 해당 설정을 덮어쓸 수 있다. evaluation_interval 옵션은 prometheus가 rule 을 얼마나 자주 평가할것인지를 제어한다. prometheus는 알람을 발생시키거나 새로운 시계열 데이터를 생성하는데 rule을 사용한다.
 
rule_files 부분은 prometheus 서버를 불러오는 규칙을 지정할 수 있다. 현재는 아무런 rule도 설정하지 않은 상태이다.
 
마지막으로 scrape_configs 에서는 prometheus가 어떤 리소스를 모니터할것인지를 제어한다. prometheus는 HTTP enpoint로 자기 자신에 대한 데이터를 배포하기 때문에 자신의 동작 상태를 모니터하고 수집할 수 있다. default 설정 파일에서는 1개의 prometheus라고 불리는 1개의 job만 있는데 prometheus server가 배포한 시계열 데이터를 수집한다. job은 정적으로 구성된 localhost의 9090포트 target 하나로 구성되어 있다. prometheus는 target의 /metrics 경로를 통해 지표를 수집한다. 그래서 데이터를 수집하는 URL은 http://localhost:9090/metrics 가 된다.
 
반환된 시계열 데이터는 prometheus 서버의 세부적인 성능과 상태를 기술할것이다.

- Prometheus 실행

새롭운 환경설정 파일을 기반으로 prometheus 시작하려면 prometheus 바이너리가 포함된 디렉토리를 변경하고 다음을 실행한다.
 
./prometheus --config.file=prometheus.yml
 
prometheus가 시작되고나서 http://localhost:9090 으로 접속하면 상태 페이지를 브라우징할 수 있게 된다. 또한 자신의 metrics endpoint (http://localhost:9090/metrics) 를 이용하여 prometheus 가 스스로 metrics을 서비스하는것을 확인해볼 수 있다.

- 브라우저 사용

prometheus가 스스로 수집한 데이터를 살펴보자. prometheus에 내장된 브라우저를 사용하기위해 http://localhost:9090/graph 에 접속해서 Graph 탭의 Table 을 선택해보자.
 
http://localhost:9090/metrics에서 수집할 수 있듯이 prometheus가 자기자신에 대해 노출한 지표중 promhttp_metric_handler_requests_total (prometheus 서버에 /metrics 요청이 들어온 횟수) 지표를 살펴보자. 브라우저에서 promhttp_metric_handler_requests_total 를 입력하고 execute를 눌러보자.
 
promhttp_metric_handler_requests_total{code="200", instance="localhost:9090", job="prometheus"}
42
promhttp_metric_handler_requests_total{code="500", instance="localhost:9090", job="prometheus"}
0
promhttp_metric_handler_requests_total{code="503", instance="localhost:9090", job="prometheus"}
0
 
Table 탭에는 위와 같은 결과가 나올것이다.  데이터가 모두 promhttp_metric_handler_requests_total 라는 metric 이름을 갖고 있지만 {} 안에는 다른 label 을 갖고 있다. 위의 label 들은 상태코드를 구분해주고 있다.
 
만약 HTTP code 200의 데이터만  보고 싶다면 아래와 같은 query를 사용하면 된다.
 
promhttp_metric_handler_requests_total{code="200"}
 
반환된 시계열의 수를 보고 싶다면 아래와 같이 쓰면 된다.
 
count(promhttp_metric_handler_requests_total)

- Graphic 인터페이스

graph 표현을 보고 싶다면 http://localhost:9090/graph 로 접속하여 Graph 탭을 이용하면 된다.
 
만약 1초 마다 상태코드 200 을 반환한 HTTP 요청의 비율을 보고 싶다면 아래와 같이 표현해주면 된다.
 
rate(promhttp_metric_handler_requests_total{code="200"}[1m])
 
 

'Framework and Tool > Prometheus' 카테고리의 다른 글

With grafana  (0) 2023.04.22
Node exporter  (0) 2023.04.22
Prometheus - 개요  (0) 2022.01.02

댓글