mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
chore: make instrumentation multiplexing proxy-based (#5410)
This commit is contained in:
parent
a06cf70d28
commit
a164f2a810
@ -72,37 +72,17 @@ export interface InstrumentationListener {
|
||||
onAfterAction?(result: ActionResult, sdkObject: SdkObject, metadata: CallMetadata): Promise<void>;
|
||||
}
|
||||
|
||||
export class InstrumentationMultiplexer implements Instrumentation {
|
||||
private _listeners: InstrumentationListener[];
|
||||
|
||||
constructor(listeners: InstrumentationListener[]) {
|
||||
this._listeners = listeners;
|
||||
}
|
||||
|
||||
async onContextCreated(context: BrowserContext): Promise<void> {
|
||||
for (const listener of this._listeners)
|
||||
await listener.onContextCreated?.(context);
|
||||
}
|
||||
|
||||
async onContextWillDestroy(context: BrowserContext): Promise<void> {
|
||||
for (const listener of this._listeners)
|
||||
await listener.onContextWillDestroy?.(context);
|
||||
}
|
||||
|
||||
async onContextDidDestroy(context: BrowserContext): Promise<void> {
|
||||
for (const listener of this._listeners)
|
||||
await listener.onContextDidDestroy?.(context);
|
||||
}
|
||||
|
||||
async onActionCheckpoint(name: string, sdkObject: SdkObject, metadata: CallMetadata): Promise<void> {
|
||||
for (const listener of this._listeners)
|
||||
await listener.onActionCheckpoint?.(name, sdkObject, metadata);
|
||||
}
|
||||
|
||||
async onAfterAction(result: ActionResult, sdkObject: SdkObject, metadata: CallMetadata): Promise<void> {
|
||||
for (const listener of this._listeners)
|
||||
await listener.onAfterAction?.(result, sdkObject, metadata);
|
||||
}
|
||||
export function multiplexInstrumentation(listeners: InstrumentationListener[]): Instrumentation {
|
||||
return new Proxy({}, {
|
||||
get: (obj: any, prop: string) => {
|
||||
if (!prop.startsWith('on'))
|
||||
return obj[prop];
|
||||
return async (...params: any[]) => {
|
||||
for (const listener of listeners)
|
||||
await (listener as any)[prop]?.(...params);
|
||||
};
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function internalCallMetadata(): CallMetadata {
|
||||
|
||||
@ -27,7 +27,7 @@ import { HarTracer } from './supplements/har/harTracer';
|
||||
import { InspectorController } from './supplements/inspectorController';
|
||||
import { WebKit } from './webkit/webkit';
|
||||
import { Registry } from '../utils/registry';
|
||||
import { InstrumentationMultiplexer, SdkObject } from './instrumentation';
|
||||
import { InstrumentationListener, multiplexInstrumentation, SdkObject } from './instrumentation';
|
||||
|
||||
export class Playwright extends SdkObject {
|
||||
readonly selectors: Selectors;
|
||||
@ -39,11 +39,13 @@ export class Playwright extends SdkObject {
|
||||
readonly options: PlaywrightOptions;
|
||||
|
||||
constructor(isInternal: boolean) {
|
||||
const instrumentation = new InstrumentationMultiplexer(isInternal ? [] : [
|
||||
new InspectorController(),
|
||||
new Tracer(),
|
||||
new HarTracer()
|
||||
]);
|
||||
const listeners: InstrumentationListener[] = [];
|
||||
if (!isInternal) {
|
||||
listeners.push(new Tracer());
|
||||
listeners.push(new HarTracer());
|
||||
listeners.push(new InspectorController());
|
||||
}
|
||||
const instrumentation = multiplexInstrumentation(listeners);
|
||||
super({ attribution: {}, instrumentation } as any);
|
||||
this.options = {
|
||||
isInternal,
|
||||
|
||||
@ -42,8 +42,6 @@ export class HarTracer implements InstrumentationListener {
|
||||
await contextTracer.flush();
|
||||
}
|
||||
}
|
||||
|
||||
async onContextDidDestroy(context: BrowserContext): Promise<void> { }
|
||||
}
|
||||
|
||||
type HarOptions = {
|
||||
|
||||
@ -28,6 +28,4 @@ export class InspectorController implements InstrumentationListener {
|
||||
});
|
||||
}
|
||||
}
|
||||
async onContextWillDestroy(context: BrowserContext): Promise<void> {}
|
||||
async onContextDidDestroy(context: BrowserContext): Promise<void> {}
|
||||
}
|
||||
|
||||
@ -47,8 +47,6 @@ export class Tracer implements InstrumentationListener {
|
||||
this._contextTracers.set(context, contextTracer);
|
||||
}
|
||||
|
||||
async onContextWillDestroy(context: BrowserContext): Promise<void> {}
|
||||
|
||||
async onContextDidDestroy(context: BrowserContext): Promise<void> {
|
||||
const contextTracer = this._contextTracers.get(context);
|
||||
if (contextTracer) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user