Skip to content

Commit ff4f97f

Browse files
authored
feat(core): Set custom transaction source for event processors (#5722)
This PR updates `BaseClient` class to set a custom transaction source if event processors change the transaction name. It also adds metadata around a transaction name change when this is done.
1 parent 8cff038 commit ff4f97f

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

packages/core/src/baseclient.ts

+21
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
resolvedSyncPromise,
3434
SentryError,
3535
SyncPromise,
36+
timestampInSeconds,
3637
truncate,
3738
uuid4,
3839
} from '@sentry/utils';
@@ -658,6 +659,26 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
658659
this._updateSessionFromEvent(session, processedEvent);
659660
}
660661

662+
// None of the Sentry built event processor will update transaction name,
663+
// so if the transaction name has been changed by an event processor, we know
664+
// it has to come from custom event processor added by a user
665+
const transactionInfo = processedEvent.transaction_info;
666+
if (isTransaction && transactionInfo && processedEvent.transaction !== event.transaction) {
667+
const source = 'custom';
668+
processedEvent.transaction_info = {
669+
...transactionInfo,
670+
source,
671+
changes: [
672+
...transactionInfo.changes,
673+
{
674+
source,
675+
timestamp: timestampInSeconds(),
676+
propagations: transactionInfo.propagations,
677+
},
678+
],
679+
};
680+
}
681+
661682
this.sendEvent(processedEvent, hint);
662683
return processedEvent;
663684
})

packages/core/test/lib/base.test.ts

+47
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,53 @@ describe('BaseClient', () => {
10921092
expect(recordLostEventSpy).toHaveBeenCalledWith('event_processor', 'error');
10931093
});
10941094

1095+
test('mutating transaction name with event processors sets transaction name change metadata', () => {
1096+
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, enableSend: true });
1097+
const client = new TestClient(options);
1098+
1099+
const transaction: Event = {
1100+
contexts: {
1101+
trace: {
1102+
op: 'pageload',
1103+
span_id: 'a3df84a60c2e4e76',
1104+
trace_id: '86f39e84263a4de99c326acab3bfe3bd',
1105+
},
1106+
},
1107+
environment: 'production',
1108+
event_id: '972f45b826a248bba98e990878a177e1',
1109+
spans: [],
1110+
start_timestamp: 1591603196.614865,
1111+
timestamp: 1591603196.728485,
1112+
transaction: 'initialName',
1113+
type: 'transaction',
1114+
transaction_info: {
1115+
source: 'url',
1116+
changes: [],
1117+
propagations: 3,
1118+
},
1119+
};
1120+
1121+
const scope = new Scope();
1122+
scope.addEventProcessor(event => {
1123+
event.transaction = 'updatedName';
1124+
return event;
1125+
});
1126+
1127+
client.captureEvent(transaction, {}, scope);
1128+
expect(TestClient.instance!.event!.transaction).toEqual('updatedName');
1129+
expect(TestClient.instance!.event!.transaction_info).toEqual({
1130+
source: 'custom',
1131+
changes: [
1132+
{
1133+
propagations: 3,
1134+
source: 'custom',
1135+
timestamp: expect.any(Number),
1136+
},
1137+
],
1138+
propagations: 3,
1139+
});
1140+
});
1141+
10951142
test('eventProcessor sends an event and logs when it crashes', () => {
10961143
expect.assertions(3);
10971144

0 commit comments

Comments
 (0)