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\Psr17Factory;
use Nyholm\Psr7\Response;
use Spiral\Goridge\RPC\RPC;
use Spiral\RoadRunner;
use Spiral\RoadRunner\Metrics\Collector;
use Spiral\RoadRunner\Metrics\Metrics;

obals()->getRPCAddress()),
);

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

while ($request = $worker->waitRequest()) {
    try {
        $response = new Response();
        $response->getBody()->write('hello world');

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

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

        $metrics->add('http_requests', 1, ['503', $request->getMethod()]);
    }
}

use Spiral\RoadRunner\Metrics\MetricsFactory;
use Spiral\RoadRunner\Metrics\MetricsOptions;

$metrics = MetricsFactory::createMetrics(
    $rpc,
    new MetricsOptions(
        retryAttempts: 3,
        retrySleepMicroseconds: 50,
        suppressExceptions: true,
        ignoreResponsesWherePossible: true,
    ),
    $logger, // any PSR-3 logger
);
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"