Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: make instrumentation multiplexing proxy-based #5410

Merged
merged 1 commit into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 11 additions & 31 deletions src/server/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 8 additions & 6 deletions src/server/playwright.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
2 changes: 0 additions & 2 deletions src/server/supplements/har/harTracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ export class HarTracer implements InstrumentationListener {
await contextTracer.flush();
}
}

async onContextDidDestroy(context: BrowserContext): Promise<void> { }
}

type HarOptions = {
Expand Down
2 changes: 0 additions & 2 deletions src/server/supplements/inspectorController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,4 @@ export class InspectorController implements InstrumentationListener {
});
}
}
async onContextWillDestroy(context: BrowserContext): Promise<void> {}
async onContextDidDestroy(context: BrowserContext): Promise<void> {}
}
2 changes: 0 additions & 2 deletions src/trace/tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down