文章目錄
Prometheus(普羅米修斯)概述
prometheus:Prometheus是一套開源的監控&報警&時間序列數據庫的組合,起始是由SoundCloud公司開發的。
非常少的外部依賴+系統集成(docker Nginx JMX等),安裝操作簡單服務自動化發現,實現一種Profiling監控方式,實時分析系統運行的狀態、執行時間、調用次數等,以找到系統的熱點,爲性能優化提供依據。
環境搭建
(1)下載prometheus
解壓-由於普羅米修斯還將自己的數據公開爲HTTP端點,因此可以對其自身的健康狀況進行刮擦和監視。
http:// localhost:9090 進行監控,驗證自身度量標準http:// localhost:9090 / metrics(其他端口後面加/metrics即可)。
下載地址https://prometheus.io/download/
(2)解壓Prometheus
tar xvfz prometheus-*.tar.gz
cd prometheus-*
./prometheus --help(查看幫助文檔)
(3)配置prometheus.yml
YAML 的數據組織主要依靠的是空白,縮進,分行等結構實現配置
在線YAML編輯器https://www.bejson.com/validators/yaml_editor/
# 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: 'tomcat'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['localhost:8080']
-
evaluation_interval選項控制普羅米修斯將多久評估規則(普羅米修斯使用規則來創建新的時間序列並生成警報)
-
rule_files塊指定我們希望Prometheus服務器加載的任何規則的位置(無規則不填寫)
-
scrape_configs控制着普羅米修斯監視的資源(由於普羅米修斯還將自己的數據公開爲HTTP端點,因此可以對其自身的健康狀況進行刮擦和監視)
(4)啓動prometheus
./prometheus --config.file=prometheus.yml
(5)打開http:// localhost:9090
輸入 rate(http_requests_total[1m]) 使用graph可進行圖形查看
(6)安裝node_exporter
普羅米修斯單獨收集指標並不能很好地發揮普羅米修斯的能力。可使用Node Exporter來監視資源
下載地址https://prometheus.io/download/#node_exporter
tar xvfz node_exporter-*.tar.gz
cd node_exporter-*
啓動:./node_exporter
(7)配置監視主機
- job_name: node
static_configs:
- targets: [‘localhost:9100’]
重啓prometheus查看
使用實例
(1)下載Grafana
- 轉載:https://www.jianshu.com/p/7e7e0d06709b
(2)下載DataSource—Prometheus
設置url爲http://localhost:9090,監聽prometheus公開的自己數據端口。eg:http://localhost:8080 job=tomcat監測tomcat數據
(3)創建DashBoard
rate(http_requests_total{job="node"}[5m]) 指標進行測試 。
數據存儲
Counter
class YourClass {
static final Gauge inprogressRequests = Gauge.build()
.name("inprogress_requests").help("Inprogress requests.").register();
void processRequest() {
inprogressRequest.inc();
// Your code here.
inprogressRequest.dec();
}
}
註冊自定義counter,調用.inc()方法,void inc() -> void inc(double amt) -> 內部類Child中inc(double amt) -> DobleAdder().add(double x)存儲到內存以供拉取。
Summary
class YourClass {
static final Summary receivedBytes = Summary.build()
.name("requests_size_bytes").help("Request size in bytes.").register();
static final Summary requestLatency = Summary.build()
.name("requests_latency_seconds").help("Request latency in seconds.").register();
void processRequest(Request req) {
Summary.Timer requestTimer = requestLatency.startTimer();
try {
// Your code here.
} finally {
receivedBytes.observe(req.size());
requestTimer.observeDuration();
}
}
}
Gauge
class YourClass {
static final Gauge inprogressRequests = Gauge.build()
.name("inprogress_requests").help("Inprogress requests.").register();
void processRequest() {
inprogressRequest.inc();
// Your code here.
inprogressRequest.dec();
}
}
Histogram
class YourClass {
static final Histogram requestLatency = Histogram.build()
.name("requests_latency_seconds").help("Request latency in seconds.").register();
void processRequest(Request req) {
Histogram.Timer requestTimer = requestLatency.startTimer();
try {
// Your code here.
} finally {
requestTimer.observeDuration();
}
}
}
HTTP API:
基礎url+/api/v1 HTTP API網址:https://prometheus.io/docs/prometheus/latest/querying/api/#expression-queries
(1)Instant queries
GET /api/v1/query eg:curl 'http://localhost:9090/api/v1/query?query=up&time=2015-07-01T20:10:51.781Z'
up爲查詢條件,時間以GMT:格林尼治所在地的標準時間爲準(比北京時間晚 8小時)
(2)Range queries
GET /api/v1/query_range eg:curl 'http://localhost:9090/api/v1/query_range?query=up&start=2018-01-18T20:10:30.781Z&end=2018-01-18T20:11:00.781Z&step=15s'
(3)Querying metadata
GET /api/v1/series
eg:curl -g 'http://localhost:9090/api/v1/seriesmatch[]=up&match[]=process_start_time_seconds{job="tomcat"}'
(4)Querying label values
GET /api/v1/label/<label_name>/values
eg:curl http://localhost:9090/api/v1/label/job/values
(5)Target
GET /api/v1/targets eg:curl http://localhost:9090/api/v1/targets
(6)Alertmanagers
GET /api/v1/alertmanagers eg:curl http://localhost:9090/api/v1/alertmanagers
源碼pom導入
<!-- The client -->
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient</artifactId>
<version>0.1.0</version>
</dependency>
<!-- Hotspot JVM metrics-->
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_hotspot</artifactId>
<version>0.1.0</version>
</dependency>
<!-- Exposition HTTPServer-->
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_httpserver</artifactId>
<version>0.1.0</version>
</dependency>
<!-- Pushgateway exposition-->
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_pushgateway</artifactId>
<version>0.1.0</version>
</dependency>
基礎架構
網址:https://prometheus.io/docs/introduction/glossary/
Github:https://github.com/prometheus/client_java