PHP code example of spiral / roadrunner-metrics

1. Go to this page and download the library: Download spiral/roadrunner-metrics library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

spiral / roadrunner-metrics example snippets




declare(strict_types=1);

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

ctory(),
    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(),]);
    }
}
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"