spiral/roadrunner-metrics

RoadRunner: Prometheus 指标 RPC

v3.2.0 2024-04-11 18:10 UTC

This package is auto-updated.

Last update: 2024-09-15 16:40:40 UTC


README

RoadRunner 指标插件

PHP Version Require Latest Stable Version phpunit psalm Total Downloads

此存储库包含使用 RoadRunner 指标插件的 PHP 桥接代码库。

安装

要安装 RoadRunner 扩展

composer require spiral/roadrunner-metrics

您可以使用方便的安装程序下载最新可用的兼容版本的 RoadRunner 组装

composer require spiral/roadrunner-cli --dev
vendor/bin/rr get

配置

在您的 .rr.yaml 文件中启用指标服务

rpc:
    listen: tcp://127.0.0.1:6001

server:
    command: "php worker.php"

http:
    address: "0.0.0.0:8080"

metrics:
    address: "0.0.0.0:2112"

用法

要从您的应用程序工作器发布指标

<?php

declare(strict_types=1);

use Nyholm\Psr7\Factory;use Spiral\Goridge;use Spiral\RoadRunner;

include "vendor/autoload.php";

$worker = new RoadRunner\Http\PSR7Worker(
    RoadRunner\Worker::create(),
    new Factory\Psr17Factory(),
    new Factory\Psr17Factory(),
    new Factory\Psr17Factory()
);

# Create metrics client
$metrics = new RoadRunner\Metrics\Metrics(
    Goridge\RPC\RPC::create(RoadRunner\Environment::fromGlobals()->getRPCAddress())
);

# Declare counter
$metrics->declare(
    'http_requests',
    RoadRunner\Metrics\Collector::counter()
        ->withHelp('Collected HTTP requests.')
        ->withLabels('status', 'method'),
);

while ($req = $worker->waitRequest()) {
    try {
        $response = new \Nyholm\Psr7\Response();
        $response->getBody()->write("hello world");

        # Publish metrics for each request with labels (status, method)
        $metrics->add('http_requests', 1, [
            $response->getStatusCode(),
            $req->getMethod(),
        ]);

        $worker->respond($rsp);
    } catch (\Throwable $e) {
        $worker->getWorker()->error((string)$e);

        $metrics->add('http_requests', 1, [503,$req->getMethod(),]);
    }
}
try Spiral Framework

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 LICENSE。由 Spiral Scout 维护。