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: else:
copy("rpc.connection.host") copy("rpc.connection.host")
copy("rpc.connection.port") copy("rpc.connection.port")
copy("rpc.logging_keys")
def _get_permissions(self, key: str) -> tuple[bool, bool, bool, str]: def _get_permissions(self, key: str) -> tuple[bool, bool, bool, str]:
level = self["bridge.permissions"].get(key, "") level = self["bridge.permissions"].get(key, "")

View File

@ -252,6 +252,11 @@ rpc:
# Only for type: tcp # Only for type: tcp
host: localhost host: localhost
port: 29392 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. # Python logging configuration.
# #

View File

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

View File

@ -424,7 +424,8 @@ export default class PeerClient {
this.maxCommandID = 0 this.maxCommandID = 0
this.peerID = null this.peerID = null
this.deviceName = "KakaoTalk Bridge" this.deviceName = "KakaoTalk Bridge"
/** @type {[string]} */
this.loggingKeys = []
/** @type {Map<string, UserClient>} */ /** @type {Map<string, UserClient>} */
this.userClients = new Map() this.userClients = new Map()
} }
@ -1166,10 +1167,12 @@ export default class PeerClient {
* @param {string} req.peer_id * @param {string} req.peer_id
* @param {Object} req.register_config * @param {Object} req.register_config
* @param {string} req.register_config.device_name * @param {string} req.register_config.device_name
* @param {?[string]} req.logging_keys
*/ */
handleRegister = async (req) => { handleRegister = async (req) => {
this.peerID = req.peer_id this.peerID = req.peer_id
this.deviceName = req.register_config.device_name || this.deviceName this.deviceName = req.register_config.device_name || this.deviceName
this.loggingKeys = req.logging_keys || this.loggingKeys
this.log(`Registered socket ${this.connID} -> ${this.peerID}`) this.log(`Registered socket ${this.connID} -> ${this.peerID}`)
if (this.manager.clients.has(this.peerID)) { if (this.manager.clients.has(this.peerID)) {
const oldClient = this.manager.clients.get(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) this.log("Ignoring old request", req.id)
return 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 this.maxCommandID = req.id
let handler let handler
if (!this.peerID) { if (!this.peerID) {