Skip to content

Commit 9e82822

Browse files
committed
add beforeSendTransaction option
1 parent 06c1a6f commit 9e82822

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

packages/core/src/baseclient.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -613,13 +613,17 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
613613
* @returns A SyncPromise that resolves with the event or rejects in case event was/will not be send.
614614
*/
615615
protected _processEvent(event: Event, hint: EventHint, scope?: Scope): PromiseLike<Event> {
616-
const { beforeSend, sampleRate } = this.getOptions();
616+
const options = this.getOptions();
617+
const { sampleRate } = options;
617618

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

622623
const isTransaction = event.type === 'transaction';
624+
const beforeSendProcessorName = isTransaction ? 'beforeSendTransaction' : 'beforeSend';
625+
const beforeSendProcessor = options[beforeSendProcessorName];
626+
623627
// 1.0 === 100% events are sent
624628
// 0.0 === 0% events are sent
625629
// Sampling for transaction happens somewhere else
@@ -641,17 +645,17 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
641645
}
642646

643647
const isInternalException = hint.data && (hint.data as { __sentry__: boolean }).__sentry__ === true;
644-
if (isInternalException || isTransaction || !beforeSend) {
648+
if (isInternalException || !beforeSendProcessor) {
645649
return prepared;
646650
}
647651

648-
const beforeSendResult = beforeSend(prepared, hint);
649-
return _ensureBeforeSendRv(beforeSendResult);
652+
const beforeSendResult = beforeSendProcessor(prepared, hint);
653+
return _ensureBeforeSendRv(beforeSendResult, beforeSendProcessorName);
650654
})
651655
.then(processedEvent => {
652656
if (processedEvent === null) {
653657
this.recordDroppedEvent('before_send', event.type || 'error');
654-
throw new SentryError('`beforeSend` returned `null`, will not send event.', 'log');
658+
throw new SentryError(`\`${beforeSendProcessorName}\` returned \`null\`, will not send event.`, 'log');
655659
}
656660

657661
const session = scope && scope.getSession();
@@ -764,10 +768,13 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
764768
}
765769

766770
/**
767-
* Verifies that return value of configured `beforeSend` is of expected type.
771+
* Verifies that return value of configured `beforeSend` or `beforeSendTransaction` is of expected type.
768772
*/
769-
function _ensureBeforeSendRv(rv: PromiseLike<Event | null> | Event | null): PromiseLike<Event | null> | Event | null {
770-
const nullErr = '`beforeSend` method has to return `null` or a valid event.';
773+
function _ensureBeforeSendRv(
774+
rv: PromiseLike<Event | null> | Event | null,
775+
beforeSendProcessorName: string,
776+
): PromiseLike<Event | null> | Event | null {
777+
const nullErr = `\`${beforeSendProcessorName}\` must return \`null\` or a valid event.`;
771778
if (isThenable(rv)) {
772779
return rv.then(
773780
event => {
@@ -777,7 +784,7 @@ function _ensureBeforeSendRv(rv: PromiseLike<Event | null> | Event | null): Prom
777784
return event;
778785
},
779786
e => {
780-
throw new SentryError(`beforeSend rejected with ${e}`);
787+
throw new SentryError(`${beforeSendProcessorName} rejected with ${e}`);
781788
},
782789
);
783790
} else if (!(isPlainObject(rv) || rv === null)) {

packages/types/src/options.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,19 +195,31 @@ export interface ClientOptions<TO extends BaseTransportOptions = BaseTransportOp
195195
tracesSampler?: (samplingContext: SamplingContext) => number | boolean;
196196

197197
/**
198-
* A callback invoked during event submission, allowing to optionally modify
199-
* the event before it is sent to Sentry.
198+
* An event-processing callback for error and message events, guaranteed to be invoked after all other event
199+
* processors, which allows an event to be modified or dropped.
200200
*
201-
* Note that you must return a valid event from this callback. If you do not
202-
* wish to modify the event, simply return it at the end.
203-
* Returning null will cause the event to be dropped.
201+
* Note that you must return a valid event from this callback. If you do not wish to modify the event, simply return
202+
* it at the end. Returning `null` will cause the event to be dropped.
204203
*
205204
* @param event The error or message event generated by the SDK.
206-
* @param hint May contain additional information about the original exception.
205+
* @param hint Event metadata useful for processing.
207206
* @returns A new event that will be sent | null.
208207
*/
209208
beforeSend?: (event: Event, hint: EventHint) => PromiseLike<Event | null> | Event | null;
210209

210+
/**
211+
* An event-processing callback for error and message events, guaranteed to be invoked after all other event
212+
* processors, which allows an event to be modified or dropped.
213+
*
214+
* Note that you must return a valid event from this callback. If you do not wish to modify the event, simply return
215+
* it at the end. Returning `null` will cause the event to be dropped.
216+
*
217+
* @param event The error or message event generated by the SDK.
218+
* @param hint Event metadata useful for processing.
219+
* @returns A new event that will be sent | null.
220+
*/
221+
beforeSendTransaction?: (event: Event, hint: EventHint) => PromiseLike<Event | null> | Event | null;
222+
211223
/**
212224
* A callback invoked when adding a breadcrumb, allowing to optionally modify
213225
* it before adding it to future events.

0 commit comments

Comments
 (0)