Skip to content

Commit

Permalink
ref(angular): Add transaction source for Angular Router (#5382)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
Lms24 authored Jul 8, 2022
1 parent 6d98f68 commit 4984870
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/angular/src/tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export function routingInstrumentation(
customStartTransaction({
name: global.location.pathname,
op: 'pageload',
metadata: { source: 'url' },
});
}
}
Expand Down Expand Up @@ -77,6 +78,7 @@ export class TraceService implements OnDestroy {
activeTransaction = stashedStartTransaction({
name: strippedUrl,
op: 'navigation',
metadata: { source: 'url' },
});
}

Expand Down
47 changes: 47 additions & 0 deletions packages/angular/test/tracing.test.ts
Original file line number Diff line number Diff line change
@@ -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<RouterEvent> = 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' },
});
});
});
});

0 comments on commit 4984870

Please sign in to comment.