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

fix(metrics): Check for cls entry sources #3775

Merged
merged 2 commits into from
Jul 5, 2021
Merged

Conversation

AbhiPrasad
Copy link
Member

The LayoutShift.sources field may not exist in certain browsers, even if
the LayoutShift API is there. This PR patches logic that adds CLS data
to make sure that ._clsEntry.sources exists.

Ref: https://developer.mozilla.org/en-US/docs/Web/API/LayoutShift

See Sentry issue: https://sentry.io/share/issue/ebd52ddcc2c64a91b61e7a5486ec71bc/

The LayoutShift.sources field may not exist in certain browsers, even if
the LayoutShift API is there. This PR patches logic that adds CLS data
to make sure that `._clsEntry.sources` exists.

Ref: https://developer.mozilla.org/en-US/docs/Web/API/LayoutShift
Copy link
Member

@lobsterkatie lobsterkatie left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -214,7 +214,8 @@ export class MetricsInstrumentation {
transaction.setTag('lcp.size', this._lcpEntry.size);
}

if (this._clsEntry) {
// See: https://developer.mozilla.org/en-US/docs/Web/API/LayoutShift
if (this._clsEntry && this._clsEntry.sources) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (this._clsEntry && this._clsEntry.sources) {
if (this._clsEntry?.sources) {

The tiniest of nits. I just really like optional chaining. 😛

Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure it's worth the extra code generated from typescript: https://www.typescriptlang.org/play?ts=3.7.5#code/MYewdgzgLgBFAWAuGBvGB9YAbCBRMUATgJ4D8yaEIArocAKYTkzSECWYA5gNoC6MAX0EwAvKgEBYAFDSA9LJgB5AA5Q24AIZYYAYXgaOHTtLYAzGAAoEAOkw58RMtaq0GEAJTjpchQCV6nNRYGoQwoGAAJmxq4CbmVvC22HgEJDAAZOlwiXYpjs40dIyeKJIyUkA, especially if every byte counts.

Thought that is why we shied away from optional chaining outside of tests?

Copy link
Contributor

Choose a reason for hiding this comment

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

We kinda started to use it out of convenience tbh. It's a shame that only ES2020 benefits from its smaller footprint, because we don't target it yet. I don't like it, but we might want to verify what's the impact of it and if it's unreasonable, us eslint to prevent usage :(

Copy link
Contributor

Choose a reason for hiding this comment

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

image (with/without chaining)

@github-actions
Copy link
Contributor

github-actions bot commented Jun 30, 2021

size-limit report

Path Size
@sentry/browser - CDN Bundle (gzipped) 21.39 KB (0%)
@sentry/browser - Webpack 22.4 KB (0%)
@sentry/react - Webpack 22.44 KB (0%)
@sentry/browser + @sentry/tracing - CDN Bundle (gzipped) 28.89 KB (+0.02% 🔺)

Copy link
Contributor

@rhcarvalho rhcarvalho left a comment

Choose a reason for hiding this comment

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

Testing this specifically might not be worth the effort, but this change together with other recent changes to web vitals suggest we could be more defensive and have try...catch around potentially breaking code. The outcome would be silencing and perhaps taking more time to find problems like this one, so I'm not 100% sure that'd be a good idea. Thoughts?

@kamilogorek
Copy link
Contributor

The outcome would be silencing and perhaps taking more time to find problems like this one, so I'm not 100% sure that'd be a good idea

Tbh I think it'd not be silencing anything, as this API is just not available on some envs anyway. It might silence some capturing code down the call stack though.

@rhcarvalho
Copy link
Contributor

Tbh I think it'd not be silencing anything, as this API is just not available on some envs anyway. It might silence some capturing code down the call stack though.

If done at an inappropriate level of granularity, it could accidentally silence SDK bugs. We would have no visibility into how many times a piece of instrumentation did its job vs threw an exception and moved on.

@kamilogorek
Copy link
Contributor

That's what I said in second sentence :P

@AbhiPrasad AbhiPrasad enabled auto-merge (squash) July 5, 2021 12:00
@AbhiPrasad AbhiPrasad merged commit c95d058 into master Jul 5, 2021
@AbhiPrasad AbhiPrasad deleted the abhi-clsentry-check branch July 5, 2021 12:11
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

5 participants