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

Set sentry autoTrackSessions default #20132

Merged
merged 9 commits into from
Jul 26, 2023
Merged

Set sentry autoTrackSessions default #20132

merged 9 commits into from
Jul 26, 2023

Conversation

brad-decker
Copy link
Contributor

@brad-decker brad-decker commented Jul 21, 2023

Explanation

  1. AutoSessionTracking defaults to true and operates by sending a session packet to sentry. This session packet does not appear to be filtered out via our beforeSend or FilterEvents integration. To avoid sending a request before we have the state tree and can validate the users preferences, we initiate this to false. Later, in startSession and endSession we modify this option and start the session or end the session manually. We also provide a toggleSession method that uses current state to determine whether to start or stop the session.

  2. In sentry-install we call toggleSession after the page loads and state is available, this handles initiating the session for a user who has opted into MetaMetrics. This script is ran in both the background and UI so it should be effective at starting the session in both places.

  3. In the MetaMetricsController the session is manually started or stopped when the user opts in or out of MetaMetrics. This occurs in the setParticipateInMetaMetrics function which is exposed to the UI via the MetaMaskController.

  4. In actions.ts, after successfully calling the setParticipateInMetaMetrics background method, we tell sentry to toggle the session details appropriately.

  5. Types for the global Sentry object, and the new methods added as part of this effort were added to global.d.ts in the types folder.

  6. Another minor change is changing the throwTestError method so that it takes an optional input for the message text so that the Dedupe filter of sentry doesn't catch it for testing purposes. Without debug enabled in the sentry options you are unaware that the message is being dropped by that handler.

  7. Added an INVERSE test case, asserting that requests are NOT sent to sentry when the flag is false.

Screenshots/Screencaps

Before

before.mov

After

after.mov

Manual Testing Steps With Existing Wallet

  1. yarn build dist -- dev mode does not have a sentry global object attached to it. If you want to use dev mode, you need to comment out the 'return undefined' line in setupSentry with the comment block that talks about the bug with sentry in dev mode.
  2. Go to settings -> security and privacy -> toggle participate in metametrics to false
  3. in full screen mode, open devtools and inspect the network tab.
  4. refresh the page and look for requests to sentry. There should be ZERO on this branch and ONE on develop.

The next steps only work on this branch:

  1. toggle metrics back on in settings.
  2. In the console. run window.stateHooks.throwTestError()
  3. See that the error is sent to sentry. (will only work once with no input, pass a string to throwTestError to get another error if you want to. Sentry dedupes the errors).
  4. toggle metrics back off in settings.
  5. in the console run window.stateHooks.throwTestError('foo')
  6. See error is not sent to sentry.

Test requests sent during onboarding

  1. yarn build:test (easier to get to a clean wallet)
  2. reset extension in plugins
  3. open extension for first time.
  4. On the welcome screen, open console and refresh.
  5. On develop: request sent to sentry, on this branch: no request.

Pre-merge author checklist

  • I've clearly explained:
    • What problem this PR is solving
    • How this problem was solved
    • How reviewers can test my changes
  • Sufficient automated test coverage has been added

Pre-merge reviewer checklist

  • Manual testing (e.g. pull and build branch, run in browser, test code being changed)
  • PR is linked to the appropriate GitHub issue
  • IF this PR fixes a bug in the release milestone, add this PR to the release milestone

If further QA is required (e.g. new feature, complex testing steps, large refactor), add the Extension QA Board label.

In this case, a QA Engineer approval will be be required.

@brad-decker brad-decker marked this pull request as ready for review July 22, 2023 19:50
@brad-decker brad-decker requested a review from a team as a code owner July 22, 2023 19:50
Comment on lines 1622 to 1645
describe('#setParticipateInMetaMetrics', () => {
beforeAll(() => {
window.sentry = {
toggleSession: jest.fn(),
endSession: jest.fn(),
};
});
it('sets participateInMetaMetrics to true', async () => {
const store = mockStore();
const setParticipateInMetaMetricsStub = sinon
.stub()
.callsFake((_, cb) => cb());

background.getApi.returns({
setParticipateInMetaMetrics: setParticipateInMetaMetricsStub,
});

_setBackgroundConnection(background.getApi());

await store.dispatch(actions.setParticipateInMetaMetrics(true));
expect(setParticipateInMetaMetricsStub.calledOnceWith(true)).toBeTruthy();
expect(window.sentry.toggleSession).toHaveBeenCalled();
});
});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

No tests existed for setParticipateInMetaMetrics before. So this minimal test case covers the newly added code plus the happy path of enabling the flag.

@codecov
Copy link

codecov bot commented Jul 22, 2023

Codecov Report

Merging #20132 (78b278f) into develop (4927ee7) will decrease coverage by 0.03%.
Report is 1 commits behind head on develop.
The diff coverage is 12.90%.

@@             Coverage Diff             @@
##           develop   #20132      +/-   ##
===========================================
- Coverage    69.48%   69.45%   -0.03%     
===========================================
  Files          984      984              
  Lines        37328    37355      +27     
  Branches     10035    10049      +14     
===========================================
+ Hits         25935    25943       +8     
- Misses       11393    11412      +19     
Files Changed Coverage Δ
app/scripts/lib/setupSentry.js 44.22% <0.00%> (-8.63%) ⬇️
ui/index.js 0.00% <0.00%> (ø)
app/scripts/controllers/metametrics.js 79.80% <100.00%> (+0.13%) ⬆️
ui/store/actions.ts 42.23% <100.00%> (+0.34%) ⬆️

@metamaskbot
Copy link
Collaborator

Builds ready [949f93a]
Page Load Metrics (1563 ± 52 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint1102031342010
domContentLoaded14251931156310852
load14261931156310852
domInteractive14251931156310852
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 1.12 KiB (0.03%)
  • ui: 65 Bytes (0.00%)
  • common: 888 Bytes (0.02%)

* change may result in the FilterEvents integration no longer being
* necessary, but that should be tested and executed in a future PR.
*
* TODO: Potentially remove the FilterEvents integration.
Copy link
Contributor

Choose a reason for hiding this comment

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

[nit] If this TODO is outside of this task, can you please create a task for it and link it in this comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good, Pedro!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

#20174 -- ill update the comment in a bit

Copy link
Member

Choose a reason for hiding this comment

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

See here: #15677

The FilterEvents integration is used over beforeSend to workaround the incompatibility between beforeSend and Dedupe

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I'll close the issue and remove the change to beforeSend then

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The changes to beforeSend were reverted and the comment removed @pedronfigueiredo and @Gudahtt

@metamaskbot
Copy link
Collaborator

Builds ready [a16a65d]
Page Load Metrics (1558 ± 39 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint106159129157
domContentLoaded1434170715588139
load1434170715588139
domInteractive1434170715588139
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 1.12 KiB (0.03%)
  • ui: 65 Bytes (0.00%)
  • common: 888 Bytes (0.02%)

@brad-decker brad-decker force-pushed the controlled-sessions branch from a16a65d to 9dff5a3 Compare July 25, 2023 13:30
@metamaskbot
Copy link
Collaborator

Builds ready [9dff5a3]
Page Load Metrics (1664 ± 51 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint1203251514321
domContentLoaded15011886166310651
load15011886166410751
domInteractive15011886166310651
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 1.1 KiB (0.03%)
  • ui: 65 Bytes (0.00%)
  • common: 874 Bytes (0.02%)

@@ -146,6 +146,14 @@ describe('MetaMetricsController', function () {
const now = new Date();
let clock;
beforeEach(function () {
globalThis.sentry = {
startSession: sinon.fake(() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we use jest.fn() here to mock?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not without rewriting the tests to jest. Currently in mocha

app/scripts/lib/setupSentry.js Show resolved Hide resolved
app/scripts/lib/setupSentry.js Show resolved Hide resolved
Comment on lines +42 to +43
await driver.fill('#password', 'correct horse battery staple');
await driver.press('#password', driver.Key.ENTER);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
await driver.fill('#password', 'correct horse battery staple');
await driver.press('#password', driver.Key.ENTER);
await unlockWallet(driver)

await driver.press('#password', driver.Key.ENTER);
// Trigger error
driver.executeScript('window.stateHooks.throwTestError()');
driver.delay(3000);
Copy link
Contributor

Choose a reason for hiding this comment

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

Question: is there a way to bypass the delay here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No we need to wait sometime to give time for a network request to occur

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the inverse test, we wait for a specific network request. I want to give a similar pause but we can't wait for anything in particular.

Comment on lines 1631 to 1633
const setParticipateInMetaMetricsStub = sinon
.stub()
.callsFake((_, cb) => cb());
Copy link
Contributor

Choose a reason for hiding this comment

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

can we use jest here as well?

Copy link
Contributor Author

@brad-decker brad-decker Jul 25, 2023

Choose a reason for hiding this comment

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

Id rather do that as a larger part of fixing the actions test file to remove sinon. I copied patterns used for other background methods here. Do we have an issue to track dropping sinon?

Changed my mind after thinking about it. We do need to try and commit to making incremental changes when we do not have the time to do the more extreme work required. This was the right call here and it also helps to set an example for future extensions of a file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mmm... i might retract my statement. let me give it a stab and see how it looks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added the jest version and removed the additional sinon usage.

Copy link
Member

@weizman weizman left a comment

Choose a reason for hiding this comment

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

lgtm

@metamaskbot
Copy link
Collaborator

Builds ready [78b278f]
Page Load Metrics (1596 ± 53 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint1193081604823
domContentLoaded14361917159611153
load14361917159611153
domInteractive14361915159611153
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 1.1 KiB (0.03%)
  • ui: 65 Bytes (0.00%)
  • common: 874 Bytes (0.02%)

@pedronfigueiredo
Copy link
Contributor

LGTM

@brad-decker brad-decker merged commit e28db07 into develop Jul 26, 2023
@brad-decker brad-decker deleted the controlled-sessions branch July 26, 2023 12:13
@github-actions github-actions bot locked and limited conversation to collaborators Jul 26, 2023
@metamaskbot metamaskbot added the release-10.36.0 Issue or pull request that will be included in release 10.36.0 label Jul 26, 2023
@Gudahtt Gudahtt added release-11.1.0 Issue or pull request that will be included in release 11.1.0 and removed release-10.36.0 Issue or pull request that will be included in release 10.36.0 labels Sep 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
release-11.1.0 Issue or pull request that will be included in release 11.1.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants