From 8366a1973925875935687e94acf89ad6b653102c Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Tue, 19 May 2026 15:28:44 +0100 Subject: [PATCH] fix: track multiplexing pending depth as up/down counter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The gauge was sampled only after enqueue, so its minimum reading was always 1 and it never decreased on dequeue — drained queues kept reporting their last enqueue-time depth. Switch to an UpDownCounter so +1 on enqueue and -1 on each dequeue (reader path and teardown) yield the true real-time depth without periodic sampling. Co-Authored-By: Claude Opus 4.7 --- src/Cache/Adapter/Redis/Multiplexing.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Cache/Adapter/Redis/Multiplexing.php b/src/Cache/Adapter/Redis/Multiplexing.php index 388580704e..2fe5f3ce98 100644 --- a/src/Cache/Adapter/Redis/Multiplexing.php +++ b/src/Cache/Adapter/Redis/Multiplexing.php @@ -11,7 +11,7 @@ use Utopia\Cache\Adapter; use Utopia\Cache\Feature\Telemetry as TelemetryFeature; use Utopia\Telemetry\Adapter as Telemetry; use Utopia\Telemetry\Adapter\None as NoTelemetry; -use Utopia\Telemetry\Gauge; +use Utopia\Telemetry\UpDownCounter; /** * Redis\Multiplexing adapter. @@ -33,7 +33,7 @@ class Multiplexing implements Adapter, TelemetryFeature */ private Lock $sendLock; - private ?Gauge $pendingDepth = null; + private ?UpDownCounter $pendingDepth = null; /** * @param string $host @@ -70,14 +70,14 @@ class Multiplexing implements Adapter, TelemetryFeature } /** - * Wire a telemetry adapter for connection-level metrics. The current - * pending-queue depth is recorded after each enqueue — a steady-state - * non-zero value means callers are queueing faster than Redis is - * replying. + * Wire a telemetry adapter for connection-level metrics. The pending-queue + * depth is tracked as an up/down counter — incremented on enqueue and + * decremented on dequeue — so a steady-state non-zero value means callers + * are queueing faster than Redis is replying. */ public function setTelemetry(Telemetry $telemetry): void { - $this->pendingDepth = $telemetry->createGauge( + $this->pendingDepth = $telemetry->createUpDownCounter( 'cache.redis_multiplexing.pending.depth', description: 'Pending response channels awaiting RESP frames on the multiplexed connection.', ); @@ -228,7 +228,7 @@ class Multiplexing implements Adapter, TelemetryFeature $error = null; $context->pending->enqueue($response); - $this->pendingDepth?->record($context->pending->count()); + $this->pendingDepth?->add(1); try { $context->client->send(Client::encode($args)); } catch (ConnectionException $sendError) { @@ -340,6 +340,7 @@ class Multiplexing implements Adapter, TelemetryFeature while (! $context->pending->isEmpty()) { $ch = $context->pending->dequeue(); if ($ch instanceof Channel) { + $this->pendingDepth?->add(-1); $ch->push(new ConnectionError($error)); } } @@ -411,6 +412,7 @@ class Multiplexing implements Adapter, TelemetryFeature // A complete frame implies a prior pending enqueue. $waiting = $context->pending->isEmpty() ? null : $context->pending->dequeue(); if ($waiting instanceof Channel) { + $this->pendingDepth?->add(-1); $waiting->push($value); } else { // Should never happen given the send-lock invariant. Log