From be472eaadd8aabb2727095a074af4ef2cf35d72b Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Thu, 9 May 2024 20:40:01 +0200 Subject: [PATCH] Improve counters --- src/Loop/Connection/CheckLoop.php | 2 +- src/Loop/Connection/WriteLoop.php | 4 ++-- src/MTProto.php | 2 +- src/MTProtoSession/ResponseHandler.php | 18 ++++++++++++++---- src/MTProtoSession/Session.php | 9 +++++++-- 5 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/Loop/Connection/CheckLoop.php b/src/Loop/Connection/CheckLoop.php index 617ecdd86..2eaff1431 100644 --- a/src/Loop/Connection/CheckLoop.php +++ b/src/Loop/Connection/CheckLoop.php @@ -60,7 +60,7 @@ final class CheckLoop extends Loop } if ($this->shared->hasTempAuthKey()) { $full_message_ids = $this->connection->getPendingCalls(); - foreach (array_chunk($full_message_ids, 8192) as $message_ids) { + foreach (array_chunk($full_message_ids, WriteLoop::MAX_IDS) as $message_ids) { $deferred = new DeferredFuture(); $list = ''; // Don't edit this here pls diff --git a/src/Loop/Connection/WriteLoop.php b/src/Loop/Connection/WriteLoop.php index 66dd0b47f..a2a500e79 100644 --- a/src/Loop/Connection/WriteLoop.php +++ b/src/Loop/Connection/WriteLoop.php @@ -40,7 +40,7 @@ use function strlen; */ final class WriteLoop extends Loop { - public const MAX_COUNT = 1020; + private const MAX_COUNT = 1020; private const MAX_SIZE = 1 << 15; public const MAX_IDS = 8192; @@ -197,7 +197,7 @@ final class WriteLoop extends Loop $body_length = \strlen($message->getSerializedBody()); $actual_length = $body_length + 32; - if ($total_length && $total_length + $actual_length > 32760 || $count >= 1020) { + if ($total_length && $total_length + $actual_length > 32760 || $count >= self::MAX_COUNT) { $this->API->logger('Length overflow, postponing part of payload', Logger::ULTRA_VERBOSE); break; } diff --git a/src/MTProto.php b/src/MTProto.php index d6918178c..e27298b7f 100644 --- a/src/MTProto.php +++ b/src/MTProto.php @@ -923,7 +923,7 @@ final class MTProto implements TLCallback, LoggerGetter, SettingsGetter */ private function cleanupProperties(): void { - $this->updateCtr = $this->getPromCounter("", "update_count", "Number of received updates since the session was created"); + $this->updateCtr = $this->getPromCounter("MadelineProto", "update_count", "Number of received updates since the session was created"); // Start IPC server if (!$this->ipcServer) { $this->ipcServer = new Server($this); diff --git a/src/MTProtoSession/ResponseHandler.php b/src/MTProtoSession/ResponseHandler.php index 7b7e32b5e..e6dd984e0 100644 --- a/src/MTProtoSession/ResponseHandler.php +++ b/src/MTProtoSession/ResponseHandler.php @@ -273,6 +273,14 @@ trait ResponseHandler } $this->gotResponseForOutgoingMessage($request); + if ($this->requestResponse !== null) { + $this->requestResponse->inc([ + 'method' => $request->constructor, + 'error_message' => 'OK', + 'error_code' => '200' + ]); + } + EventLoop::queue($request->reply(...), $response); } /** @@ -281,10 +289,12 @@ trait ResponseHandler */ private function handleRpcError(MTProtoOutgoingMessage $request, array $response): ?callable { - if ($response['error_code'] === 420) { - $this->rpcErrors?->inc(['message' => preg_replace('/\d+/', '', $response['error_message']), 'code' => (string) $response['error_code']]); - } else { - $this->rpcErrors?->inc(['message' => $response['error_message'], 'code' => (string) $response['error_code']]); + if ($this->requestResponse !== null) { + $this->requestResponse->inc([ + 'method' => $request->constructor, + 'error_message' => preg_replace('/\d+/', 'X', $response['error_message']), + 'error_code' => (string) $response['error_code'] + ]); } if ($request->isMethod && $request->constructor !== 'auth.bindTempAuthKey' diff --git a/src/MTProtoSession/Session.php b/src/MTProtoSession/Session.php index c33b64cdf..219d32c0c 100644 --- a/src/MTProtoSession/Session.php +++ b/src/MTProtoSession/Session.php @@ -23,6 +23,7 @@ namespace danog\MadelineProto\MTProtoSession; use Amp\Sync\LocalKeyedMutex; use danog\BetterPrometheus\BetterCounter; use danog\BetterPrometheus\BetterGauge; +use danog\BetterPrometheus\BetterHistogram; use danog\MadelineProto\Logger; use danog\MadelineProto\MTProto\MTProtoIncomingMessage; use danog\MadelineProto\MTProto\MTProtoOutgoingMessage; @@ -47,7 +48,10 @@ trait Session public ?BetterCounter $outgoingCtr = null; public ?BetterCounter $incomingBytesCtr = null; public ?BetterCounter $outgoingBytesCtr = null; - public ?BetterCounter $rpcErrors = null; + + public ?BetterHistogram $requestLatencies = null; + + public ?BetterCounter $requestResponse = null; /** * Incoming message array. * @@ -185,7 +189,8 @@ trait Session $this->outgoingCtr = $this->API->getPromCounter("MadelineProto", "outgoing_mtproto_messages", "Number of sent MTProto messages", $labels); $this->incomingBytesCtr = $this->API->getPromCounter("MadelineProto", "incoming_bytes", "Number of received bytes", $labels); $this->outgoingBytesCtr = $this->API->getPromCounter("MadelineProto", "outgoing_bytes", "Number of sent bytes", $labels); - $this->rpcErrors = $this->API->getPromCounter("MadelineProto", "rpc_errors", "Number of received RPC errors by type", $labels); + $this->requestResponse = $this->API->getPromCounter("MadelineProto", "request_responses", "Received RPC error or success status of requests by method.", $labels); + $this->requestLatencies = $this->API->getPromHistogram("MadelineProto", "request_latencies", "Request latency by method", $labels); if ($this->session_id === null) { $this->resetSession("creating initial session"); }