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(core): Send Baggage in Envelope Header #5104

Merged
merged 11 commits into from
May 18, 2022
Merged

Conversation

Lms24
Copy link
Member

@Lms24 Lms24 commented May 13, 2022

This PR introduces the first step of getting Dynamic Sampling working with the JS SDKs. It contains a very simple approach of attaching baggage to the envelope header of a transaction's event envelope. It does not take immutability of baggage in multiple transactions per trace scenarios into account. This is something we have to address when we're going to add the baggage header to outgoing requests. However, the simple approach should be enough to send baggage information to relay to get DS working on individual transactions.

Open question which I'm happy to address in this PR or in follow-ups:

  • Should we hide this behind an experimental flag?
  • I added more properties to be sent than introduced in feat(utils): Introduce Baggage API #5066. My reasoning: At the time of creating the baggage for envelopes, we - theoretically and if provided by the user - have all the data we need. As long as we're not caring about immutability after the first baggage creation in the trace, we're safe to add the other properties as well. Also, this might give the DS teams more testing opportunities on single transactions.

ref: https://getsentry.atlassian.net/browse/WEB-919

@Lms24 Lms24 changed the title feat(utils): Introduce Baggage API feat(core): Send baggage in envelope header May 13, 2022
@Lms24 Lms24 changed the title feat(core): Send baggage in envelope header feat(core): Send Baggage in Envelope Header May 13, 2022
@github-actions
Copy link
Contributor

github-actions bot commented May 13, 2022

size-limit report 📦

Path Size
@sentry/browser - ES5 CDN Bundle (gzipped + minified) 18.96 KB (-5.88% 🔽)
@sentry/browser - ES5 CDN Bundle (minified) 58.67 KB (-9.21% 🔽)
@sentry/browser - ES6 CDN Bundle (gzipped + minified) 17.82 KB (-5.53% 🔽)
@sentry/browser - ES6 CDN Bundle (minified) 52.57 KB (-9.32% 🔽)
@sentry/browser - Webpack (gzipped + minified) 19.55 KB (-15.85% 🔽)
@sentry/browser - Webpack (minified) 62.03 KB (-24.1% 🔽)
@sentry/react - Webpack (gzipped + minified) 19.58 KB (-15.9% 🔽)
@sentry/nextjs Client - Webpack (gzipped + minified) 43.03 KB (-10.46% 🔽)
@sentry/browser + @sentry/tracing - ES5 CDN Bundle (gzipped + minified) 24.6 KB (-5.67% 🔽)
@sentry/browser + @sentry/tracing - ES6 CDN Bundle (gzipped + minified) 23.17 KB (-5.37% 🔽)

@Lms24 Lms24 added this to the Dynamic Sampling milestone May 17, 2022
@Lms24 Lms24 force-pushed the lms-baggage-in-envelope-headers branch from 708a608 to 40da8e7 Compare May 17, 2022 09:15
const session = scope && scope.getSession && scope.getSession();
const session = scope && scope.getSession();
Copy link
Member Author

Choose a reason for hiding this comment

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

This doesn't have anything to do with baggage but it's a no longer necessary check I stumbled across. See this conversation for context

Copy link
Contributor

Choose a reason for hiding this comment

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

Btw, why not use scope?.getSession() notation?

Copy link
Member

Choose a reason for hiding this comment

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

Great question!

It's because we try to optimize our codebase around targeting es6. Optional Chaining is a newer JS feature, ES2020 - "ES11" as per: https://gist.github.com/rajaramtt/7df3702a04c644b0b62c9a64f48f3dbf#64-optional-chaining. This means that it'll have to be down-compiled to es6 compatible code when we generate the final assets to distribute on npm and the CDN (this is for max browser + node version support).

Something like so:

if (hey?.me) {
 console.log('me'); 
}

ends up getting down-compiled to:

if (hey !== null && hey !== void 0 && hey.me) {
  console.log('me');
}

See the babel demo.

which is way more bytes, which is bad for bundle size.

We made a decision as a result to not use any optional chaining in code that might be used in browser environments. See some examples on bundle wins we made here: https://github.com/getsentry/sentry-javascript/pulls?q=is%3Apr+optional+chaining+is%3Aclosed+milestone%3A%22Tree+shaking+%2F+Bundle+Size%22

@Lms24 Lms24 force-pushed the lms-baggage-in-envelope-headers branch from 40da8e7 to cf5fe65 Compare May 17, 2022 09:25
@AbhiPrasad
Copy link
Member

#5066 has been merged, I think we can rebase this accordingly.

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.

🚀

packages/core/src/envelope.ts Outdated Show resolved Hide resolved
packages/core/src/envelope.ts Outdated Show resolved Hide resolved
@AbhiPrasad
Copy link
Member

Should we hide this behind an experimental flag

In Relay we allow for arbitrary headers for forwards compatibility reasons, so I think we should be fine to send these: https://github.com/getsentry/relay/blob/7e63150f486fa6cd35db599aef6287040601f8e7/relay-server/src/envelope.rs#L705

@Lms24 Lms24 force-pushed the lms-baggage-in-envelope-headers branch from 0e2fe8c to 8bcf2a6 Compare May 17, 2022 16:14
@Lms24 Lms24 marked this pull request as ready for review May 17, 2022 16:14
@Lms24
Copy link
Member Author

Lms24 commented May 17, 2022

Thanks for the review, I'll address the feedback first thing tomorrow morning.

@Lms24
Copy link
Member Author

Lms24 commented May 18, 2022

In Relay we allow for arbitrary headers for forwards compatibility reasons, so I think we should be fine to send these

Ahh right, that makes sense. Then I think for now we're good. I was Just thinking that we might have to revisit this when we're adding the header to outgoing requests (due to the potential CORS problems we talked about). If we decide hide the baggage header in outgoing requests behind an experimental flag for v7, we should discuss if we want to continue sending baggage data in the envelope header when the flag is not set. Don't know what this entails in terms of consistency with traces/transactions. But this is a discussion for another time.

Anyway, I implemented the suggestions and I think this should be good to get merged in. Do you wanna take another look @AbhiPrasad ?

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.

I was Just thinking that we might have to revisit this when we're adding the header to outgoing requests (due to the potential CORS problems we talked about).

Yes, let’s bring this up as a TSC topic actually.

Everything else looks good for me to ship

@Lms24 Lms24 merged commit d478c77 into 7.x May 18, 2022
@Lms24 Lms24 deleted the lms-baggage-in-envelope-headers branch May 18, 2022 12:54
AbhiPrasad pushed a commit that referenced this pull request May 30, 2022
This patch Introduces the first step of getting Dynamic Sampling working with the JS SDKs. It adds a very simple approach of attaching baggage to the envelope header of a transaction's event envelope. It does not take immutability of baggage in multiple transactions per trace scenarios into account. This is something we have to address when we're going to add the baggage header to outgoing requests. However, the simple approach should be enough to send baggage information to relay to get DS working on individual transactions.
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.

3 participants