hyperf| hyperf/metric 上手指南

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
可观测可视化 Grafana 版,10个用户账号 1个月
简介: 这期又开始聊微服务的基础设施之一: 实时监控. 更准确的说法, 基于 prometheus 的实时监控. 关于技术选型这里就不多啰嗦啦, 很多时候「从众」或者「用脚投票」往往是最有效的

这期又开始聊微服务的基础设施之一: 实时监控. 更准确的说法, 基于 prometheus 的实时监控. 关于技术选型这里就不多啰嗦啦, 很多时候「从众」或者「用脚投票」往往是最有效的

真的猛士, 敢于走少有人走的路. 我们选择 prometheus, 这条猛士已经走过的路.

prometheus 实战

之所以先把实战放上来, 在于「看得见」往往不如「摸得着」来得 刺激 深刻. 有了一套环境在那放着, 变学变实践, 才不会停留在 花了时间学, 用的时候还是不会 这种低效的循环里. 至于环境问题怎么解决, 又到了我们的老朋友 docker 了.

version: '3.1'
services:
    hyperf:
        image: hyperf/hyperf
        volumes:
            - ./:/data
        ports:
            - "9501:9501"
        tty: true
    # https://prometheus.io/docs/prometheus/latest/installation/
    prometheus:
        image: prom/prometheus
        ports:
            - "9090:9090"
        volumes:
            - ./config/prometheus.yml:/etc/prometheus/prometheus.yml
    # https://grafana.com/docs/installation/docker/
    grafana:
        image: grafana/grafana
        ports:
            - "3000:3000"

docker 知识不熟悉的小伙伴, 可以先学着把自己本地使用的环境 docker 化.

把 docker 当做一个工具, 就会发现这家伙是的真的简单. 原理可能很复杂, 但是我只是使用呀, 这和 CRUD 能有多大差别?

hyperf/metric 配置

首先是 hyperf 里面使用 prometheus, 这里参考官网文档 hyperf/metric 即可. 当然官网的 metric 包比较强大(这不是吹水, 官方通常要考虑更 通用, 监控选项的支持会更多一些), 这里我们只关注 prometheus:

  • 安装
# 安装
composer require hyperf/metric

# 添加配置
php bin/hyperf vendor:publish hyperf/metric
  • 配置
return [
    'default' => env('METRIC_DRIVER', 'prometheus'),
    'use_standalone_process' => env('METRIC_USE_STANDALONE_PROCESS', true),
    'enable_default_metric' => env('METRIC_ENABLE_DEFAULT_METRIC', true),
    'default_metric_interval' => env('DEFAULT_METRIC_INTERVAL', 5),
    'metric' => [
        'prometheus' => [
            'driver' => Hyperf\Metric\Adapter\Prometheus\MetricFactory::class,
            'mode' => Constants::SCRAPE_MODE,
            'namespace' => env('APP_NAME', 'skeleton'),
            'scrape_host' => env('PROMETHEUS_SCRAPE_HOST', '0.0.0.0'),
            'scrape_port' => env('PROMETHEUS_SCRAPE_PORT', '9502'),
            'scrape_path' => env('PROMETHEUS_SCRAPE_PATH', '/metrics'),
            'push_host' => env('PROMETHEUS_PUSH_HOST', '0.0.0.0'),
            'push_port' => env('PROMETHEUS_PUSH_PORT', '9091'),
            'push_interval' => env('PROMETHEUS_PUSH_INTERVAL', 5),
        ],
        ...

使用默认配置就好, 默认配置就是 prometheus, 之后访问 http://localhost:9502/metrics 就可以查看组件默认配置的 metric

prometheus 配置

重复一句, 你只是使用 prometheus, 根据「学习思维三部曲: what->how->why」, 只是 what 其实很简单(简单不代表低级, 命名很简单却还没有完成的问题比比皆是), 这里都使用默认配置运行起来即可

参考文件, 即上面 docker-compose 中配置的:

    prometheus:
        image: prom/prometheus
        ports:
            - "9090:9090"
        volumes:
            - ./config/prometheus.yml:/etc/prometheus/prometheus.yml

对应其实只需要修改:

# my global config
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
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: 'hyperf'
    static_configs:
    - targets: ['ms:9502']
  - job_name: 'grafana'
    static_configs:
    - targets: ['grafana:3000']

只需要添加需要监控的 job 即可, 这里我添加 grafanahyperf 作为示例

grafana 配置

grafana 纯 webUI 交互, 启动后按照页面提示, 就可以添加 prometheus 作为数据源, 然后可以选择一个 grafana 的 UI 模板, 就可以看到采集的效果了

同理, hyperf 官方团队也提供了 UI 模板, 在 grafana theme 中搜索即可, 替换相应的参数, 就可以看到 hyperf 默认提供的 metric 参数

实践小结

实践是检验真理的唯一标准, 这句话在面对新技术, 面对未知时尤其有用, 学习一个新知识, 通常面对的知识 what 一级的内容, 很多时候天然具备 简单 属性, 这个时候的摸爬滚打几乎是 最公平 的 -- 只需要像小孩子一样, 保持好奇心, 多试几次.

prometheus 的典型生产实践

项目中使用 prometheus 监控最常见的 2 个场景:

  • api 监控 -> 扩展在所有的业务服务监控
  • db 监控 -> 扩展到所有基础服务监控

到了这里, 我们需要补充一点 prometheus 的基础知识:

prometheus docs: https://prometheus.io/docs/introduction/overview/

prometheus 文档并不长, 阅读大概需要 15min

PS: 阅读英文有障碍, 其实主要是心理作用, 安装一个网页翻译查词插件, 哪里不会点哪里, 常用的计算机词汇熟悉后, 几乎就不会影响阅读体验了

摘抄一下需要掌握的基础知识:

  • 基础概念

    • metric: 监控指标, 比如 pv_total
    • label: 相当于 tag, 附在 metric 上, 可以做细化的查询分析
  • 指标类型

    • count: 只增计数器, 比如 pv
    • gauge: 可增减计数器, 比如当前 qps
    • histogrm: 直方图, 比如 95值
  • query: PromQL

熟悉 MySQL 记忆有一些代码量的读者, 我相信 show me the code 绝对是绝佳的学习方式:

# api 访问次数
sum(rate(app_api_query_time_count{api_name="self"}[1m]))
# service 对应 request_uri
sum(rate(app_api_query_time_count{api_name="self"}[1m])) by (service)

# api 访问耗时95值
# https://prometheus.io/docs/concepts/metric_types/#histogram
histogram_quantile(0.95, sum(rate(app_api_query_time_bucket{api_name="self"}[1m])) by (le))
histogram_quantile(0.95, sum(rate(app_api_query_time_bucket{api_name="self"}[1m])) by (le, service))

# 表查询次数
sum(irate(app_db_query_time_count{db=~"mysql:.*"}[1m])) by (db,table)
# 表访问次数
# type: curd
sum(rate(app_db_query_time_count{db=~"mysql:.*"}[30s])) by (type)
# db 响应时间95值
histogram_quantile(0.95, sum(irate(app_db_query_time_bucket{db=~"mysql:.*"}[1m])) by (le))
# db 平均查询时间
sum(rate(app_db_query_time_sum{db=~"mysql:.*"}[30s]))/sum(rate(app_db_query_time_count{db=~"mysql:.*"}[30s]))

# api qps
sum(rate(app_api_query_time_count{api_name!="self"}[1m])) by (api_name)
# api 平均响应时间
sum(rate(app_api_query_time_sum{api_name!="self"}[1m])) by (api_name)/sum(rate(app_api_query_time_count{api_name!="self"}[1m])) by (api_name)

PS: 关于95值, 熟悉 web 性能指标对 95值 就不陌生了, 直译是 95%的请求在多久时间内返回, 95值 在监控服务指标上使用广泛, 如果需要更好的性能, 可以使用 97值 甚至 99值, 作为衡量标准

一次 PHP微服务 prometheus 落地实践之旅

监控由微服务基础设施团队下的服务保障组来统一维护, 落实好 技术执行(业务开发) / 技术决策(架构师/服务 owner) / 技术支撑(基础设施/服务保障) 3大角色的分工合作, 上线 prometheus 选择了对业务方几乎零打扰的方案:

  • prometheus 使用的单独的 redis 进行存储
<?php

namespace Mt\Metric;

use Hyperf\Redis\RedisFactory;
use Hyperf\Metric\Adapter\Prometheus\Redis;

class RedisStorageFactory
{
    public function __invoke()
    {
        $redis = container(RedisFactory::class)->get('metric');
        Redis::setPrefix(config('app_name'));
        return Redis::fromExistingConnection($redis);
    }
}
  • 添加默认 metric 配置
'metric' => [
        'enable' => true,
        'default' => 'prometheus',
        // 不适用单独进程
        'use_standalone_process' => false,
        // 不使用默认 metric
        'enable_default_metric' => false,
        // 5s 统计周期
        'default_metric_interval' => 5,
        'metric' => [
            'prometheus' => [
                'driver' => \Hyperf\Metric\Adapter\Prometheus\MetricFactory::class,
                // 自定义模式
                'mode' => \Hyperf\Metric\Adapter\Prometheus\Constants::CUSTOM_MODE,
                // hyperf/metric 已修复这个问题, 使用 _ 作为分隔符
                'namespace' => \Hyperf\Utils\Str::camel(env('APP_NAME', 'app')),
            ],
        ],
    ],
  • 统一监控 api/db
<?php

namespace Mt\Metric;

use Hyperf\Metric\Metric;

class Prometheus
{
    public static function isEnable()
    {
        return config('metric.enable') === true;
    }

    public static function apiQuery($api, $http_code, $use_time, $method = '')
    {
        if (!self::isEnable()) {
            return;
        }
        $name = 'api_query_time';
        $labels = [
            'hostname' => php_uname('n'),
            'service' => config('app_name', 'app'),
            'api_name' => $api,
            'code' => (string)$http_code,
            'method' => $method,
        ];
        Metric::put($name, (float)($use_time * 1000), $labels);
    }

    public static function dbQuery($type, $db, $table, $use_time, $result = 'ok')
    {
        if (!self::isEnable()) {
            return;
        }
        $name = 'db_query_time';
        $labels = [
            'hostname' => php_uname('n'),
            'service' => config('app_name', 'app'),
            'type' => $type,
            'db' => $db,
            'table' => $table,
            'result' => $result,
        ];
        Metric::put($name, (float)$use_time, $labels);
    }

    public static function responseCode($api, $response_code)
    {
        if (!self::isEnable()) {
            return;
        }
        $name = 'api_response_code';
        $labels = [
            'hostname' => php_uname('n'),
            'service' => config('app_name', 'app'),
            'api' => $api,
            'response_code' => (string)$response_code,
        ];
        Metric::count($name, 1, $labels);
    }
}
  • 最后一步, 由一个基础设施的 tools 服务中暴露路由给 prometheus 服务即可
// metrics
$renderer = new RenderTextFormat();
$prometheus_collector = new PrometheusCollector();
$micro_services = ['app1', 'app2', 'app3'];
foreach ($micro_services as $service_name) {
    Router::get('/' . $service_name . '/metrics', function() use($service_name, $prometheus_collector, $renderer) {
        $prometheus_collector->setPrefix($service_name);
        return $renderer->render($prometheus_collector->collect());
    }, ['middleware' => [MetricAuthMiddleware::class]]);
}

写在最后

prometheus 的监控之旅还没有走完, 不断暴露问题, 不断提升监控的数量和质量, 对了, 用一句经典的话语:

用发展的眼光来看问题
目录
相关文章
|
Java API PHP
(转载)为什么不推荐使用swoole和hyperf官方框架
(转载)为什么不推荐使用swoole和hyperf官方框架
3163 0
|
3月前
spring-state-machine守卫踩坑
spring-state-machine守卫踩坑
15 0
|
3月前
|
中间件
SAP UI5 Tooling 实战:动手创建自己的 Custom UI5 Server Middleware 试读版
SAP UI5 Tooling 实战:动手创建自己的 Custom UI5 Server Middleware 试读版
19 0
SAP UI5 Tooling 实战:动手创建自己的 Custom UI5 Server Middleware 试读版
|
5月前
|
编译器 Shell Go
|
10月前
|
中间件 Go 网络架构
Traefik Middleware 插件实践
话说,作为一款开源的边缘路由器,Traefik 或多或少算是一款比较牛逼的新生代组件,其不仅可以作为接入层组件,实现流量转发等相关功能,同时也可以支撑网关层的相关功能,尤其是 2.x 版本中的自定义中间件功能。
270 0
|
12月前
|
关系型数据库 MySQL Go
(7)go-micro微服务zap日志配置
(7)go-micro微服务zap日志配置
155 0
|
PHP 容器
hyperf| hyperf 源码解读 2: start
上篇我们跟着 `php bin/hyperf.php` 命令, 看到了框架的核心 `container`, 这篇我们跟着 `php bin/hyperf.php start`, 来会一会强大到爆炸的 `swoole`
605 0
|
算法 Cloud Native NoSQL
hyperf| hyperf 编程入门小结 1
最近在带组内的新人基于 [hyperf](https://github.com/hyperf-cloud/hyperf) 项目进行开发, 目标是构建一整套 PHP 微服务的体系. 千里之行始于足下, 这里先 mark 一下一些基础的编程注意事项
415 0
|
程序员 PHP 容器
hyperf| hyperf 源码解读 1: 启动
hyperf| hyperf 源码解读 1: 启动
1029 0
|
消息中间件 NoSQL 关系型数据库
hyperf| 带你一起看 hyperf 文档之 amqp
花 2 个小时认真看一遍文档, 比遇到问题就卡住然后到处问要高效得多.
1281 0

热门文章

最新文章