PHP code example of phpstreamserver / metrics

1. Go to this page and download the library: Download phpstreamserver/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/ */

    

phpstreamserver / metrics example snippets


// server.php

use PHPStreamServer\Core\Server;
use PHPStreamServer\Core\Worker\WorkerProcess;
use PHPStreamServer\Plugin\Metrics\MetricsPlugin;
use PHPStreamServer\Plugin\Metrics\RegistryInterface;
use Revolt\EventLoop;

$server = new Server();

$server->addPlugin(
    new MetricsPlugin(listen: '0.0.0.0:8081'),
);

$server->addWorker(
    new WorkerProcess(
        name: 'Metrics test',
        count: 2,
        onStart: function (WorkerProcess $worker): void {
            $registry = $worker->container->getService(RegistryInterface::class);
            $counter = $registry->registerCounter(namespace: 'test', name: 'ticks', help: 'Demonsration');

            EventLoop::repeat(1, function () use ($counter) {
                $counter->inc();
            });
        },
    ),
);

exit($server->run());
bash
$ php server.php start