들어가며

Prometheus(P8s)의 기본쿼리를 이해했다면 Join 쿼리로 다양한 Vector를 활용해보자.

Join 쿼리 이해하기(Vector matching)

PromQL를 활용해야 하는 이유입니다.

여러가지 다른이름의 Instant Vector들의 Label을 통해 matching하여 다음과 같은 목적에 활용할 수 있습니다.

  • 비율, 퍼센트 등 (두개 이상의 값 연산)
  • Label 결합

Vector matching를 할때는 아래의 경우를 참고하세요.

  • One-to-One: Vector가 1:1로 정확히 일치하는 경우(Label matching으로 두개 이상의 vector들이 1:1로 매핑되는 경우)
  • One-to-Many: Vector가 1:N 혹은 N:1의 관계가 되는 경우
  • Many-to-Many: Vector가 N:M 관계가 되는 경우

2개 이상의 vector 가 매칭되어 하나의 Value가 되어야 하므로 Many-to-Many 는 불가능합니다.

One-to-One

**<문법>**
{vector expr} {bin-op} ignoring({label list}) {vector expr}
{vector expr} {bin-op} on({label list}) {vector expr}

왼쪽 {vector expr}과 오른쪽 {vector expr}는 매칭할 2개를 의미.

{bin-op}는 vector 두개의 value를 연산히야 1개의 값이 되므로 이 연산에서는 operator를 의미.
on은 매칭할 label 리스트를 의미하고 ignoring은 on의 반대. (ignoring에 들어간 label을 제외하고 나머지를 매칭)

method_code:http_errors:rate5m{method=”get”, code=”500”} 1
method_code:http_errors:rate5m{method=”get”, code=”404”} 2
method_code:http_errors:rate5m{method=”put”, code=”501”} 3
method_code:http_errors:rate5m{method=”post”, code=”500”} 4
method_code:http_errors:rate5m{method=”post”, code=”404”} 5
method:http_requests:rate5m{method=”get”} 6
method:http_requests:rate5m{method=”del”} 7
method:http_requests:rate5m{method=”post”} 8

(쿼리설명) code가 500인 method_code:http_errors:rate5m과 method:http_requests:rate5m을 매칭

method_code:http_errors:rate5m{code=”500”} / ignoring(code) method:http_requests:rate5m

결과는?

method_code:http_errors:rate5m{method=”get”, code=”500”} 1
method_code:http_errors:rate5m{method=”post”, code=”500”} 4

ignoring 으로 code를 제외해서 남아있는 method label로만 매칭을 해보면,

method_code:http_errors:rate5m{method=”get”, code=”500”} 1
method_code:http_errors:rate5m{method=”post”, code=”500”} 4
method:http_requests:rate5m{method=”get”} 6
method:http_requests:rate5m{method=”del”} 7
method:http_requests:rate5m{method=”post”} 8

결과는? {method=”get”} 0.16 // 1 / 6
{method=”post”} 0.50 // 8 / 4

One-to-many

**<문법>**
{vector expr} {bin-op} ignoring({label list}) group_left({label list}) {vector expr}
{vector expr} {bin-op} ignoring({label list}) group_right({label list}) {vector expr}
{vector expr} {bin-op} on({label list}) group_left({label list}) {vector expr}
{vector expr} {bin-op} on({label list}) group_right({label list}) {vector expr}

method_code:http_errors:rate5m / ignoring(code) group_left method:http_requests:rate5m

code를 제외하고 method 만으로 매칭하면, method=”get”인 경우 다음과 같이 2:1(N:1) 의 관계.

method_code:http_errors:rate5m{method=”get”, code=”500”} 1
method_code:http_errors:rate5m{method=”get”, code=”404”} 2
method:http_requests:rate5m{method=”get”} 3

최종으로 보여줄수 있는 값은 N에 해당되는 method_code:http_errors:rate5m vector 이다.

{method=”get”, code=”500”} 0.33 // 1 / 3
{method=”get”, code=”404”} 0.66 // 2 / 3

(쿼리설명) cadinality는 vector의 샘플범위(레이블)을 의미. method는 get 하나뿐이고, code는 하나의 get에도 여러가지 종류를 가진다.
즉, method:code = 1:N 의 관계를 가지고, code를 가진 method_code:http_errors:rate5m 가 high cardinality.

Label 병합

group_left/group_right 에 label list를 넣으면, 낮은 cardinality를 가진 vector의 label을 높은 cardinality의 vector로 결합시킬 수 있다.

method_code:http_errors:rate5m 1
method_code:http_errors:rate5m 2
method_code:http_errors:rate5m 3
method_code:http_errors:rate5m 4
method_code:http_errors:rate5m 5
method:http_requests:rate5m 6
method:http_requests:rate5m 7
method:http_requests:rate5m 8

(쿼리설명) message label 을 병합

method_code:http_errors:rate5m / ignoring(code) group_left(message) method:http_requests:rate5m

결과는?

0.33 // 1 / 3
0.66 // 2 / 3

마무리

앞으로 트렌비는 Kubernetes를 적극 활용할 계획이 있으니 적극 활용해보기로 해보고 더 복잡한 쿼리를 다루어 다음 블로그를 이어가길 기대해 봅니다.