[Prometheus][PromQL] 개요
Updated:
PromQL ?
- Prometheus Query Language
- 사용자가 실시간으로 시계열 데이터를 선택하고 집계 할 수 있는 쿼리 언어
- 주석(#) 지원
Data Model
- https://prometheus.io/docs/concepts/data_model/
- 표기법
<metric name>{<label name>=<label value>, ...}
- 기본적으로 모든 데이터를 시계열로 저장
- 저장된 시계열 외에도 쿼리 결과로 임시 파생 시계열 생성 가능
- 모든 시계열은 매트릭 이름과 레이블(선택적 키-값 쌍)로 유일하게 식별
- 레이블
- 차원 데이터 모델 활성화
- 레이블 조합은 동일한 메트릭 이름에 대해 특정 메트릭 인스턴스화를 식별
- 쿼리로 차원 기반의 필터링 및 집계 가능
- 레이블 값이 비어있으면 없는 것과 동일
- 레이블 추가/제거/값변경은 새 시계열이 생성
- 레이블
- 메트릭 및 레이블 이름 가이드
Data Types
- Instant vector
- 같은 타임 스탬프를 공유하는 각 시계열에 대한 시계열 집합
- Range vector
- 각 시계열에 대해 시간 경과에 따른 데이터 범위를 포함하는 시계열 집합
- Scalar
- 숫자
- String
- 문자열
- 현재 사용되지 않음
Time series Selectors
- https://prometheus.io/docs/prometheus/latest/querying/basics/
- Instant vector selectors
- 주어진 타임 스탬프에서 시계열 집합과 단일 샘플 값 선택 가능
- 매트릭 이름만 지정하면 해당 매트릭 이름을 가진 모든 시계열에 대해 인스턴스 벡터 생성
process_cpu_seconds_total
- 레이블을 지정하면 해당 시계열에 대해 인스턴스 벡터 생성
process_cpu_seconds_total{app="istiod"}
process_cpu_seconds_total{app="istiod", istio="pilot"}
- 레이블 매칭 연산자 제공
- =(문자열 일치), !=(문자열 불일치), =~(정규식 일치), !~(정규식 불일치)
process_cpu_seconds_total{app=~"istiod|jaeger", kubernetes_namespace="istio-system"}
- 레이블 매칭을 이용하여 매트릭 이름에 대한 가능
- 매트릭 이름으로 사용할 수 없는 이름(bool, on, ignoring, group_left, group_right)을 사용할 때 이용 가능
{__name__="process_cpu_seconds_total"}
- Range Vector Selectors
- 현재 순간에서 뒤로 범위를 지정한다는 점을 제외하면 Instant vector selectors와 동일
- 끝에 대괄호로 범위 지정
- ms, s, m, h, d, w, y
process_cpu_seconds_total{app="istiod"}[5m]
process_cpu_seconds_total{app="istiod"}[1h5m]
- Offset modifier
- 시간 오프셋 변경 가능
rate(process_cpu_seconds_total[5m] offset 1w)
- 일주일 전 5분 데이터
- @ modifier
- 평가 시간 변경(유닉스 타임 스탬프)
- 평가 시간을 앞지를 수 있기 때문에 기본적으로 비활성화이며 ‘–enable-feature=promql-at-modifier’를 통해 활성화 가능
- 범위 쿼리의 경우 start(), end()로 평가 시간 변경
process_cpu_seconds_total{app="istiod"} @ 1615267221
process_cpu_seconds_total{app="istiod"}[5m] @ 1615267221
process_cpu_seconds_total{app="istiod"} @ 1615267221 offset 1h
process_cpu_seconds_total{app="istiod"}[5m] @ start()
process_cpu_seconds_total{app="istiod"}[5m] @ end()
Subquery
- https://prometheus.io/blog/2019/01/28/subquery-support/
- 주어진 범위 및 해상도에 대해 쿼리 수행 가능
- 결과는 범위 벡터
Syntax : <instant_query> '[' <range> ':' [<resolution>] ']' [ @ <float_literal> ] [ offset <duration> ]
min_over_time(rate(process_cpu_seconds_total{app="istiod"}[5m])[30m:1m])
Operators
- https://prometheus.io/docs/prometheus/latest/querying/operators/
- Binary operators
- Arithmetic binary operators
- +, -, *, /, %, ^
- scalar/scalar, vector/scalar, vector/vector에 적용 가능
- Comparison binary operators
- ==, !=, >, <, >=, <=
- scalar/scalar, vector/scalar, vector/vector에 적용 가능
- Logical/set binary operators
- and, or, unless
- vector/vector에 적용 가능
- Arithmetic binary operators
- Vector matching
- 벡터 간의 연산은 왼쪽 벡터 기준으로 오른쪽 벡터에서 매칭되는 요소를 찾아서 연산
- 3가지 유형 존재 : One-to-one, many-to-one, one-to-many
- Aggregation operators
- 단일 인스턴스 벡터에 대해 연산
- sum, min, max, avg, group, stddev, stdvar, count, count_values, bottomk, topk, quantile
- Binary operator precedence
- ^
- *, /, %
- +, -
- ==, !=, <=, <, >=, >
- and, unless
- or
Functions
- https://prometheus.io/docs/prometheus/latest/querying/functions/
- abs(), ceil(), changes(), irate(), histogram_quantile(), rate(), sort(), …