Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vue): Update Vue trackComponents list to match components with or without <> #13543

Merged
merged 5 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion packages/vue/src/tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,25 @@ function finishRootSpan(vm: VueSentry, timestamp: number, timeout: number): void
}, timeout);
}

// Find if the current component exists in the provided `TracingOptions.trackComponents` array option.
/**
*
*/
export function findTrackComponent(trackComponents: string[], formattedName: string): boolean {
/**
*
*/
function extractComponentName(name: string): string {
return name.replace(/^<([^\s]*)>(?: at [^\s]*)?$/, '$1');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on what you suggested @Lms24, just an added non-capture group. The ReDoS Checker passes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me, thanks!

}

const isMatched = trackComponents.some(compo => {
return extractComponentName(formattedName) === extractComponentName(compo);
});

return isMatched;
}

export const createTracingMixins = (options: TracingOptions): Mixins => {
const hooks = (options.hooks || [])
.concat(DEFAULT_HOOKS)
Expand Down Expand Up @@ -84,8 +103,9 @@ export const createTracingMixins = (options: TracingOptions): Mixins => {

// Skip components that we don't want to track to minimize the noise and give a more granular control to the user
const name = formatComponentName(this, false);

const shouldTrack = Array.isArray(options.trackComponents)
? options.trackComponents.indexOf(name) > -1
? findTrackComponent(options.trackComponents, name)
: options.trackComponents;

// We always want to track root component
Expand Down
44 changes: 44 additions & 0 deletions packages/vue/test/tracing/trackComponents.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { describe, expect, it } from 'vitest';
import { findTrackComponent } from '../../src/tracing';

describe('findTrackComponent', () => {
describe('when user-defined array contains `<Component>`', () => {
it('returns true if a match is found', () => {
// arrange
const trackComponents = ['<ABC>', '<XYZ>'];
const formattedComponentName = '<XYZ>';

// act
const shouldTrack = findTrackComponent(trackComponents, formattedComponentName);

// assert
expect(shouldTrack).toBe(true);
});
});
describe('when user-defined array contains `Component` without the `<>`', () => {
it('returns true if a match is found', () => {
// arrange
const trackComponents = ['ABC', 'XYZ'];
const formattedComponentName = '<XYZ>';

// act
const shouldTrack = findTrackComponent(trackComponents, formattedComponentName);

// assert
expect(shouldTrack).toBe(true);
});
});
describe('when the vue file name is include in the formatted component name', () => {
it('returns true if a match is found', () => {
// arrange
const trackComponents = ['ABC', 'XYZ'];
const formattedComponentName = '<XYZ> at XYZ.vue';

// act
const shouldTrack = findTrackComponent(trackComponents, formattedComponentName);

// assert
expect(shouldTrack).toBe(true);
});
});
});
Loading