## metrics類型
---
Prometheus客戶庫提供了四個核心的metrics類型。這四種類型目前僅在客戶庫和wire協議中區分。Prometheus服務還沒有充分利用這些類型。不久的將來就會發生改變。
### Counter(計數器)
*counter* 是一個累計度量指標,它是一個只能遞增的數值。計數器主要用于統計服務的請求數、任務完成數和錯誤出現的次數等等。計數器是一個遞增的值。反例:統計goroutines的數量。計數器的使用方式在下面的各個客戶端例子中:
客戶端使用計數器的文檔:
- [Go](http://godoc.org/github.com/prometheus/client_golang/prometheus#Counter)
- [Java](https://github.com/prometheus/client_java/blob/master/simpleclient/src/main/java/io/prometheus/client/Counter.java)
- [Python](https://github.com/prometheus/client_python#counter)
- [Ruby](https://github.com/prometheus/client_ruby#counter)
### Gauge(測量器)
*gauge*是一個度量指標,它表示一個既可以遞增, 又可以遞減的值。
測量器主要測量類似于溫度、當前內存使用量等,也可以統計當前服務運行隨時增加或者減少的Goroutines數量
客戶端使用計量器的文檔:
- [Go](http://godoc.org/github.com/prometheus/client_golang/prometheus#Gauge)
- [Java](https://github.com/prometheus/client_java/blob/master/simpleclient/src/main/java/io/prometheus/client/Gauge.java)
- [Python](https://github.com/prometheus/client_python#gauge)
- [Ruby](https://github.com/prometheus/client_ruby#gauge)
### Histogram(柱狀圖)
*histogram*,是柱狀圖,在Prometheus系統中的查詢語言中,有三種作用:
1. 對每個采樣點進行統計,打到各個分類值中(bucket)
2. 對每個采樣點值累計和(sum)
3. 對采樣點的次數累計和(count)
度量指標名稱: `[basename]`的柱狀圖, 上面三類的作用度量指標名稱
- [basename]_bucket{le="上邊界"}, 這個值為小于等于上邊界的所有采樣點數量
- [basename]_sum
- [basename]_count
小結:所以如果定義一個度量類型為Histogram,則Prometheus系統會自動生成三個對應的指標
**histogram的最簡單的理解, [DEMO](histogram.go)*
使用[histogram_quantile()](https://prometheus.io/docs/querying/functions/#histogram_quantile)函數, 計算直方圖或者是直方圖聚合計算的分位數閾值。 一個直方圖計算[Apdex值](http://en.wikipedia.org/wiki/Apdex)也是合適的, 當在buckets上操作時,記住直方圖是累計的。詳見[直方圖和總結](https://prometheus.io/docs/practices/histograms)
客戶庫的直方圖使用文檔:
- [Go](http://godoc.org/github.com/prometheus/client_golang/prometheus#Histogram)
- [Java](https://github.com/prometheus/client_java/blob/master/simpleclient/src/main/java/io/prometheus/client/Histogram.java)
- [Python](https://github.com/prometheus/client_python#histogram)
- [Ruby](https://github.com/prometheus/client_ruby#histogram)
### [Summary]總結
類似*histogram*柱狀圖,*summary*是采樣點分位圖統計,(通常的使用場景:請求持續時間和響應大小)。 它也有三種作用:
1. 對于每個采樣點進行統計,并形成分位圖。(如:正態分布一樣,統計低于60分不及格的同學比例,統計低于80分的同學比例,統計低于95分的同學比例)
2. 統計班上所有同學的總成績(sum)
3. 統計班上同學的考試總人數(count)
帶有度量指標的`[basename]`的`summary` 在抓取時間序列數據展示。
- 觀察時間的φ-quantiles (0 ≤ φ ≤ 1), 顯示為`[basename]{分位數="[φ]"}`
- `[basename]_sum`, 是指所有觀察值的總和
- `[basename]_count`, 是指已觀察到的事件計數值
**summary的最簡單的理解, [DEMO](summary.go)*
詳見[histogram和summaries](https://prometheus.io/docs/practices/histograms)
有關`summaries`的客戶端使用文檔:
- [Go](http://godoc.org/github.com/prometheus/client_golang/prometheus#Summary)
- [Java](https://github.com/prometheus/client_java/blob/master/simpleclient/src/main/java/io/prometheus/client/Summary.java)
- [Python](https://github.com/prometheus/client_python#summary)
- [Ruby](https://github.com/prometheus/client_ruby#summary)