Skip to content

Commit

Permalink
add beforeSendTransaction option
Browse files Browse the repository at this point in the history
  • Loading branch information
lobsterkatie committed Nov 3, 2022
1 parent 82e4572 commit d8f5d51
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
21 changes: 13 additions & 8 deletions packages/core/src/baseclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -613,13 +613,17 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
* @returns A SyncPromise that resolves with the event or rejects in case event was/will not be send.
*/
protected _processEvent(event: Event, hint: EventHint, scope?: Scope): PromiseLike<Event> {
const { beforeSend, sampleRate } = this.getOptions();
const options = this.getOptions();
const { sampleRate } = options;

if (!this._isEnabled()) {
return rejectedSyncPromise(new SentryError('SDK not enabled, will not capture event.', 'log'));
}

const isTransaction = event.type === 'transaction';
const beforeSendProcessorName = isTransaction ? 'beforeSendTransaction' : 'beforeSend';
const beforeSendProcessor = options[beforeSendProcessorName];

// 1.0 === 100% events are sent
// 0.0 === 0% events are sent
// Sampling for transaction happens somewhere else
Expand All @@ -641,17 +645,17 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
}

const isInternalException = hint.data && (hint.data as { __sentry__: boolean }).__sentry__ === true;
if (isInternalException || isTransaction || !beforeSend) {
if (isInternalException || !beforeSendProcessor) {
return prepared;
}

const beforeSendResult = beforeSend(prepared, hint);
return _validateBeforeSendResult(beforeSendResult);
const beforeSendResult = beforeSendProcessor(prepared, hint);
return _validateBeforeSendResult(beforeSendResult, beforeSendProcessorName);
})
.then(processedEvent => {
if (processedEvent === null) {
this.recordDroppedEvent('before_send', event.type || 'error');
throw new SentryError('`beforeSend` returned `null`, will not send event.', 'log');
throw new SentryError(`\`${beforeSendProcessorName}\` returned \`null\`, will not send event.`, 'log');
}

const session = scope && scope.getSession();
Expand Down Expand Up @@ -764,12 +768,13 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
}

/**
* Verifies that return value of configured `beforeSend` is of expected type, and returns the value if so.
* Verifies that return value of configured `beforeSend` or `beforeSendTransaction` is of expected type, and returns the value if so.
*/
function _validateBeforeSendResult(
beforeSendResult: PromiseLike<Event | null> | Event | null,
beforeSendProcessorName: 'beforeSend' | 'beforeSendTransaction',
): PromiseLike<Event | null> | Event | null {
const invalidValueError = '`beforeSend` must return `null` or a valid event.';
const invalidValueError = `\`${beforeSendProcessorName}\` must return \`null\` or a valid event.`;
if (isThenable(beforeSendResult)) {
return beforeSendResult.then(
event => {
Expand All @@ -779,7 +784,7 @@ function _validateBeforeSendResult(
return event;
},
e => {
throw new SentryError(`beforeSend rejected with ${e}`);
throw new SentryError(`\`${beforeSendProcessorName}\` rejected with ${e}`);
},
);
} else if (!isPlainObject(beforeSendResult) && beforeSendResult !== null) {
Expand Down
13 changes: 13 additions & 0 deletions packages/types/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,19 @@ export interface ClientOptions<TO extends BaseTransportOptions = BaseTransportOp
*/
beforeSend?: (event: Event, hint: EventHint) => PromiseLike<Event | null> | Event | null;

/**
* An event-processing callback for error and message events, guaranteed to be invoked after all other event
* processors, which allows an event to be modified or dropped.
*
* Note that you must return a valid event from this callback. If you do not wish to modify the event, simply return
* it at the end. Returning `null` will cause the event to be dropped.
*
* @param event The error or message event generated by the SDK.
* @param hint Event metadata useful for processing.
* @returns A new event that will be sent | null.
*/
beforeSendTransaction?: (event: Event, hint: EventHint) => PromiseLike<Event | null> | Event | null;

/**
* A callback invoked when adding a breadcrumb, allowing to optionally modify
* it before adding it to future events.
Expand Down

0 comments on commit d8f5d51

Please sign in to comment.