Skip to content

Commit

Permalink
fix (cherry-pick): custom tracing in production builds (#27124) (#27265)
Browse files Browse the repository at this point in the history
Cherry pick of #27124 for version 12.3.0.
  • Loading branch information
matthewwalsh0 authored Sep 19, 2024
1 parent 29094e0 commit 5e6d703
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 11 deletions.
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: '',
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);
}

0 comments on commit 5e6d703

Please sign in to comment.