Skip to content

Commit

Permalink
chore: make instrumentation multiplexing proxy-based (#5410)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman committed Feb 11, 2021
1 parent a06cf70 commit a164f2a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 43 deletions.
42 changes: 11 additions & 31 deletions src/server/instrumentation.ts
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
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
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
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
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

0 comments on commit a164f2a

Please sign in to comment.