業務監控平台
Prometheus是什么
Prometheus(普羅米修斯)是一個名字非常酷的開源監控系統。
它支持多維度的指標數據模型,服務端通過HTTP協議定時拉取數據后,通過靈活的查詢語言,實現監控的目的。
如上圖,客戶端記錄相關指標數據,對外提供查詢接口。Prometheus服務端通過服務器發現機制找到客戶端,並定時抓取存儲為時間序列數據。最后通過Grafana等圖表工具集成展示。
Prometheus可以做什么
-
在業務層用作埋點系統
Prometheus支持各個主流開發語言(Go,java,python,ruby官方提供客戶端,其他語言有第三方開源客戶端)。我們可以通過客戶端方面的對核心業務進行埋點。如下單流程、添加購物車流程。 -
在應用層用作應用監控系統
一些主流應用可以通過官方或第三方的導出器,來對這些應用做核心指標的收集。如redis,mysql。 -
在系統層用作系統監控
除了常用軟件, prometheus也有相關系統層和網絡層exporter,用以監控服務器或網絡。 -
集成其他的監控
prometheus還可以通過各種exporte,集成其他的監控系統,收集監控數據,如AWS CloudWatch,JMX,Pingdom等等。
不要用Prometheus做什么
prometheus也提供了Grok exporter等工具可以用來讀取日志,但是prometheus是監控系統,不是日志系統。應用的日志還是應該走ELK等工具棧。
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'
-
maven中添加相關依賴
<!-- The client --> <dependency> <groupId>io.prometheus</groupId> <artifactId>simpleclient</artifactId> </dependency> <!-- Exposition servlet--> <dependency> <groupId>io.prometheus</groupId> <artifactId>simpleclient_servlet</artifactId> </dependency> <dependency> <groupId>io.prometheus</groupId> <artifactId>simpleclient_spring_boot</artifactId> </dependency>
-
關閉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等工具,讓你全方位掌控你的系統。
相關資料:
https://github.com/knyar/nginx-lua-prometheus