zoukankan      html  css  js  c++  java
  • prometheus监控系统

    关于Prometheus

    Prometheus是一套开源的监控系统,它将所有信息都存储为时间序列数据;因此实现一种Profiling监控方式,实时分析系统运行的状态、执行时间、调用次数等,以找到系统的热点,为性能优化提供依据。
    监控方式
    程序代码收集运行数据写入到redis,通过API接口开放给Prometheus,Prometheus定时去抓取接口上的数据,形成时间系数据存入到本地。同时通过promql与开放接口可视化数据到前端。
     
    一、采集运行数据写入到redis
    在采集数据之前,先要明白Prometheus所支持的几种数据类型
     
    Counter,统计类数据
    Counter作为一种计数类数据类型,常用于接口调用次数、请求总数、当前在线人数等等
     
    Gauge,记录对象或许事物的瞬时值
    Gauge是最简单的度量类型,只有一个简单的返回值,他用来记录一些对象或者事物的瞬时值,如CPU使用率、内存使用情况、磁盘空间等。
     
    Histograms,直方图
    Histrogram是用来度量数据中值的分布情况,如程序执行时间:0-100ms、100-200ms、200-300ms、>300ms 的分布情况
     
    在使用Prometheus监控之前,先要明白都需要监控哪些项,以及每一中项都应该采用什么数据类型来表示
     
    明白以上类型之后,就可以开始将监控数据项写入到redis。写入的工具可采用开源的prometheus_client_php;github:https://github.com/Jimdo/prometheus_client_php 
    下载后需要用composer部署其依赖,如果你不知道composer的使用:http://www.phpcomposer.com 
    以下代码记录一个counter数据
     
    require __DIR__ . '/../vendor/autoload.php';
    use PrometheusCollectorRegistry;
    use PrometheusStorageRedis;
    
    Redis::setDefaultOptions(array('host' => 'master.redis.reg'));
    $adapter = new PrometheusStorageRedis();
    $registry = new CollectorRegistry($adapter);
    
    $counter = $registry->registerCounter('test', 'some_counter', 'it increases', ['type']);
    $counter->incBy(1, ['blue']);  //将统计结果增加1
     
    采集的过程比较简单,Gauge与Histograms与此类似
     
    二、提供Prometheus访问的API Gateway
     
    prometheus_client_php工具已经准好了prometheus访问的相关接口,我们至需要几行简单的代码
     
    require __DIR__ . '/../vendor/autoload.php';
    
    use PrometheusCollectorRegistry;
    use PrometheusRenderTextFormat;
    use PrometheusStorageRedis;
    
    Redis::setDefaultOptions(array('host' => 'master.redis.reg')); //对应写入的redis
    $adapter = new PrometheusStorageRedis();
    $registry = new CollectorRegistry($adapter);
    $renderer = new RenderTextFormat();
    $result = $renderer->render($registry->getMetricFamilySamples());
    
    header('Content-type: ' . RenderTextFormat::MIME_TYPE);
    echo $result; //输出结果给prometheus服务
     
    将以上脚本部署,能通过HTTP访问到,下一步即可提供给prometheus服务
     
    三、安装与配置Prometheus
    安装(略过)
    配置Prometheus
    打开prometheus.yml文件,在scrape_configs配置第二步中的API Gateway
     - job_name: 'prometheus2'
        metrics_path: '/metrics.php'
        static_configs:  
          - targets: ['localhost:9999']
     
    配置完成后,启动Prometheus
     
    四、查看监控数据
    Prometheus具有多种模式的可视化数据,可集成Grafana可视化工具。其内置PromDash界面,启动后打开http://localhost:9090(默认9090端口)即可访问内置的PromDash监控界面。
    上图是Prometheus的直方图展现形式,顶部的下拉框是你所监控的数据项,点击Execute点击后查看所监控的数据。输入框部分是是查询的表达式,Prometheus支持丰富的查询表达式,详情参阅:https://prometheus.io/docs/querying/basics/ 。
    图中的每一条线表示大于该桶的数据,红色的数据线是所有桶的总数累计。另外Prometheus还提供了sum与cont的统计。Gauge与Counter数据类型也与此类似。
  • 相关阅读:
    eclipse中的Invalid text string (xxx).
    在jsp文件中出现Unknown tag (c:out)
    eclipse 界面复原
    ecilpse 纠错插件
    Multiple annotations found at this line:- The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
    Port 8080 required by Tomcat v9.0 Server at localhost is already in use. The server may already be running in another process, or a system process may be using the port.
    调用第三方https接口
    调用第三方http接口
    创建带值枚举
    spring整合redis之Redis配置文件
  • 原文地址:https://www.cnblogs.com/xiezhengcai/p/prometheus.html
Copyright © 2011-2022 走看看