Prometheus是什么
Prometheus(普罗米修斯)是一个名字很是酷的开源监控系统。java
它支持多维度的指标数据模型,服务端经过HTTP协议定时拉取数据后,经过灵活的查询语言,实现监控的目的。python
如上图,客户端记录相关指标数据,对外提供查询接口。Prometheus服务端经过服务器发现机制找到客户端,并定时抓取存储为时间序列数据。最后经过Grafana等图表工具集成展现。mysql
Prometheus能够作什么
在业务层用做埋点系统
Prometheus支持各个主流开发语言(Go,java,python,ruby官方提供客户端,其余语言有第三方开源客户端)。咱们能够经过客户端方面的对核心业务进行埋点。以下单流程、添加购物车流程。nginx
在应用层用做应用监控系统
一些主流应用能够经过官方或第三方的导出器,来对这些应用作核心指标的收集。如redis,mysql。git
在系统层用做系统监控
除了经常使用软件, prometheus也有相关系统层和网络层exporter,用以监控服务器或网络。github
集成其余的监控
prometheus还能够经过各类exporte,集成其余的监控系统,收集监控数据,如AWS CloudWatch,JMX,Pingdom等等。redis
不要用Prometheus作什么
prometheus也提供了Grok exporter等工具能够用来读取日志,可是prometheus是监控系统,不是日志系统。应用的日志仍是应该走ELK等工具栈。spring
Prometheus 和 spring boot集成
Prometheus中配置服务发现
- job_name: 'consul' consul_sd_configs: - server: '192.168.1.248:8500' relabel_configs: - source_labels: [__meta_consul_service] regex: .*,prometheus.* target_label: job metrics_path: '/prometheus'sql
maven中添加相关依赖ruby
io.prometheus
simpleclient
io.prometheus
simpleclient_servlet
io.prometheus
simpleclient_spring_boot
关闭spring boot原生metrics
spring.metrics.servo.enabled: false
Application类添加注解
@EnablePrometheusEndpoint
@EnableSpringBootMetricsCollector
业务类定义埋点
static final Counter orderCount = Counter.build()
.name("b2c_order_count").help("order count.").labelNames("shop","siteUid").register();
业务埋点
orderCount.labels("shein","mus").inc();
Prometheus监控nginx
Prometheus能够经过nginx-lua-prometheus这个库对nginx进行埋点。
使用起来也很是简单:
lua_shared_dict prometheus_metrics 10M;
lua_package_path "/path/to/nginx-lua-prometheus/?.lua";
init_by_lua '
prometheus = require("prometheus").init("prometheus_metrics")
metric_requests = prometheus:counter(
"nginx_http_requests_total", "Number of HTTP requests", {"host", "status"})
metric_latency = prometheus:histogram(
"nginx_http_request_duration_seconds", "HTTP request latency", {"host"})
metric_connections = prometheus:gauge(
"nginx_http_connections", "Number of HTTP connections", {"state"})
';
log_by_lua '
local host = ngx.var.host:gsub("^www.", "")
metric_requests:inc(1, {host, ngx.var.status})
metric_latency:observe(ngx.now() - ngx.req.start_time(), {host})
';
可是,经过基准测试,发现使用了histogram类型的指标后,吞吐量会有5%-10%左右的下降。
总结
借助Prometheus,咱们能够快速搭建一个业务监控系统,同时还能增长核心应用的监控手段。丰富咱们的监控渠道,配合zabbix、zipkin、ELK、Grafana等工具,让你全方位掌控你的系统。
相关资料: