diff --git a/README.md b/README.md index b5bb5a79f..f6f297366 100644 --- a/README.md +++ b/README.md @@ -840,6 +840,10 @@ Want to add your own open-source project to this list? [Click here!](https://doc * Return all message drafts.: messages.getAllDrafts * Return current settings: getSettings * Returns a Telegram Passport authorization form for sharing data with a service: account.getAuthorizationForm + * Returns a closure linked to the specified prometheus counter: getPromCounter + * Returns a closure linked to the specified prometheus gauge: getPromGauge + * Returns a closure linked to the specified prometheus histogram: getPromHistogram + * Returns a closure linked to the specified prometheus summary: getPromSummary * Returns a list of available wallpapers: account.getWallPapers * Returns an HTTP URL which can be used to automatically log in into translation platform and suggest new emoji keywords ». The URL will be valid for 30 seconds after generation: messages.getEmojiURL * Returns attachment menu entry for a bot mini app that can be launched from the attachment menu »: messages.getAttachMenuBot diff --git a/docs b/docs index 3091dff6f..ab4b4782f 160000 --- a/docs +++ b/docs @@ -1 +1 @@ -Subproject commit 3091dff6fac05cf7e7b4a8d782d4428d15b54ed4 +Subproject commit ab4b4782f8582ca413bcd7c94eb4900c828fd970 diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 60587210e..ea51fa8fa 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -2292,6 +2292,25 @@ + + + + + + + + + + + + + + + + + + + @@ -2443,22 +2462,9 @@ - - - - - - - - - - + - - - - @@ -2466,7 +2472,6 @@ - diff --git a/schemas b/schemas index 6fc533efe..ce6c26483 160000 --- a/schemas +++ b/schemas @@ -1 +1 @@ -Subproject commit 6fc533efe6e1ce440f6da4b0d7037460f73ce96b +Subproject commit ce6c264833ab1496f2cef6489aa646f8fc191f76 diff --git a/src/InternalDoc.php b/src/InternalDoc.php index 1b455d002..4614462b9 100644 --- a/src/InternalDoc.php +++ b/src/InternalDoc.php @@ -994,46 +994,6 @@ abstract class InternalDoc { return $this->wrapper->getAPI()->getPlugin($class); } - /** - * Returns a closure linked to the specified prometheus counter. - * - * @return Closure(): void Call to increment the counter - */ - final public function getPromCounter(string $namespace, string $name, string $help, array $labels = [ - ]): \Closure - { - return $this->wrapper->getAPI()->getPromCounter($namespace, $name, $help, $labels); - } - /** - * Returns a closure linked to the specified prometheus gauge. - * - * @return Closure(float): void - */ - final public function getPromGauge(string $namespace, string $name, string $help, array $labels = [ - ]): \Closure - { - return $this->wrapper->getAPI()->getPromGauge($namespace, $name, $help, $labels); - } - /** - * Returns a closure linked to the specified prometheus histogram. - * - * @return Closure(float): void - */ - final public function getPromHistogram(string $namespace, string $name, string $help, $labels = [ - ], ?array $buckets = null): \Closure - { - return $this->wrapper->getAPI()->getPromHistogram($namespace, $name, $help, $labels, $buckets); - } - /** - * Returns a closure linked to the specified prometheus summary. - * - * @return Closure(float): void - */ - final public function getPromSummary(string $namespace, string $name, string $help, $labels = [ - ], int $maxAgeSeconds = 600, ?array $quantiles = null): \Closure - { - return $this->wrapper->getAPI()->getPromSummary($namespace, $name, $help, $labels, $maxAgeSeconds, $quantiles); - } /** * Gets info of the propic of a user. */ diff --git a/src/MTProto.php b/src/MTProto.php index 428e36ab4..12ae0327e 100644 --- a/src/MTProto.php +++ b/src/MTProto.php @@ -515,8 +515,10 @@ final class MTProto implements TLCallback, LoggerGetter, SettingsGetter * Returns a closure linked to the specified prometheus gauge. * * @internal - * - * @return Closure(int): void + * + * @param array $labels + * + * @return Closure(int|float): void */ public function getPromGauge(string $namespace, string $name, string $help, array $labels = []): Closure { @@ -532,7 +534,9 @@ final class MTProto implements TLCallback, LoggerGetter, SettingsGetter * Returns a closure linked to the specified prometheus counter. * * @internal - * + * + * @param array $labels + * * @return Closure(): void Call to increment the counter */ public function getPromCounter(string $namespace, string $name, string $help, array $labels = []): Closure @@ -549,10 +553,13 @@ final class MTProto implements TLCallback, LoggerGetter, SettingsGetter * Returns a closure linked to the specified prometheus summary. * * @internal - * + * + * @param array $labels + * @param ?list $quantiles + * * @return Closure(float): void */ - public function getPromSummary(string $namespace, string $name, string $help, $labels = [], int $maxAgeSeconds = 600, ?array $quantiles = null): Closure + public function getPromSummary(string $namespace, string $name, string $help, array $labels = [], int $maxAgeSeconds = 600, ?array $quantiles = null): Closure { return Magic::getSummary( $namespace, @@ -568,10 +575,13 @@ final class MTProto implements TLCallback, LoggerGetter, SettingsGetter * Returns a closure linked to the specified prometheus histogram. * * @internal - * + * + * @param array $labels + * @param ?list $buckets + * * @return Closure(float): void */ - public function getPromHistogram(string $namespace, string $name, string $help, $labels = [], ?array $buckets = null): Closure + public function getPromHistogram(string $namespace, string $name, string $help, array $labels = [], ?array $buckets = null): Closure { return Magic::getHistogram( $namespace, @@ -894,6 +904,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"); // Start IPC server if (!$this->ipcServer) { $this->ipcServer = new Server($this); diff --git a/src/MTProtoTools/UpdateHandler.php b/src/MTProtoTools/UpdateHandler.php index 502bba96f..656b1ea2a 100644 --- a/src/MTProtoTools/UpdateHandler.php +++ b/src/MTProtoTools/UpdateHandler.php @@ -28,6 +28,7 @@ use Amp\Http\Client\Request; use Amp\Http\Client\Response; use Amp\TimeoutException; use AssertionError; +use Closure; use danog\AsyncOrm\Annotations\OrmMappedArray; use danog\AsyncOrm\DbArray; use danog\AsyncOrm\KeyType; @@ -1260,8 +1261,11 @@ trait UpdateHandler $this->handleUpdate($update); } + /** @var Closure(): void */ + private Closure $updateCtr; private function handleUpdate(array $update): void { + ($this->updateCtr)(['type' => $update['_']]); /** @var UpdateHandlerType::EVENT_HANDLER|UpdateHandlerType::WEBHOOK|UpdateHandlerType::GET_UPDATES $this->updateHandlerType */ match ($this->updateHandlerType) { UpdateHandlerType::EVENT_HANDLER => $this->eventUpdateHandler($update), diff --git a/src/Magic.php b/src/Magic.php index ce80c13be..57c5df039 100644 --- a/src/Magic.php +++ b/src/Magic.php @@ -208,6 +208,7 @@ final class Magic */ public static bool $hasBasedirLimitation = false; private static CollectorRegistry $prometheus; + /** @var array */ private static array $promLabels; /** * Encoded emojis. @@ -351,22 +352,22 @@ final class Magic $alloc = self::getGauge("", "php_memstats_alloc_bytes", "RAM allocated by the PHP memory pool", []); $inuse = self::getGauge("", "php_memstats_inuse_bytes", "RAM actually used by PHP", []); EventLoop::unreference(EventLoop::repeat(1.0, static function () use ($alloc, $inuse): void { - $alloc((float) memory_get_usage(true)); - $inuse((float) memory_get_usage(false)); + $alloc(memory_get_usage(true)); + $inuse(memory_get_usage(false)); })); GarbageCollector::start(); self::$inited = true; } /** * @param array $labels - * @return Closure(int): void + * @return Closure(int|float): void */ public static function getGauge(string $namespace, string $name, string $help, array $labels): Closure { $labels += self::$promLabels; $gauge = self::$prometheus->getOrRegisterGauge($namespace, $name, $help, array_keys($labels)); $labels = array_values($labels); - return static function (int $by) use ($labels, $gauge): void { + return static function (int|float $by) use ($labels, $gauge): void { $gauge->incBy($by); }; } @@ -385,6 +386,7 @@ final class Magic } /** * @param array $labels + * @param ?list $buckets * @return Closure(float): void */ public static function getHistogram(string $namespace, string $name, string $help, array $labels, ?array $buckets = null): Closure @@ -392,12 +394,13 @@ final class Magic $labels += self::$promLabels; $gauge = self::$prometheus->getOrRegisterHistogram($namespace, $name, $help, array_keys($labels), $buckets); $labels = array_values($labels); - return static function ($value) use ($labels, $gauge): void { + return static function (float $value) use ($labels, $gauge): void { $gauge->observe($value, $labels); }; } /** * @param array $labels + * @param ?list $quantiles * @return Closure(float): void */ public static function getSummary(string $namespace, string $name, string $help, array $labels, int $maxAgeSeconds = 600, ?array $quantiles = null): Closure @@ -405,7 +408,7 @@ final class Magic $labels += self::$promLabels; $gauge = self::$prometheus->getOrRegisterSummary($namespace, $name, $help, array_keys($labels), $maxAgeSeconds, $quantiles); $labels = array_values($labels); - return static function ($value) use ($labels, $gauge): void { + return static function (float $value) use ($labels, $gauge): void { $gauge->observe($value, $labels); }; }