Laravels 接入 Prometheus 详解

本文详细介绍了如何在Laravel项目中集成Hhxsv5/Laravel-S的Prometheus监控,包括APCu扩展安装、配置中间件、注册ServiceProvider、创建监控接口和配置Prometheus收集器。

本文是对https://github.com/hhxsv5/laravel-s/blob/master/README-CN.md#prometheus
做更详细的解释

1、安装APCu扩展

依赖APCu >= 5.0.0扩展

pecl install apcu

检查安装结果

php -m

在这里插入图片描述
安装成功后php.ini配置添加:

extension=apcu.so
apc.enable_cli=1

注意:提一下之前有个APC是旧版本php使用的,现已弃用不再维护,取而代之的是APCu

2、拷贝prometheus配置文件

路径:vendor/hhxsv5/laravel-s/config/prometheus.php,把prometheus.php拷贝到你的Laravel项目config目录项目。
可在laravel根目录下执行命令:

cp vendor/hhxsv5/laravel-s/config/prometheus.php config/

或者你自己找到prometheus.php复制过来

3、配置全局中间件

Hhxsv5\LaravelS\Components\Prometheus\RequestMiddleware::class。为了尽可能精确地统计请求耗时,RequestMiddleware必须作为第一个全局中间件,需要放在其他中间件的前面。
1.打开 app/Http/Kernel.php 文件。

protected $middleware = [
    \Hhxsv5\LaravelS\Components\Prometheus\RequestMiddleware::class,
    // 其他中间件...
];
//将 'prometheus' 中间件名称添加到 $middlewareGroups 数组的 'web' 或 'api' 中,具体取决于你希望将其应用于哪个路由分组。确保将其放在其他中间件之前。
protected $middlewareGroups = [
    'web' => [
        'prometheus',
        // 其他中间件...
    ],

    'api' => [
        'prometheus',
        // 其他中间件...
    ],
];
protected $routeMiddleware = [
    'prometheus' => \Hhxsv5\LaravelS\Components\Prometheus\RequestMiddleware::class,
    // 其他中间件...
];

4、注册 ServiceProvider

打开 config/app.php 文件,找到 providers 数组,然后将 Hhxsv5\LaravelS\Components\Prometheus\ServiceProvider::class 添加到数组中。

'providers' => [
    // 其他服务提供者...
    Hhxsv5\LaravelS\Components\Prometheus\ServiceProvider::class,
],

5、配置 CollectorProcess 进程

用于定时采集 Swoole Worker/Task/Timer 进程的指标,在config/laravels.php中配置:

'processes' => Hhxsv5\LaravelS\Components\Prometheus\CollectorProcess::getDefinition(),

6、创建路由,输出监控指标数据。

就是弄一个接口,请求后返回数据就行

use Hhxsv5\LaravelS\Components\Prometheus\Exporter;

Route::get('/actuator/prometheus', function () {
    $result = app(Exporter::class)->render();
    return response($result, 200, ['Content-Type' => Exporter::REDNER_MIME_TYPE]);
});

你也可以在控制器里实现:

<?php

namespace App\Http\Controllers\Api;

use Hhxsv5\LaravelS\Components\Prometheus\Exporter;

/**
 * @apiDefine debug
 * 运维调试接口
 */
class DebugController extends AbstractController
{
    public function prometheus()
    {
        $result = app(Exporter::class)->render();
        return response($result, 200, ['Content-Type' => Exporter::REDNER_MIME_TYPE]);
    }
}

7、完成Prometheus的配置,启动Prometheus。

global:
  scrape_interval: 5s
  scrape_timeout: 5s
  evaluation_interval: 30s
scrape_configs:
- job_name: laravel-s-test
  honor_timestamps: true
  metrics_path: /actuator/prometheus
  scheme: http
  follow_redirects: true
  static_configs:
  - targets:
    - 127.0.0.1:5200 # The ip and port of the monitored service
# Dynamically discovered using one of the supported service-discovery mechanisms
# https://prometheus.io/docs/prometheus/latest/configuration/configuration/#scrape_config
# - job_name: laravels-eureka
#   honor_timestamps: true
#   scrape_interval: 5s
#   metrics_path: /actuator/prometheus
#   scheme: http
#   follow_redirects: true
  # eureka_sd_configs:
  # - server: http://127.0.0.1:8080/eureka
  #   follow_redirects: true
  #   refresh_interval: 5s

本文主要是PHP开发视角。

另调接口如报错:APC must be enabled to use APCUIterator …
则是apcu安装后php.ini只添加了extension=apcu.so 而没有添加 apc.enable_cli=1 导致cli没有开启导致。

### 如何配置Kubernetes Pod以接入Prometheus监控系统 为了使 Kubernetes 中的 Pod 被 Prometheus 正确抓取并纳入监控范围,通常需要完成几个关键步骤。 #### 1. 创建必要的 RBAC 权限 确保已经为 Prometheus 设置了适当的角色绑定和服务账户权限。这一步骤通过应用包含必要角色定义的 YAML 文件实现[^2]: ```bash $ kubectl apply -f prom.rbac.yaml ``` #### 2. 修改目标服务或Pod的选择器标签 为了让 Prometheus 发现待监控的服务或 Pod,在这些资源上添加特定的标签是非常重要的。例如: ```yaml metadata: labels: app.kubernetes.io/name: "example-app" prometheus.io/scrape: "true" # 启用此pod被Prometheus抓取 prometheus.io/port: "9090" # 指定暴露指标的端口号 ``` 上述配置中的 `prometheus.io/scrape` 和 `prometheus.io/port` 是两个常用的自定义标签,用于指示 Prometheus 抓取行为以及监听的具体端口位置[^4]。 #### 3. 更新 Prometheus 的配置文件 (prometheus.yml) 在 Prometheus 的主要配置文件中指定要抓取的目标。对于带有特定标签的 Pods 或 Services, 可以利用 K8s API Server 提供的信息自动发现它们。下面是一个简单的例子说明如何设置 job_name 并匹配相应的 label selectors: ```yaml scrape_configs: - job_name: 'kubernetes-pods' kubernetes_sd_configs: - source_labels: [__meta_kubernetes_pod_label_app_kubernetes_io_name] action: keep regex: example-app ``` 这段配置告诉 Prometheus 去查找所有具有 `app.kubernetes.io/name=example-app` 标签的 Pods,并定期从中收集度量数据。 #### 4. 访问 Prometheus Web UI 查看监控状态 一旦完成了前面几步的操作,默认情况下可以访问 http://<PROMETHEUS-SERVER>:9090 地址来浏览 Prometheus Web 用户界面,这里能够看到已连接节点的状态以及其他有用信息。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值