prometheus+grafana+springboot2监控集成配置
前⾔
本⽂基于设计开发⼀个应⽤功能监控项⽬的需求,其能在不⼊侵系统的情况下正常监控应⽤功能。调研之后选择了
prometheus+grafana+springboot2的⽅案。本⽂基于此简单讲述⼀下这⼏个系统之间的配置、交互和使⽤。并从prometheus的功能,配置,使⽤,以及与dashboard和springboot/springcloud的集成等⽅⾯简单分析,对于各单独系统的安装不在本⽂讲述范围之内。
⼀、prometheus
1.简介
,是⼀个开源的系统监控和告警的⼯具包,其采⽤Pull⽅式采集时间序列的度量数据(也⽀持push⽅式),通过Http协议传输。它的⼯作⽅式是被监控的服务需要公开⼀个,这端点是⼀个HTTP接⼝,该接⼝公开了度量的列表和当前的值,然后Prometheus应⽤从此接⼝定时拉取数据,⼀般可以存放在时序数据库中,然后通过可视化的Grafana)进⾏数据展⽰。
2.⽀持的prometheus metrics
Counter,Gauge,Histogram,Summary,不进⾏细节介绍了,后⾯会专门写prometheus的⽂章。需要注意的是counter只能增不能减,适⽤于服务请求量,⽤户访问数等统计,但是如果需要统计有增有减的指标需要⽤Gauge。
3.⽀持的很多,可以⽅便的监控很多应⽤,同时也可以⾃定义开发⾮官⽅提供的exporter。
⼆、grafana
1.简介
,是⼀个开源的dashboard展⽰⼯具,可以⽀持很多主流数据源,包括时序性的和⾮时序性的。其提供的展⽰配置以及可扩展性能满⾜绝⼤部分时间序列数据展⽰需求,是⼀个⽐较优秀的⼯具。
2.⽀持的数据源
prometheus,inflexdb,elasticsearch,mysql,postgreSQL,openTSDB等,
三、springboot2与prometheus的集成
1.springboot集成配置
springboot2.x与prometheus的集成与springboot1.x区别⽐较⼤。prometheus官⽅提供的Java client不能⽀持2.x的处理。很多其中调⽤的class都不到了。所以在2.x中 simpleclient_spring_boot就不起作⽤了,启动会报错。幸好开源⼯具包io.micrometer提供了2.x中对prometheus采集端点的⽀持。所以在依赖中添加以下配置(使⽤gradle作为编译⼯具):
compile group: 'io.micrometer', name: 'micrometer-registry-prometheus', version: '1.0.6'
另外springboot2.x中提供了actuator作为采集exporter,为prometheus暴露采集端点,只需要在项⽬依赖中添加以下配置即可:compile('org.springframework.boot:spring-boot-starter-actuator')
完成之后就可以在springboot项⽬中⾃定义采集样本和⽅式了。
同时在配置⽂件application.yaml中要增加如下配置
management:
security:
# 仅限于开发环境可对security进⾏关闭。
enabled: false
metrics:
export:
prometheus:
enabled: true
step: 1m
descriptions: true
web:
server:
auto-time-requests: true
endpoints:
prometheus:
id: springmetrics
web:
exposure:
include:        health,info,env,prometheus,metrics,httptrace,threaddump,heapdump,springmetrics
配置完成之后即可在启动应⽤之后通过host:port/actuator/prometheus 访问采集信息
springboot架构图
之后在prometheus的配置⽂件l中添加scrape_configs:
- job_name: 'demo'
metrics_path: /actuator/prometheus
static_configs:
- targets: ['localhost:9009']
即可与prometheus建⽴联系。之后就可以在grafana中配置prometheus的数据源进⾏监控展⽰。
2.集成框架
micrometer提供了基于Java的monitor facade,其与springboot应⽤和prometheus的集成⽅式如下图展⽰
上图中展⽰的很清楚,应⽤通过micrometer采集和暴露监控端点给prometheus,prometheus通过pull模式来采集监控时序数据信息。之后作为数据源提供给grafana进⾏展⽰。
3.micrometer⽀持的度量⽅式及在springboot中的应⽤⽰例
Counter
Counter(计数器)简单理解就是⼀种只增不减的计数器。它通常⽤于记录服务的请求数量、完成的任务数量、错误的发⽣数量等等。
@Service("collectorService")
public class CollectorService {
static final Counter userCounter = Metrics.
counter("al", "services", "demo");
public void processCollectResult() throws InterruptedException {
while (true){
userCounter.increment(1D);
}
}
}
Gauge
Gauge(仪表)是⼀个表⽰单个数值的度量,它可以表⽰任意地上下移动的数值测量。Gauge通常⽤于变动的测量值,如当前的内存使⽤情况,同时也可以测量上下移动的"计数",⽐如队列中的消息数量
@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 + " + asure() + " : " + passCases);
}else {
int val = passCases.addAndGet(-100);
if (val < 0){
passCases.set(1);
}
System.out.println("DECR - " + asure() + " : " + passCases);
}
}
}
}
这⾥使⽤了⼀个true的循环⽤来展⽰不断更新的效果。
同样的可以在grafana中看到监控展⽰信息