Skip to content

Commit

Permalink
🎨 [RUM] Precompute intake urls
Browse files Browse the repository at this point in the history
  • Loading branch information
webNeat committed Nov 20, 2020
1 parent 193cfae commit 454a50e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 58 deletions.
5 changes: 1 addition & 4 deletions packages/core/src/boot/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ export const INTAKE_SITE = {
[Datacenter.US]: 'datadoghq.com',
}

export const STAGING_INTAKE_SITE = {
[Datacenter.EU]: 'datad0g.eu',
[Datacenter.US]: 'datad0g.com',
}
export const NEW_INTAKE_DOMAIN_ALLOWED_SITES = [INTAKE_SITE[Datacenter.US], 'datad0g.com']

export enum BuildMode {
RELEASE = 'release',
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/domain/configuration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ describe('configuration', () => {

expect(isIntakeRequest('https://rum.browser-intake-datadoghq.com/v1/input/xxx', configuration)).toBe(true)
expect(isIntakeRequest('https://logs.browser-intake-datadoghq.com/v1/input/xxx', configuration)).toBe(true)
expect(isIntakeRequest('https://rum-http-intake.logs.datadoghq.com/v1/input/xxx', configuration)).toBe(true)
expect(isIntakeRequest('https://browser-http-intake.logs.datadoghq.com/v1/input/xxx', configuration)).toBe(true)
})
})
})
100 changes: 46 additions & 54 deletions packages/core/src/domain/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BuildEnv, BuildMode, Datacenter, INTAKE_SITE, STAGING_INTAKE_SITE } from '../boot/init'
import { BuildEnv, BuildMode, Datacenter, INTAKE_SITE, NEW_INTAKE_DOMAIN_ALLOWED_SITES } from '../boot/init'
import { CookieOptions, getCurrentSite } from '../browser/cookie'
import { getOrigin, getPathName, haveSameOrigin } from '../tools/urlPolyfill'
import { includes, ONE_KILO_BYTE, ONE_SECOND } from '../tools/utils'
Expand Down Expand Up @@ -76,7 +76,7 @@ export type Configuration = typeof DEFAULT_CONFIGURATION & {
internalMonitoringEndpoint?: string
proxyHost?: string

legacyEndpoints: string[]
intakeUrls: string[]

service?: string

Expand Down Expand Up @@ -107,9 +107,9 @@ interface TransportConfiguration {
}

enum EndpointType {
BROWSER = 'browser',
LOGS = 'logs',
RUM = 'rum',
TRACE = 'public-trace',
TRACE = 'trace',
}

export function buildConfiguration(userConfiguration: UserConfiguration, buildEnv: BuildEnv): Configuration {
Expand All @@ -134,28 +134,21 @@ export function buildConfiguration(userConfiguration: UserConfiguration, buildEn
isEnabled: (feature: string) => {
return includes(enableExperimentalFeatures, feature)
},
logsEndpoint: getEndpoint(EndpointType.BROWSER, transportConfiguration),
logsEndpoint: getEndpoint(EndpointType.LOGS, transportConfiguration),
proxyHost: userConfiguration.proxyHost,
rumEndpoint: getEndpoint(EndpointType.RUM, transportConfiguration),
service: userConfiguration.service,
traceEndpoint: getEndpoint(EndpointType.TRACE, transportConfiguration),

legacyEndpoints: [
getEndpoint(EndpointType.BROWSER, transportConfiguration, undefined, true),
getEndpoint(EndpointType.RUM, transportConfiguration, undefined, true),
getEndpoint(EndpointType.TRACE, transportConfiguration, undefined, true),
],
intakeUrls: getIntakeUrls(transportConfiguration),
...DEFAULT_CONFIGURATION,
}
if (userConfiguration.internalMonitoringApiKey) {
configuration.internalMonitoringEndpoint = getEndpoint(
EndpointType.BROWSER,
EndpointType.LOGS,
transportConfiguration,
'browser-agent-internal-monitoring'
)
configuration.legacyEndpoints.push(
getEndpoint(EndpointType.BROWSER, transportConfiguration, 'browser-agent-internal-monitoring', true)
)
}

if ('allowedTracingOrigins' in userConfiguration) {
Expand Down Expand Up @@ -191,18 +184,16 @@ export function buildConfiguration(userConfiguration: UserConfiguration, buildEn
configuration.replica = {
applicationId: userConfiguration.replica.applicationId,
internalMonitoringEndpoint: getEndpoint(
EndpointType.BROWSER,
EndpointType.LOGS,
replicaTransportConfiguration,
'browser-agent-internal-monitoring'
),
logsEndpoint: getEndpoint(EndpointType.BROWSER, replicaTransportConfiguration),
logsEndpoint: getEndpoint(EndpointType.LOGS, replicaTransportConfiguration),
rumEndpoint: getEndpoint(EndpointType.RUM, replicaTransportConfiguration),
}
configuration.legacyEndpoints.push(
getEndpoint(EndpointType.BROWSER, replicaTransportConfiguration, 'browser-agent-internal-monitoring', true),
getEndpoint(EndpointType.BROWSER, replicaTransportConfiguration, undefined, true),
getEndpoint(EndpointType.RUM, replicaTransportConfiguration, undefined, true)
)
if (transportConfiguration.site !== INTAKE_SITE[Datacenter.US]) {
configuration.intakeUrls.push(...getIntakeUrls(replicaTransportConfiguration))
}
}
}

Expand All @@ -222,58 +213,59 @@ export function buildCookieOptions(userConfiguration: UserConfiguration) {
return cookieOptions
}

function getEndpoint(type: EndpointType, conf: TransportConfiguration, source?: string, withLegacyHost = false) {
function getEndpoint(type: EndpointType, conf: TransportConfiguration, source?: string) {
const tags =
`sdk_version:${conf.sdkVersion}` +
`${conf.env ? `,env:${conf.env}` : ''}` +
`${conf.service ? `,service:${conf.service}` : ''}` +
`${conf.version ? `,version:${conf.version}` : ''}`
const datadogHost = getHost(type, conf, withLegacyHost)
const datadogHost = getHost(type, conf)
const host = conf.proxyHost ? conf.proxyHost : datadogHost
const proxyParameter = conf.proxyHost ? `ddhost=${datadogHost}&` : ''
const applicationIdParameter = conf.applicationId ? `_dd.application_id=${conf.applicationId}&` : ''
const parameters = `${applicationIdParameter}${proxyParameter}ddsource=${source || 'browser'}&ddtags=${tags}`

return `https://${host}/v1/input/${conf.clientToken}?${parameters}`
}
function getHost(type: EndpointType, conf: TransportConfiguration, useLegacyDomain: boolean) {
if (!useLegacyDomain) {
const subdomains = {
[EndpointType.BROWSER]: 'logs',
[EndpointType.RUM]: 'rum',
[EndpointType.TRACE]: 'trace',
}
if (conf.site === INTAKE_SITE[Datacenter.US]) {
return `${subdomains[type]}.browser-intake-datadoghq.com`
}
if (conf.site === STAGING_INTAKE_SITE[Datacenter.US]) {
return `${subdomains[type]}.browser-intake-datad0g.com`
}
}
return `${type}-http-intake.logs.${conf.site}`
}

export function isIntakeRequest(url: string, configuration: Configuration) {
if (getPathName(url).indexOf('/v1/input/') !== -1) {
const origins = getIntakeEndpoints(configuration).map(getOrigin)
return origins.indexOf(getOrigin(url)) !== -1
function getHost(type: EndpointType, conf: TransportConfiguration) {
if (NEW_INTAKE_DOMAIN_ALLOWED_SITES.indexOf(conf.site) !== -1) {
return `${type}.browser-intake-${conf.site}`
}
return false
const oldTypes = {
[EndpointType.LOGS]: 'browser',
[EndpointType.RUM]: 'rum',
[EndpointType.TRACE]: 'public-trace',
}
return `${oldTypes[type]}-http-intake.logs.${conf.site}`
}

function getIntakeEndpoints(configuration: Configuration) {
const endpoints = [configuration.logsEndpoint, configuration.rumEndpoint, configuration.traceEndpoint]
if (!!configuration.internalMonitoringEndpoint) {
endpoints.push(configuration.internalMonitoringEndpoint)
function getIntakeUrls(conf: TransportConfiguration) {
if (conf.proxyHost) {
return [`https://${conf.proxyHost}/v1/input/`]
}
if (!!configuration.replica) {
endpoints.push(
configuration.replica.logsEndpoint,
configuration.replica.rumEndpoint,
configuration.replica.internalMonitoringEndpoint
const domains = [
`https://rum-http-intake.logs.${conf.site}/v1/input/`,
`https://browser-http-intake.logs.${conf.site}/v1/input/`,
`https://public-trace-http-intake.logs.${conf.site}/v1/input/`,
]
if (NEW_INTAKE_DOMAIN_ALLOWED_SITES.indexOf(conf.site) !== -1) {
domains.push(
`https://rum.browser-intake-${conf.site}/v1/input/`,
`https://logs.browser-intake-${conf.site}/v1/input/`,
`https://trace.browser-intake-${conf.site}/v1/input/`
)
}
return endpoints.concat(configuration.legacyEndpoints || [])
return domains
}

export function isIntakeRequest(url: string, configuration: Configuration) {
for (const intakeUrl of configuration.intakeUrls) {
if (url.indexOf(intakeUrl) === 0) {
return true
}
}
return false
}

function mustUseSecureCookie(userConfiguration: UserConfiguration) {
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/tools/specHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import { Configuration } from '../domain/configuration'
import { noop, objectEntries } from './utils'

export const SPEC_ENDPOINTS: Partial<Configuration> = {
intakeUrls: [
'https://monitoring-intake.com/v1/input/',
'https://logs-intake.com/v1/input/',
'https://rum-intake.com/v1/input/',
'https://trace-intake.com/v1/input/',
],
internalMonitoringEndpoint: 'https://monitoring-intake.com/v1/input/abcde?foo=bar',
logsEndpoint: 'https://logs-intake.com/v1/input/abcde?foo=bar',
rumEndpoint: 'https://rum-intake.com/v1/input/abcde?foo=bar',
Expand Down

0 comments on commit 454a50e

Please sign in to comment.