Skip to content

Commit 2a905cd

Browse files
committed
Log transaction contents
1 parent 690f470 commit 2a905cd

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

mautrix/appservice/as_handler.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,11 @@ async def _read_transaction_header(self, request: web.Request) -> tuple[str, dic
161161
async def _http_handle_transaction(self, request: web.Request) -> web.Response:
162162
transaction_id, data = await self._read_transaction_header(request)
163163

164+
txn_content_log = []
164165
try:
165166
events = data.pop("events")
167+
if events:
168+
txn_content_log.append(f"{len(events)} PDUs")
166169
except KeyError:
167170
raise web.HTTPBadRequest(
168171
content_type="application/json",
@@ -171,11 +174,12 @@ async def _http_handle_transaction(self, request: web.Request) -> web.Response:
171174
),
172175
)
173176

174-
ephemeral = (
175-
self._get_with_fallback(data, "ephemeral", "de.sorunome.msc2409")
176-
if self.ephemeral_events
177-
else None
178-
)
177+
if self.ephemeral_events:
178+
ephemeral = self._get_with_fallback(data, "ephemeral", "de.sorunome.msc2409")
179+
if ephemeral:
180+
txn_content_log.append(f"{len(ephemeral)} EDUs")
181+
else:
182+
ephemeral = None
179183
if self.encryption_events:
180184
to_device = self._get_with_fallback(data, "to_device", "de.sorunome.msc2409")
181185
device_lists = DeviceLists.deserialize(
@@ -190,11 +194,27 @@ async def _http_handle_transaction(self, request: web.Request) -> web.Response:
190194
data, "device_one_time_keys_count", "org.matrix.msc3202", default={}
191195
).items()
192196
}
197+
if to_device:
198+
txn_content_log.append(f"{len(to_device)} to-device events")
199+
if device_lists.changed:
200+
txn_content_log.append(f"{len(device_lists.changed)} device list changes")
201+
if otk_counts:
202+
txn_content_log.append(
203+
f"{sum(len(vals) for vals in otk_counts.values())} OTK counts"
204+
)
193205
else:
194206
otk_counts = {}
195207
device_lists = None
196208
to_device = None
197209

210+
if len(txn_content_log) > 2:
211+
txn_content_log = [", ".join(txn_content_log[:-1]), txn_content_log[-1]]
212+
if not txn_content_log:
213+
txn_description = "nothing?"
214+
else:
215+
txn_description = " and ".join(txn_content_log)
216+
self.log.debug(f"Handling transaction {transaction_id} with {txn_description}")
217+
198218
try:
199219
output = await self.handle_transaction(
200220
transaction_id,

mautrix/types/misc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ class DeviceLists(SerializableAttrs):
1919
changed: List[UserID] = attr.ib(factory=lambda: [])
2020
left: List[UserID] = attr.ib(factory=lambda: [])
2121

22+
def __bool__(self) -> bool:
23+
return bool(self.changed or self.left)
24+
2225

2326
@dataclass
2427
class DeviceOTKCount(SerializableAttrs):

0 commit comments

Comments
 (0)