-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Changes from 3 commits
2172be8
2b3735a
cd754fa
4a7e091
6df9fb4
8a25268
dadf492
578da07
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { configureScope, getCurrentHub } from '@sentry/browser'; | ||
import { addGlobalEventProcessor, configureScope, getCurrentHub } from '@sentry/browser'; | ||
import type { Scope } from '@sentry/types'; | ||
import { addNonEnumerableProperty } from '@sentry/utils'; | ||
|
||
|
@@ -49,6 +49,12 @@ type StoreEnhancerStoreCreator<Ext = Record<string, unknown>, StateExt = never> | |
) => Store<ExtendState<S, StateExt>, A, StateExt, Ext> & Ext; | ||
|
||
export interface SentryEnhancerOptions<S = any> { | ||
/** | ||
* Redux state in attachments or not. | ||
* @default true | ||
*/ | ||
attachReduxState?: boolean; | ||
|
||
/** | ||
* Transforms the state before attaching it to an event. | ||
* Use this to remove any private data before sending it to Sentry. | ||
|
@@ -71,6 +77,7 @@ const ACTION_BREADCRUMB_CATEGORY = 'redux.action'; | |
const ACTION_BREADCRUMB_TYPE = 'info'; | ||
|
||
const defaultOptions: SentryEnhancerOptions = { | ||
attachReduxState: true, | ||
actionTransformer: action => action, | ||
stateTransformer: state => state || null, | ||
}; | ||
|
@@ -89,6 +96,22 @@ function createReduxEnhancer(enhancerOptions?: Partial<SentryEnhancerOptions>): | |
|
||
return (next: StoreEnhancerStoreCreator): StoreEnhancerStoreCreator => | ||
<S = any, A extends Action = AnyAction>(reducer: Reducer<S, A>, initialState?: PreloadedState<S>) => { | ||
options.attachReduxState && | ||
addGlobalEventProcessor((event, hint) => { | ||
if ( | ||
event.contexts && | ||
event.contexts.state && | ||
event.contexts.state.state && | ||
event.contexts.state.state.type === 'redux' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if we add an extra condition, such as There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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. |
||
) { | ||
hint.attachments = [ | ||
...(hint.attachments || []), | ||
{ filename: 'redux_state.json', data: JSON.stringify(event.contexts.state.state.value) }, | ||
]; | ||
} | ||
return event; | ||
}); | ||
|
||
const sentryReducer: Reducer<S, A> = (state, action): S => { | ||
const newState = reducer(state, action); | ||
|
||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright let's after we get some tests in @malay44!