业务监控平台

业务监控平台

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://prometheus.io/

 

https://github.com/knyar/nginx-lua-prometheus

原文地址:https://www.cnblogs.com/Leo_wl/p/8143222.html