Metrics plugin for PHPStreamServer

v0.3.0 2024-12-06 17:26 UTC

This package is auto-updated.

Last update: 2024-12-06 17:57:02 UTC


README

Metrics Plugin for PHPStreamServer

PHP >=8.2 Version Downloads

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.

Features:

  • Provides counter, gauge, histogram and summary metrics.
  • User defined metrics.

Install

$ composer require phpstreamserver/core phpstreamserver/metrics

Configure

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\Plugin\Supervisor\WorkerProcess;
use PHPStreamServer\Core\Server;
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());

Run

$ php server.php start