들어가며

Prometheus(P8s)의 Query(PromQL 이라고도 함)은 SQL과는 차이가 있어 기본문법과 Metric Join에 대해 설명하고자 합니다.

Data Model

P8s에서 Metric을 출력하는 형태를 보면,

http_requests_total{container=”A”} 1000

**<메트릭 이름="">{<레이블키>=<레이블값>,<레이블 키="">=<레이블값>...}

Data Type

P8s의 Data Type은 4가지로 분류하고 사용하는 값의 타입은 오로지 float64 입니다.

  • 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
  • Scalar - a simple numeric floating point value
  • String - a simple string value; currently unused

(참고 1) https://prometheus.io/docs/prometheus/latest/querying/basics/#expression-language-data-types

기본 쿼리 이해하기

Selectors

가장 기본이 되는 문법으로, label을 조건으로 원하는 Instant vector를 검색할 수 있다.

http_requests_total

  • =~ 는 regex를 이용할 수 있는 operator이며
  • environment label 값이 staging 혹은 testing 혹은 development 이고
  • method가 GET이 아닌 http_requests_total 이름의 instant vector를 검색함

모든 Label 값을 검색하고자 할때는 .* 보다는 .+를 사용해야 합니다. Metric 이름은 name 이란 Label로 저장되어 여러 metric의 이름이나 패턴으로 검색하고 싶다면 metric 이름은 비우고 name label로 검색하면 됩니다.

Aggregation Operators

검색된 metric 결과를 합산하거나 평균내는 등의 수학적인 계산이 필요할 때 아래와 같은 Operator들을 이용합니다. (SQL에서의 Group by와 같음)

  • sum (calculate sum over dimensions)
  • min (select minimum over dimensions)
  • max (select maximum over dimensions)
  • avg (calculate the average over dimensions)
  • 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)

(참고 2) https://prometheus.io/docs/prometheus/latest/querying/operators/#aggregation-operators

([parameter,] ) [without|by (

by 와 without 은 group by 와 같은 역할이지만 without은 by의 반대 관계입니다. 즉, without을 사용하면 해당 label은 제외하고 나머지로 group by를 해야 합니다.

sum(http_requests_total) by (method)

http_requests_total을 method 로 group by 로 합산합니다. (결과값) 2000 3000

sum by (method) (http_requests_total)

by 를 앞으로 이동해 사용할 수 있다.

마무리

궁극의 목표인 Join 쿼리는 다음 블로그에서 이어가겠습니다.