diff --git a/packages/angular/src/tracing.ts b/packages/angular/src/tracing.ts index 1a4352e6994a..73bf94c89f73 100644 --- a/packages/angular/src/tracing.ts +++ b/packages/angular/src/tracing.ts @@ -32,6 +32,7 @@ export function routingInstrumentation( customStartTransaction({ name: global.location.pathname, op: 'pageload', + metadata: { source: 'url' }, }); } } @@ -77,6 +78,7 @@ export class TraceService implements OnDestroy { activeTransaction = stashedStartTransaction({ name: strippedUrl, op: 'navigation', + metadata: { source: 'url' }, }); } diff --git a/packages/angular/test/tracing.test.ts b/packages/angular/test/tracing.test.ts new file mode 100644 index 000000000000..403c9b7c3cf1 --- /dev/null +++ b/packages/angular/test/tracing.test.ts @@ -0,0 +1,47 @@ +import { NavigationStart, Router, RouterEvent } from '@angular/router'; +import { Subject } from 'rxjs'; + +import { instrumentAngularRouting, TraceService } from '../src/index'; + +describe('Angular Tracing', () => { + const startTransaction = jest.fn(); + describe('instrumentAngularRouting', () => { + it('should attach the transaction source on the pageload transaction', () => { + instrumentAngularRouting(startTransaction); + expect(startTransaction).toHaveBeenCalledWith({ + name: '/', + op: 'pageload', + metadata: { source: 'url' }, + }); + }); + }); + + describe('TraceService', () => { + let traceService: TraceService; + const routerEvents$: Subject = new Subject(); + + beforeAll(() => instrumentAngularRouting(startTransaction)); + beforeEach(() => { + jest.resetAllMocks(); + + traceService = new TraceService({ + events: routerEvents$, + } as unknown as Router); + }); + + afterEach(() => { + traceService.ngOnDestroy(); + }); + + it('attaches the transaction source on a navigation change', () => { + routerEvents$.next(new NavigationStart(0, 'user/123/credentials')); + + expect(startTransaction).toHaveBeenCalledTimes(1); + expect(startTransaction).toHaveBeenCalledWith({ + name: 'user/123/credentials', + op: 'navigation', + metadata: { source: 'url' }, + }); + }); + }); +});