Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add metrics about Matrix API calls #68

Merged
merged 8 commits into from
Nov 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion mautrix/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@

from mautrix.errors import make_request_error, MatrixConnectionError, MatrixRequestError
from mautrix.util.logging import TraceLogger
from mautrix.util.opt_prometheus import Counter
from mautrix import __version__ as mautrix_version, __optional_imports__

if __optional_imports__:
# Safe to import, but it's not actually needed, so don't force-import the whole types module.
from mautrix.types import JSON

API_CALLS = Counter("bridge_matrix_api_calls",
"The number of Matrix client API calls made", ("method",))
API_CALLS_FAILED = Counter("bridge_matrix_api_calls_failed",
"The number of Matrix client API calls which failed", ("method",))

class APIPath(Enum):
"""
Expand Down Expand Up @@ -251,7 +256,8 @@ async def request(self, method: Method, path: Union[PathBuilder, str],
content: Optional[Union[dict, list, bytes, str]] = None,
headers: Optional[Dict[str, str]] = None,
query_params: Optional[Mapping[str, str]] = None,
retry_count: Optional[int] = None) -> JSON:
retry_count: Optional[int] = None,
metrics_method: Optional[str] = "") -> JSON:
"""
Make a raw Matrix API request.

Expand Down Expand Up @@ -295,8 +301,12 @@ async def request(self, method: Method, path: Union[PathBuilder, str],
backoff = 4
while True:
self._log_request(method, path, content, orig_content, query_params, req_id)
API_CALLS.labels(method=metrics_method).inc()
try:
return await self._send(method, full_url, content, query_params, headers or {})
except Exception:
API_CALLS_FAILED.labels(method=metrics_method).inc()
raise
except MatrixRequestError as e:
if retry_count > 0 and e.http_status in (502, 503, 504):
self.log.warning(f"Request #{req_id} failed with HTTP {e.http_status}, "
Expand Down
21 changes: 11 additions & 10 deletions mautrix/client/api/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def sync(self, since: SyncToken = None, timeout: int = 30000, filter_id: FilterI
request["full_state"] = "true" if full_state else "false"
if set_presence:
request["set_presence"] = str(set_presence)
return self.api.request(Method.GET, Path.sync, query_params=request, retry_count=0)
return self.api.request(Method.GET, Path.sync, query_params=request, retry_count=0, metrics_method="sync")

# endregion
# region 8.3 Getting events for a room
Expand All @@ -76,7 +76,7 @@ async def get_event(self, room_id: RoomID, event_id: EventID) -> Event:
.. _/event/{eventId} API reference:
https://matrix.org/docs/spec/client_server/r0.5.0#get-matrix-client-r0-rooms-roomid-event-eventid
"""
content = await self.api.request(Method.GET, Path.rooms[room_id].event[event_id])
content = await self.api.request(Method.GET, Path.rooms[room_id].event[event_id], metrics_method="getEvent")
try:
return Event.deserialize(content)
except SerializerError as e:
Expand All @@ -102,7 +102,8 @@ async def get_state_event(self, room_id: RoomID, event_type: EventType,
https://matrix.org/docs/spec/client_server/r0.5.0#get-matrix-client-r0-rooms-roomid-state-eventtype-statekey
"""
content = await self.api.request(Method.GET,
Path.rooms[room_id].state[event_type][state_key])
Path.rooms[room_id].state[event_type][state_key],
metrics_method="getStateEvent")
try:
return state_event_content_map[event_type].deserialize(content)
except KeyError:
Expand All @@ -123,7 +124,7 @@ async def get_state(self, room_id: RoomID) -> List[StateEvent]:
.. _/state API reference:
https://matrix.org/docs/spec/client_server/r0.5.0#get-matrix-client-r0-rooms-roomid-state
"""
content = await self.api.request(Method.GET, Path.rooms[room_id].state)
content = await self.api.request(Method.GET, Path.rooms[room_id].state, metrics_method="getState")
try:
return [StateEvent.deserialize(event) for event in content]
except SerializerError as e:
Expand Down Expand Up @@ -161,7 +162,7 @@ async def get_members(self, room_id: RoomID, at: Optional[SyncToken] = None,
if not_membership:
query["not_membership"] = not_membership.value
content = await self.api.request(Method.GET, Path.rooms[room_id].members,
query_params=query)
query_params=query, metrics_method="getMembers")
try:
return [StateEvent.deserialize(event) for event in content["chunk"]]
except KeyError:
Expand All @@ -188,7 +189,7 @@ async def get_joined_members(self, room_id: RoomID) -> Dict[UserID, Member]:
.. _/members:
https://matrix.org/docs/spec/client_server/r0.5.0#get-matrix-client-r0-rooms-roomid-members
"""
content = await self.api.request(Method.GET, Path.rooms[room_id].joined_members)
content = await self.api.request(Method.GET, Path.rooms[room_id].joined_members, metrics_method="getJoinedMembers")
try:
return {user_id: Member(membership=Membership.JOIN,
displayname=member.get("display_name", ""),
Expand Down Expand Up @@ -234,7 +235,7 @@ async def get_messages(self, room_id: RoomID, direction: PaginationDirection,
"filter_json": filter_json,
}
content = await self.api.request(Method.GET, Path.rooms[room_id].messages,
query_params=query_params)
query_params=query_params, metrics_method="getMessages")
try:
return PaginatedMessages(content["start"], content["end"],
[Event.deserialize(event) for event in content["chunk"]])
Expand Down Expand Up @@ -276,7 +277,7 @@ async def send_state_event(self, room_id: RoomID, event_type: EventType,
"""
content = content.serialize() if isinstance(content, Serializable) else content
resp = await self.api.request(Method.PUT, Path.rooms[room_id].state[event_type][state_key],
content, **kwargs)
content, **kwargs, metrics_method="sendStateEvent")
try:
return resp["event_id"]
except KeyError:
Expand Down Expand Up @@ -311,7 +312,7 @@ async def send_message_event(self, room_id: RoomID, event_type: EventType,
raise ValueError("Event type not given")
url = Path.rooms[room_id].send[event_type][txn_id or self.api.get_txn_id()]
content = content.serialize() if isinstance(content, Serializable) else content
resp = await self.api.request(Method.PUT, url, content, **kwargs)
resp = await self.api.request(Method.PUT, url, content, **kwargs, metrics_method="sendMessageEvent")
try:
return resp["event_id"]
except KeyError:
Expand Down Expand Up @@ -515,7 +516,7 @@ async def redact(self, room_id: RoomID, event_id: EventID, reason: Optional[str]
https://matrix.org/docs/spec/client_server/r0.5.0#put-matrix-client-r0-rooms-roomid-redact-eventid-txnid
"""
url = Path.rooms[room_id].redact[event_id][self.api.get_txn_id()]
resp = await self.api.request(Method.PUT, url, content={"reason": reason}, **kwargs)
resp = await self.api.request(Method.PUT, url, content={"reason": reason}, **kwargs, metrics_method="redact")
try:
return resp["event_id"]
except KeyError:
Expand Down