Add config for logging arguments of RPC commands

This commit is contained in:
Andrew Ferrazzutti 2022-05-10 01:10:47 -04:00
parent 3c0d890577
commit 20bdbf9cd1
4 changed files with 24 additions and 4 deletions

View File

@ -117,6 +117,7 @@ class Config(BaseBridgeConfig):
else:
copy("rpc.connection.host")
copy("rpc.connection.port")
copy("rpc.logging_keys")
def _get_permissions(self, key: str) -> tuple[bool, bool, bool, str]:
level = self["bridge.permissions"].get(key, "")

View File

@ -252,6 +252,11 @@ rpc:
# Only for type: tcp
host: localhost
port: 29392
# Command arguments to print in logs. Optional.
# TODO Support nested arguments, like channel_props.ktid
logging_keys:
- mxid
#- channel_props
# Python logging configuration.
#

View File

@ -86,6 +86,7 @@ class RPCClient:
_is_connected: CancelableEvent
_is_disconnected: CancelableEvent
_connection_lock: asyncio.Lock
_logging_keys: list[str]
def __init__(self, config: Config, register_config_key: str) -> None:
self.config = config
@ -105,6 +106,7 @@ class RPCClient:
self._is_disconnected = CancelableEvent(self.loop)
self._is_disconnected.set()
self._connection_lock = asyncio.Lock()
self._logging_keys = config["rpc.logging_keys"]
async def connect(self) -> None:
async with self._connection_lock:
@ -147,7 +149,8 @@ class RPCClient:
self._read_task = self.loop.create_task(self._try_read_loop())
await self._raw_request("register",
peer_id=self.config["appservice.address"],
register_config=self.config[self.register_config_key])
register_config=self.config[self.register_config_key],
logging_keys=self._logging_keys)
self._is_connected.set()
self._is_disconnected.clear()
@ -302,7 +305,10 @@ class RPCClient:
req_id = self._next_req_id
future = self._response_waiters[req_id] = self.loop.create_future()
req = {"id": req_id, "command": command, **data}
self.log.debug("Request %d: %s", req_id, command)
self.log.debug("Request %d: %s", req_id,
', '.join(
[command] +
[f"{k}: {data[k]}" for k in self._logging_keys if k in data]))
assert self._writer is not None
self._writer.write(json.dumps(req).encode("utf-8"))
self._writer.write(b"\n")

View File

@ -424,7 +424,8 @@ export default class PeerClient {
this.maxCommandID = 0
this.peerID = null
this.deviceName = "KakaoTalk Bridge"
/** @type {[string]} */
this.loggingKeys = []
/** @type {Map<string, UserClient>} */
this.userClients = new Map()
}
@ -1166,10 +1167,12 @@ export default class PeerClient {
* @param {string} req.peer_id
* @param {Object} req.register_config
* @param {string} req.register_config.device_name
* @param {?[string]} req.logging_keys
*/
handleRegister = async (req) => {
this.peerID = req.peer_id
this.deviceName = req.register_config.device_name || this.deviceName
this.loggingKeys = req.logging_keys || this.loggingKeys
this.log(`Registered socket ${this.connID} -> ${this.peerID}`)
if (this.manager.clients.has(this.peerID)) {
const oldClient = this.manager.clients.get(this.peerID)
@ -1200,7 +1203,12 @@ export default class PeerClient {
this.log("Ignoring old request", req.id)
return
}
this.log("Received request", req.id, "with command", req.command)
this.log(
`Request ${req.id}:`,
[req.command].concat(
this.loggingKeys.filter(k => k in req).map(k => `${k}: ${JSON.stringify(req[k], this.#writeReplacer)}`))
.join(', ')
)
this.maxCommandID = req.id
let handler
if (!this.peerID) {