From 6a1c7fe9305fd5adad9ba97bae3f6a6faca7a711 Mon Sep 17 00:00:00 2001 From: "Luke B. Silver" <22452787+loks0n@users.noreply.github.com> Date: Wed, 5 Aug 2026 19:37:51 +0100 Subject: [PATCH] Merge pull request #115 from utopia-php/fix/swoole-server-stats-registration fix(http): register server-wide Swoole gauges only on worker 0 --- src/Http/Adapter/Swoole/Server.php | 23 ++++++++----- tests/SwooleServerTelemetryTest.php | 51 +++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 8 deletions(-) create mode 100644 tests/SwooleServerTelemetryTest.php diff --git a/src/Http/Adapter/Swoole/Server.php b/src/Http/Adapter/Swoole/Server.php index e9c034fe75..57ba985c8a 100755 --- a/src/Http/Adapter/Swoole/Server.php +++ b/src/Http/Adapter/Swoole/Server.php @@ -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 diff --git a/tests/SwooleServerTelemetryTest.php b/tests/SwooleServerTelemetryTest.php new file mode 100644 index 0000000000..398c0abe48 --- /dev/null +++ b/tests/SwooleServerTelemetryTest.php @@ -0,0 +1,51 @@ +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)); + } +}