# rate then sum YES, sum then rate NO

> Clean Markdown view of GeekNews topic #17929. Use the original source for factual precision when an external source URL is present.

## Metadata

- GeekNews HTML: [https://news.hada.io/topic?id=17929](https://news.hada.io/topic?id=17929)
- GeekNews Markdown: [https://news.hada.io/topic/17929.md](https://news.hada.io/topic/17929.md)
- Type: news
- Author: [dongho42](https://news.hada.io/@dongho42)
- Published: 2024-11-24T22:33:04+09:00
- Updated: 2024-11-24T22:33:04+09:00
- Original source: [robustperception.io](https://www.robustperception.io/rate-then-sum-never-sum-then-rate/)
- Points: 2
- Comments: 0

## Topic Body

- Prometheus에서 counter를 다룰 때, rate 같은 counter-only 함수와 aggregation을 적용할때 실수하기 쉬운게 있음  
- Prometheus에서 counter는 오직, "증가하거나", "유지되거나", "초기화되거나"만 가능  
- Node Exporter의 초당 요청율을 구하고 싶을때 다음과 같은 쿼리를 작성할 수 있음?  
    - `sum by (job)(rate(http_requests_total{job="node"}[5m]))`  
- 그런데? 다음과 같은 쿼리는?  
    - `rate(sum by (job)(http_requests_total{job="node"})[5m])`  
    - 이 쿼리는 문제가 있음  
- 만약 어떤 노드가 재시작되었다 하면, 해당 노드 위 Node Exporter의 `http_requests_total`는 0으로 초기화 됨  
    - 그럼 전체 `sum(...)`의 값은 줄어들게 됨  
    - `rate()`은 counter가 초기화된것이라 판단하고, 잘못된 spike가 생기게 됨  
        - *n에서 n-a만큼 값이 줄어들면, rate입장에선 (n-a)만큼 값이 튀었다고 보게 되므로  
- 따라서...  
    - `rate(counter_a[5m] + counter_b[5m])` 이렇게 하면 NO  
    - `rate(counter_a[5m]) + rate(counter_b[5m])` 이렇게 하는건 YES  
- `sum()` 말고도 `min()`, `max()`, `avg()`, `ceil()`, `histogram_quantile()`, `predict_linear()` 같은 것들에도 해당되는 얘기임  
- counter에 대한 함수에는 `rate()` 말고도 `irate()`, `increase()`, `resets()`들도 있음  
---  
  
`*`가 붙은 요약은 원문에 없는 부연 설명이라 따로 표시했습니다.

## Comments



_No public comments on this page._
