組件介紹
prometheus
Prometheus是一個開源的服務(wù)監(jiān)控系統(tǒng),它通過HTTP協(xié)議從遠程的機器收集數(shù)據(jù)并存儲在本地的時序數(shù)據(jù)庫上耀石。
多維數(shù)據(jù)模型(時序列數(shù)據(jù)由metric名和一組key/value組成)
在多維度上靈活的查詢語言(PromQl)
不依賴分布式存儲瓮栗,單主節(jié)點工作.
通過基于HTTP的pull方式采集時序數(shù)據(jù)
可以通過push gateway進行時序列數(shù)據(jù)推送(pushing)
可以通過服務(wù)發(fā)現(xiàn)或者靜態(tài)配置去獲取要采集的目標(biāo)服務(wù)器
多種可視化圖表及儀表盤支持
Prometheus通過安裝在遠程機器上的exporter來收集監(jiān)控數(shù)據(jù)寥掐,后面我們將使用到node_exporter收集系統(tǒng)數(shù)據(jù)最岗。
grafana
Grafana 是一個開箱即用的可視化工具蛔六,具有功能齊全的度量儀表盤和圖形編輯器荆永,有靈活豐富的圖形化選項,可以混合多種風(fēng)格国章,支持多個數(shù)據(jù)源特點具钥。
exporter
Exporter 組件是一類組件,它們的主要作用就是提供 metrics 信息以供加工提煉液兽。
有的組件會自行提供 metrics 信息骂删,比如 Grafana掌动、Prometheus、Etcd 等等宁玫,在本文的 3.1
3.1 中給出的 metrics 就是 Grafana 本身產(chǎn)生的粗恢。
有的組件不會提供 metrics 信息,比如說我們自己寫的一些程序欧瘪。
而有的甚至不是組件眷射,比如 Linux 系統(tǒng)本身
監(jiān)控springboot2.0的服務(wù) 和 node-exporter
服務(wù)依賴
<!-- autuator 服務(wù)端點監(jiān)控 只需加依賴 默認(rèn)開4個端點 1 yml配置開啟更多端點 2手動up/down 支持手動下機 hystrix,dashboard,admin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 開啟prometheus監(jiān)控-->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
服務(wù)yml
management:
endpoints:
web:
exposure:
include: "*" #inlude包含哪些端點 exclude 去除哪些端點
endpoint:
shutdown:
enabled: true #開啟遠程shutdown端口
health:
show-details: always
prometheus:
enabled: true
metrics: #開啟prometheus 監(jiān)控
export:
prometheus:
enabled: true
step: 1m
descriptions: true
web:
server:
request:
autotime:
enabled: true
#設(shè)置為true來收集所有Spring MVC度量》鹨矗或者妖碉,當(dāng)它設(shè)置為false時,您可以通過用@timed對其進行注釋來為特定的REST控制器啟用度量芥被。
#可以在控制器內(nèi)注釋單個方法欧宜,以僅為特定端點生成度量
服務(wù)配置
@Bean
MeterRegistryCustomizer meterRegistryCustomizer(MeterRegistry meterRegistry) {
return meterRegistry1 -> meterRegistry.config()
.commonTags("application", "service1");
}
啟動服務(wù) 訪問 localhost:8096/actuator/prometheus
可以看到一些metrix信息
配置 prometheus
global:
scrape_interval: 15s # 默認(rèn)抓取間隔, 15秒向目標(biāo)抓取一次數(shù)據(jù)。
external_labels:
monitor: 'codelab-monitor'
# 這里表示抓取對象的配置
scrape_configs:
- job_name: 'prometheus'
#這個配置是表示在這個配置內(nèi)的時間序例拴魄,每一條都會自動添加上這個{job_name:"prometheus"}的標(biāo)簽 - job_name: 'prometheus'
scrape_interval: 5s # 重寫了全局抓取間隔時間冗茸,由15秒重寫成5秒
static_configs:
- targets: ['localhost:9090']
- job_name: 'node'
static_configs:
- targets: ['172.17.0.9:9100']
labels:
instance: "node"
- job_name: 'service1'
metrics_path: /actuator/prometheus
static_configs:
- targets: ['192.168.10.134:8096']
labels:
instance: "service"
啟動 node-exporter
docker pull prom/node-exporter
docker pull prom/node-exporter
啟動 prometheus
docker pull prom/prometheus
docker run -idt -p 9090:9090 -v /home/spf/docker/promethues/server/prometheus.yml:/etc/prometheus/prometheus.yml -v /home/spf/docker/promethues/server/rules.yml:/etc/prometheus/rules.yml --name prometheus prom/prometheus --config.file=/etc/prometheus/prometheus.yml --web.enable-lifecycle
#--web.enable-lifecycle允許遠程修改配置
這里注意 ip的設(shè)置 因為prometheus是在docker內(nèi)部啟動的所以 localhost訪問的是docker 容器內(nèi)部不能訪問到外部的服務(wù)
可以使用 docker exec -u root -it prometheus /bin/sh
ping進去試一下
啟動grafana
#下載鏡像
docker pull grafana/grafana
#運行
docker run -d --name=grafana -p 3000:3000 grafana/grafana
訪問 localhost:3000 默認(rèn)賬號密碼為 admin/admin
home第二項添加datasource 選擇promeetheus 添加ip:port
可以查看dashboard了
dashboard
補充:
Counter
Counter(計數(shù)器)簡單理解就是一種只增不減的計數(shù)器。它通常用于記錄服務(wù)的請求數(shù)量羹铅、完成的任務(wù)數(shù)量蚀狰、錯誤的發(fā)生數(shù)量等等。
@Service("collectorService")
public class CollectorService {
static final Counter userCounter = Metrics.
counter("user.counter.total", "services", "demo");
public void processCollectResult() throws InterruptedException {
while (true){
userCounter.increment(1D);
}
}
}
Gauge
Gauge(儀表)是一個表示單個數(shù)值的度量职员,它可以表示任意地上下移動的數(shù)值測量麻蹋。Gauge通常用于變動的測量值,如當(dāng)前的內(nèi)存使用情況焊切,同時也可以測量上下移動的"計數(shù)"扮授,比如隊列中的消息數(shù)量
@Component("passCaseMetric")
public class PassCaseMetric {
List<Tag> init(){
ArrayList<Tag> list = new ArrayList(){};
list.add(new ImmutableTag("service", "demo"));
return list;
}
AtomicInteger atomicInteger = new AtomicInteger(0);
Gauge passCaseGuage = Gauge.builder("pass.cases.guage", atomicInteger, AtomicInteger::get)
.tag("service", "demo")
.description("pass cases guage of demo")
.register(new SimpleMeterRegistry());
AtomicInteger passCases = Metrics.gauge("pass.cases.guage.value", init(), atomicInteger);
public void handleMetrics() {
while (true){
if (System.currentTimeMillis() % 2 == 0){
passCases.addAndGet(100);
System.out.println("ADD + " + passCaseGuage.measure() + " : " + passCases);
}else {
int val = passCases.addAndGet(-100);
if (val < 0){
passCases.set(1);
}
System.out.println("DECR - " + passCaseGuage.measure() + " : " + passCases);
}
}
}
}
啟動springboot應(yīng)用,可以在http://host:port/actuator/prometheus 看到端點收集到的數(shù)據(jù)专肪。其他的也是類似的不再一一截圖展示刹勃。
這里使用了一個true的循環(huán)用來展示不斷更新的效果。
同樣的可以在grafana中看到監(jiān)控展示信息
Timer
Timer(計時器)同時測量一個特定的代碼邏輯塊的調(diào)用(執(zhí)行)速度和它的時間分布嚎尤。簡單來說荔仁,就是在調(diào)用結(jié)束的時間點記錄整個調(diào)用塊執(zhí)行的總時間,適用于測量短時間執(zhí)行的事件的耗時分布芽死,例如消息隊列消息的消費速率乏梁。
@Test
public void testTimerSample(){
Timer timer = Timer.builder("timer")
.tag("timer", "timersample")
.description("timer sample test.")
.register(new SimpleMeterRegistry());
for(int i=0; i<2; i++) {
timer.record(() -> {
try {
TimeUnit.SECONDS.sleep(2);
}catch (InterruptedException e){
}
});
}
System.out.println(timer.count());
System.out.println(timer.measure());
System.out.println(timer.totalTime(TimeUnit.SECONDS));
System.out.println(timer.mean(TimeUnit.SECONDS));
System.out.println(timer.max(TimeUnit.SECONDS));
}
響應(yīng)數(shù)據(jù)
2
[Measurement{statistic='COUNT', value=2.0}, Measurement{statistic='TOTAL_TIME', value=4.005095763}, Measurement{statistic='MAX', value=2.004500494}]
4.005095763
2.0025478815
2.004500494
Summary
Summary(摘要)用于跟蹤事件的分布。它類似于一個計時器关贵,但更一般的情況是遇骑,它的大小并不一定是一段時間的測量值。在micrometer中揖曾,對應(yīng)的類是DistributionSummary落萎,它的用法有點像Timer亥啦,但是記錄的值是需要直接指定,而不是通過測量一個任務(wù)的執(zhí)行時間练链。
@Test
public void testSummary(){
DistributionSummary summary = DistributionSummary.builder("summary")
.tag("summary", "summarySample")
.description("summary sample test")
.register(new SimpleMeterRegistry());
summary.record(2D);
summary.record(3D);
summary.record(4D);
System.out.println(summary.count());
System.out.println(summary.measure());
System.out.println(summary.max());
System.out.println(summary.mean());
System.out.println(summary.totalAmount());
}
響應(yīng)數(shù)據(jù):
3
[Measurement{statistic='COUNT', value=3.0}, Measurement{statistic='TOTAL', value=9.0}, Measurement{statistic='MAX', value=4.0}]
4.0
3.0
9.0