Prometheus是一個開源的監控報警系統,它被納入了由谷歌發起的Linux基金會旗下的雲原生基金會,并成為僅次于Kubernetes的第二大開源項目。
Prometheus簡介
Prometheus是一個開源的監控報警系統,它最初由SoundCloud開發。
2016年,Prometheus被納入了由谷歌發起的Linux基金會旗下的雲原生基金會( Cloud Native Computing Foundation),并成為僅次于Kubernetes的第二大開源項目。自此,它成為了一個獨立的開源項目,獨立于任何公司進行維護。
Prometheus擁有非常活躍的開發人員和使用者社群,目前在GitHub上已擁有三萬多的Star。
Prometheus特點
- 提供多元度資料模型,使用名額名稱和鍵值對辨別的時間序列資料
- 提供靈活的PromQL查詢方式,還提供了HTTP查詢接口,可以很友善地結合Grafana等元件展示資料。
- 不依賴外部存儲,支援單節點的本地存儲。通過Prometheus自帶的時序資料庫,可以完成每秒百萬及的資料存儲,如果需要存儲大量曆史資料,還可以對接第三方的時序資料庫。
- 時間序列收集通過HTTP的拉取方式進行,并提供了開放的名額資料标準。
- 支援向中間網關推送時序資料,可以更加靈活地适用于多種監控場景。
- 支援通過動态服務發現和靜态檔案配置擷取監控對象,目前已支援Kubernetes、Etcd、Consul等多種服務發現機制。
- 支援多種模式的圖形展示和儀表盤。
- 大多數Prometheus的元件都是使用Go語言編寫的,這使得它們很容易以二進制檔案的形式建構和部署。
歡迎關注微信公衆号:
萬貓學社,每周一分享Java技術幹貨。
Prometheus架構
Prometheus生态圈由多個元件構成,其中許多元件是可選的:
- Prometheus Server:用于收集、存儲和查詢時間序列資料。通過靜态配置檔案管理監控目标,也可以配合使用動态服務發現的方式動态管理監控目标,并從這些監控目标中擷取資料。它将采集到的資料按照時間序列的方式存儲在本地磁盤當中或者外部的時序資料庫中,可通過PromQL語言對資料的查詢以及分析。
- Client Library:為被監控的應用生成相應的名額(Metric)資料并暴露給Prometheus Server。當Prometheus Server 來拉取時,直接傳回實時狀态的名額資料。
- Push Gateway:主要用于短期存在的Jobs。由于這類Jobs存在時間較短,可能在Prometheus Server來拉取資料之前就消失了。是以,Jobs可以直接向Push Gateway推送它們的名額資料,然後Prometheus Server再從Push Gateway拉取。
- Exporters:用于暴露已有的第三方服務的名額資料通過HTTP服務的形式暴露給Prometheus Server,比如HAProxy、StatsD、Graphite等等。Prometheus Server通過通路該Exporter提供的Endpoint,即可擷取到需要采集的監控資料。
- Alertmanager:從Prometheus Server接收到告警後,會進行去除重複資料,分組,并路由到對收的接受方式,發出報警。Alertmanager的告警方式非常靈活,支援通過郵件、slack或釘釘等多種途徑發出告警。
- 一些其他的元件。
下面這張圖展示了Prometheus的架構和各個元件是如何互動和協作的:

其大概的工作流程是:
- Prometheus Server直接從HTTP接口或者Push Gateway拉取名額(Metric)資料。
- Prometheus Server在本地存儲所有采集的名額(Metric)資料,并在這些資料上運作規則,從現有資料中聚合和記錄新的時間序列,或者生成告警。
- Alertmanager根據配置檔案,對接收到的告警進行處理,發出報警。
- 在Grafana或其他API用戶端中,可視化收集的資料。
Prometheus資料模型
Prometheus會将所有采集到的監控資料以時間序列的方式儲存在記憶體資料庫中,并且定時儲存到硬碟上。每一條資料由以下三部分組成:
- 名額(Metric):由名額名稱和描述目前資料特征的标簽組成。
- 時間戳(Timestamp):一個精确到毫秒的時間戳。
- 資料值(Value):一個float64的浮點型資料表示目前資料的值。
其中,名額(Metric)通過如下格式辨別:
<名額名稱>{<标簽名稱>=<标簽值>, ...}
名額名稱(Metric Name)可以反映被監控資料的含義。名額名稱隻能由ASCII字元、數字、下劃線以及冒号組成并必須符合正規表達式
[a-zA-Z_:][a-zA-Z0-9_:]*
。
标簽(Label)反映了目前資料的特征次元,通過這些次元Prometheus可以對資料進行過濾,聚合等操作。标簽的名稱隻能由ASCII字元、數字以及下劃線組成并滿足正規表達式
[a-zA-Z_][a-zA-Z0-9_]*
比如:
prometheus_http_requests_total{code="200",handler="/metrics"}
名額類型
Prometheus定義了4種不同的名額類型(Metric Type):
- Counter(計數器)
- Gauge(儀表盤)
- Histogram(直方圖)
- Summary(摘要)
Counter類型和計數器一樣,隻增不減(除非系統發生重置),一般在定義Counter類型名額的名稱時推薦使用_total作為字尾。
比如,Prometheus Server中prometheus_http_requests_total, 表示Prometheus處理的HTTP請求總數:
# HELP prometheus_http_requests_total Counter of HTTP requests.
# TYPE prometheus_http_requests_total counter
prometheus_http_requests_total{code="200",handler="/api/v1/label/:name/values"} 3
prometheus_http_requests_total{code="200",handler="/api/v1/query"} 5
prometheus_http_requests_total{code="200",handler="/api/v1/query_range"} 15
prometheus_http_requests_total{code="200",handler="/graph"} 3
prometheus_http_requests_total{code="200",handler="/metrics"} 23
prometheus_http_requests_total{code="200",handler="/static/*filepath"} 18
prometheus_http_requests_total{code="302",handler="/"} 1
Gauge類型側重于反應系統的某一個瞬時的值,這類名額的資料可增可減。
比如,Prometheus Server中go_threads, 表示Prometheus目前go線程的數量:
# HELP go_threads Number of OS threads created.
# TYPE go_threads gauge
go_threads 13
Histogram類型由_bucket{le=""},_bucket{le="+Inf"}, _sum,_count 組成,主要用于表示一段時間範圍内對資料進行采樣,并能夠對其指定區間以及總數進行統計,通常它采集的資料展示為直方圖。
比如,Prometheus Server中prometheus_http_response_size_bytes:
# HELP prometheus_http_response_size_bytes Histogram of response size for HTTP requests.
# TYPE prometheus_http_response_size_bytes histogram
prometheus_http_response_size_bytes_bucket{handler="/",le="100"} 1
prometheus_http_response_size_bytes_bucket{handler="/",le="1000"} 1
prometheus_http_response_size_bytes_bucket{handler="/",le="10000"} 1
prometheus_http_response_size_bytes_bucket{handler="/",le="100000"} 1
prometheus_http_response_size_bytes_bucket{handler="/",le="1e+06"} 1
prometheus_http_response_size_bytes_bucket{handler="/",le="1e+07"} 1
prometheus_http_response_size_bytes_bucket{handler="/",le="1e+08"} 1
prometheus_http_response_size_bytes_bucket{handler="/",le="1e+09"} 1
prometheus_http_response_size_bytes_bucket{handler="/",le="+Inf"} 1
prometheus_http_response_size_bytes_sum{handler="/"} 29
prometheus_http_response_size_bytes_count{handler="/"} 1
Summary類型由 {quantile="<φ>"},_sum,_count 組成,主要用于表示一段時間内資料采樣結果,它直接存儲了分位資料,而不是根據統計區間計算出來的。
比如,Prometheus Server中prometheus_target_interval_length_seconds:
# HELP prometheus_target_interval_length_seconds Actual intervals between scrapes.
# TYPE prometheus_target_interval_length_seconds summary
prometheus_target_interval_length_seconds{interval="15s",quantile="0.01"} 14.9986249
prometheus_target_interval_length_seconds{interval="15s",quantile="0.05"} 14.998999
prometheus_target_interval_length_seconds{interval="15s",quantile="0.5"} 15.0000428
prometheus_target_interval_length_seconds{interval="15s",quantile="0.9"} 15.0012009
prometheus_target_interval_length_seconds{interval="15s",quantile="0.99"} 15.0016468
prometheus_target_interval_length_seconds_sum{interval="15s"} 315.0013755
prometheus_target_interval_length_seconds_count{interval="15s"} 21
安裝Prometheus Server
從官方網站(https://prometheus.io/download/)上找到最新版本的Prometheus Sevrer軟體包,如下圖:
根據自己的系統下載下傳對應的壓縮包,這裡以Windows為例,下載下傳prometheus-2.19.0.windows-amd64.tar.gz。
解壓後目前目錄會包含預設的Prometheus配置檔案promethes.yml:
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['localhost:9090']
暫且不做修改,輕按兩下prometheus.exe即可啟動,如下圖:
通路http://localhost:9090/graph,就可以看到Prometheus自身的監控資料:
尾聲
Prometheus的大緻介紹已經告一段落了,但是隻是萬裡長征的第一步,Prometheus的更多強大功能和使用方法還等待我們去挖掘。
微信公衆号:萬貓學社
微信掃描二維碼
獲得更多Java技術幹貨
作者:萬貓學社
出處:http://www.cnblogs.com/heihaozi/
版權聲明:本文遵循 CC 4.0 BY-NC-SA 版權協定,轉載請附上原文出處連結和本聲明。
微信掃描二維碼,關注
,回複「
電子書」,免費擷取12本Java必讀技術書籍。