Prometheus學習系列(十一)之Hello World

本教程是相似"hello,world"的教程,展現怎樣在一個簡單地例子中安裝、配置和使用Prometheus。你將下載和本地化運行Prometheus服務,並寫一個配置文件,監控Prometheus服務自己和一個簡單的應用,而後配合使用query、rules和graphs展現收集的時間序列數據。git

下載和運行Prometheus

下載Prometheus最新的發佈版本,而後提取和運行它:github

tar zxvf prometheus-*.tar.gz
cd prometheus-*
複製代碼

在開始啓動Prometheus以前,咱們要配置它golang

配置Prometheus監控自身

Prometheus從監控的目標上經過http方式拉取指標數據,它也能夠拉取自身服務數據並監控自身的健康情況。shell

固然Prometheus服務拉取自身服務數據,並無多大的用處,可是它是一個好的開始例子。保存下面的基本Prometheus配置,並命名爲:prometheus.yml:express

global:
  scrape_interval:     15s # 默認狀況下,每15s拉取一次目標採樣點數據。

  # 咱們能夠附加一些指定標籤到採樣點度量標籤列表中, 用於和第三方系統進行通訊, 包括:federation, remote storage, Alertmanager
  external_labels:
    monitor: 'codelab-monitor'

# 下面就是拉取自身服務數據配置
scrape_configs:
  # job名稱會增長到拉取到的全部採樣點上,同時還有一個instance目標服務的host:port標籤也會增長到採樣點上
  - job_name: 'prometheus'

    # 覆蓋global的採樣點,拉取時間間隔5s
    scrape_interval: 5s

    static_configs:
      - targets: ['localhost:9090']
複製代碼

對於一個完整的配置選項,請見配置文檔瀏覽器

啓動Prometheus

要使用新建立的配置文件啓動Prometheus,請切換到包含Prometheus二進制文件的目錄並運行:bash

./prometheus --config.file=prometheus.yml
複製代碼

Prometheus服務應該啓動了。你能夠在瀏覽器上輸入:http://localhost:9090, 給它幾秒鐘從本身的HTTP指標端點收集有關自身的數據。dom

您還能夠經過導航到其指標端點來驗證Prometheus是否正在提供有關自身的指標:http://localhost:9090/metricside

使用expression browser

讓咱們試着看一下Prometheus收集的關於本身的一些數據。 使用Prometheus的內置表達式瀏覽器,導航到http://localhost:9090/graph,並選擇帶有"Graph"的"Console".測試

http://localhost:9090/gmetrics中收集中,有一個metric叫prometheus_target_interval_length_seconds(從目標收集數據的實際時間量),在表達式的console中輸入:

prometheus_target_interval_length_seconds
複製代碼

這個應該會返回不少不一樣的時間序列數據(以及每一個記錄的最新值),這些度量名稱都是prometheus_target_interval_length_seconds,可是帶有不一樣的標籤列表值,這些標籤列表值指定了不一樣的延遲百分比和目標組間隔。

若是咱們僅僅對99%的延遲感興趣,則咱們可使用下面的查詢去清洗信息:

prometheus_target_interval_length_seconds{quantile="0.99"}
複製代碼

爲了統計返回時間序列數據個數,你能夠寫:

count(prometheus_target_interval_length_seconds)
複製代碼

有關更多的表達式語言,請見表達式語言文檔

使用graph interface

見圖表表達式,導航到http://localhost:9090/graph, 而後使用"Graph" tab

例如,輸入如下表達式來繪製在自我抓取的Prometheus中建立的每秒塊速率:

rate(prometheus_tsdb_head_chunks_created_total[1m])
複製代碼

試驗graph範圍參數和其餘設置。

啓動其餘一些採樣目標

讓咱們讓這個更有趣,並開始一些示例目標,讓Prometheus抓取。

Go客戶端庫包含一個示例,該示例爲具備不一樣延遲分佈的三個服務導出虛構的RPC延遲。

確保已安裝Go編譯器並設置了正常工做的Go構建環境(具備正確的GOPATH)。

下載Prometheus的Go客戶端,運行三個服務:

git clone https://github.com/prometheus/client_golang.git
cd client_golang/examples/random
go get -d
go build
 ## 啓動三個服務
./random -listen-address=:8080
./random -listen-address=:8081
./random -listen-address=:8082
複製代碼

如今你在瀏覽器輸入:http://localhost:8080/metrics, http://localhost:8081/metrics, http://localhost:8082/metrics, 能看到全部採集到的採樣點數據。

配置Prometheus去監控這三個目標服務

如今咱們將會配置Prometheus,拉取三個目標服務的採樣點。咱們把這三個目標服務組成一個job, 叫example-radom。 然而,想象成,前兩個服務是生產環境服務,後者是測試環境服務。咱們能夠經過group標籤分組,要在Prometheus中對此進行建模,咱們能夠將多組端點添加到單個做業中,爲每組目標添加額外的標籤。在此示例中,咱們將group ="production"標籤添加到第一組目標,同時將group ="canary"添加到第二組。

要實現此目的,請將如下做業定義添加到prometheus.yml中的scrape_configs部分,而後從新啓動Prometheus實例:

scrape_configs:
  - job_name:       'example-random'

    scrape_interval: 5s

    static_configs:
      - targets: ['localhost:8080', 'localhost:8081']
        labels:
          group: 'production'

      - targets: ['localhost:8082']
        labels:
          group: 'test'
複製代碼

轉到表達式瀏覽器並驗證Prometheus如今是否有關於這些示例端點公開的時間序列的信息,例如rpc_durations_seconds指標。

爲抓取的數據聚合配置規則

雖然在咱們的示例中不是問題,可是在計算ad-hoc時,聚合了數千個時間序列的查詢會變慢。 爲了提升效率,Prometheus容許您經過配置的錄製規則將表達式預先記錄到全新的持久時間序列中。 假設咱們感興趣的是記錄在5分鐘窗口內測量的全部實例(但保留做業和服務維度)的平均示例RPC(rpc_durations_seconds_count)的每秒速率。 咱們能夠這樣寫:

avg(rate(rpc_durations_seconds_count[5m])) by (job, service)
複製代碼

要將此表達式生成的時間序列記錄到名爲job_service:rpc_durations_seconds_count:avg_rate5m的新度量標準中,請使用如下記錄規則建立一個文件並將其另存爲prometheus.rules.yml

groups:
- name: example
  rules:
  - record: job_service:rpc_durations_seconds_count:avg_rate5m
    expr: avg(rate(rpc_durations_seconds_count[5m])) by (job, service)
複製代碼

要使Prometheus選擇此新規則,請在prometheus.yml中添加rule_files語句。 配置如今應該以下所示:

global:
  scrape_interval:     15s # By default, scrape targets every 15 seconds.
  evaluation_interval: 15s # Evaluate rules every 15 seconds.
 # Attach these extra labels to all timeseries collected by this Prometheus instance.
  external_labels:
    monitor: 'codelab-monitor'

rule_files:
  - 'prometheus.rules.yml'

scrape_configs:
  - job_name: 'prometheus'

    # Override the global default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s

    static_configs:
      - targets: ['localhost:9090']

  - job_name:       'example-random'

    # Override the global default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s

    static_configs:
      - targets: ['localhost:8080', 'localhost:8081']
        labels:
          group: 'production'

      - targets: ['localhost:8082']
        labels:
          group: 'canary'
複製代碼

使用新配置從新啓動Prometheus,並經過表達式瀏覽器查詢或繪製圖表,驗證帶有度量標準名稱job_service:rpc_durations_seconds_count:avg_rate5m的新時間序列如今可用。

連接

Prometheus官網地址:prometheus.io/

個人Github:github.com/Alrights/pr…

相關文章
相關標籤/搜索