Skip to content

Commit

Permalink
Annotate debugger events with app ID, device name etc (facebook#39108)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#39108

TSIA

Changelog: [Internal]

Reviewed By: huntie

Differential Revision: D48484712

fbshipit-source-id: 3d875fdd098f2ef98b3bccb03f0053d0e5cc3be7
  • Loading branch information
motiz88 authored and facebook-github-bot committed Aug 22, 2023
1 parent d10a809 commit cbbf169
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 15 deletions.
41 changes: 30 additions & 11 deletions packages/dev-middleware/src/inspector-proxy/Device.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ export default class Device {
this._deviceSocket = socket;
this._projectRoot = projectRoot;
this._deviceEventReporter = eventReporter
? new DeviceEventReporter(eventReporter)
? new DeviceEventReporter(eventReporter, {
deviceId: id,
deviceName: name,
appId: app,
})
: null;

// $FlowFixMe[incompatible-call]
Expand Down Expand Up @@ -167,7 +171,13 @@ export default class Device {
// 2. Forwards all messages from the debugger to device as wrappedEvent
// 3. Sends disconnect event to device when debugger connection socket closes.
handleDebuggerConnection(socket: WS, pageId: string) {
this._deviceEventReporter?.logConnection('debugger');
// Clear any commands we were waiting on.
this._deviceEventReporter?.logDisconnection('debugger');

this._deviceEventReporter?.logConnection('debugger', {
pageId: pageId,
});

// Disconnect current debugger if we already have debugger connected.
if (this._debuggerConnection) {
this._debuggerConnection.socket.close();
Expand All @@ -190,14 +200,13 @@ export default class Device {
},
});

// Clear any commands we were waiting on.
this._deviceEventReporter?.logDisconnection('debugger');

// $FlowFixMe[incompatible-call]
socket.on('message', (message: string) => {
debug('(Debugger) -> (Proxy) (Device): ' + message);
const debuggerRequest = JSON.parse(message);
this._deviceEventReporter?.logRequest(debuggerRequest, 'debugger');
this._deviceEventReporter?.logRequest(debuggerRequest, 'debugger', {
pageId: this._debuggerConnection?.pageId ?? null,
});
const handled = this._interceptMessageFromDebugger(
debuggerRequest,
debuggerInfo,
Expand Down Expand Up @@ -322,7 +331,9 @@ export default class Device {

const parsedPayload = JSON.parse(message.payload.wrappedEvent);
if ('id' in parsedPayload) {
this._deviceEventReporter?.logResponse(parsedPayload, 'device');
this._deviceEventReporter?.logResponse(parsedPayload, 'device', {
pageId: this._debuggerConnection?.pageId ?? null,
});
}

if (this._debuggerConnection) {
Expand Down Expand Up @@ -401,7 +412,9 @@ export default class Device {
];

for (const message of toSend) {
this._deviceEventReporter?.logRequest(message, 'proxy');
this._deviceEventReporter?.logRequest(message, 'proxy', {
pageId: this._debuggerConnection?.pageId ?? null,
});
this._sendMessageToDevice({
event: 'wrappedEvent',
payload: {
Expand Down Expand Up @@ -495,7 +508,9 @@ export default class Device {
// This is not an issue in VSCode/Nuclide where the IDE knows to resume
// at its convenience.
const resumeMessage = {method: 'Debugger.resume', id: 0};
this._deviceEventReporter?.logRequest(resumeMessage, 'proxy');
this._deviceEventReporter?.logRequest(resumeMessage, 'proxy', {
pageId: this._debuggerConnection?.pageId ?? null,
});
this._sendMessageToDevice({
event: 'wrappedEvent',
payload: {
Expand Down Expand Up @@ -562,7 +577,9 @@ export default class Device {
const result: GetScriptSourceResponse = {scriptSource};
const response = {id: req.id, result};
socket.send(JSON.stringify(response));
this._deviceEventReporter?.logResponse(response, 'proxy');
this._deviceEventReporter?.logResponse(response, 'proxy', {
pageId: this._debuggerConnection?.pageId ?? null,
});
};
const sendErrorResponse = (error: string) => {
// Tell the client that the request failed
Expand All @@ -572,7 +589,9 @@ export default class Device {

// Send to the console as well, so the user can see it
this._sendErrorToDebugger(error);
this._deviceEventReporter?.logResponse(response, 'proxy');
this._deviceEventReporter?.logResponse(response, 'proxy', {
pageId: this._debuggerConnection?.pageId ?? null,
});
};

const pathToSource = this._scriptIdToSourcePathMapping.get(
Expand Down
56 changes: 53 additions & 3 deletions packages/dev-middleware/src/inspector-proxy/DeviceEventReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,19 @@ type PendingCommand = {
method: string,
requestOrigin: 'proxy' | 'debugger',
requestTime: number,
metadata: RequestMetadata,
};

type DeviceMetadata = $ReadOnly<{
appId: string,
deviceId: string,
deviceName: string,
}>;

type RequestMetadata = $ReadOnly<{
pageId: string | null,
}>;

class DeviceEventReporter {
_eventReporter: EventReporter;

Expand All @@ -35,18 +46,23 @@ class DeviceEventReporter {
},
});

constructor(eventReporter: EventReporter) {
_metadata: DeviceMetadata;

constructor(eventReporter: EventReporter, metadata: DeviceMetadata) {
this._eventReporter = eventReporter;
this._metadata = metadata;
}

logRequest(
req: $ReadOnly<{id: number, method: string, ...}>,
origin: 'debugger' | 'proxy',
metadata: RequestMetadata,
): void {
this._pendingCommands.set(req.id, {
method: req.method,
requestOrigin: origin,
requestTime: Date.now(),
metadata,
});
}

Expand All @@ -57,6 +73,9 @@ class DeviceEventReporter {
...
}>,
origin: 'device' | 'proxy',
metadata: $ReadOnly<{
pageId: string | null,
}>,
): void {
const pendingCommand = this._pendingCommands.get(res.id);
if (!pendingCommand) {
Expand All @@ -69,6 +88,10 @@ class DeviceEventReporter {
errorCode: 'UNMATCHED_REQUEST_ID',
responseOrigin: 'proxy',
timeSinceStart: null,
appId: this._metadata.appId,
deviceId: this._metadata.deviceId,
deviceName: this._metadata.deviceName,
pageId: metadata.pageId,
});
return;
}
Expand All @@ -89,6 +112,10 @@ class DeviceEventReporter {
errorDetails: message,
responseOrigin: origin,
timeSinceStart,
appId: this._metadata.appId,
deviceId: this._metadata.deviceId,
deviceName: this._metadata.deviceName,
pageId: pendingCommand.metadata.pageId,
});
return;
}
Expand All @@ -100,17 +127,32 @@ class DeviceEventReporter {
status: 'success',
responseOrigin: origin,
timeSinceStart,
appId: this._metadata.appId,
deviceId: this._metadata.deviceId,
deviceName: this._metadata.deviceName,
pageId: pendingCommand.metadata.pageId,
});
}

logConnection(connectedEntity: 'debugger') {
this._eventReporter?.logEvent({
logConnection(
connectedEntity: 'debugger',
metadata: $ReadOnly<{pageId: string}>,
) {
this._eventReporter.logEvent({
type: 'connect_debugger_frontend',
status: 'success',
appId: this._metadata.appId,
deviceName: this._metadata.deviceName,
deviceId: this._metadata.deviceId,
pageId: metadata.pageId,
});
}

logDisconnection(disconnectedEntity: 'device' | 'debugger') {
const eventReporter = this._eventReporter;
if (!eventReporter) {
return;
}
const errorCode =
disconnectedEntity === 'device'
? 'DEVICE_DISCONNECTED'
Expand All @@ -125,6 +167,10 @@ class DeviceEventReporter {
errorCode,
responseOrigin: 'proxy',
timeSinceStart: Date.now() - pendingCommand.requestTime,
appId: this._metadata.appId,
deviceId: this._metadata.deviceId,
deviceName: this._metadata.deviceName,
pageId: pendingCommand.metadata.pageId,
});
}
this._pendingCommands.clear();
Expand All @@ -140,6 +186,10 @@ class DeviceEventReporter {
errorCode: 'TIMED_OUT',
responseOrigin: 'proxy',
timeSinceStart: Date.now() - pendingCommand.requestTime,
appId: this._metadata.appId,
deviceId: this._metadata.deviceId,
deviceName: this._metadata.deviceName,
pageId: pendingCommand.metadata.pageId,
});
}
}
Expand Down
10 changes: 9 additions & 1 deletion packages/dev-middleware/src/types/EventReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ type CodedErrorResult<ErrorCode: string> = {
errorDetails?: string,
};

type DebuggerSessionIDs = {
appId: string,
deviceName: string,
deviceId: string,
pageId: string | null,
};

export type ReportableEvent =
| {
type: 'launch_debugger_frontend',
Expand All @@ -34,7 +41,7 @@ export type ReportableEvent =
}
| {
type: 'connect_debugger_frontend',
...SuccessResult<void> | ErrorResult<mixed>,
...SuccessResult<DebuggerSessionIDs> | ErrorResult<mixed>,
}
| {
type: 'debugger_command',
Expand All @@ -44,6 +51,7 @@ export type ReportableEvent =
requestOrigin: 'proxy' | 'debugger' | null,
responseOrigin: 'proxy' | 'device',
timeSinceStart: number | null,
...DebuggerSessionIDs,
...
| SuccessResult<void>
| CodedErrorResult<
Expand Down

0 comments on commit cbbf169

Please sign in to comment.