Skip to content

Commit

Permalink
perf: add trace for UI startup (#26636)
Browse files Browse the repository at this point in the history
Support use of `trace` utility function in non-asynchronous contexts.
Remove `getMetaMetricsEnabled` check.
Generate default ID if omitted when starting and ending traces.
  • Loading branch information
matthewwalsh0 authored and Gudahtt committed Sep 4, 2024
1 parent c946ef7 commit 6654704
Show file tree
Hide file tree
Showing 9 changed files with 322 additions and 189 deletions.
10 changes: 7 additions & 3 deletions app/scripts/lib/createTracingMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import { MESSAGE_TYPE } from '../../../shared/constants/app';
import { trace } from '../../../shared/lib/trace';
import { trace, TraceName } from '../../../shared/lib/trace';

export default function createTracingMiddleware() {
return async function tracingMiddleware(
Expand All @@ -14,12 +14,16 @@ export default function createTracingMiddleware() {

if (method === MESSAGE_TYPE.ETH_SEND_TRANSACTION) {
req.traceContext = await trace({
name: 'Transaction',
name: TraceName.Transaction,
id,
tags: { source: 'dapp' },
});

await trace({ name: 'Middleware', id, parentContext: req.traceContext });
await trace({
name: TraceName.Middleware,
id,
parentContext: req.traceContext,
});
}

next();
Expand Down
28 changes: 15 additions & 13 deletions app/scripts/lib/ppom/ppom-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
LOADING_SECURITY_ALERT_RESPONSE,
SECURITY_PROVIDER_SUPPORTED_CHAIN_IDS,
} from '../../../../shared/constants/security-provider';
import { trace, TraceContext } from '../../../../shared/lib/trace';
import { trace, TraceContext, TraceName } from '../../../../shared/lib/trace';
import {
generateSecurityAlertId,
handlePPOMError,
Expand Down Expand Up @@ -92,19 +92,21 @@ export function createPPOMMiddleware<

const securityAlertId = generateSecurityAlertId();

trace({ name: 'PPOM Validation', parentContext: req.traceContext }, () =>
validateRequestWithPPOM({
ppomController,
request: req,
securityAlertId,
chainId,
}).then((securityAlertResponse) => {
updateSecurityAlertResponse(
req.method,
trace(
{ name: TraceName.PPOMValidation, parentContext: req.traceContext },
() =>
validateRequestWithPPOM({
ppomController,
request: req,
securityAlertId,
securityAlertResponse,
);
}),
chainId,
}).then((securityAlertResponse) => {
updateSecurityAlertResponse(
req.method,
securityAlertId,
securityAlertResponse,
);
}),
);

const loadingSecurityAlertResponse: SecurityAlertResponse = {
Expand Down
6 changes: 3 additions & 3 deletions app/scripts/lib/transaction/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
SECURITY_PROVIDER_EXCLUDED_TRANSACTION_TYPES,
SECURITY_PROVIDER_SUPPORTED_CHAIN_IDS,
} from '../../../../shared/constants/security-provider';
import { endTrace } from '../../../../shared/lib/trace';
import { endTrace, TraceName } from '../../../../shared/lib/trace';

export type AddTransactionOptions = NonNullable<
Parameters<TransactionController['addTransaction']>[1]
Expand Down Expand Up @@ -76,7 +76,7 @@ export async function addDappTransaction(
securityAlertResponse,
};

endTrace({ name: 'Middleware', id: actionId });
endTrace({ name: TraceName.Middleware, id: actionId });

const { waitForHash } = await addTransactionOrUserOperation({
...request,
Expand All @@ -88,7 +88,7 @@ export async function addDappTransaction(

const hash = (await waitForHash()) as string;

endTrace({ name: 'Transaction', id: actionId });
endTrace({ name: TraceName.Transaction, id: actionId });

return hash;
}
Expand Down
37 changes: 35 additions & 2 deletions app/scripts/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { isManifestV3 } from '../../shared/modules/mv3.utils';
import { checkForLastErrorAndLog } from '../../shared/modules/browser-runtime.utils';
import { SUPPORT_LINK } from '../../shared/lib/ui-utils';
import { getErrorHtml } from '../../shared/lib/error-utils';
import { endTrace, trace, TraceName } from '../../shared/lib/trace';
import ExtensionPlatform from './platforms/extension';
import { setupMultiplex } from './lib/stream-utils';
import { getEnvironmentType, getPlatform } from './lib/util';
Expand All @@ -43,6 +44,24 @@ let extensionPort;
start().catch(log.error);

async function start() {
const startTime = performance.now();

const traceContext = trace({
name: TraceName.UIStartup,
startTime: performance.timeOrigin,
});

trace({
name: TraceName.LoadScripts,
startTime: performance.timeOrigin,
parentContext: traceContext,
});

endTrace({
name: 'Load Scripts',
timestamp: performance.timeOrigin + startTime,
});

// create platform global
global.platform = new ExtensionPlatform();

Expand Down Expand Up @@ -74,6 +93,7 @@ async function start() {
// in later version we might try to improve it by reviving same streams.
updateUiStreams();
} else {
endTrace({ name: TraceName.BackgroundConnect });
initializeUiWithTab(activeTab);
}
await loadPhishingWarningPage();
Expand Down Expand Up @@ -189,18 +209,30 @@ async function start() {

extensionPort.onMessage.addListener(messageListener);
extensionPort.onDisconnect.addListener(resetExtensionStreamAndListeners);

trace({
name: TraceName.BackgroundConnect,
parentContext: traceContext,
});
} else {
const messageListener = async (message) => {
if (message?.data?.method === 'startUISync') {
endTrace({ name: TraceName.BackgroundConnect });
initializeUiWithTab(activeTab);
extensionPort.onMessage.removeListener(messageListener);
}
};

extensionPort.onMessage.addListener(messageListener);

trace({
name: TraceName.BackgroundConnect,
parentContext: traceContext,
});
}

function initializeUiWithTab(tab) {
initializeUi(tab, connectionStream, (err, store) => {
initializeUi(tab, connectionStream, traceContext, (err, store) => {
if (err) {
// if there's an error, store will be = metamaskState
displayCriticalError('troubleStarting', err, store);
Expand Down Expand Up @@ -276,7 +308,7 @@ async function queryCurrentActiveTab(windowType) {
return { id, title, origin, protocol, url };
}

function initializeUi(activeTab, connectionStream, cb) {
function initializeUi(activeTab, connectionStream, traceContext, cb) {
connectToAccountManager(connectionStream, (err, backgroundConnection) => {
if (err) {
cb(err, null);
Expand All @@ -288,6 +320,7 @@ function initializeUi(activeTab, connectionStream, cb) {
activeTab,
container,
backgroundConnection,
traceContext,
},
cb,
);
Expand Down
Loading

0 comments on commit 6654704

Please sign in to comment.