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

Sentry middleware for Zustand #5430

Closed
Thanaen opened this issue Jul 18, 2022 · 9 comments
Closed

Sentry middleware for Zustand #5430

Thanaen opened this issue Jul 18, 2022 · 9 comments

Comments

@Thanaen
Copy link

Thanaen commented Jul 18, 2022

Problem Statement

Hello,
Today, Sentry makes available a method to create a redux store enhancer easily, and thus log actions and values of a redux state.
There is no equivalent for the Zustand library yet.

Solution Brainstorm

Do you think it would be possible to create, in the same philosophy, a middleware for the Zustand library (https://github.com/pmndrs/zustand) that would log the actions and the last value of the state?

According to the Zustand documentation, it seems that it is necessary to use middlewares for that (https://github.com/pmndrs/zustand/blob/main/docs/typescript.md#using-middlewares)

I'll try to tinker with one on my side, but having an "official" one would make me feel better 😄. It might not be necessary to integrate it as code in the sentry-javascript package, it could just be written in a wiki or documentation.

Thanks a lot in advance

@AbhiPrasad
Copy link
Member

AbhiPrasad commented Jul 18, 2022

Hey, sounds like a great idea! I would first recommend making an unofficial one, we can help you review code and give feedback. After that gets stable we can think about how to move it into the monorepo :)

@Thanaen
Copy link
Author

Thanaen commented Jul 22, 2022

Here's my first attempt: https://gist.github.com/Thanaen/abfcc669f28765651f8e0ff59ea62527

I wasn't able to log actions for this first version, only the latest scope value is added in Sentry's scope context.

@AbhiPrasad
Copy link
Member

AbhiPrasad commented Jul 26, 2022

Looks great @Thanaen. Having the latest scope value I think is the correct behaviour.

As per https://develop.sentry.dev/sdk/event-payloads/contexts/#state-context, we should probably make the state context call like so:

scope.setContext('state', { type: 'zustand', value: transformedState });

I just realized that our redux integration still uses the old way

scope.setContext(STATE_CONTEXT_KEY, transformedState);

lemme fix that real quick! (more details about that here: getsentry/sentry#20184)

@Thanaen
Copy link
Author

Thanaen commented Jul 26, 2022

Great, thanks for the code review!
I will update it to reflect your comment.

Glad I could reveal a possible improvement for the redux integration! I was inspired by the source code of this integration to write my code 😁

@AbhiPrasad
Copy link
Member

Awesome. I think as a next step, we gotta look at 2 things. First, how we can add breadcrumbs so there's a breadcrumb on action. Then, we gotta add some reasonable defaults for things - basically options to control how breadcrumbs get created, and how what part of the state gets in context. To get some direction, we can get inspired by the Redux integration's options: https://docs.sentry.io/platforms/javascript/guides/react/configuration/integrations/redux/#redux-enhancer-options

@AbhiPrasad
Copy link
Member

Also, highly encourage you to publish this as a package and get some feedback that way! Getting stuff into the main Sentry repo might be a slow process, so at least then you can see what other folks think (and get some well deserved recognition).

@Thanaen
Copy link
Author

Thanaen commented Jul 29, 2022

Alright, I'll try to make it a package then!
It'll give me a chance to explore the process from start to finish 😁

Thanks for the feedback!

@Thanaen
Copy link
Author

Thanaen commented Aug 1, 2022

I just created the package!
It's a very very rough first version, but I'll try to improve it with time 😄
https://github.com/Thanaen/sentry-zustand-middleware
https://www.npmjs.com/package/sentry-zustand-middleware

@AbhiPrasad
Copy link
Member

AbhiPrasad commented Aug 2, 2022

Great work @Thanaen! Feel free to open a PR against our docs repo to add your package to the community supported list: https://github.com/getsentry/sentry-docs/blob/master/src/platforms/index.mdx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants