2022년 1월 9일 일요일

[ Prometheus ] PromQL 예제 (Prometheus Query Language)


[ Data Types ]

  • String - 문자열 변수
  • Scalar - 숫자 변수
  • Instant vector - a set of time series containing a single sample for each time series, all sharing the same timestamp
  • Range vector - a set of time series containing a range of data points over time for each time series


[ String ]

Example:

"this is a string"
'these are unescaped: \n \\ \t'
`these are not unescaped: \n ' " \t`



[ Scalar ]

Examples:

23
-2.43
3.4e-9
0x8f
-Inf
NaN



[ Instant vector selectors ]

{}를 사용하여 filtering 

prometheus_http_requests_total{job="prometheus",code="302"}
prometheus_http_requests_total{code=~"200|302|404",method!="GET"}

적어도 하나의 레이블과 매치시켜야 한다.

prometheus_http_requests_total{code=~".*"} # Bad
prometheus_http_requests_total{code=~".*",job="prometheus"} # Good!



[ Range Vector Selectors ]

[]를 범위를 선택한다. 그래프는 그릴 수 없다.

prometheus_http_requests_total{job="prometheus"}[3m]

Offset modifier를 이용하여 집계함수 사용

prometheus_http_requests_total offset 5m
sum(prometheus_http_requests_total{code="200"} offset 10m)
rate(prometheus_http_requests_total[10m] offset -1w)



[ SubQuery Example ]

min_over_time( rate(prometheus_http_requests_total[5m])[30m:1m] )
max_over_time( deriv( rate(distance_covered_meters_total[1m])[5m:1m] )[10m:] )



[ Operators ]

집계연산자

  • sum (calculate sum over dimensions)
  • min (select minimum over dimensions)
  • max (select maximum over dimensions)
  • avg (calculate the average over dimensions)
  • group (all values in the resulting vector are 1)
  • stddev (calculate population standard deviation over dimensions)
  • stdvar (calculate population standard variance over dimensions)
  • count (count number of elements in the vector)
  • count_values (count number of elements with the same value)
  • bottomk (smallest k elements by sample value)
  • topk (largest k elements by sample value)
  • quantile (calculate φ-quantile (0 ≤ φ ≤ 1) over dimensions)

Example

sum without (code) (prometheus_http_requests_total) # code를 빼고 집계
sum by (instance, code) (prometheus_http_requests_total) # group by sum(instance), sum(code)
topk(5, prometheus_http_requests_total) # 가장 큰 요청 수



[ Function ]

# 지정된 함수 사용
round(rate(prometheus_http_requests_total[10m]),0.0001)
# 사용자 함수 정의
sum by (job) (
  rate(http_requests_total[5m])
)

정의된 함수 참조 : https://prometheus.io/docs/prometheus/latest/querying/functions/



댓글 없음:

댓글 쓰기