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: custom tracing in production builds #27124

Merged
merged 26 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
1f6340e
Add scuttling exceptions for session tracking
matthewwalsh0 Sep 10, 2024
c97d186
Merge branch 'develop' into fix/2555-sentry-sessions-production
matthewwalsh0 Sep 11, 2024
7da3779
Merge branch 'develop' into fix/2555-sentry-sessions-production
matthewwalsh0 Sep 13, 2024
87ae6af
Update LavaMoat policies
matthewwalsh0 Sep 13, 2024
fd724af
Fix linting
matthewwalsh0 Sep 13, 2024
6220520
Remove lavamoat warning logs
matthewwalsh0 Sep 14, 2024
20a68ce
Update LavaMoat build policy
matthewwalsh0 Sep 14, 2024
ee6b05a
Align LavaMoat build policy
matthewwalsh0 Sep 14, 2024
5f19389
Add E2E tests
matthewwalsh0 Sep 14, 2024
35190bc
Merge branch 'fix/2555-sentry-sessions-production' into fix/custom-tr…
matthewwalsh0 Sep 14, 2024
382fb1a
Remove duplicate helper
matthewwalsh0 Sep 14, 2024
62fcdd7
Fix E2E tests
matthewwalsh0 Sep 14, 2024
0277a51
Rename test
matthewwalsh0 Sep 14, 2024
2ffcad1
Fix linting
matthewwalsh0 Sep 14, 2024
929782d
Merge branch 'fix/2555-sentry-sessions-production' into fix/custom-tr…
matthewwalsh0 Sep 14, 2024
469c98e
Merge branch 'develop' into fix/custom-tracing-production
matthewwalsh0 Sep 16, 2024
12705a8
Use global object
matthewwalsh0 Sep 18, 2024
6096186
Merge branch 'develop' into fix/custom-tracing-production
matthewwalsh0 Sep 18, 2024
735cf0a
Revert unnecessary changes
matthewwalsh0 Sep 18, 2024
24fe544
Fix unit tests
matthewwalsh0 Sep 18, 2024
934edcc
Fix unit tests
matthewwalsh0 Sep 18, 2024
bda0a53
Fix linting
matthewwalsh0 Sep 18, 2024
d1d2de1
Fix more unit tests
matthewwalsh0 Sep 18, 2024
41cf178
Fix linting
matthewwalsh0 Sep 18, 2024
d18feb4
Fix typo
matthewwalsh0 Sep 19, 2024
5bac41e
Merge branch 'develop' into fix/custom-tracing-production
legobeat Sep 19, 2024
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
4 changes: 2 additions & 2 deletions app/scripts/lib/createTracingMiddleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ describe('createTracingMiddleware', () => {

request = { ...REQUEST_MOCK };

global.sentry = {
getMetaMetricsEnabled: () => Promise.resolve(true),
globalThis.sentry = {
withIsolationScope: jest.fn().mockReturnValue({}),
};
});

Expand Down
2 changes: 1 addition & 1 deletion app/scripts/lib/createTracingMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Request and repsones are currently untyped.
// Request and responses are currently untyped.
/* eslint-disable @typescript-eslint/no-explicit-any */

import { MESSAGE_TYPE } from '../../../shared/constants/app';
Expand Down
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 @@ -111,6 +111,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 @@ -336,6 +336,10 @@ describe('MetaMaskController', () => {
},
]),
);

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

afterEach(() => {
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
6 changes: 3 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),
globalThis.sentry.startSpan(spanOptions, callback),
);
}

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

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

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

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

return callback(spanOptions);
Expand Down
129 changes: 129 additions & 0 deletions test/e2e/tests/metrics/traces.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { MockttpServer } from 'mockttp';
import FixtureBuilder from '../../fixture-builder';
import {
defaultGanacheOptions,
unlockWallet,
withFixtures,
} from '../../helpers';
import {
expectMockRequest,
expectNoMockRequest,
} from '../../helpers/mock-server';

async function mockSentryCustomTrace(mockServer: MockttpServer) {
return [
await mockServer
.forPost(/sentry/u)
.withBodyIncluding('"transaction":"UI Startup"')
.thenCallback(() => {
return {
statusCode: 200,
json: {},
};
}),
];
}

async function mockSentryAutomatedTrace(mockServer: MockttpServer) {
return [
await mockServer
.forPost(/sentry/u)
.withBodyIncluding('"transaction":"/home.html"')
.thenCallback(() => {
return {
statusCode: 200,
json: {},
};
}),
];
}

describe('Traces', function () {
it('sends custom trace when opening UI if metrics enabled', async function () {
await withFixtures(
{
fixtures: new FixtureBuilder()
.withMetaMetricsController({
participateInMetaMetrics: true,
})
.build(),
ganacheOptions: defaultGanacheOptions,
title: this.test?.fullTitle(),
testSpecificMock: mockSentryCustomTrace,
manifestFlags: {
doNotForceSentryForThisTest: true,
},
matthewwalsh0 marked this conversation as resolved.
Show resolved Hide resolved
},
async ({ driver, mockedEndpoint }) => {
await unlockWallet(driver);
await expectMockRequest(driver, mockedEndpoint[0], { timeout: 3000 });
},
);
});

it('does not send custom trace when opening UI if metrics disabled @no-mmi', async function () {
await withFixtures(
{
fixtures: new FixtureBuilder()
.withMetaMetricsController({
participateInMetaMetrics: false,
})
.build(),
ganacheOptions: defaultGanacheOptions,
title: this.test?.fullTitle(),
testSpecificMock: mockSentryCustomTrace,
manifestFlags: {
doNotForceSentryForThisTest: true,
},
},
async ({ driver, mockedEndpoint }) => {
await unlockWallet(driver);
await expectNoMockRequest(driver, mockedEndpoint[0], { timeout: 3000 });
},
);
});

it('sends automated trace when opening UI if metrics enabled', async function () {
await withFixtures(
{
fixtures: new FixtureBuilder()
.withMetaMetricsController({
participateInMetaMetrics: true,
})
.build(),
ganacheOptions: defaultGanacheOptions,
title: this.test?.fullTitle(),
testSpecificMock: mockSentryAutomatedTrace,
manifestFlags: {
doNotForceSentryForThisTest: true,
},
},
async ({ driver, mockedEndpoint }) => {
await unlockWallet(driver);
await expectMockRequest(driver, mockedEndpoint[0], { timeout: 3000 });
},
);
});

it('does not send automated trace when opening UI if metrics disabled @no-mmi', async function () {
await withFixtures(
{
fixtures: new FixtureBuilder()
.withMetaMetricsController({
participateInMetaMetrics: false,
})
.build(),
ganacheOptions: defaultGanacheOptions,
title: this.test?.fullTitle(),
testSpecificMock: mockSentryAutomatedTrace,
manifestFlags: {
doNotForceSentryForThisTest: true,
},
},
async ({ driver, mockedEndpoint }) => {
await unlockWallet(driver);
await expectNoMockRequest(driver, mockedEndpoint[0], { timeout: 3000 });
},
);
});
});