Add event/update counter to metrics

This commit is contained in:
Tulir Asokan
2019-06-01 21:10:01 +03:00
parent a279835cf8
commit 145eb8f611
2 changed files with 15 additions and 5 deletions
+8 -3
View File
@@ -52,13 +52,16 @@ UpdateMessage = Union[UpdateShortChatMessage, UpdateShortMessage, UpdateNewChann
UpdateMessageContent = Union[UpdateShortMessage, UpdateShortChatMessage, Message, MessageService] UpdateMessageContent = Union[UpdateShortMessage, UpdateShortChatMessage, Message, MessageService]
try: try:
from prometheus_client import Histogram from prometheus_client import Histogram, Counter
UPDATE_COUNT = Counter("telegram_update_count", "Number of Telegram updates processed",
["update_type"])
UPDATE_TIME = Histogram("telegram_update", "Time spent processing Telegram updates", UPDATE_TIME = Histogram("telegram_update", "Time spent processing Telegram updates",
["update_type"]) ["update_type"])
except ImportError: except ImportError:
Histogram = None Histogram = None
UPDATE_TIME = None UPDATE_TIME = None
UPDATE_COUNT = None
class AbstractUser(ABC): class AbstractUser(ABC):
session_container = None # type: AlchemySessionContainer session_container = None # type: AlchemySessionContainer
@@ -166,8 +169,10 @@ class AbstractUser(ABC):
await self._update(update) await self._update(update)
except Exception: except Exception:
self.log.exception("Failed to handle Telegram update") self.log.exception("Failed to handle Telegram update")
if UPDATE_TIME: if UPDATE_TIME and UPDATE_COUNT:
UPDATE_TIME.labels(update_type=type(update).__name__).observe(time.time() - start_time) update_type = type(update).__name__
UPDATE_TIME.labels(update_type=update_type).observe(time.time() - start_time)
UPDATE_COUNT.labels(update_type=update_type).inc()
async def get_dialogs(self, limit: int = None) -> List[Union[Chat, Channel]]: async def get_dialogs(self, limit: int = None) -> List[Union[Chat, Channel]]:
if self.is_bot: if self.is_bot:
+7 -2
View File
@@ -29,13 +29,17 @@ if TYPE_CHECKING:
from .context import Context from .context import Context
try: try:
from prometheus_client import Histogram from prometheus_client import Histogram, Counter
EVENT_COUNT = Counter("matrix_event_count", "Number of Matrix events processed",
["event_type"])
EVENT_TIME = Histogram("matrix_event", "Time spent processing Matrix events", EVENT_TIME = Histogram("matrix_event", "Time spent processing Matrix events",
["event_type"]) ["event_type"])
except ImportError: except ImportError:
Histogram = None Histogram = None
EVENT_TIME = None EVENT_TIME = None
EVENT_COUNT = None
class MatrixHandler: class MatrixHandler:
log = logging.getLogger("mau.mx") # type: logging.Logger log = logging.getLogger("mau.mx") # type: logging.Logger
@@ -442,5 +446,6 @@ class MatrixHandler:
await self.handle_typing(room_id, content.get("user_ids", [])) await self.handle_typing(room_id, content.get("user_ids", []))
else: else:
return return
if EVENT_TIME: if EVENT_TIME and EVENT_COUNT:
EVENT_TIME.labels(event_type=evt_type).observe(time.time() - start_time) EVENT_TIME.labels(event_type=evt_type).observe(time.time() - start_time)
EVENT_COUNT.labels(event_type=evt_type).inc()