Await onApiCallEnd/onApiCallBegin hooks

This commit is contained in:
Mateusz Burzyński 2024-05-14 15:13:43 +02:00
parent 64b4ac1732
commit 4674f21a70
2 changed files with 8 additions and 7 deletions

View file

@ -146,7 +146,8 @@ export abstract class ChannelOwner<T extends channels.Channel = channels.Channel
const { apiName, frames, csi, callCookie, wallTime } = apiZone.reported ? { apiName: undefined, csi: undefined, callCookie: undefined, frames: [], wallTime: undefined } : apiZone; const { apiName, frames, csi, callCookie, wallTime } = apiZone.reported ? { apiName: undefined, csi: undefined, callCookie: undefined, frames: [], wallTime: undefined } : apiZone;
apiZone.reported = true; apiZone.reported = true;
if (csi && apiName) if (csi && apiName)
csi.onApiCallBegin(apiName, params, frames, wallTime, callCookie); // this is meant to be awaited so all async hooks can be completed before the actual command gets dispatched
await csi.onApiCallBegin(apiName, params, frames, wallTime, callCookie);
return await this._connection.sendMessageToServer(this, prop, validator(params, '', { tChannelImpl: tChannelImplToWire, binary: this._connection.rawBuffers() ? 'buffer' : 'toBase64' }), apiName, frames, wallTime); return await this._connection.sendMessageToServer(this, prop, validator(params, '', { tChannelImpl: tChannelImplToWire, binary: this._connection.rawBuffers() ? 'buffer' : 'toBase64' }), apiName, frames, wallTime);
}); });
}; };
@ -188,7 +189,7 @@ export abstract class ChannelOwner<T extends channels.Channel = channels.Channel
logApiCall(logger, `=> ${apiName} started`, isInternal); logApiCall(logger, `=> ${apiName} started`, isInternal);
const apiZone: ApiZone = { apiName, frames, isInternal, reported: false, csi, callCookie, wallTime }; const apiZone: ApiZone = { apiName, frames, isInternal, reported: false, csi, callCookie, wallTime };
const result = await zones.run('apiZone', apiZone, async () => await func(apiZone)); const result = await zones.run('apiZone', apiZone, async () => await func(apiZone));
csi?.onApiCallEnd(callCookie); await csi?.onApiCallEnd(callCookie);
logApiCall(logger, `<= ${apiName} succeeded`, isInternal); logApiCall(logger, `<= ${apiName} succeeded`, isInternal);
return result; return result;
} catch (e) { } catch (e) {
@ -200,7 +201,7 @@ export abstract class ChannelOwner<T extends channels.Channel = channels.Channel
e.stack = e.message + stackFrames; e.stack = e.message + stackFrames;
else else
e.stack = ''; e.stack = '';
csi?.onApiCallEnd(callCookie, e); await csi?.onApiCallEnd(callCookie, e);
logApiCall(logger, `<= ${apiName} failed`, isInternal); logApiCall(logger, `<= ${apiName} failed`, isInternal);
throw e; throw e;
} }

View file

@ -22,8 +22,8 @@ export interface ClientInstrumentation {
addListener(listener: ClientInstrumentationListener): void; addListener(listener: ClientInstrumentationListener): void;
removeListener(listener: ClientInstrumentationListener): void; removeListener(listener: ClientInstrumentationListener): void;
removeAllListeners(): void; removeAllListeners(): void;
onApiCallBegin(apiCall: string, params: Record<string, any>, frames: StackFrame[], wallTime: number, userData: any): void; onApiCallBegin(apiCall: string, params: Record<string, any>, frames: StackFrame[], wallTime: number, userData: any): Promise<void> | void;
onApiCallEnd(userData: any, error?: Error): void; onApiCallEnd(userData: any, error?: Error): Promise<void> | void;
onDidCreateBrowserContext(context: BrowserContext): Promise<void>; onDidCreateBrowserContext(context: BrowserContext): Promise<void>;
onDidCreateRequestContext(context: APIRequestContext): Promise<void>; onDidCreateRequestContext(context: APIRequestContext): Promise<void>;
onWillPause(): void; onWillPause(): void;
@ -32,8 +32,8 @@ export interface ClientInstrumentation {
} }
export interface ClientInstrumentationListener { export interface ClientInstrumentationListener {
onApiCallBegin?(apiName: string, params: Record<string, any>, frames: StackFrame[], wallTime: number, userData: any): void; onApiCallBegin?(apiName: string, params: Record<string, any>, frames: StackFrame[], wallTime: number, userData: any): Promise<void> | void;
onApiCallEnd?(userData: any, error?: Error): void; onApiCallEnd?(userData: any, error?: Error): Promise<void> | void;
onDidCreateBrowserContext?(context: BrowserContext): Promise<void>; onDidCreateBrowserContext?(context: BrowserContext): Promise<void>;
onDidCreateRequestContext?(context: APIRequestContext): Promise<void>; onDidCreateRequestContext?(context: APIRequestContext): Promise<void>;
onWillPause?(): void; onWillPause?(): void;