-2 Prometheus PromQL 과 사용법

 

일단 켜야하는데 필자는 도커로 올려서 쓴다.

아래는 정리해둔 사용법인데 AI로 받았다. 근데 어지간한 블로그 글보다 정리잘해서

그냥 올려서 보면될듯 

 

나도 이걸 외워서 쓸생각 보다는 필요할때 찾아서 쓰다보니

이정도면 충분히 사용법을 익히는데는 괜찮을 것 같다.

 

🔹 PromQL이란?

PromQL(Prometheus Query Language)은 Prometheus에서 수집한 시계열 데이터(time-series data)를 조회하고 분석하기 위한 쿼리 언어입니다.

SQL과 비슷한 개념이지만, PromQL은 시계열 데이터를 다루는 데 최적화되어 있으며, 메트릭(metric) 데이터를 필터링, 집계(aggregation), 변환 등의 기능을 수행할 수 있습니다.


🔹 PromQL 기본 개념

PromQL에서 사용하는 데이터는 **Metric(메트릭)**이라고 하며, 메트릭은 다음과 같은 형태를 가집니다:

plaintext
복사편집
metric_name{label1="value1", label2="value2", ...}

  • metric_name: 수집된 메트릭 이름 (예: node_cpu_seconds_total)
  • labels: 메트릭을 분류하는 추가적인 속성 (예: {mode="idle", cpu="0"})
  • 값(value): 해당 메트릭의 수치 데이터 (예: 0.123)

예제 메트릭 데이터:

plaintext
복사편집
node_cpu_seconds_total{cpu="0", mode="idle"}  34567.89
node_cpu_seconds_total{cpu="1", mode="user"}  1234.56


🔹 PromQL 기본 사용법

PromQL은 다양한 연산자와 함수를 제공하여 데이터를 조회하고 가공할 수 있습니다.

1️⃣ 메트릭 조회

단순히 특정 메트릭의 값을 조회할 때 사용합니다.

promql
복사편집
node_cpu_seconds_total

➡️ node_cpu_seconds_total 메트릭의 모든 데이터를 가져옵니다.


2️⃣ 특정 레이블(Label) 필터링

특정 조건을 가진 데이터만 조회할 수 있습니다.

promql
복사편집
node_cpu_seconds_total{mode="idle"}

➡️ mode="idle"인 CPU 사용량만 조회

promql
복사편집
node_cpu_seconds_total{cpu="0", mode="system"}

➡️ cpu="0"이면서 mode="system"인 데이터만 조회

promql
복사편집
node_cpu_seconds_total{mode!="idle"}

➡️ mode가 "idle"이 아닌 데이터 조회


3️⃣ 시간 범위 지정 (Range Vector)

특정 시간 범위 동안의 데이터를 조회할 수 있습니다.

promql
복사편집
node_cpu_seconds_total[5m]

➡️ 최근 5분 동안의 데이터를 가져옴

promql
복사편집
node_memory_Active_bytes[1h]

➡️ 최근 1시간 동안의 메모리 사용량 조회


4️⃣ 집계(Aggregation)

PromQL은 여러 데이터를 그룹화하여 합산, 평균, 최대/최소 등의 연산을 수행할 수 있습니다.

연산자 설명

sum() 총합
avg() 평균
min() 최소값
max() 최대값
count() 데이터 개수
stddev() 표준편차
stdvar() 분산
quantile() 백분위수

✅ CPU 사용량 합산

promql
복사편집
sum(node_cpu_seconds_total)

➡️ 모든 CPU 사용량을 합산

promql
복사편집
sum(node_cpu_seconds_total) by (cpu)

➡️ CPU별(cpu 레이블 기준) 사용량 합산

promql
복사편집
avg(node_cpu_seconds_total) by (mode)

➡️ CPU 사용 모드(mode 레이블 기준)별 평균 계산


5️⃣ 변화율 (Rate & Increase)

시계열 데이터에서 변화율을 계산하는 함수입니다.

함수 설명

rate() 초당 증가율 계산
increase() 일정 기간 동안 증가량 계산

✅ 초당 CPU 사용량 증가율

promql
복사편집
rate(node_cpu_seconds_total[5m])

➡️ 최근 5분 동안의 CPU 사용량 증가율

✅ 네트워크 트래픽 증가량

promql
복사편집
increase(node_network_receive_bytes_total[10m])

➡️ 최근 10분 동안 수신된 네트워크 트래픽 증가량


6️⃣ 비교 및 조건 필터링

연산자 설명

== 같음
!= 다름
> 초과
< 미만
>= 이상
<= 이하

✅ CPU 사용량 100초 이상인 경우만 필터링

promql
복사편집
node_cpu_seconds_total > 100

➡️ 100초 이상 사용된 CPU 데이터만 조회

✅ 메모리 사용량이 특정 값을 초과한 경우

promql
복사편집
node_memory_Active_bytes > 5000000000

➡️ 사용 중인 메모리가 5GB 이상인 경우만 필터링


7️⃣ Alert 조건 설정

Prometheus에서 알람(Alertmanager) 설정을 할 때 특정 조건을 정의할 수 있습니다.

✅ CPU 사용률이 80% 이상일 때 알람 설정

promql
복사편집
100 - (avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 80

➡️ 최근 5분 동안 평균 CPU idle 비율을 구하고, 이를 통해 CPU 사용률이 80% 이상인지 확인


🔹 PromQL 실전 예제

1️⃣ 컨테이너 메모리 사용량 모니터링

promql
복사편집
sum(container_memory_usage_bytes) by (container_label_io_kubernetes_pod_name)

➡️ 각 Kubernetes Pod별 메모리 사용량 조회

2️⃣ 컨테이너 CPU 사용량 증가율 확인

promql
복사편집
rate(container_cpu_usage_seconds_total[5m])

➡️ 컨테이너별 최근 5분 동안 CPU 사용률 증가율 조회

3️⃣ MySQL 쿼리 실행 시간 평균값 확인

promql
복사편집
avg(mysql_global_status_questions) by (instance)

➡️ MySQL 인스턴스별 평균 쿼리 실행 횟수 조회