1. 程式人生 > >從零開始學習Prometheus監控報警系統

從零開始學習Prometheus監控報警系統

### Prometheus簡介 Prometheus是一個開源的監控報警系統,它最初由[SoundCloud](https://soundcloud.com)開發。 2016年,Prometheus被納入了由谷歌發起的Linux基金會旗下的雲原生基金會( Cloud Native Computing Foundation),併成為僅次於[Kubernetes](https://kubernetes.io)的第二大開源專案。自此,它成為了一個獨立的開源專案,獨立於任何公司進行維護。 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架構圖](https://img-blog.csdnimg.cn/20200616154005125.png#pic_center) 其大概的工作流程是: 1. Prometheus Server直接從HTTP介面或者Push Gateway拉取指標(Metric)資料。 2. Prometheus Server在本地儲存所有采集的指標(Metric)資料,並在這些資料上執行規則,從現有資料中聚合和記錄新的時間序列,或者生成告警。 3. Alertmanager根據配置檔案,對接收到的告警進行處理,發出報警。 4. 在Grafana或其他API客戶端中,視覺化收集的資料。 ### Prometheus資料模型 Prometheus會將所有采集到的監控資料以時間序列的方式儲存在記憶體資料庫中,並且定時儲存到硬碟上。每一條資料由以下三部分組成: - 指標(Metric):由指標名稱和描述當前資料特徵的標籤組成。 - 時間戳(Timestamp):一個精確到毫秒的時間戳。 - 資料值(Value):一個float64的浮點型資料表示當前資料的值。 其中,指標(Metric)通過如下格式標識: ```java <指標名稱>{<標籤名稱>=<標籤值>, ...} ``` 指標名稱(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"} ``` 歡迎關注微信公眾號:萬貓學社
,每週一分享Java技術乾貨。 ### 指標型別 Prometheus定義了4種不同的指標型別(Metric Type): - Counter(計數器) - Gauge(儀表盤) - Histogram(直方圖) - Summary(摘要) #### Counter(計數器) 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(儀表盤) Gauge型別側重於反應系統的某一個瞬時的值,這類指標的資料可增可減。 比如,Prometheus Server中go_threads, 表示Prometheus當前go執行緒的數量: ``` # HELP go_threads Number of OS threads created. # TYPE go_threads gauge go_threads 13 ``` #### Histogram(直方圖) 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(摘要) 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 ``` 歡迎關注微信公眾號:萬貓學社
,每週一分享Java技術乾貨。 ### 安裝Prometheus Server 從官方網站([https://prometheus.io/download/](https://prometheus.io/download/))上找到最新版本的Prometheus Sevrer軟體包,如下圖: ![](https://img-blog.csdnimg.cn/20200619081626954.png#pic_center)根據自己的系統下載對應的壓縮包,這裡以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=` 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即可啟動,如下圖: ![](https://img-blog.csdnimg.cn/20200619082518407.png#pic_center)訪問[http://localhost:9090/graph](http://localhost:9090/graph),就可以看到Prometheus自身的監控資料: ![](https://img-blog.csdnimg.cn/20200619082933316.png#pic_center) ### 尾聲 Prometheus的大致介紹已經告一段落了,但是隻是萬里長征的第一步,Prometheus的更多強大功能和使用方法還等待我們去挖掘。

微信公眾號:萬貓學社

微信掃描二維碼

獲得更多Java技術乾貨