Skip to content

Commit

Permalink
Add feature flag
Browse files Browse the repository at this point in the history
  • Loading branch information
amortemousque committed Aug 26, 2021
1 parent 973e0b9 commit 9bdca58
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 52 deletions.
5 changes: 3 additions & 2 deletions packages/core/src/domain/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,14 @@ export function buildConfiguration(initConfiguration: InitConfiguration, buildEn
? initConfiguration.enableExperimentalFeatures
: []

const isEnabled = (feature: string) => includes(enableExperimentalFeatures, feature)
const configuration: Configuration = {
beforeSend:
initConfiguration.beforeSend && catchUserErrors(initConfiguration.beforeSend, 'beforeSend threw an error:'),
cookieOptions: buildCookieOptions(initConfiguration),
isEnabled: (feature: string) => includes(enableExperimentalFeatures, feature),
isEnabled,
service: initConfiguration.service,
...buildTransportConfiguration(initConfiguration, buildEnv),
...buildTransportConfiguration(initConfiguration, buildEnv, isEnabled('support-intake-v2')),
...DEFAULT_CONFIGURATION,
}

Expand Down
54 changes: 25 additions & 29 deletions packages/core/src/domain/configuration/endpointBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,33 +38,35 @@ type IntakeType = keyof typeof ENDPOINTS
type IntakeApiVersion = 1 | 2

export class EndpointBuilder {
intakeApiVersion: IntakeApiVersion
intakeType: IntakeType
private site: string
private clientToken: string
private env: string | undefined
private proxyHost: string | undefined
private sdkVersion: string
private service: string | undefined
private version: string | undefined

constructor(initConfiguration: InitConfiguration, buildEnv: BuildEnv) {
private intakeApiVersion: IntakeApiVersion
private useAlternateIntakeDomains: boolean
private isIntakeV2Enabled: boolean
constructor(initConfiguration: InitConfiguration, buildEnv: BuildEnv, isIntakeV2Enabled?: boolean) {
this.isIntakeV2Enabled = !!isIntakeV2Enabled
this.site = initConfiguration.site || INTAKE_SITE_US
this.clientToken = initConfiguration.clientToken
this.env = initConfiguration.env
this.proxyHost = initConfiguration.proxyHost
this.sdkVersion = buildEnv.sdkVersion
this.service = initConfiguration.service
this.site = initConfiguration.site || INTAKE_SITE_US
this.version = initConfiguration.version
this.intakeApiVersion = initConfiguration.intakeApiVersion || 1
this.useAlternateIntakeDomains = !!initConfiguration.useAlternateIntakeDomains
}

this.intakeApiVersion =
initConfiguration.intakeApiVersion !== 2 && includes(INTAKE_V1_ALLOWED_SITES, this.site) ? 1 : 2
supportIntakeV2 = (endpointType?: EndpointType): boolean =>
this.isIntakeV2Enabled &&
(this.intakeApiVersion === 2 || !includes(INTAKE_V1_ALLOWED_SITES, this.site) || endpointType === 'sessionReplay')

this.intakeType =
!initConfiguration.useAlternateIntakeDomains && includes(CLASSIC_ALLOWED_SITES, this.site)
? 'classic'
: 'alternate'
}
supportAlternateDomain = (endpointType?: EndpointType): boolean =>
this.useAlternateIntakeDomains || !includes(CLASSIC_ALLOWED_SITES, this.site) || endpointType === 'sessionReplay'

build(endpointType: EndpointType, source?: string) {
const tags =
Expand All @@ -75,13 +77,12 @@ export class EndpointBuilder {
const datadogHost = this.buildHost(endpointType)
const proxyParameter = this.proxyHost ? `ddhost=${datadogHost}&` : ''
const parameters = `${proxyParameter}ddsource=${source || 'browser'}&ddtags=${encodeURIComponent(tags)}`
const newIntakeParameters =
this.intakeApiVersion === 2 || endpointType === 'sessionReplay'
? `?dd-api-key=${this.clientToken}&` +
`dd-evp-origin-version=${this.sdkVersion}&` +
`dd-evp-origin=browser&` +
`dd-request-id=${generateUUID()}&`
: `${this.clientToken}?`
const newIntakeParameters = this.supportIntakeV2(endpointType)
? `?dd-api-key=${this.clientToken}&` +
`dd-evp-origin-version=${this.sdkVersion}&` +
`dd-evp-origin=browser&` +
`dd-request-id=${generateUUID()}&`
: `${this.clientToken}?`

return `${this.buildIntakeUrl(endpointType)}${newIntakeParameters}${parameters}`
}
Expand All @@ -93,23 +94,18 @@ export class EndpointBuilder {
}

private buildHost(endpointType: EndpointType) {
let host
if (this.intakeType === 'classic' && endpointType !== 'sessionReplay') {
const endpoint = ENDPOINTS.classic[endpointType]!
host = `${endpoint}-http-intake.logs.${this.site}`
} else {
if (this.supportAlternateDomain(endpointType)) {
const endpoint = ENDPOINTS.alternate[endpointType]
const domainParts = this.site.split('.')
const extension = domainParts.pop()
const suffix = `${domainParts.join('-')}.${extension!}`
host = `${endpoint}.browser-intake-${suffix}`
return `${endpoint}.browser-intake-${suffix}`
}
return host
const endpoint = ENDPOINTS.classic[endpointType]!
return `${endpoint}-http-intake.logs.${this.site}`
}

private buildPath(endpointType: EndpointType) {
return this.intakeApiVersion === 1 && endpointType !== 'sessionReplay'
? `/v1/input/`
: `/api/v2/${INTAKE_TRACKS[endpointType]}/`
return this.supportIntakeV2(endpointType) ? `/api/v2/${INTAKE_TRACKS[endpointType]}/` : `/v1/input/`
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe('transportConfiguration', () => {

describe('new intake parameters', () => {
it('should be set when intake v2 is enabled', () => {
const configuration = buildTransportConfiguration({ clientToken, intakeApiVersion: 2 }, buildEnv)
const configuration = buildTransportConfiguration({ clientToken, intakeApiVersion: 2 }, buildEnv, true)
expect(configuration.rumEndpoint).toContain(
`?dd-api-key=${clientToken}&` +
`dd-evp-origin-version=some_version&` +
Expand Down Expand Up @@ -142,14 +142,16 @@ describe('transportConfiguration', () => {
it('should detect alternate domains intake v2', () => {
let configuration = buildTransportConfiguration(
{ clientToken, useAlternateIntakeDomains: true, intakeApiVersion: 2 },
buildEnv
buildEnv,
true
)
expect(configuration.isIntakeUrl('https://rum.browser-intake-datadoghq.com/api/v2/rum/xxx')).toBe(true)
expect(configuration.isIntakeUrl('https://logs.browser-intake-datadoghq.com/api/v2/logs/xxx')).toBe(true)

configuration = buildTransportConfiguration(
{ clientToken, site: 'datadoghq.eu', useAlternateIntakeDomains: true, intakeApiVersion: 2 },
buildEnv
buildEnv,
true
)
expect(configuration.isIntakeUrl('https://rum.browser-intake-datadoghq.eu/api/v2/rum/xxx')).toBe(true)
expect(configuration.isIntakeUrl('https://logs.browser-intake-datadoghq.eu/api/v2/logs/xxx')).toBe(true)
Expand All @@ -170,14 +172,16 @@ describe('transportConfiguration', () => {
it('should detect alternate domains intake v2', () => {
let configuration = buildTransportConfiguration(
{ clientToken, site: 'us3.datadoghq.com', intakeApiVersion: 2 },
buildEnv
buildEnv,
true
)
expect(configuration.isIntakeUrl('https://rum.browser-intake-us3-datadoghq.com/api/v2/rum/xxx')).toBe(true)
expect(configuration.isIntakeUrl('https://logs.browser-intake-us3-datadoghq.com/api/v2/logs/xxx')).toBe(true)

configuration = buildTransportConfiguration(
{ clientToken, site: 'ddog-gov.com', intakeApiVersion: 2 },
buildEnv
buildEnv,
true
)
expect(configuration.isIntakeUrl('https://rum.browser-intake-ddog-gov.com/api/v2/rum/xxx')).toBe(true)
expect(configuration.isIntakeUrl('https://rum-http-intake.logs.ddog-gov.com/api/v2/logs/xxx')).toBe(false)
Expand All @@ -186,7 +190,7 @@ describe('transportConfiguration', () => {

describe('on us5', () => {
it('should force alternate domains intake v2', () => {
const configuration = buildTransportConfiguration({ clientToken, site: 'us5.datadoghq.com' }, buildEnv)
const configuration = buildTransportConfiguration({ clientToken, site: 'us5.datadoghq.com' }, buildEnv, true)
expect(configuration.isIntakeUrl('https://rum.browser-intake-us5-datadoghq.com/api/v2/rum/xxx')).toBe(true)
expect(configuration.isIntakeUrl('https://logs.browser-intake-us5-datadoghq.com/api/v2/logs/xxx')).toBe(true)
})
Expand All @@ -195,35 +199,35 @@ describe('transportConfiguration', () => {

describe('when session-replay on all env', () => {
it('should force alternate domains intake v2', () => {
let configuration = buildTransportConfiguration({ clientToken }, buildEnv)
let configuration = buildTransportConfiguration({ clientToken }, buildEnv, true)
expect(
configuration.isIntakeUrl('https://session-replay.browser-intake-datadoghq.com/api/v2/session-replay/xxx')
).toBe(true)

configuration = buildTransportConfiguration({ clientToken, site: 'datadoghq.eu' }, buildEnv)
configuration = buildTransportConfiguration({ clientToken, site: 'datadoghq.eu' }, buildEnv, true)
expect(
configuration.isIntakeUrl('https://session-replay.browser-intake-datadoghq.eu/api/v2/session-replay/xxx')
).toBe(true)

configuration = buildTransportConfiguration({ clientToken, site: 'us3.datadoghq.com' }, buildEnv)
configuration = buildTransportConfiguration({ clientToken, site: 'us3.datadoghq.com' }, buildEnv, true)
expect(
configuration.isIntakeUrl('https://session-replay.browser-intake-us3-datadoghq.com/api/v2/session-replay/xxx')
).toBe(true)

configuration = buildTransportConfiguration({ clientToken, site: 'ddog-gov.com' }, buildEnv)
configuration = buildTransportConfiguration({ clientToken, site: 'ddog-gov.com' }, buildEnv, true)
expect(
configuration.isIntakeUrl('https://session-replay.browser-intake-ddog-gov.com/api/v2/session-replay/xxx')
).toBe(true)

configuration = buildTransportConfiguration({ clientToken, site: 'us5.datadoghq.com' }, buildEnv)
configuration = buildTransportConfiguration({ clientToken, site: 'us5.datadoghq.com' }, buildEnv, true)
expect(
configuration.isIntakeUrl('https://session-replay.browser-intake-us5-datadoghq.com/api/v2/session-replay/xxx')
).toBe(true)
})
})

it('should handle sites with subdomains', () => {
const configuration = buildTransportConfiguration({ clientToken, site: 'foo.datadoghq.com' }, buildEnv)
const configuration = buildTransportConfiguration({ clientToken, site: 'foo.datadoghq.com' }, buildEnv, true)
expect(configuration.isIntakeUrl('https://rum.browser-intake-foo-datadoghq.com/api/v2/rum/xxx')).toBe(true)
expect(configuration.isIntakeUrl('https://logs.browser-intake-foo-datadoghq.com/api/v2/logs/xxx')).toBe(true)
expect(
Expand Down Expand Up @@ -251,7 +255,8 @@ describe('transportConfiguration', () => {
it('should detect replica intake request with classic intake domains', () => {
const configuration = buildTransportConfiguration(
{ clientToken, site: 'datadoghq.eu', replica: { clientToken } },
{ ...buildEnv, buildMode: BuildMode.STAGING }
{ ...buildEnv, buildMode: BuildMode.STAGING },
true
)
expect(configuration.isIntakeUrl('https://rum-http-intake.logs.datadoghq.eu/v1/input/xxx')).toBe(true)
expect(configuration.isIntakeUrl('https://browser-http-intake.logs.datadoghq.eu/v1/input/xxx')).toBe(true)
Expand All @@ -269,7 +274,8 @@ describe('transportConfiguration', () => {
it('should detect replica intake request with alternate intake domains version 1', () => {
const configuration = buildTransportConfiguration(
{ clientToken, site: 'datadoghq.eu', useAlternateIntakeDomains: true, replica: { clientToken } },
{ ...buildEnv, buildMode: BuildMode.STAGING }
{ ...buildEnv, buildMode: BuildMode.STAGING },
true
)
expect(configuration.isIntakeUrl('https://rum.browser-intake-datadoghq.eu/v1/input/xxx')).toBe(true)
expect(configuration.isIntakeUrl('https://logs.browser-intake-datadoghq.eu/v1/input/xxx')).toBe(true)
Expand All @@ -287,7 +293,8 @@ describe('transportConfiguration', () => {
it('should detect replica intake request with alternate intake domains version 2', () => {
const configuration = buildTransportConfiguration(
{ clientToken, site: 'foo.com', replica: { clientToken } },
{ ...buildEnv, buildMode: BuildMode.STAGING }
{ ...buildEnv, buildMode: BuildMode.STAGING },
true
)
expect(configuration.isIntakeUrl('https://rum.browser-intake-foo.com/api/v2/rum/xxx')).toBe(true)
expect(configuration.isIntakeUrl('https://logs.browser-intake-foo.com/api/v2/logs/xxx')).toBe(true)
Expand Down
13 changes: 7 additions & 6 deletions packages/core/src/domain/configuration/transportConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ export interface ReplicaConfiguration {

export function buildTransportConfiguration(
initConfiguration: InitConfiguration,
buildEnv: BuildEnv
buildEnv: BuildEnv,
isIntakeV2Enabled?: boolean
): TransportConfiguration {
const endpointBuilder = new EndpointBuilder(initConfiguration, buildEnv)
const endpointBuilder = new EndpointBuilder(initConfiguration, buildEnv, isIntakeV2Enabled)
const intakeUrls: string[] = ENDPOINTS_TYPES.map((endpointType) => endpointBuilder.buildIntakeUrl(endpointType))

console.info(intakeUrls)
const configuration: TransportConfiguration = {
isIntakeUrl: (url: string) => intakeUrls.some((intakeUrl) => url.indexOf(intakeUrl) === 0),
logsEndpoint: endpointBuilder.build('logs'),
Expand All @@ -51,10 +52,10 @@ export function buildTransportConfiguration(
site: INTAKE_SITE_US,
applicationId: initConfiguration.replica.applicationId,
clientToken: initConfiguration.replica.clientToken,
useAlternateIntakeDomains: endpointBuilder.intakeType === 'alternate',
intakeApiVersion: endpointBuilder.intakeApiVersion,
useAlternateIntakeDomains: endpointBuilder.supportAlternateDomain(),
intakeApiVersion: endpointBuilder.supportIntakeV2() ? 2 : (1 as 1 | 2),
}
const replicaEndpointBuilder = new EndpointBuilder(replicaConfiguration, buildEnv)
const replicaEndpointBuilder = new EndpointBuilder(replicaConfiguration, buildEnv, isIntakeV2Enabled)

configuration.replica = {
applicationId: initConfiguration.replica.applicationId,
Expand Down

0 comments on commit 9bdca58

Please sign in to comment.