Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔉[RUM-7840] Update remote configuration #3239

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/core/src/domain/configuration/intakeSites.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { getSiteShortName } from './intakeSites'

describe('getSiteShortName', () => {
it('should return "us1" by default', () => {
expect(getSiteShortName(undefined)).toBe('us1')
})

it('should return "staging" for staging site', () => {
expect(getSiteShortName('staging.datadoghq.com')).toBe('staging')
})

it('should return the inferred short name from subdomain-based site', () => {
expect(getSiteShortName('us3.datadoghq.com')).toBe('us3')
expect(getSiteShortName('ap1.datadoghq.com')).toBe('ap1')
})

it('should return short name for non subdomain-based site', () => {
expect(getSiteShortName('datadoghq.com')).toBe('us1')
expect(getSiteShortName('datadoghq.eu')).toBe('eu1')
expect(getSiteShortName('ddog-gov.com')).toBe('us1-fed')
})
})
24 changes: 24 additions & 0 deletions packages/core/src/domain/configuration/intakeSites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,27 @@ export const INTAKE_SITE_US1_FED = 'ddog-gov.com'

export const PCI_INTAKE_HOST_US1 = 'pci.browser-intake-datadoghq.com'
export const INTAKE_URL_PARAMETERS = ['ddsource', 'ddtags']

const predefinedSites: Record<string, string> = {
[INTAKE_SITE_STAGING]: 'staging',
[INTAKE_SITE_FED_STAGING]: 'fed-staging',
[INTAKE_SITE_US1]: 'us1',
[INTAKE_SITE_EU1]: 'eu1',
[INTAKE_SITE_US1_FED]: 'us1-fed',
}

export function getSiteShortName(site: string | undefined) {
if (!site) {
return 'us1'
}

if (predefinedSites[site]) {
return predefinedSites[site]
}

// Infer short names for subdomain-based datacenters to support for new DCs without code changes.
const match = /^([a-z0-9]+)\.datadoghq\.com$/.exec(site)
if (match) {
return match[1]
}
}
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export {
INTAKE_SITE_US1_FED,
INTAKE_SITE_EU1,
INTAKE_URL_PARAMETERS,
getSiteShortName,
isIntakeUrl,
} from './domain/configuration'
export { TrackingConsent, TrackingConsentState, createTrackingConsentState } from './domain/trackingConsent'
Expand Down
17 changes: 14 additions & 3 deletions packages/rum-core/src/boot/preStartRum.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ import {
mockExperimentalFeatures,
mockSyntheticsWorkerValues,
} from '@datadog/browser-core/test'
import type { HybridInitConfiguration, RumConfiguration, RumInitConfiguration } from '../domain/configuration'
import type {
HybridInitConfiguration,
RemoteConfigurationEvent,
RumConfiguration,
RumInitConfiguration,
} from '../domain/configuration'
import type { CommonContext } from '../domain/contexts/commonContext'
import type { ViewOptions } from '../domain/view/trackViews'
import { ActionType, VitalType } from '../rawRumEvent.types'
Expand All @@ -34,6 +39,12 @@ import type { StartRumResult } from './startRum'
import { createPreStartStrategy } from './preStartRum'

const DEFAULT_INIT_CONFIGURATION = { applicationId: 'xxx', clientToken: 'xxx' }
const FAKE_REMOTE_CONFIGURATION_EVENT: RemoteConfigurationEvent = {
rum: {
application_id: 'remote_application_id',
session_sample_rate: 50,
},
}
const INVALID_INIT_CONFIGURATION = { clientToken: 'yes' } as RumInitConfiguration
const AUTO_CONFIGURATION = { ...DEFAULT_INIT_CONFIGURATION }
const MANUAL_CONFIGURATION = { ...AUTO_CONFIGURATION, trackViewsManually: true }
Expand Down Expand Up @@ -463,7 +474,7 @@ describe('preStartRum', () => {
mockExperimentalFeatures([ExperimentalFeature.REMOTE_CONFIGURATION])

interceptor.withMockXhr((xhr) => {
xhr.complete(200, '{"sessionSampleRate":50}')
xhr.complete(200, JSON.stringify(FAKE_REMOTE_CONFIGURATION_EVENT))

expect(doStartRumSpy.calls.mostRecent().args[0].sessionSampleRate).toEqual(50)
done()
Expand Down Expand Up @@ -636,7 +647,7 @@ describe('preStartRum', () => {
it('returns the initConfiguration with the remote configuration when a remoteConfigurationId is provided', (done) => {
addExperimentalFeatures([ExperimentalFeature.REMOTE_CONFIGURATION])
interceptor.withMockXhr((xhr) => {
xhr.complete(200, '{"sessionSampleRate":50}')
xhr.complete(200, JSON.stringify(FAKE_REMOTE_CONFIGURATION_EVENT))

expect(strategy.initConfiguration?.sessionSampleRate).toEqual(50)
done()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
import { DefaultPrivacyLevel, display } from '@datadog/browser-core'
import { interceptRequests } from '@datadog/browser-core/test'
import type { RumInitConfiguration } from './configuration'
import type { RemoteConfigurationEvent } from './remoteConfiguration'
import { applyRemoteConfiguration, fetchRemoteConfiguration } from './remoteConfiguration'

const DEFAULT_INIT_CONFIGURATION = {
application_id: 'xxx',
clientToken: 'xxx',
applicationId: 'xxx',
samplingRate: 100,
sessionReplaySamplingRate: 100,
sessionSampleRate: 100,
sessionReplaySampleRate: 100,
defaultPrivacyLevel: DefaultPrivacyLevel.MASK,
} as RumInitConfiguration

const FAKE_REMOTE_CONFIGURATION_EVENT: RemoteConfigurationEvent = {
rum: {
application_id: 'remote_application_id',
service: 'remote_service',
version: 'remote_version',
session_sample_rate: 50,
session_replay_sample_rate: 50,
default_privacy_level: DefaultPrivacyLevel.ALLOW,
enable_privacy_for_action_name: true,
},
}

describe('remoteConfiguration', () => {
let displayErrorSpy: jasmine.Spy<typeof display.error>
let interceptor: ReturnType<typeof interceptRequests>
Expand All @@ -30,12 +44,36 @@ describe('remoteConfiguration', () => {

it('should fetch the remote configuration', (done) => {
interceptor.withMockXhr((xhr) => {
xhr.complete(200, '{"sessionSampleRate":50,"sessionReplaySampleRate":50,"defaultPrivacyLevel":"allow"}')
xhr.complete(200, JSON.stringify(FAKE_REMOTE_CONFIGURATION_EVENT))

expect(remoteConfigurationCallback).toHaveBeenCalledWith({
applicationId: 'remote_application_id',
service: 'remote_service',
version: 'remote_version',
sessionSampleRate: 50,
sessionReplaySampleRate: 50,
defaultPrivacyLevel: DefaultPrivacyLevel.ALLOW,
enablePrivacyForActionName: true,
})

done()
})
fetchRemoteConfiguration(configuration, remoteConfigurationCallback)
})

it('should fetch the remote configuration with unknown options', (done) => {
interceptor.withMockXhr((xhr) => {
xhr.complete(
200,
JSON.stringify({
rum: {
unknown: 'unknown',
},
})
)

expect(remoteConfigurationCallback).toHaveBeenCalledWith({
unknown: 'unknown',
})

done()
Expand All @@ -55,15 +93,28 @@ describe('remoteConfiguration', () => {
})

describe('applyRemoteConfiguration', () => {
it('should override the iniConfiguration options with the ones from the remote configuration', () => {
it('should override the iniConfiguration options with remote configuration options', () => {
const remoteConfiguration = {
samplingRate: 1,
sessionReplaySamplingRate: 1,
applicationId: 'remote_application_id',
service: 'remote_service',
version: 'remote_version',
sessionSampleRate: 50,
sessionReplaySampleRate: 50,
defaultPrivacyLevel: DefaultPrivacyLevel.ALLOW,
enablePrivacyForActionName: true,
}
expect(applyRemoteConfiguration(DEFAULT_INIT_CONFIGURATION, remoteConfiguration)).toEqual(
jasmine.objectContaining(remoteConfiguration)
)
})

it('should merge the iniConfiguration options with unknown remote configuration options', () => {
const remoteConfiguration = {
unknown: 'unknown',
} as Partial<RumInitConfiguration>
expect(applyRemoteConfiguration(DEFAULT_INIT_CONFIGURATION, remoteConfiguration)).toEqual(
jasmine.objectContaining(remoteConfiguration)
)
})
})
})
55 changes: 45 additions & 10 deletions packages/rum-core/src/domain/configuration/remoteConfiguration.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
import type { DefaultPrivacyLevel } from '@datadog/browser-core'
import { display, addEventListener, assign } from '@datadog/browser-core'
import { display, addEventListener, getSiteShortName, assign } from '@datadog/browser-core'
import type { RumInitConfiguration } from './configuration'

export const REMOTE_CONFIGURATION_URL = 'https://d3uc069fcn7uxw.cloudfront.net/configuration'
export const REMOTE_CONFIGURATION_ORIGIN = 'http://dt887evijcmkm.cloudfront.net'
const REMOTE_CONFIGURATION_VERSION = 'v1'

export interface RumRemoteConfiguration {
sessionSampleRate?: number
sessionReplaySampleRate?: number
defaultPrivacyLevel?: DefaultPrivacyLevel
export interface RemoteConfigurationEvent {
rum: {
application_id: string
service?: string
version?: string
session_sample_rate?: number
session_replay_sample_rate?: number
default_privacy_level?: DefaultPrivacyLevel
enable_privacy_for_action_name?: boolean
}
}

/**
* Fetches and applies the remote configuration.
* The logic enables adding new options without requiring code updates.
*
* - `snakeToCamelCaseKeys` is used to transform the RC event into a partial RUM init configuration.
* - `assign` is used to merge the remote configuration with the existing RUM init configuration.
*
*/
export function fetchAndApplyRemoteConfiguration(
initConfiguration: RumInitConfiguration,
callback: (initConfiguration: RumInitConfiguration) => void
Expand All @@ -21,20 +36,22 @@ export function fetchAndApplyRemoteConfiguration(

export function applyRemoteConfiguration(
initConfiguration: RumInitConfiguration,
remoteInitConfiguration: RumRemoteConfiguration
remoteInitConfiguration: Partial<RumInitConfiguration>
) {
return assign({}, initConfiguration, remoteInitConfiguration)
}

export function fetchRemoteConfiguration(
configuration: RumInitConfiguration,
callback: (remoteConfiguration: RumRemoteConfiguration) => void
callback: (remoteConfiguration: Partial<RumInitConfiguration>) => void
) {
const xhr = new XMLHttpRequest()

addEventListener(configuration, xhr, 'load', function () {
if (xhr.status === 200) {
callback(JSON.parse(xhr.responseText))
const remoteConfigurationEvent = JSON.parse(xhr.responseText)
const remoteConfiguration = snakeToCamelCase(remoteConfigurationEvent.rum)
callback(remoteConfiguration)
} else {
displayRemoteConfigurationFetchingError()
}
Expand All @@ -44,10 +61,28 @@ export function fetchRemoteConfiguration(
displayRemoteConfigurationFetchingError()
})

xhr.open('GET', `${REMOTE_CONFIGURATION_URL}/${encodeURIComponent(configuration.remoteConfigurationId!)}.json`)
xhr.open('GET', buildRemoteConfigurationUrl(configuration))
xhr.send()
}

function buildRemoteConfigurationUrl(configuration: RumInitConfiguration) {
return `${REMOTE_CONFIGURATION_ORIGIN}/${getSiteShortName(configuration.site)}/${REMOTE_CONFIGURATION_VERSION}/${encodeURIComponent(configuration.remoteConfigurationId!)}.json`
}

function displayRemoteConfigurationFetchingError() {
display.error('Error fetching the remote configuration.')
}

function snakeToCamelCase(obj: object): any {
if (obj && typeof obj === 'object') {
return Object.entries(obj).reduce(
(acc, [key, value]) => {
const camelKey = key.replace(/_([a-z])/g, (_, letter: string) => letter.toUpperCase()) // Convert to camelCase
acc[camelKey] = snakeToCamelCase(value)
return acc
},
{} as Record<string, any>
)
}
return obj
}
2 changes: 1 addition & 1 deletion packages/rum-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export { RumSessionManager, RumSession } from './domain/rumSessionManager'
export { getMutationObserverConstructor } from './browser/domMutationObservable'
export { initViewportObservable, getViewportDimension, ViewportDimension } from './browser/viewportObservable'
export { getScrollX, getScrollY } from './browser/scroll'
export { RumInitConfiguration, RumConfiguration, RumRemoteConfiguration } from './domain/configuration'
export { RumInitConfiguration, RumConfiguration } from './domain/configuration'
export { DEFAULT_PROGRAMMATIC_ACTION_NAME_ATTRIBUTE } from './domain/action/getActionNameFromElement'
export { STABLE_ATTRIBUTES } from './domain/getSelectorFromElement'
export * from './browser/htmlDomUtils'
Expand Down
Loading