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

feat(redux): Add 'attachReduxState' option #8953

Merged
merged 8 commits into from Sep 11, 2023

Conversation

malay44
Copy link
Contributor

@malay44 malay44 commented Sep 6, 2023

This pull request introduces a new configuration option, attachReduxState, with a default value of true. This option lets users control whether the Redux state should automatically attach to Sentry events.

Changes Made:

  • Added attachReduxState configuration option, default true.
  • Implemented attachments to store the Redux state when attachReduxState is set to true.
  • Added tests to verify that the attachment logic correctly attaches the Redux state when available, avoids it when it's empty or undefined, and verifies that it's of the correct type, redux.

Motivation:
By default, the 'attachReduxState' option is set to true, enabling the attachment of the Redux state to Sentry events. This enhancement improves error tracking for Redux-based applications.

Testing:
I have thoroughly tested these changes and confirmed that they work as intended.

Related Issues:
Closes GH-6266

Screenshots:
Screenshot 2023-09-06 at 10 58 07 AM

malay44 and others added 2 commits September 6, 2023 10:36
Introduces 'attachReduxState' (default: true) for
controlling Redux state attachment to Sentry events.

Fixes getsentryGH-6266
HazAT
HazAT previously requested changes Sep 6, 2023
Copy link
Member

@HazAT HazAT left a comment

Choose a reason for hiding this comment

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

This is awesome, looking forward merging this!
Maybe we can add a small test for this, should be too hard since we already have quite a few tests for Redux.

packages/react/src/redux.ts Outdated Show resolved Hide resolved
@malay44
Copy link
Contributor Author

malay44 commented Sep 6, 2023

Thanks for the positive feedback! I'll add a test for this feature and let you know the progress. 🤗

This is awesome, looking forward merging this! Maybe we can add a small test for this, should be too hard since we already have quite a few tests for Redux.

packages/react/src/redux.ts Outdated Show resolved Hide resolved
@@ -71,6 +77,7 @@ const ACTION_BREADCRUMB_CATEGORY = 'redux.action';
const ACTION_BREADCRUMB_TYPE = 'info';

const defaultOptions: SentryEnhancerOptions = {
attachReduxState: true,
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure we can default this to be true because attachments have their own quota. Thoughts @HazAT?

Copy link
Member

Choose a reason for hiding this comment

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

hmm I think it's fine - Redux while popular is not in every React app - and this feature is very helpful.

Copy link
Member

Choose a reason for hiding this comment

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

Alright let's :shipit: after we get some tests in @malay44!

@malay44 malay44 force-pushed the feature/redux-state-attachment branch 3 times, most recently from 35fa934 to 2b3735a Compare September 6, 2023 22:12
Refactors the code responsible for attaching the Redux
state to Sentry events. The change enhances
efficiency by avoiding the addition of empty JSON
attachments when there is no Redux state available.
event.contexts &&
event.contexts.state &&
event.contexts.state.state &&
event.contexts.state.state.type === 'redux'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

What if we add an extra condition, such as event instanceof ErrorEvent ? I noticed that when I was debugging in the browser, it was also attaching the Redux state when the event type was 'transaction'

Copy link
Member

Choose a reason for hiding this comment

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

yup we should do this.

to filter for just errors you can add event.type === undefined. The reason there is no error type is because of backwards compatibility 😢 (error is undefined, all other events have a type).

Also, if you put this whole block into a try catch you can skip this deep nested check, which helps save bundle size:

try {
  // @ts-expect-error try catch to reduce bundle size
  if (event.type === undefined && event.contexts.state.state.type === 'redux') {
    hint.attachments = [
      ...(hint.attachments || []),
      // @ts-expect-error try catch to reduce bundle size
      { filename: 'redux_state.json', data: JSON.stringify(event.contexts.state.state.value) },
    ];
  }
} catch (_) {
  // empty
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, if you put this whole block into a try catch you can skip this deep nested check, which helps save bundle size:

This article is fantastic! I've learned a lot while working on this issue. Thank you all so much for your guidance; I appreciate it.

I'm eager to take on more challenging issues. Please consider assigning some to me. I'm looking forward to learning and growing while contributing to the project.

New test cases to validate the attachment of Redux
state to Sentry events. The test ensures that the
attachment logic correctly adds the Redux state when
available and does not add it when it's empty or
undefined and when not of type redux.
Copy link
Member

@AbhiPrasad AbhiPrasad left a comment

Choose a reason for hiding this comment

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

thanks for adding tests!

packages/react/test/reduxStateAttachments.test.ts Outdated Show resolved Hide resolved
Comment on lines 35 to 45
Sentry.configureScope(scope => {
expect(scope.getAttachments()).toContainEqual(
expect.objectContaining({
filename: 'redux_state.json',
data: JSON.stringify({
value: 'updated',
}),
}),
);
});
});
Copy link
Member

Choose a reason for hiding this comment

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

We can access the attachments on the scope via Sentry.getCurrentHub().getScope().getAttachments() - let's do that for the rest of the uses of configureScope as well.

This comment was marked as resolved.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I discovered today that I made an error when writing the tests. They currently pass for every scenario. I'll investigate this issue further and let you know how I'm doing.

@malay44 malay44 force-pushed the feature/redux-state-attachment branch from dff0eb2 to 4a7e091 Compare September 8, 2023 09:07
Refined the test suite for Redux state attachment by:
1. Removing an incorrect test from`reduxStateAttachments.test.ts`.
2. Adding a new test case to `redux.ts` to verify that the attachment
    logic correctly attaches the Redux state when available, avoids it
    when it's empty or undefined, and verifies that it's of the correct
    type 'redux'.
Copy link
Member

@AbhiPrasad AbhiPrasad left a comment

Choose a reason for hiding this comment

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

let's make the redux state only attach for errors and add a test for this, and we are good to merge.

event.contexts &&
event.contexts.state &&
event.contexts.state.state &&
event.contexts.state.state.type === 'redux'
Copy link
Member

Choose a reason for hiding this comment

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

yup we should do this.

to filter for just errors you can add event.type === undefined. The reason there is no error type is because of backwards compatibility 😢 (error is undefined, all other events have a type).

Also, if you put this whole block into a try catch you can skip this deep nested check, which helps save bundle size:

try {
  // @ts-expect-error try catch to reduce bundle size
  if (event.type === undefined && event.contexts.state.state.type === 'redux') {
    hint.attachments = [
      ...(hint.attachments || []),
      // @ts-expect-error try catch to reduce bundle size
      { filename: 'redux_state.json', data: JSON.stringify(event.contexts.state.state.value) },
    ];
  }
} catch (_) {
  // empty
}

packages/types/src/context.ts Outdated Show resolved Hide resolved
Copy link
Member

@AbhiPrasad AbhiPrasad left a comment

Choose a reason for hiding this comment

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

Thanks @malay44!

@AbhiPrasad AbhiPrasad enabled auto-merge (squash) September 11, 2023 18:52
@AbhiPrasad AbhiPrasad merged commit 595e4e2 into getsentry:develop Sep 11, 2023
68 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use attachments to store the Redux state
3 participants