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(core): Make beforeSend handling defensive for different event types #6507

Merged
merged 8 commits into from
Dec 14, 2022

Conversation

mydea
Copy link
Member

@mydea mydea commented Dec 13, 2022

Our current handling of beforeSend is not very defensive, in the sense that it will just use beforeSend for anything that is not a transaction. So e.g. for profiles, or replays, if they ever go through this code path (which replay currently isn't, but may in the near future, not sure about profiles), this is subject to break subtly.

This PR refactors this to be more defensive, by explicitly calling the respective callback. While at it, I also narrowed the type for the handlers a bit.

@mydea mydea self-assigned this Dec 13, 2022
const beforeSendProcessor = options[beforeSendProcessorName];
const isError = !event.type;
const eventType = event.type || 'error';
const beforeSendLabel = `before send for type \`${eventType}\``;
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 completely sure about this... I figured this is the most "compact" solution. Other options:

  1. Make a map - but how to handle "unknown" beforeSend stuff (e.g. if type === 'profile', what should this be?
  2. Auto-build this like beforeSend${event.type ? capitzalize(event.type) : ''} - same issue as before, this would become beforeSendProfile which doesn't really exist as of now I guess.

Copy link
Member

Choose a reason for hiding this comment

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

I think this approach is fine

@github-actions
Copy link
Contributor

github-actions bot commented Dec 13, 2022

size-limit report 📦

Path Size
@sentry/browser - ES5 CDN Bundle (gzipped + minified) 19.78 KB (+0.29% 🔺)
@sentry/browser - ES5 CDN Bundle (minified) 61.27 KB (+0.24% 🔺)
@sentry/browser - ES6 CDN Bundle (gzipped + minified) 18.57 KB (+0.32% 🔺)
@sentry/browser - ES6 CDN Bundle (minified) 54.8 KB (+0.28% 🔺)
@sentry/browser - Webpack (gzipped + minified) 20.34 KB (+0.29% 🔺)
@sentry/browser - Webpack (minified) 66.48 KB (+0.24% 🔺)
@sentry/react - Webpack (gzipped + minified) 20.36 KB (+0.28% 🔺)
@sentry/nextjs Client - Webpack (gzipped + minified) 47.57 KB (+0.13% 🔺)
@sentry/browser + @sentry/tracing - ES5 CDN Bundle (gzipped + minified) 26.76 KB (+0.25% 🔺)
@sentry/browser + @sentry/tracing - ES6 CDN Bundle (gzipped + minified) 25.19 KB (+0.2% 🔺)
@sentry/replay ES6 CDN Bundle (gzipped + minified) 41.87 KB (-0.01% 🔽)
@sentry/replay - Webpack (gzipped + minified) 38.1 KB (0%)

@@ -628,13 +630,14 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
}

const isTransaction = event.type === 'transaction';
const beforeSendProcessorName = isTransaction ? 'beforeSendTransaction' : 'beforeSend';
const beforeSendProcessor = options[beforeSendProcessorName];
const isError = !event.type;
Copy link
Member Author

Choose a reason for hiding this comment

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

The alternative implementation would be IMHO:

if (!isTransaction && !isError) {
  // skip...
}

This would probably by smaller in footprint, but we may need to revisit this if/when we also want these hooks for e.g. replay or profiles.

Copy link
Member

Choose a reason for hiding this comment

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

hmm probably missing something but why not use the new isErrorEvent function?

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!

packages/types/src/event.ts Outdated Show resolved Hide resolved
Copy link
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

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

I like it, especially the new processBeforeSend function. Makes things a lot clearer

Comment on lines +827 to +833
function isErrorEvent(event: Event): event is ErrorEvent {
return event.type === undefined;
}

function isTransactionEvent(event: Event): event is TransactionEvent {
return event.type === 'transaction';
}
Copy link
Member

Choose a reason for hiding this comment

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

Random thought: We have this is.ts file in utills. WDYT, does it make sense to move them there?

Copy link
Member Author

Choose a reason for hiding this comment

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

hmm, not sure, that could be confusing, as there it is a lot about built ins etc. there is actually also this in there:

export function isErrorEvent(wat: unknown): boolean {
  return isBuiltin(wat, 'ErrorEvent');
}

but that checks something else I guess 😅

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.

good change!

@mydea mydea force-pushed the fn/before-send-defensive branch 2 times, most recently from d170dbf to 2a95f58 Compare December 14, 2022 08:12
@mydea mydea merged commit 5f14111 into master Dec 14, 2022
@mydea mydea deleted the fn/before-send-defensive branch December 14, 2022 14:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants