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

fix (cherry-pick): custom tracing in production builds (#27124) #27265

Merged
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
8 changes: 8 additions & 0 deletions app/scripts/lib/ppom/ppom-middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ describe('PPOMMiddleware', () => {
generateSecurityAlertIdMock.mockReturnValue(SECURITY_ALERT_ID_MOCK);
handlePPOMErrorMock.mockReturnValue(SECURITY_ALERT_RESPONSE_MOCK);
isChainSupportedMock.mockResolvedValue(true);

globalThis.sentry = {
withIsolationScope: jest
.fn()
.mockImplementation((fn) => fn({ setTags: jest.fn() })),
startSpan: jest.fn().mockImplementation((_, fn) => fn({})),
startSpanManual: jest.fn().mockImplementation((_, fn) => fn({})),
};
});

it('updates alert response after validating request', async () => {
Expand Down
4 changes: 4 additions & 0 deletions app/scripts/metamask-controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,10 @@ describe('MetaMaskController', () => {
},
]),
);

globalThis.sentry = {
withIsolationScope: jest.fn(),
};
});

afterEach(() => {
Expand Down
13 changes: 5 additions & 8 deletions development/build/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,15 @@ const scuttlingConfigBase = {
extra: '',
stateHooks: '',
nw: '',
// Sentry Custom Tracing
document: '',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note for audit sake that document was already an exception in MV2, this just excludes it in both MV2 and MV3.

isNaN: '',
parseInt: '',
},
};

const mv3ScuttlingConfig = { ...scuttlingConfigBase };

const standardScuttlingConfig = {
...scuttlingConfigBase,
'scripts/sentry-install.js': {
...scuttlingConfigBase['scripts/sentry-install.js'],
document: '',
},
};
const standardScuttlingConfig = { ...scuttlingConfigBase };

const noopWriteStream = through.obj((_file, _fileEncoding, callback) =>
callback(),
Expand Down
6 changes: 6 additions & 0 deletions shared/lib/trace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ describe('Trace', () => {
beforeEach(() => {
jest.resetAllMocks();

globalThis.sentry = {
startSpan: startSpanMock,
startSpanManual: startSpanManualMock,
withIsolationScope: withIsolationScopeMock,
};

startSpanMock.mockImplementation((_, fn) => fn({} as Span));

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
47 changes: 44 additions & 3 deletions shared/lib/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ function traceCallback<T>(request: TraceRequest, fn: TraceCallback<T>): T {
};

return startSpan(request, (spanOptions) =>
Sentry.startSpan(spanOptions, callback),
sentryStartSpan(spanOptions, callback),
);
}

Expand All @@ -138,7 +138,7 @@ function startTrace(request: TraceRequest): TraceContext {
};

return startSpan(request, (spanOptions) =>
Sentry.startSpanManual(spanOptions, callback),
sentryStartSpanManual(spanOptions, callback),
);
}

Expand All @@ -157,7 +157,7 @@ function startSpan<T>(
startTime,
};

return Sentry.withIsolationScope((scope) => {
return sentryWithIsolationScope((scope: Sentry.Scope) => {
scope.setTags(tags as Record<string, Primitive>);

return callback(spanOptions);
Expand Down Expand Up @@ -207,3 +207,44 @@ function tryCatchMaybePromise<T>(

return undefined;
}

function sentryStartSpan<T>(
spanOptions: StartSpanOptions,
callback: (span: Sentry.Span | null) => T,
): T {
const actual = globalThis.sentry?.startSpan;

if (!actual) {
return callback(null);
}

return actual(spanOptions, callback);
}

function sentryStartSpanManual<T>(
spanOptions: StartSpanOptions,
callback: (span: Sentry.Span | null) => T,
): T {
const actual = globalThis.sentry?.startSpanManual;

if (!actual) {
return callback(null);
}

return actual(spanOptions, callback);
}

function sentryWithIsolationScope<T>(callback: (scope: Sentry.Scope) => T): T {
const actual = globalThis.sentry?.withIsolationScope;

if (!actual) {
const scope = {
// eslint-disable-next-line no-empty-function
setTags: () => {},
} as unknown as Sentry.Scope;

return callback(scope);
}

return actual(callback);
}