mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
Merge pull request #115 from utopia-php/fix/swoole-server-stats-registration
fix(http): register server-wide Swoole gauges only on worker 0
This commit is contained in:
1 parent
7a9419858c
commit
6a1c7fe930
2 files changed
+66
-8
No files matched your search
@@ -174,7 +174,8 @@ class Server extends Adapter
|
||||
$settings = $server->setting ?? [];
|
||||
|
||||
// Register an observable gauge whose value is read on each collect().
|
||||
// A null reading is skipped so absent keys don't emit a series.
|
||||
// A null reading skips the observation, so only register a gauge this
|
||||
// process can actually fill — see the worker-id note below.
|
||||
$observe = function (string $name, callable $value) use ($telemetry): void {
|
||||
$telemetry->createObservableGauge($name)->observe(function (callable $observer) use ($value): void {
|
||||
$reading = $value();
|
||||
@@ -214,14 +215,20 @@ class Server extends Adapter
|
||||
});
|
||||
|
||||
// Server-wide stats are master-tracked, so only worker 0 emits them to
|
||||
// avoid every worker reporting the same numbers. The worker is checked
|
||||
// at collect time, so registration needs no worker id. reactor threads
|
||||
// run in the master (SWOOLE_PROCESS mode) too.
|
||||
$onWorker0 = fn(callable $read) => fn() => $server->getWorkerId() === 0 ? $read() : null;
|
||||
foreach (self::SERVER_STATS as $key => $name) {
|
||||
$observe($name, $onWorker0(fn() => $server->stats()[$key] ?? null));
|
||||
// avoid every worker reporting the same numbers. The check belongs here,
|
||||
// at registration, NOT inside the callback: an instrument whose callback
|
||||
// observes nothing is still exported, as a metric with zero data points,
|
||||
// and Prometheus 3.13 and later rejects the whole OTLP request over one
|
||||
// of those. This runs on worker start, so the worker id is already
|
||||
// assigned. reactor threads run in the master (SWOOLE_PROCESS mode) too.
|
||||
if ($server->getWorkerId() !== 0) {
|
||||
return;
|
||||
}
|
||||
$observe(self::METRIC_REACTOR_THREADS, $onWorker0(fn() => $settings['reactor_num'] ?? swoole_cpu_num()));
|
||||
|
||||
foreach (self::SERVER_STATS as $key => $name) {
|
||||
$observe($name, fn() => $server->stats()[$key] ?? null);
|
||||
}
|
||||
$observe(self::METRIC_REACTOR_THREADS, fn() => $settings['reactor_num'] ?? swoole_cpu_num());
|
||||
}
|
||||
|
||||
public function onStart(callable $callback): void
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Utopia\Tests;
|
||||
|
||||
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Utopia\Http\Adapter\Swoole\Server;
|
||||
use Utopia\Telemetry\Adapter\Test as TestTelemetry;
|
||||
|
||||
/**
|
||||
* The Swoole adapter must only register gauges the calling process can fill.
|
||||
* A registered gauge whose callback observes nothing is still exported, as a
|
||||
* metric with zero data points, and Prometheus 3.13 and later rejects the
|
||||
* entire OTLP request over one of those.
|
||||
*/
|
||||
#[RequiresPhpExtension('swoole')]
|
||||
final class SwooleServerTelemetryTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* Server-wide stats are master-tracked and reported by worker 0 alone. This
|
||||
* test process is not a worker (`getWorkerId()` is -1), so none of them may
|
||||
* be registered here.
|
||||
*/
|
||||
public function testServerWideGaugesAreNotRegisteredOutsideWorkerZero(): void
|
||||
{
|
||||
$telemetry = new TestTelemetry();
|
||||
$server = new Server('127.0.0.1', '0');
|
||||
|
||||
$server->setTelemetry($telemetry);
|
||||
|
||||
$registered = array_keys($telemetry->observableGauges);
|
||||
|
||||
foreach (['swoole.connection.count', 'swoole.request.count', 'swoole.worker.count', 'swoole.reactor.threads'] as $serverWide) {
|
||||
$this->assertNotContains($serverWide, $registered);
|
||||
}
|
||||
|
||||
// Per-worker and runtime gauges are still registered: every process can
|
||||
// report its own coroutine, timer and memory state.
|
||||
$this->assertContains('swoole.coroutine.count', $registered);
|
||||
$this->assertContains('swoole.memory.usage_bytes', $registered);
|
||||
|
||||
// Re-registering the same adapter is a no-op, so the worker-id gate is
|
||||
// not re-evaluated into a different answer. Only one Swoole\Http\Server
|
||||
// may exist per process, so this shares the server above.
|
||||
$server->setTelemetry($telemetry);
|
||||
|
||||
$this->assertSame($registered, array_keys($telemetry->observableGauges));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user