Add operational metrics for webhook and Matrix health

This commit is contained in:
Abel Luck 2026-03-06 11:57:48 +01:00
parent 2fde5ffc87
commit 0056f819b6
3 changed files with 195 additions and 26 deletions

View file

@ -10,6 +10,12 @@ from nio import AsyncClient, AsyncClientConfig, LoginResponse
from pydantic import BaseModel
from pydantic_settings import BaseSettings
from ops_bot.metrics import (
MATRIX_AUTH_TOTAL,
MATRIX_SYNC_ERRORS_TOTAL,
classify_sync_error,
)
class ClientCredentials(BaseModel):
homeserver: str
@ -95,7 +101,11 @@ class MatrixClient:
await client.join(room)
await client.joined_rooms()
await client.sync_forever(timeout=300000, full_state=True)
try:
await client.sync_forever(timeout=300000, full_state=True)
except Exception as exc:
MATRIX_SYNC_ERRORS_TOTAL.labels(reason=classify_sync_error(exc)).inc()
raise
def save_credentials(self, resp: LoginResponse, homeserver: str) -> None:
credentials = ClientCredentials(
@ -120,8 +130,10 @@ class MatrixClient:
)
if isinstance(response, LoginResponse):
MATRIX_AUTH_TOTAL.labels(mode="fresh_login", result="success").inc()
self.save_credentials(response, self.settings.homeserver)
else:
MATRIX_AUTH_TOTAL.labels(mode="fresh_login", result="failure").inc()
logging.error(
f'Login for "{self.settings.user_id}" via homeserver="{self.settings.homeserver}"'
)
@ -129,22 +141,27 @@ class MatrixClient:
sys.exit(1)
async def login_with_credentials(self) -> None:
credentials = self.credential_store.read()
try:
credentials = self.credential_store.read()
self.client = AsyncClient(
homeserver=credentials.homeserver,
user=credentials.user_id,
device_id=credentials.device_id,
store_path=str(self.store_path),
config=self.client_config,
ssl=True,
)
self.client = AsyncClient(
homeserver=credentials.homeserver,
user=credentials.user_id,
device_id=credentials.device_id,
store_path=str(self.store_path),
config=self.client_config,
ssl=True,
)
self.client.restore_login(
user_id=credentials.user_id,
device_id=credentials.device_id,
access_token=credentials.access_token,
)
self.client.restore_login(
user_id=credentials.user_id,
device_id=credentials.device_id,
access_token=credentials.access_token,
)
except Exception:
MATRIX_AUTH_TOTAL.labels(mode="credential_restore", result="failure").inc()
raise
MATRIX_AUTH_TOTAL.labels(mode="credential_restore", result="success").inc()
async def login(self) -> None:
if self.credential_store.exists():
@ -171,12 +188,14 @@ class MatrixClient:
message_formatted, extensions=["extra"]
)
await self.client.room_send(
response = await self.client.room_send(
room_id=room,
message_type="m.room.message",
content=content,
ignore_unverified_devices=True,
)
if response.__class__.__name__.endswith("Error"):
raise RuntimeError(f"Matrix room_send failed: {response}")
async def shutdown(self) -> None:
if self.client is not None: