Skip to content

Commit

Permalink
✨ [RUMF-993] New proxy strategy (#1016)
Browse files Browse the repository at this point in the history
* ✨ New proxy strategy
  • Loading branch information
amortemousque authored Aug 31, 2021
1 parent 7692a4f commit 589ade3
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 26 deletions.
5 changes: 5 additions & 0 deletions packages/core/src/domain/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ export interface InitConfiguration {
silentMultipleInit?: boolean
trackInteractions?: boolean
trackViewsManually?: boolean

/**
* @deprecated Favor proxyUrl option
*/
proxyHost?: string
proxyUrl?: string
beforeSend?: BeforeSendCallback
initialPrivacyLevel?: InitialPrivacyLevel

Expand Down
55 changes: 32 additions & 23 deletions packages/core/src/domain/configuration/endpointBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,41 +46,31 @@ export function createEndpointBuilder(
clientToken,
env,
proxyHost,
proxyUrl,
service,
version,
intakeApiVersion,
useAlternateIntakeDomains,
} = initConfiguration

function build(endpointType: EndpointType, source?: string) {
const tags =
`sdk_version:${sdkVersion}` +
`${env ? `,env:${env}` : ''}` +
`${service ? `,service:${service}` : ''}` +
`${version ? `,version:${version}` : ''}`

let parameters = `ddsource=${source || 'browser'}&ddtags=${encodeURIComponent(tags)}`

if (proxyHost) {
const datadogHost = buildHost(endpointType)
parameters += `&ddhost=${datadogHost}`
const host = buildHost(endpointType)
const path = buildPath(endpointType)
const queryParameters = buildQueryParameters(endpointType, source)
const endpoint = `https://${host}${path}?${queryParameters}`

if (proxyUrl) {
return `${proxyUrl}?ddforward=${encodeURIComponent(endpoint)}`
} else if (proxyHost) {
return `https://${proxyHost}${path}?ddhost=${host}&${queryParameters}`
}

if (shouldUseIntakeV2(endpointType)) {
parameters +=
`&dd-api-key=${clientToken}&` +
`dd-evp-origin-version=${sdkVersion}&` +
`dd-evp-origin=browser&` +
`dd-request-id=${generateUUID()}`
}

return `${buildIntakeUrl(endpointType)}?${parameters}`
return endpoint
}

function buildIntakeUrl(endpointType: EndpointType): string {
const datadogHost = buildHost(endpointType)
const host = proxyHost ? proxyHost : datadogHost
return `https://${host}${buildPath(endpointType)}`
const endpoint = build(endpointType)
return endpoint.slice(0, endpoint.indexOf('?'))
}

function buildHost(endpointType: EndpointType) {
Expand All @@ -99,6 +89,25 @@ export function createEndpointBuilder(
return shouldUseIntakeV2(endpointType) ? `/api/v2/${INTAKE_TRACKS[endpointType]}` : `/v1/input/${clientToken}`
}

function buildQueryParameters(endpointType: EndpointType, source?: string) {
const tags =
`sdk_version:${sdkVersion}` +
`${env ? `,env:${env}` : ''}` +
`${service ? `,service:${service}` : ''}` +
`${version ? `,version:${version}` : ''}`

let parameters = `ddsource=${source || 'browser'}&ddtags=${encodeURIComponent(tags)}`

if (shouldUseIntakeV2(endpointType)) {
parameters +=
`&dd-api-key=${clientToken}&` +
`dd-evp-origin-version=${sdkVersion}&` +
`dd-evp-origin=browser&` +
`dd-request-id=${generateUUID()}`
}
return parameters
}

function shouldUseIntakeV2(endpointType?: EndpointType): boolean {
return (
!!isIntakeV2Enabled &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,35 @@ describe('transportConfiguration', () => {
})
})

describe('query parameters', () => {
it('should add new intake query parameters when "support-intake-v2" enabled', () => {
const configuration = computeTransportConfiguration({ clientToken, intakeApiVersion: 2 }, buildEnv, true)
expect(configuration.rumEndpoint).toMatch(
`&dd-api-key=${clientToken}&dd-evp-origin-version=(.*)&dd-evp-origin=browser&dd-request-id=(.*)`
)
})
})

describe('proxyHost', () => {
it('should replace endpoint host add set it as a query parameter', () => {
it('should replace endpoint host and add set it as a query parameter', () => {
const configuration = computeTransportConfiguration(
{ clientToken, site: 'datadoghq.eu', proxyHost: 'proxy.io' },
buildEnv
)
expect(configuration.rumEndpoint).toMatch(/^https:\/\/proxy\.io\//)
expect(configuration.rumEndpoint).toContain('&ddhost=rum-http-intake.logs.datadoghq.eu')
expect(configuration.rumEndpoint).toMatch(
`https://proxy.io/v1/input/${clientToken}\\?ddhost=rum-http-intake.logs.datadoghq.eu&ddsource=(.*)&ddtags=(.*)`
)
})
})

describe('proxyUrl', () => {
it('should replace the full endpoint by the proxyUrl and set it in the attribute ddforward', () => {
const configuration = computeTransportConfiguration({ clientToken, proxyUrl: 'https://proxy.io/path' }, buildEnv)
expect(configuration.rumEndpoint).toMatch(
`https://proxy.io/path\\?ddforward=${encodeURIComponent(
`https://rum-http-intake.logs.datadoghq.com/v1/input/${clientToken}?ddsource=(.*)&ddtags=(.*)`
)}`
)
})
})

Expand Down

0 comments on commit 589ade3

Please sign in to comment.