feat(log): trace the inbound receive chain (symmetric to outbound)
Sending got full-chain trace via the api-handler chokepoint; receiving had none. Wrap convertAndDispatch — the single path every inbound message/notice takes (decode → convert → dispatch) — as the receive-side mirror: - When trace is live, run the chain inside a request id so the whole receive (incl. any rkey-fetch packets the conversion triggers) is correlated under one [req#N]; otherwise skip the wrap entirely (zero overhead). - trace the raw inbound event on entry (deep render) and the converted post_type + elapsed on exit (or "dropped"). Reuses the existing trace/ALS/renderParamsVerbose machinery; no new mechanism.
This commit is contained in:
1 file changed
+32
-6
@@ -1,26 +1,32 @@
|
||||
import { createLogger, getLogLevel, nextRequestId, runWithRequestId, type Logger } from '@snowluma/common/logger';
|
||||
import { renderParamsVerbose } from '@snowluma/common/log-summary';
|
||||
import type { QQEventVariant } from '@snowluma/protocol/events';
|
||||
import { convertEvent } from './event-converter';
|
||||
import type { OneBotInstanceContext } from './instance-context';
|
||||
import { GROUP_MESSAGE_EVENT, PRIVATE_MESSAGE_EVENT, hashMessageIdInt32 } from './message-id';
|
||||
|
||||
const moduleLog = createLogger('Event');
|
||||
|
||||
export function registerEventPipeline(ctx: OneBotInstanceContext): () => void {
|
||||
const uinNum = Number.parseInt(ctx.uin, 10);
|
||||
const log = Number.isFinite(uinNum) && uinNum > 0 ? moduleLog.child({ uin: uinNum }) : moduleLog;
|
||||
const disposers: Array<() => void> = [];
|
||||
disposers.push(
|
||||
ctx.bridge.events.on('group_message', async (event) => {
|
||||
cacheGroupMessageMeta(ctx, event);
|
||||
await convertAndDispatch(ctx, event);
|
||||
await convertAndDispatch(ctx, log, event);
|
||||
}),
|
||||
);
|
||||
disposers.push(
|
||||
ctx.bridge.events.on('friend_message', async (event) => {
|
||||
cachePrivateMessageMeta(ctx, event.senderUin, event.msgSeq, event.time, event.msgId);
|
||||
await convertAndDispatch(ctx, event);
|
||||
await convertAndDispatch(ctx, log, event);
|
||||
}),
|
||||
);
|
||||
disposers.push(
|
||||
ctx.bridge.events.on('temp_message', async (event) => {
|
||||
cachePrivateMessageMeta(ctx, event.senderUin, event.msgSeq, event.time, 0);
|
||||
await convertAndDispatch(ctx, event);
|
||||
await convertAndDispatch(ctx, log, event);
|
||||
}),
|
||||
);
|
||||
for (const kind of NOTICE_KINDS) {
|
||||
@@ -29,7 +35,7 @@ export function registerEventPipeline(ctx: OneBotInstanceContext): () => void {
|
||||
if (event.kind === 'group_msg_emoji_like') {
|
||||
cacheReaction(ctx, event);
|
||||
}
|
||||
await convertAndDispatch(ctx, event);
|
||||
await convertAndDispatch(ctx, log, event);
|
||||
}),
|
||||
);
|
||||
}
|
||||
@@ -58,10 +64,30 @@ const NOTICE_KINDS = [
|
||||
'group_msg_emoji_like',
|
||||
] as const satisfies readonly QQEventVariant['kind'][];
|
||||
|
||||
async function convertAndDispatch(ctx: OneBotInstanceContext, event: QQEventVariant): Promise<void> {
|
||||
async function convertAndDispatch(ctx: OneBotInstanceContext, log: Logger, event: QQEventVariant): Promise<void> {
|
||||
// Inbound choke point — the receive-side mirror of the outbound api-handler.
|
||||
// Correlate the whole receive chain (decode → convert, incl. any rkey-fetch
|
||||
// packets the conversion triggers, → dispatch) under one [req#N]. Only pay
|
||||
// the AsyncLocalStorage wrap + id when trace is actually live.
|
||||
if (getLogLevel() !== 'trace') {
|
||||
await runConvertAndDispatch(ctx, log, event);
|
||||
return;
|
||||
}
|
||||
await runWithRequestId(nextRequestId(), () => runConvertAndDispatch(ctx, log, event));
|
||||
}
|
||||
|
||||
async function runConvertAndDispatch(ctx: OneBotInstanceContext, log: Logger, event: QQEventVariant): Promise<void> {
|
||||
// Raw inbound event, memory-only (trace). Lazy → the deep render runs only
|
||||
// when trace is live.
|
||||
log.trace(() => [`recv ${event.kind} ⇐ %s`, renderParamsVerbose(event)]);
|
||||
const startedAt = Date.now();
|
||||
const converted = await convertEvent(ctx.converterCtx, event);
|
||||
if (!converted) return;
|
||||
if (!converted) {
|
||||
log.trace(() => [`recv ${event.kind} ⇒ dropped (${Date.now() - startedAt}ms)`]);
|
||||
return;
|
||||
}
|
||||
ctx.dispatchEvent(converted);
|
||||
log.trace(() => [`recv ${event.kind} ⇒ ${String(converted.post_type ?? '?')} (${Date.now() - startedAt}ms)`]);
|
||||
}
|
||||
|
||||
function cacheGroupMessageMeta(ctx: OneBotInstanceContext, event: Extract<QQEventVariant, { kind: 'group_message' }>): void {
|
||||
|
||||
Reference in new issue
Block a user