From c0ab95266a68853156de804b1f2895755df4e3f7 Mon Sep 17 00:00:00 2001 From: moander Date: Wed, 27 Oct 2021 09:44:15 +0200 Subject: [PATCH] fix(core): support regex global flag in urlMatches (#2560) Co-authored-by: Daniel Dyla --- packages/opentelemetry-core/src/utils/url.ts | 2 +- packages/opentelemetry-core/test/utils/url.test.ts | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/opentelemetry-core/src/utils/url.ts b/packages/opentelemetry-core/src/utils/url.ts index 9db9725b82..a6122ae784 100644 --- a/packages/opentelemetry-core/src/utils/url.ts +++ b/packages/opentelemetry-core/src/utils/url.ts @@ -17,7 +17,7 @@ export function urlMatches(url: string, urlToMatch: string | RegExp): boolean { if (typeof urlToMatch === 'string') { return url === urlToMatch; } else { - return urlToMatch.test(url); + return !!url.match(urlToMatch); } } /** diff --git a/packages/opentelemetry-core/test/utils/url.test.ts b/packages/opentelemetry-core/test/utils/url.test.ts index 91ae9b75c5..8855cff23c 100644 --- a/packages/opentelemetry-core/test/utils/url.test.ts +++ b/packages/opentelemetry-core/test/utils/url.test.ts @@ -71,5 +71,18 @@ describe('Core - Utils - url', () => { ); }); }); + describe('when regex has global flag', () => { + it('should return true', () => { + const ignoredUrls = [/myaddr/g]; + // Run test multiple times to ensure same result (git.io/JimS1) + for (let i = 0; i < 3; i++) { + assert.strictEqual( + isUrlIgnored(urlToTest, ignoredUrls), + true, + urlIgnored + ); + } + }); + }); }); });