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): Add beforeSendSpan hook (#11886) #11918

Merged
merged 4 commits into from
May 16, 2024
Merged

Conversation

andreiborza
Copy link
Member

No description provided.

Copy link
Contributor

github-actions bot commented May 6, 2024

size-limit report 📦

Path Size
@sentry/browser 21.72 KB (+0.19% 🔺)
@sentry/browser (incl. Tracing) 32.86 KB (+0.28% 🔺)
@sentry/browser (incl. Tracing, Replay) 68.21 KB (+0.17% 🔺)
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 61.6 KB (+0.16% 🔺)
@sentry/browser (incl. Tracing, Replay with Canvas) 72.25 KB (+0.16% 🔺)
@sentry/browser (incl. Tracing, Replay, Feedback) 84.25 KB (+0.14% 🔺)
@sentry/browser (incl. Feedback) 37.68 KB (+0.12% 🔺)
@sentry/browser (incl. sendFeedback) 26.29 KB (+0.16% 🔺)
@sentry/browser (incl. FeedbackAsync) 30.66 KB (+0.14% 🔺)
@sentry/react 24.41 KB (+0.15% 🔺)
@sentry/react (incl. Tracing) 35.85 KB (+0.32% 🔺)
@sentry/vue 25.65 KB (+0.48% 🔺)
@sentry/vue (incl. Tracing) 34.68 KB (+0.36% 🔺)
@sentry/svelte 21.86 KB (+0.2% 🔺)
CDN Bundle 24.26 KB (+0.17% 🔺)
CDN Bundle (incl. Tracing) 34.29 KB (+0.35% 🔺)
CDN Bundle (incl. Tracing, Replay) 67.99 KB (+0.19% 🔺)
CDN Bundle (incl. Tracing, Replay, Feedback) 72.95 KB (+0.15% 🔺)
CDN Bundle - uncompressed 71.33 KB (+0.18% 🔺)
CDN Bundle (incl. Tracing) - uncompressed 101.68 KB (+0.33% 🔺)
CDN Bundle (incl. Tracing, Replay) - uncompressed 211.3 KB (+0.16% 🔺)
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 223.6 KB (+0.15% 🔺)
@sentry/nextjs (client) 35.07 KB (+0.32% 🔺)
@sentry/sveltekit (client) 33.44 KB (+0.32% 🔺)
@sentry/node 141.06 KB (+0.03% 🔺)
@sentry/aws-serverless 128.07 KB (+0.03% 🔺)

@@ -109,6 +114,15 @@ export function createSpanEnvelope(spans: SentrySpan[]): SpanEnvelope {
sent_at: new Date().toISOString(),
...(dscHasRequiredProps(dsc) && { trace: dsc }),
};
const items = spans.map(span => createSpanEnvelopeItem(spanToJSON(span)));

const beforeSend = client && client.getOptions().beforeSendSpan;
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
const beforeSend = client && client.getOptions().beforeSendSpan;
const beforeSendSpan = client && client.getOptions().beforeSendSpan;

let's call it that, I was confused for a sec because we also have an actual beforeSend hook (for error events) 😅

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, not sure why I named it that way 😅 ...

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

Comment on lines 121 to 130
if (beforeSend) {
items = spans.map(span => createSpanEnvelopeItem(beforeSend(spanToJSON(span) as SpanJSON)));
} else {
items = spans.map(span => createSpanEnvelopeItem(spanToJSON(span)));
}
Copy link
Member

Choose a reason for hiding this comment

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

I'm not even sure if there is a difference, but feels like it could be a micro-bundlesize-improvement: What if we do this in a single loop, something like this:

const items = spans.map(span => {
  const spanJson = spanToJSON(span) as SpanJSON;
  return beforeSend ? beforeSend(spanJson) : spanJson;
});

added benefit is that we save a let items that we need to re-assign 🤔 but no strong feelings, it probably doesn't matter either way.

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 was considering that but thought it wastes cycles checking the same thing over and over again. Not sure how big spans gets tho. Thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

yeah, you're probably right!

What about a combination of both:

const convertToSpanJson = beforeSendSpan 
  ? (span: Span) => beforeSendSpan(spanToJSON(span) as SpanJSON)) 
  : (span: Span) => spanToJSON(span) as SpanJSON;
const items = spans.map(convertToSpanJson);

Copy link
Member Author

Choose a reason for hiding this comment

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

Yea nice, fixed.

@andreiborza andreiborza force-pushed the ab/before-send-span branch 3 times, most recently from a7df1ef to ff1193c Compare May 7, 2024 11:47
return beforeSendTransaction(event, hint);
if (isTransactionEvent(event)) {
if (event.spans && beforeSendSpan) {
event.spans = event.spans.map(span => beforeSendSpan(span));
Copy link
Member

Choose a reason for hiding this comment

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

Just a note for ourselves: Wait with merging this until we verified with the performance team that this is OK, if this is undefined, for example.

Copy link
Member

@mydea mydea left a comment

Choose a reason for hiding this comment

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

sweet, pretty awesome first PR (well, first since you officially joined the team ;)) 🚀 let's just wait for feedback from the performance team about how to handle/allow dropping spans etc!

@andreiborza andreiborza marked this pull request as draft May 7, 2024 12:22
*/
export function createSpanEnvelope(spans: SentrySpan[]): SpanEnvelope {
export function createSpanEnvelope(spans: SentrySpan[], client?: Client): SpanEnvelope | null {
Copy link
Member

Choose a reason for hiding this comment

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

technically speaking this is breaking (as we export it from @sentry/core) 🤔 it is unlikely that a user is using this, but... hmm, not sure - maybe it is "safer" to still always return an envelope, and then check (outside) to only send this envelope if there are any spans inside of it?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point, thanks!

I changed it to check the span items on the returned envelope before sending it and pulled everything down into the helper method for sending.

Copy link
Member

@mydea mydea left a comment

Choose a reason for hiding this comment

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

very nice!

@andreiborza andreiborza marked this pull request as ready for review May 16, 2024 13:55
@andreiborza andreiborza merged commit f1d1a16 into develop May 16, 2024
100 checks passed
@andreiborza andreiborza deleted the ab/before-send-span branch May 16, 2024 13:56
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