[Hyperf] 在 Hyperf 框架中使用 prometheus + grafana 部署基本的监控

参考: hyperf利用prometheus接入服务监控,使用grafana实现数据的实时监控显示
hyperf文档

本文章记录本人的第一次部署所踩的坑,未深入了解prometheus 和grafana 如有不当的地方请指正,谢谢!

一. 使用docker-compose部署

version: '2'
networks:
  monitor:
    driver: bridge

services:
  prometheus:
    image: prom/prometheus
    container_name: prometheus
    hostname: prometheus
    restart: always
    volumes:
        # 将你的prometheus.yml文件放在当前文件同级下,或自定义
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
      #- /home/prometheus/node_down.yml:/etc/prometheus/node_down.yml
    ports:
      - "9090:9090"
    networks:
      monitor:
        ipv4_address: 172.18.0.3

  grafana:
    image: grafana/grafana
    container_name: grafana
    hostname: grafana
    restart: always
    volumes:
    # 创建 etc目录,data目录存储grafana的数据
    - ./etc:/etc/grafana
    - ./data:/var/lib/grafana
    ports:
      - "3000:3000"
    networks:
      monitor:
        ipv4_address: 172.18.0.4

  node-exporter:
    image: prom/node-exporter
    container_name: node-exporter
    hostname: node-exporter
    restart: always
    ports:
      - "9100:9100"
    networks:
      monitor:
        ipv4_address: 172.18.0.2

注意:为了避免每次docker-compose 启动之后 ip会发生变化,我这里配置了固定IP,请根据个人实际情况配置,或参阅docker相关文档

使用命令docker-compose up启动容器

二. 项目配置

因为对 prometheus的不了解,我直接使用hyperf默认配置

  • 引入组件 composer require hyperf/metric

  • 发布默认配置文件 php bin/hyperf.php vendor:publish hyperf/metric

  • config/autoload/dependencies.php中添加对应的Redis存储

    return [
      \Prometheus\Storage\Adapter::class => \Hyperf\Metric\Adapter\Prometheus\RedisStorageFactory::class,
    ];

    在上面的第一篇文章中,老哥说使用swoole_table更高效,我还不知道如何使用,有兴趣的老哥可以自己研究一下。

  • 增加中间件
    config/autoload/middlewares.php文件中增加对应的中间件

    return [
      'http' => [
          \Hyperf\Metric\Middleware\MetricMiddleware::class,
      ],
    ];
  • 添加 metrics路由

    Router::get('/metrics', function(){
      $registry = Hyperf\Utils\ApplicationContext::getContainer()->get(Prometheus\CollectorRegistry::class);
      $renderer = new Prometheus\RenderTextFormat();
      return $renderer->render($registry->getMetricFamilySamples());
    });

    这样对项目的配置就完成了

三. prometheus的配置

prometheus.yml文件中增加对应的配置

scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
    - targets: ['localhost:9090']

  - job_name: 'node'

     # 注意这里的IP需要填写 node-exporter 容器的ip  
    static_configs:
    - targets: ['172.18.0.2:9100']


  - job_name: 'skeleton'
    # 这里填写的是宿主机的ip
    static_configs:
    - targets: ['10.0.75.1:9502']

配置完成之后,再次 dokcer-compose up

访问 http://localhost:9090 查看 prometheus

[Hyperf]  在Hyperf框架中使用prometheus + grafana 部署基本的监控

如图所示,nodeskeleton 都已启动

四. Grafana 配置

上面都配置完了,开始配置 Grafana
打开 http://localhost:3000 默认密码是: admin/admin

  • 新建datasource
    左侧边栏 add datasources 选择Prometheus

[Hyperf]  在 Hyperf 框架中使用 prometheus + grafana 部署基本的监控

  • 配置 datasource

[Hyperf]  在 Hyperf 框架中使用 prometheus + grafana 部署基本的监控

填写容器的IP:端口

  • 导入hyperf官方的JSON文件

[Hyperf]  在 Hyperf 框架中使用 prometheus + grafana 部署基本的监控

导入之后需要将默认的 app_name改成你自己的
如:admin-api 就需要填写admin_api 改成下划线形式

[Hyperf]  在 Hyperf 框架中使用 prometheus + grafana 部署基本的监控

  • 查看监控
    在Home中你就可以看到了

[Hyperf]  在 Hyperf 框架中使用 prometheus + grafana 部署基本的监控

点进去查看

[Hyperf]  在 Hyperf 框架中使用 prometheus + grafana 部署基本的监控

到此结束,小白第一次配置监控,还有很多东西没弄清楚

本作品采用《CC 协议》,转载必须注明作者和本文链接
marun
讨论数量: 8

我严重怀疑你引用的第一篇文章的作者不了解 Prometheus 的架构或者表达能力存在问题:

由于中间件会影响程序执行速度,通过测试,直接在中间件将数据写入prometheus严重影响执行效率。故可以通过写入到swoole_table的高性能内存,再从定时器写入prometheus,并发性能测试结果反映,这办法对性能影响极低,并且满足日常监控的需求!

Prometheus 采用的是 Scrape metrics 的方式,而不是由被监控的对象主动上报。如果需要推送数据到 Prometheus 需要使用 Pushgateway,不过文章中似乎并没有提到 Pushgateway 相关内容。

4年前 评论
huangzhhui 4年前
marun (楼主) 4年前
Wi1dcard (作者) 4年前
yuandj 1年前

写得不错,流程很清晰

4年前 评论
marun

@huangzhhui 哈哈 谢谢。刚上手hyperf

4年前 评论

使用阿里云的redis集群爆出错误ERR bad lua script for redis cluster, first parameter of redis.call/redis.pcall must be a single literal string"大佬是如何解决的?

2年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!