Skip to content

Commit

Permalink
πŸ‘ŒπŸŽ¨ Reuse matchList in tracer
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickadam committed Dec 12, 2022
1 parent 0074234 commit 4b7ba12
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 33 deletions.
5 changes: 5 additions & 0 deletions packages/core/src/tools/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,11 @@ describe('matchList', () => {
expect(matchList(list, 'qux')).toBe(false)
})

it('should compare strings using startsWith when enabling the option', () => {
const list = ['http://my.domain.com']
expect(matchList(list, 'http://my.domain.com/action', true)).toBe(true)
})

it('should catch error from provided function', () => {
spyOn(display, 'error')
const list = [
Expand Down
25 changes: 15 additions & 10 deletions packages/core/src/tools/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -666,20 +666,25 @@ export function isMatchOption(item: unknown): item is MatchOption {
const itemType = getType(item)
return itemType === 'string' || itemType === 'function' || item instanceof RegExp
}
export function matchList(list: MatchOption[], value: string): boolean {
/**
* Returns true if value can be matched by at least one of the provided MatchOptions.
* When comparing strings, setting useStartsWith to true will compare the value with the start of
* the option, instead of requiring an exact match.
*/
export function matchList(list: MatchOption[], value: string, useStartsWith = false): boolean {
return list.some((item) => {
if (typeof item === 'function') {
try {
try {
if (typeof item === 'function') {
return item(value)
} catch (e) {
display.error(e)
return false
} else if (item instanceof RegExp) {
return item.test(value)
} else if (typeof item === 'string') {
return useStartsWith ? startsWith(value, item) : item === value
}
} catch (e) {
display.error(e)
}
if (item instanceof RegExp) {
return item.test(value)
}
return item === value
return false
})
}

Expand Down
27 changes: 4 additions & 23 deletions packages/rum-core/src/domain/tracing/tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import {
performDraw,
isNumber,
assign,
display,
find,
startsWith,
getType,
isMatchOption,
matchList,
} from '@datadog/browser-core'
import type { RumConfiguration } from '../configuration'
import type {
Expand Down Expand Up @@ -110,7 +109,9 @@ function injectHeadersIfTracingAllowed(
return
}

const tracingOption = findTracingOptionForUrl(configuration.allowedTracingUrls, context.url!)
const tracingOption = find(configuration.allowedTracingUrls, (tracingOption: TracingOption) =>
matchList([tracingOption.match], context.url!, true)
)
if (!tracingOption) {
return
}
Expand All @@ -121,26 +122,6 @@ function injectHeadersIfTracingAllowed(
inject(makeTracingHeaders(context.traceId, context.spanId, context.traceSampled, tracingOption.headersTypes))
}

function findTracingOptionForUrl(configurationTracingUrls: TracingOption[], url: string) {
return find(configurationTracingUrls, (config: TracingOption) => {
try {
const item = config.match
if (typeof item === 'function') {
return item(url)
}
if (item instanceof RegExp) {
return item.test(url)
}
if (typeof item === 'string') {
return startsWith(url, item)
}
} catch (e) {
display.error(e)
}
return false
})
}

export function isTracingSupported() {
return getCrypto() !== undefined
}
Expand Down

0 comments on commit 4b7ba12

Please sign in to comment.