1. Prometheus是什麼html
Prometheus是一個具備活躍生態系統的開源系統監控和告警工具包。一言以蔽之,它是一套開源監控解決方案。java
Prometheus主要特性:node
爲何用pull(拉取)而不用push(推送)呢?linux
由於,pull有如下優點:web
目標暴露HTTP端點,Prometheus服務端經過HTTP主動拉取數據。既然是服務端本身主動向目標拉取數據,那麼服務端運行在本地(咱們本身的電腦上)也是能夠的,只要能訪問目標端點便可,同時就像心跳檢測同樣能夠判斷目標是否下線,還有,服務端本身主動拉取,那麼想拉取誰的數據就拉取誰的數據,於是能夠隨意切換拉取目標。spring
回想一下Skywalking是怎麼作的,SkyWalking有客戶端和服務端,須要在目標服務上安裝探針(agent),探針採集目標服務的指標數據,上報給服務端OAP服務,這個對目標有必定的侵入性,不過能夠接受。Prometheus不須要探針,能夠藉助push gateway來實現push效果。apache
對了,有個名詞要先說清楚,metrics (譯:度量,指標),我的更傾向於把它翻譯成指標,後面說指標就是metricsapi
2. 基本概念瀏覽器
2.1. 數據模型springboot
Prometheus基本上將全部數據存儲爲時間序列:具備時間戳的值流,它們屬於同一個指標和同一組標記的維度。除了存儲的時間序列外,Prometheus還能夠生成臨時派生的時間序列做爲查詢的結果。
Metric names and labels
Every time series is uniquely identified by its metric name and optional key-value pairs called labels.
每一個時間序列都由它的指標名稱和稱爲標籤的可選鍵/值對惟一標識。
樣本構成實際的時間序列數據。 每一個樣本包括:
給定指標名稱和一組標籤,時間序列一般使用這種符號來標識:
<metric name>{<label name>=<label value>, ...}
例如,有一個時間序列,指標名稱是api_http_requests_total,標籤有method="POST"和handler="/messages",那麼它可能被表示成這樣:
api_http_requests_total{method="POST", handler="/messages"}
2.2. 指標類型
Counter
counter是一個累積量度,表明一個單調遞增的計數器,其值只能增長或在從新啓動時重置爲零。例如,可使用計數器來表示已服務請求數,已完成任務或錯誤的數量。
不要使用計數器來顯示能夠減少的值。例如,請勿對當前正在運行的進程數使用計數器,代替的應該使用量規。
Gauge
量規是一種指標,表明能夠任意上下波動的單個數值。
量規一般用於測量值,例如溫度或當前內存使用量,還用於可能上升和降低的「計數」,例如併發請求數。
Histogram
直方圖對觀察結果(一般是請求持續時間或響應大小)進行抽樣,並在可配置的桶中對它們進行計數。它還提供了全部觀測值的總和。
一個基礎指標名稱爲<basename>的直方圖在抓取期間會暴露多個時間序列:
Summary
與直方圖相似,摘要對觀察結果(一般是請求持續時間和響應大小等內容)進行抽樣分析。雖然它還提供了觀測值的總數和全部觀測值的總和,但它能夠計算滑動時間窗口內的可配置分位數。
一個基礎指標名稱爲<basename>的摘要在抓取期間暴露多個時間序列:
2.3. 做業和實例
在Prometheus的術語中,能夠抓取的端點稱爲實例,一般對應於單個進程。具備相同目的的實例集合,稱爲做業。
例如,一個做業有四個實例:
當Prometheus抓取目標時,它會自動在抓取的時間序列上附加一些標籤,以識別被抓取的目標:
3. 安裝與配置
Prometheus經過抓取指標HTTP端點從目標收集指標。因爲Prometheus以相同的方式暴露本身的數據,所以它也能夠抓取並監視其自身的健康情況。
默認狀況下,不用更改配置,直接運行就能夠抓取prometheus自身的健康情況數據
# Start Prometheus. # By default, Prometheus stores its database in ./data (flag --storage.tsdb.path)
./prometheus --config.file=prometheus.yml
直接訪問 localhost:9090
訪問 localhost:9090/metrics 能夠查看各項指標
舉個例子
輸入如下表達式,點「Execute」,能夠看到如下效果
prometheus_target_interval_length_seconds
這應該返回多個不一樣的時間序列(以及每一個序列的最新值),每一個序列的指標名稱均爲prometheus_target_interval_length_seconds,但具備不一樣的標籤。
這個是以圖形化的方式展現指標,經過localhost:9090/metrics查看也是同樣的
若是咱們只對99%的延遲感興趣,咱們可使用如下查詢:
prometheus_target_interval_length_seconds{quantile="0.99"}
爲了計算返回的時間序列數,查詢應該這樣寫:
count(prometheus_target_interval_length_seconds)
接下來,讓咱們利用Node Exporter來多添加幾個目標:
tar -xzvf node_exporter-*.*.tar.gz cd node_exporter-*.* # Start 3 example targets in separate terminals: ./node_exporter --web.listen-address 127.0.0.1:8080 ./node_exporter --web.listen-address 127.0.0.1:8081 ./node_exporter --web.listen-address 127.0.0.1:8082
接下來,配置Prometheus來抓取這三個新目標
首先,定義一個名爲'node'的做業,這個做業負責從這三個目標端點抓取數據。假設,想象前兩個端點是生產環境的,另外一個是非生產環境的,爲了以示區別,咱們將其打上兩個不一樣的標籤。在本示例中,咱們將group="production"標籤添加到第一個目標組,同時將group="canary"添加到第二個目標。
scrape_configs: - job_name: 'node' # 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'
3.1. 配置
爲了查看全部的命令行參數,運行以下命令
./prometheus -h
配置文件是YAML格式的,可使用 --config.file參數指定
配置文件的主要結構以下:
global: # How frequently to scrape targets by default.
[ scrape_interval: <duration> | default = 1m ] # How long until a scrape request times out.
[ scrape_timeout: <duration> | default = 10s ] # How frequently to evaluate rules.
[ evaluation_interval: <duration> | default = 1m ] # The labels to add to any time series or alerts when communicating with
# external systems (federation, remote storage, Alertmanager).
external_labels: [ <labelname>: <labelvalue> ... ] # File to which PromQL queries are logged.
# Reloading the configuration will reopen the file.
[ query_log_file: <string> ] # Rule files specifies a list of globs. Rules and alerts are read from # all matching files.
rule_files: [ - <filepath_glob> ... ] # A list of scrape configurations.
scrape_configs: [ - <scrape_config> ... ] # Alerting specifies settings related to the Alertmanager.
alerting: alert_relabel_configs: [ - <relabel_config> ... ] alertmanagers: [ - <alertmanager_config> ... ] # Settings related to the remote write feature.
remote_write: [ - <remote_write> ... ] # Settings related to the remote read feature.
remote_read: [ - <remote_read> ... ]
4. 抓取 Spring Boot 應用
Prometheus但願抓取或輪詢單個應用程序實例以獲取指標。 Spring Boot在 /actuator/prometheus 提供了一個actuator端點,以適當的格式提供Prometheus抓取。
爲了以Prometheus服務器能夠抓取的格式公開指標,須要依賴 micrometer-registry-prometheus
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<version>1.6.4</version>
</dependency>
下面是一個示例 prometheus.yml
scrape_configs:
- job_name: 'spring'
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['HOST:PORT']
接下來,建立一個項目,名爲prometheus-example
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cjs.example</groupId>
<artifactId>prometheus-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>prometheus-example</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
spring:
application:
name: prometheus-example
management:
endpoints:
web:
exposure:
include: "*"
metrics:
tags:
application: ${spring.application.name}
這句別忘了: management.metrics.tags.application=${spring.application.name}
Spring Boot Actuator 默認的端點不少,詳見
啓動項目,瀏覽器訪問 /actuator/prometheus 端點
配置Prometheus抓取該應用
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']
- job_name: 'springboot-prometheus'
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['192.168.100.93:8080']
重啓服務
./prometheus --config.file=prometheus.yml
4.1. Grafana
https://grafana.com/tutorials/
下載&解壓
wget https://dl.grafana.com/oss/release/grafana-7.4.3.linux-amd64.tar.gz
tar -zxvf grafana-7.4.3.linux-amd64.tar.gz
啓動
./bin/grafana-server web
瀏覽器訪問 http://localhost:3000
默認帳號是 admin/admin
首次登錄後咱們將密碼改爲admin1234
先配置一個數據源,一下子添加儀表盤的時候要選擇數據源的
Grafana官方提供了不少模板,咱們能夠直接使用
首先要找到咱們想要的模板
好比,咱們這裏隨便選了一個模板
能夠直接將模板JSON文件下載下來導入,也能夠直接輸入模板ID加載,這裏咱們直接輸入模板ID
立竿見影,立刻就看到漂亮的展現界面了
咱們再添加一個DashBoard (ID:12856)