Skip to content

Commit

Permalink
feat(core): Set custom transaction source for event processors (#5722)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
AbhiPrasad committed Sep 13, 2022
1 parent 8cff038 commit ff4f97f
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/core/src/baseclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
resolvedSyncPromise,
SentryError,
SyncPromise,
timestampInSeconds,
truncate,
uuid4,
} from '@sentry/utils';
Expand Down Expand Up @@ -658,6 +659,26 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
this._updateSessionFromEvent(session, processedEvent);
}

// None of the Sentry built event processor will update transaction name,
// so if the transaction name has been changed by an event processor, we know
// it has to come from custom event processor added by a user
const transactionInfo = processedEvent.transaction_info;
if (isTransaction && transactionInfo && processedEvent.transaction !== event.transaction) {
const source = 'custom';
processedEvent.transaction_info = {
...transactionInfo,
source,
changes: [
...transactionInfo.changes,
{
source,
timestamp: timestampInSeconds(),
propagations: transactionInfo.propagations,
},
],
};
}

this.sendEvent(processedEvent, hint);
return processedEvent;
})
Expand Down
47 changes: 47 additions & 0 deletions packages/core/test/lib/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,53 @@ describe('BaseClient', () => {
expect(recordLostEventSpy).toHaveBeenCalledWith('event_processor', 'error');
});

test('mutating transaction name with event processors sets transaction name change metadata', () => {
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, enableSend: true });
const client = new TestClient(options);

const transaction: Event = {
contexts: {
trace: {
op: 'pageload',
span_id: 'a3df84a60c2e4e76',
trace_id: '86f39e84263a4de99c326acab3bfe3bd',
},
},
environment: 'production',
event_id: '972f45b826a248bba98e990878a177e1',
spans: [],
start_timestamp: 1591603196.614865,
timestamp: 1591603196.728485,
transaction: 'initialName',
type: 'transaction',
transaction_info: {
source: 'url',
changes: [],
propagations: 3,
},
};

const scope = new Scope();
scope.addEventProcessor(event => {
event.transaction = 'updatedName';
return event;
});

client.captureEvent(transaction, {}, scope);
expect(TestClient.instance!.event!.transaction).toEqual('updatedName');
expect(TestClient.instance!.event!.transaction_info).toEqual({
source: 'custom',
changes: [
{
propagations: 3,
source: 'custom',
timestamp: expect.any(Number),
},
],
propagations: 3,
});
});

test('eventProcessor sends an event and logs when it crashes', () => {
expect.assertions(3);

Expand Down

0 comments on commit ff4f97f

Please sign in to comment.