Skip to content
Draft
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
2 changes: 1 addition & 1 deletion packages/core/src/domain/telemetry/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type { Telemetry } from './telemetry'
export type { Telemetry, SampleRateByMetric } from './telemetry'
export {
TelemetryService,
addTelemetryDebug,
Expand Down
29 changes: 24 additions & 5 deletions packages/core/src/domain/telemetry/telemetry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ import {
startTelemetryCollection,
addTelemetryMetrics,
addTelemetryDebug,
type SampleRateByMetric,
} from './telemetry'
import type { TelemetryEvent } from './telemetryEvent.types'
import { StatusType, TelemetryType } from './rawTelemetryEvent.types'

const NETWORK_METRICS_KIND = 'Network metrics'
const PERFORMANCE_METRICS_KIND = 'Performance metrics'

function startAndSpyTelemetry(configuration?: Partial<Configuration>) {
function startAndSpyTelemetry(configuration?: Partial<Configuration>, sampleRateByMetric: SampleRateByMetric = {}) {
const observable = new Observable<TelemetryEvent & Context>()

const events: TelemetryEvent[] = []
Expand All @@ -42,7 +43,8 @@ function startAndSpyTelemetry(configuration?: Partial<Configuration>) {
...configuration,
} as Configuration,
hooks,
observable
observable,
sampleRateByMetric
)

return {
Expand Down Expand Up @@ -157,7 +159,10 @@ describe('telemetry', () => {

describe('addTelemetryMetrics', () => {
it('should collect metrics when sampled', async () => {
const { getTelemetryEvents } = startAndSpyTelemetry({ telemetrySampleRate: 100 })
const { getTelemetryEvents } = startAndSpyTelemetry(
{ telemetrySampleRate: 100 },
{ [PERFORMANCE_METRICS_KIND]: 100 }
)

addTelemetryMetrics(PERFORMANCE_METRICS_KIND, { speed: 1000 })

Expand All @@ -172,8 +177,22 @@ describe('telemetry', () => {
])
})

it('should not notify metrics when not sampled', async () => {
const { getTelemetryEvents } = startAndSpyTelemetry({ telemetrySampleRate: 0 })
it('should not notify metrics when telemetry not sampled', async () => {
const { getTelemetryEvents } = startAndSpyTelemetry(
{ telemetrySampleRate: 0 },
{ [PERFORMANCE_METRICS_KIND]: 100 }
)

addTelemetryMetrics(PERFORMANCE_METRICS_KIND, { speed: 1000 })

expect(await getTelemetryEvents()).toEqual([])
})

it('should not notify metrics when metric not sampled', async () => {
const { getTelemetryEvents } = startAndSpyTelemetry(
{ telemetrySampleRate: 100 },
{ [PERFORMANCE_METRICS_KIND]: 0 }
)

addTelemetryMetrics(PERFORMANCE_METRICS_KIND, { speed: 1000 })

Expand Down
31 changes: 27 additions & 4 deletions packages/core/src/domain/telemetry/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ export const enum TelemetryService {
export interface Telemetry {
stop: () => void
enabled: boolean
enabledMetrics: { [metricName: string]: boolean }
}

export interface SampleRateByMetric {
[metricName: string]: number
}

const TELEMETRY_EXCLUDED_SITES: string[] = [INTAKE_SITE_US1_FED]
Expand All @@ -78,25 +83,34 @@ export function startTelemetry(
hooks: AbstractHooks,
reportError: (error: RawError) => void,
pageMayExitObservable: Observable<PageMayExitEvent>,
createEncoder: (streamId: DeflateEncoderStreamId) => Encoder
createEncoder: (streamId: DeflateEncoderStreamId) => Encoder,
sampleRateByMetric: SampleRateByMetric = {}
): Telemetry {
const observable = new Observable<TelemetryEvent & Context>()

const { stop } = startTelemetryTransport(configuration, reportError, pageMayExitObservable, createEncoder, observable)

const { enabled } = startTelemetryCollection(telemetryService, configuration, hooks, observable)
const { enabled, enabledMetrics } = startTelemetryCollection(
telemetryService,
configuration,
hooks,
observable,
sampleRateByMetric
)

return {
stop,
enabled,
enabledMetrics,
}
}

export function startTelemetryCollection(
telemetryService: TelemetryService,
configuration: Configuration,
hooks: AbstractHooks,
observable: Observable<TelemetryEvent & Context>
observable: Observable<TelemetryEvent & Context>,
sampleRateByMetric: SampleRateByMetric
) {
const alreadySentEventsByKind: Record<string, Set<string>> = {}

Expand All @@ -109,10 +123,18 @@ export function startTelemetryCollection(
[TelemetryType.USAGE]: telemetryEnabled && performDraw(configuration.telemetryUsageSampleRate),
}

const telemetryEnabledPerMetrics: { [metricName: string]: boolean } = {}
Object.keys(sampleRateByMetric).forEach((metricName) => {
telemetryEnabledPerMetrics[metricName] = telemetryEnabled && performDraw(sampleRateByMetric[metricName])
})

const runtimeEnvInfo = getRuntimeEnvInfo()
const telemetryObservable = getTelemetryObservable()
telemetryObservable.subscribe(({ rawEvent, kind }) => {
if (!telemetryEnabledPerType[rawEvent.type!]) {
if (
!telemetryEnabledPerType[rawEvent.type!] ||
(kind in telemetryEnabledPerMetrics && !telemetryEnabledPerMetrics[kind])
) {
return
}

Expand Down Expand Up @@ -154,6 +176,7 @@ export function startTelemetryCollection(

return {
enabled: telemetryEnabled,
enabledMetrics: telemetryEnabledPerMetrics,
}

function toTelemetryEvent(
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export type {
TelemetryUsageEvent,
RawTelemetryUsage,
RawTelemetryUsageFeature,
SampleRateByMetric,
} from './domain/telemetry'
export {
startTelemetry,
Expand Down
2 changes: 2 additions & 0 deletions packages/rum-core/src/boot/rumPublicApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
Account,
RumInternalContext,
Telemetry,
SampleRateByMetric,
} from '@datadog/browser-core'
import {
ContextManagerMethod,
Expand Down Expand Up @@ -473,6 +474,7 @@ export interface RecorderApi {
isRecording: () => boolean
getReplayStats: (viewId: string) => ReplayStats | undefined
getSessionReplayLink: () => string | undefined
getTelemetrySampleRateByMetric: (configuration: RumConfiguration) => SampleRateByMetric | undefined
}

export interface ProfilerApi {
Expand Down
14 changes: 11 additions & 3 deletions packages/rum-core/src/boot/startRum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ import { startRumEventBridge } from '../transport/startRumEventBridge'
import { startUrlContexts } from '../domain/contexts/urlContexts'
import { createLocationChangeObservable } from '../browser/locationChangeObservable'
import type { RumConfiguration } from '../domain/configuration'
import { REMOTE_CONFIGURATION_METRIC_NAME } from '../domain/configuration'
import type { ViewOptions } from '../domain/view/trackViews'
import { startFeatureFlagContexts } from '../domain/contexts/featureFlagContext'
import { startCustomerDataTelemetry } from '../domain/startCustomerDataTelemetry'
import { startCustomerDataTelemetry, CUSTOMER_DATA_METRIC_NAME } from '../domain/startCustomerDataTelemetry'
import type { PageStateHistory } from '../domain/contexts/pageStateHistory'
import { startPageStateHistory } from '../domain/contexts/pageStateHistory'
import { startDisplayContext } from '../domain/contexts/displayContext'
Expand Down Expand Up @@ -94,13 +95,20 @@ export function startRum(
})
cleanupTasks.push(() => pageMayExitSubscription.unsubscribe())

const sampleRateByMetric = {
[CUSTOMER_DATA_METRIC_NAME]: configuration.customerDataTelemetrySampleRate,
[REMOTE_CONFIGURATION_METRIC_NAME]: configuration.remoteConfigurationTelemetrySampleRate,
...recorderApi.getTelemetrySampleRateByMetric(configuration),
}

const telemetry = startTelemetry(
TelemetryService.RUM,
configuration,
hooks,
reportError,
pageMayExitObservable,
createEncoder
createEncoder,
sampleRateByMetric
)
cleanupTasks.push(telemetry.stop)

Expand All @@ -118,7 +126,7 @@ export function startRum(
createEncoder
)
cleanupTasks.push(() => batch.stop())
startCustomerDataTelemetry(configuration, telemetry, lifeCycle, batch.flushController.flushObservable)
startCustomerDataTelemetry(telemetry, lifeCycle, batch.flushController.flushObservable)
} else {
startRumEventBridge(lifeCycle)
}
Expand Down
2 changes: 2 additions & 0 deletions packages/rum-core/src/domain/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ export interface RumConfiguration extends Configuration {
customerDataTelemetrySampleRate: number
initialViewMetricsTelemetrySampleRate: number
replayTelemetrySampleRate: number
remoteConfigurationTelemetrySampleRate: number
traceContextInjection: TraceContextInjection
plugins: RumPlugin[]
trackFeatureFlagsForEvents: FeatureFlagsForEvents[]
Expand Down Expand Up @@ -322,6 +323,7 @@ export function validateAndBuildRumConfiguration(
customerDataTelemetrySampleRate: 1,
initialViewMetricsTelemetrySampleRate: 1,
replayTelemetrySampleRate: 1,
remoteConfigurationTelemetrySampleRate: 1,
traceContextInjection: objectHasValue(TraceContextInjection, initConfiguration.traceContextInjection)
? initConfiguration.traceContextInjection
: TraceContextInjection.SAMPLED,
Expand Down
Loading