The Metrics Plugin for PHPStreamServer extends the core functionality by providing integration with Prometheus, providing an endpoint for exposing application metrics. It provides default metrics to monitor and allows users to define custom metrics as needed.
- Provides counter, gauge, histogram and summary metrics.
- User defined metrics.
$ composer require phpstreamserver/core phpstreamserver/metrics
Here is an example of a simple server configuration with metrics.
After startup, metrics are available at http://127.0.0.1:8081/metrics
// 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());
$ php server.php start