Skip to content

Commit 9b817e3

Browse files
committed
ref to option for routing instrumentation
1 parent e6a49eb commit 9b817e3

File tree

5 files changed

+50
-32
lines changed

5 files changed

+50
-32
lines changed

packages/vue/src/router.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1-
import { captureException, getCurrentHub, WINDOW } from '@sentry/browser';
1+
import { captureException, WINDOW } from '@sentry/browser';
22
import type { Transaction, TransactionContext, TransactionSource } from '@sentry/types';
33

44
import { getActiveTransaction } from './tracing';
5-
import type { Options } from './types';
5+
6+
interface VueRouterInstrumationOptions {
7+
/**
8+
* What to use for route labels.
9+
* By default, we use route.name (if set) and else the path.
10+
*
11+
* Default: 'name'
12+
*/
13+
routeLabel: 'name' | 'path';
14+
}
615

716
export type VueRouterInstrumentation = <T extends Transaction>(
817
startTransaction: (context: TransactionContext) => T | undefined,
@@ -36,12 +45,15 @@ interface VueRouter {
3645
/**
3746
* Creates routing instrumentation for Vue Router v2, v3 and v4
3847
*
48+
* You can optionally pass in an options object with the available option:
49+
* * `routeLabel`: Set this to `route` to opt-out of using `route.name` for transaction names.
50+
*
3951
* @param router The Vue Router instance that is used
4052
*/
41-
export function vueRouterInstrumentation(router: VueRouter): VueRouterInstrumentation {
42-
const client = getCurrentHub().getClient();
43-
const options = ((client && client.getOptions()) || {}) as Partial<Options>;
44-
53+
export function vueRouterInstrumentation(
54+
router: VueRouter,
55+
options: Partial<VueRouterInstrumationOptions> = {},
56+
): VueRouterInstrumentation {
4557
return (
4658
startTransaction: (context: TransactionContext) => Transaction | undefined,
4759
startTransactionOnPageLoad: boolean = true,

packages/vue/src/sdk.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ const DEFAULT_CONFIG: Options = {
1616
hooks: DEFAULT_HOOKS,
1717
timeout: 2000,
1818
trackComponents: false,
19-
routeLabel: 'name',
2019
_metadata: {
2120
sdk: {
2221
name: 'sentry.javascript.vue',

packages/vue/src/types.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,6 @@ export interface Options extends TracingOptions, BrowserOptions {
4646

4747
/** {@link TracingOptions} */
4848
tracingOptions?: Partial<TracingOptions>;
49-
50-
/**
51-
* What to use for route labels.
52-
* By default, we use route.name (if set) and else the path.
53-
*
54-
* Default: 'name'
55-
*/
56-
routeLabel: 'name' | 'path';
5749
}
5850

5951
/** Vue specific configuration for Tracing Integration */

packages/vue/test/errorHandler.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,6 @@ const testHarness = ({
368368
tracingOptions: {},
369369
trackComponents: [],
370370
timeout: 0,
371-
routeLabel: 'name',
372371
hooks: [] as Operation[],
373372
};
374373

packages/vue/test/router.test.ts

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import * as SentryBrowser from '@sentry/browser';
22
import type { Transaction } from '@sentry/types';
3-
import { createApp } from 'vue';
43

5-
import { init, vueRouterInstrumentation } from '../src';
4+
import { vueRouterInstrumentation } from '../src';
65
import type { Route } from '../src/router';
76
import * as vueTracing from '../src/tracing';
87

@@ -171,22 +170,39 @@ describe('vueRouterInstrumentation()', () => {
171170
);
172171

173172
it('allows to configure routeLabel=path', () => {
174-
// Need to setup a proper client with options etc.
175-
const app = createApp({
176-
template: '<div>hello</div>',
177-
});
178-
const el = document.createElement('div');
173+
// create instrumentation
174+
const instrument = vueRouterInstrumentation(mockVueRouter, { routeLabel: 'path' });
179175

180-
init({
181-
app,
182-
defaultIntegrations: false,
183-
routeLabel: 'path',
184-
});
176+
// instrument
177+
instrument(mockStartTransaction, true, true);
178+
179+
// check
180+
const beforeEachCallback = mockVueRouter.beforeEach.mock.calls[0][0];
185181

186-
app.mount(el);
182+
const from = testRoutes.normalRoute1;
183+
const to = testRoutes.namedRoute;
184+
beforeEachCallback(to, from, mockNext);
187185

186+
// first startTx call happens when the instrumentation is initialized (for pageloads)
187+
expect(mockStartTransaction).toHaveBeenLastCalledWith({
188+
name: '/login',
189+
metadata: {
190+
source: 'route',
191+
},
192+
data: {
193+
params: to.params,
194+
query: to.query,
195+
},
196+
op: 'navigation',
197+
tags: {
198+
'routing.instrumentation': 'vue-router',
199+
},
200+
});
201+
});
202+
203+
it('allows to configure routeLabel=name', () => {
188204
// create instrumentation
189-
const instrument = vueRouterInstrumentation(mockVueRouter);
205+
const instrument = vueRouterInstrumentation(mockVueRouter, { routeLabel: 'name' });
190206

191207
// instrument
192208
instrument(mockStartTransaction, true, true);
@@ -200,9 +216,9 @@ describe('vueRouterInstrumentation()', () => {
200216

201217
// first startTx call happens when the instrumentation is initialized (for pageloads)
202218
expect(mockStartTransaction).toHaveBeenLastCalledWith({
203-
name: '/login',
219+
name: 'login-screen',
204220
metadata: {
205-
source: 'route',
221+
source: 'custom',
206222
},
207223
data: {
208224
params: to.params,

0 commit comments

Comments
 (0)