Skip to content

Commit

Permalink
HTTP Pool Metrics (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas-angeli-gimenes authored May 21, 2024
1 parent 18c311b commit 49b40a2
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/Listener/HttpPoolWatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

/**
* This file is part of Hyperf + OpenCodeCo
*
* @link https://opencodeco.dev
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/opencodeco/hyperf-metric/blob/main/LICENSE
*/

namespace Hyperf\Metric\Listener;

use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\ContainerInterface;
use Hyperf\Coordinator\Timer;
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Framework\Event\BeforeWorkerStart;
use Hyperf\Metric\Metric;
use Hyperf\Pool\SimplePool\PoolFactory;
use Hyperf\Server\Event\MainCoroutineServerStart;

class HttpPoolWatcher implements ListenerInterface
{
private const GUZZLE_PREFIX = 'guzzle.handler';

protected Timer $timer;

public function __construct(protected ContainerInterface $container)
{
$this->timer = new Timer();
}

public function listen(): array
{
return [
BeforeWorkerStart::class,
MainCoroutineServerStart::class,
];
}

public function process(object $event): void
{
/** @var PoolFactory $factory */
$factory = $this->container->get(PoolFactory::class);
$worker = (string) ($event->workerId ?? 0);

$config = $this->container->get(ConfigInterface::class);

$timer_interval = $config->get('metric.default_metric_interval', 5);

$this->timer->tick($timer_interval, function () use ($factory, $worker) {
foreach ($factory->getPoolNames() as $name) {
if (strpos($name, self::GUZZLE_PREFIX) !== 0) {
continue;
}

$pool = $factory->get($name, function () {});

$labels = [
'worker' => $worker,
'pool' => implode('.', array_slice(explode('.', (string) $name), 2, -2)),
];

Metric::gauge('http_connections_in_use', (float) $pool->getCurrentConnections(), $labels);
Metric::gauge('http_connections_in_waiting', (float) $pool->getConnectionsInChannel(), $labels);
Metric::gauge('http_max_connections', (float) $pool->getOption()->getMaxConnections(), $labels);
}
});
}
}

0 comments on commit 49b40a2

Please sign in to comment.