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

ref(gatsby): Move SDK initialization logic to SDK #4040

Merged
merged 7 commits into from
Oct 11, 2021

Conversation

iker-barriocanal
Copy link
Member

Follow-up to #4033.

This PR refactors how the Gatsby plugin initializes the SDK. The initialization was done in the gatsby-browser script, and the Gatsby SDK was initializing the React SDK. This approach forces to use that script to initialize it, without providing any alternative.

The refactor provides another way to initialize the SDK - calling Sentry.init, as you do with the other SDKs. gatsby-browser will call this Sentry.init and initialize the Gatsby SDK, which initializes the React SDK under the hood; just as it was happening previously. For now, even if Sentry.init is exposed, the only way to do the initialization is through gatsby-browser.

Why this refactor?
The options defined in the user's gatsby-config are serialized. This means that only serializable options can be defined, excluding important SDK features, such as beforeSend. After this refactoring, additional features to support SDK initialization with non-serializable options will come.

@iker-barriocanal iker-barriocanal requested a review from a team October 8, 2021 11:46
@iker-barriocanal iker-barriocanal self-assigned this Oct 8, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Oct 8, 2021

size-limit report

Path Size
@sentry/browser - CDN Bundle (gzipped) 22.33 KB (-0.01% 🔽)
@sentry/browser - Webpack 23.31 KB (0%)
@sentry/react - Webpack 23.34 KB (0%)
@sentry/browser + @sentry/tracing - CDN Bundle (gzipped) 29.79 KB (0%)

packages/gatsby/src/utils/metadataBuilder.ts Outdated Show resolved Hide resolved
*/
export function init(options: GatsbyOptions): void {
new MetadataBuilder(options, ['gatsby']).addSdkMetadata();
const integrations = getIntegrationsFromOptions(options);
Copy link
Member

Choose a reason for hiding this comment

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

nit: This function can just be in the same file for now, it's just added overhead to keep it in a separate file

Copy link
Member Author

Choose a reason for hiding this comment

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

If you mean gatsby-browser, no. If we want to provide users the option to initialize the SDK from a place without gatsby-browser, this code must live in the SDK itself. Of course, this isn't required at the moment, but without that goal in mind, the whole PR isn't required at all. Moving it here is a refactoring, included it in this PR (and not in the feature PR that will follow-up) to make it easier to review and identify bugs.

Copy link
Member

Choose a reason for hiding this comment

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

No meaning getIntegrationsFromOptions from packages/gatsby/src/utils/integrations.ts can just be a part of packages/gatsby/src/sdk.ts. Agree with the change from gatsby-browser

* @param options The options users have defined.
*/
export function getIntegrationsFromOptions(options: GatsbyOptions): Integration[] {
const integrations = [...(options.integrations || [])];
Copy link
Member

Choose a reason for hiding this comment

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

integrations can be a function as well

Copy link
Member Author

Choose a reason for hiding this comment

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

Functions are non-serializable. The end goal is to provide a way to define these options. This refactoring is the previous step to that feature, which only supports non-serializable options. So no, at the moment they can't be functions.

import { GatsbyOptions } from './utils/types';

export const defaultOptions = {
autoSessionTracking: true,
Copy link
Member

Choose a reason for hiding this comment

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

This should be on by default now (since v6), we can rm -rf this

Copy link
Member Author

Choose a reason for hiding this comment

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

I want the behavior to be as close as possible to the state prior to this refactoring. I'll make another PR to remove these options.


export const defaultOptions = {
autoSessionTracking: true,
environment: process.env.NODE_ENV || 'development',
Copy link
Member

Choose a reason for hiding this comment

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

Tbh, should just be undefined by default

Copy link
Member Author

Choose a reason for hiding this comment

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

Same answer as previous.

"@testing-library/react": "^10.4.9",
"@types/node": "^16.10.3",
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need node types?

Copy link
Member Author

Choose a reason for hiding this comment

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

Linter was complaining about process, used in defaultOptions. Since they'll be removed, I can remove this dev dep too.

packages/gatsby/package.json Outdated Show resolved Hide resolved
packages/gatsby/gatsby-browser.js 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.

Cool, looking forward to the next PR

*/
export function init(options: GatsbyOptions): void {
new MetadataBuilder(options, ['gatsby']).addSdkMetadata();
const integrations = getIntegrationsFromOptions(options);
Copy link
Member

Choose a reason for hiding this comment

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

No meaning getIntegrationsFromOptions from packages/gatsby/src/utils/integrations.ts can just be a part of packages/gatsby/src/sdk.ts. Agree with the change from gatsby-browser

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.

None yet

2 participants