cached /export/prometheus/metrics

This commit is contained in:
Maxim Devaev 2023-07-24 16:25:25 +03:00
parent 78ceb345ac
commit 2b0515dc55
3 changed files with 9 additions and 1 deletions

View File

@ -44,6 +44,7 @@ depends=(
python-yaml python-yaml
python-aiohttp python-aiohttp
python-aiofiles python-aiofiles
python-async-lru
python-passlib python-passlib
python-pyotp python-pyotp
python-qrcode python-qrcode

View File

@ -24,6 +24,8 @@ import asyncio
from typing import Any from typing import Any
import async_lru
from aiohttp.web import Request from aiohttp.web import Request
from aiohttp.web import Response from aiohttp.web import Response
@ -49,6 +51,10 @@ class ExportApi:
@exposed_http("GET", "/export/prometheus/metrics") @exposed_http("GET", "/export/prometheus/metrics")
async def __prometheus_metrics_handler(self, _: Request) -> Response: async def __prometheus_metrics_handler(self, _: Request) -> Response:
return Response(text=(await self.__get_prometheus_metrics()))
@async_lru.alru_cache(maxsize=1, ttl=5)
async def __get_prometheus_metrics(self) -> str:
(atx_state, hw_state, fan_state, gpio_state) = await asyncio.gather(*[ (atx_state, hw_state, fan_state, gpio_state) = await asyncio.gather(*[
self.__atx.get_state(), self.__atx.get_state(),
self.__info_manager.get_submanager("hw").get_state(), self.__info_manager.get_submanager("hw").get_state(),
@ -68,7 +74,7 @@ class ExportApi:
self.__append_prometheus_rows(rows, hw_state["health"], "pikvm_hw") self.__append_prometheus_rows(rows, hw_state["health"], "pikvm_hw")
self.__append_prometheus_rows(rows, fan_state, "pikvm_fan") self.__append_prometheus_rows(rows, fan_state, "pikvm_fan")
return Response(text="\n".join(rows)) return "\n".join(rows)
def __append_prometheus_rows(self, rows: list[str], value: Any, path: str) -> None: def __append_prometheus_rows(self, rows: list[str], value: Any, path: str) -> None:
if isinstance(value, bool): if isinstance(value, bool):

View File

@ -41,6 +41,7 @@ RUN pacman --noconfirm --ask=4 -Syy \
python-yaml \ python-yaml \
python-aiohttp \ python-aiohttp \
python-aiofiles \ python-aiofiles \
python-async-lru \
python-periphery \ python-periphery \
python-passlib \ python-passlib \
python-pyotp \ python-pyotp \