Skip to content

Commit 4984870

Browse files
authored
ref(angular): Add transaction source for Angular Router (#5382)
Add the transaction name source annotation to the Angular SDK. Since the Angular SDK currently does not parameterize URLs, we only assign `'url'` as the transaction name source. We're revisiting parameterization in Angular in a follow up PR. Additionally, add a two tests to cover this change (might be more relevant once we add parameterization).
1 parent 6d98f68 commit 4984870

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

packages/angular/src/tracing.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export function routingInstrumentation(
3232
customStartTransaction({
3333
name: global.location.pathname,
3434
op: 'pageload',
35+
metadata: { source: 'url' },
3536
});
3637
}
3738
}
@@ -77,6 +78,7 @@ export class TraceService implements OnDestroy {
7778
activeTransaction = stashedStartTransaction({
7879
name: strippedUrl,
7980
op: 'navigation',
81+
metadata: { source: 'url' },
8082
});
8183
}
8284

packages/angular/test/tracing.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { NavigationStart, Router, RouterEvent } from '@angular/router';
2+
import { Subject } from 'rxjs';
3+
4+
import { instrumentAngularRouting, TraceService } from '../src/index';
5+
6+
describe('Angular Tracing', () => {
7+
const startTransaction = jest.fn();
8+
describe('instrumentAngularRouting', () => {
9+
it('should attach the transaction source on the pageload transaction', () => {
10+
instrumentAngularRouting(startTransaction);
11+
expect(startTransaction).toHaveBeenCalledWith({
12+
name: '/',
13+
op: 'pageload',
14+
metadata: { source: 'url' },
15+
});
16+
});
17+
});
18+
19+
describe('TraceService', () => {
20+
let traceService: TraceService;
21+
const routerEvents$: Subject<RouterEvent> = new Subject();
22+
23+
beforeAll(() => instrumentAngularRouting(startTransaction));
24+
beforeEach(() => {
25+
jest.resetAllMocks();
26+
27+
traceService = new TraceService({
28+
events: routerEvents$,
29+
} as unknown as Router);
30+
});
31+
32+
afterEach(() => {
33+
traceService.ngOnDestroy();
34+
});
35+
36+
it('attaches the transaction source on a navigation change', () => {
37+
routerEvents$.next(new NavigationStart(0, 'user/123/credentials'));
38+
39+
expect(startTransaction).toHaveBeenCalledTimes(1);
40+
expect(startTransaction).toHaveBeenCalledWith({
41+
name: 'user/123/credentials',
42+
op: 'navigation',
43+
metadata: { source: 'url' },
44+
});
45+
});
46+
});
47+
});

0 commit comments

Comments
 (0)