Skip to content

Commit

Permalink
Add telemetry_configuration ff and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amortemousque committed Oct 7, 2022
1 parent a9c9cee commit b63fb59
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 10 deletions.
2 changes: 2 additions & 0 deletions packages/core/src/domain/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export interface Configuration extends TransportConfiguration {
cookieOptions: CookieOptions
sampleRate: number
telemetrySampleRate: number
telemetryConfigurationSampleRate: number
service: string | undefined
silentMultipleInit: boolean

Expand Down Expand Up @@ -105,6 +106,7 @@ export function validateAndBuildConfiguration(initConfiguration: InitConfigurati
cookieOptions: buildCookieOptions(initConfiguration),
sampleRate: initConfiguration.sampleRate ?? 100,
telemetrySampleRate: initConfiguration.telemetrySampleRate ?? 20,
telemetryConfigurationSampleRate: 20,
service: initConfiguration.service,
silentMultipleInit: !!initConfiguration.silentMultipleInit,

Expand Down
48 changes: 46 additions & 2 deletions packages/core/src/domain/telemetry/telemetry.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import type { StackTrace } from '@datadog/browser-core'
import { callMonitored } from '../../tools/monitor'
import type { Configuration } from '../configuration'
import { updateExperimentalFeatures, INTAKE_SITE_US1, INTAKE_SITE_US1_FED } from '../configuration'
import { resetTelemetry, startTelemetry, scrubCustomerFrames, formatError } from './telemetry'
import {
resetExperimentalFeatures,
updateExperimentalFeatures,
INTAKE_SITE_US1,
INTAKE_SITE_US1_FED,
} from '../configuration'
import {
resetTelemetry,
startTelemetry,
scrubCustomerFrames,
formatError,
addTelemetryConfiguration,
} from './telemetry'

function startAndSpyTelemetry(configuration?: Partial<Configuration>) {
const telemetry = startTelemetry({
Expand Down Expand Up @@ -31,6 +42,39 @@ describe('telemetry', () => {
expect(notifySpy).toHaveBeenCalledTimes(1)
})

describe('addTelemetryConfiguration', () => {
afterEach(() => {
resetExperimentalFeatures()
})

it('should notify configuration when sampled and ff is enabled', () => {
updateExperimentalFeatures(['telemetry_configuration'])

const { notifySpy } = startAndSpyTelemetry({ telemetrySampleRate: 100, telemetryConfigurationSampleRate: 100 })

addTelemetryConfiguration({})

expect(notifySpy).toHaveBeenCalled()
})

it('should not notify configuration when sampled and ff is disabled', () => {
const { notifySpy } = startAndSpyTelemetry({ telemetrySampleRate: 100, telemetryConfigurationSampleRate: 100 })

addTelemetryConfiguration({})

expect(notifySpy).not.toHaveBeenCalled()
})

it('should not notify configuration when not sampled and ff is enabled', () => {
updateExperimentalFeatures(['telemetry_configuration'])
const { notifySpy } = startAndSpyTelemetry({ telemetrySampleRate: 100, telemetryConfigurationSampleRate: 0 })

addTelemetryConfiguration({})

expect(notifySpy).not.toHaveBeenCalled()
})
})

it('should contains feature flags', () => {
updateExperimentalFeatures(['foo'])
const { notifySpy } = startAndSpyTelemetry()
Expand Down
9 changes: 6 additions & 3 deletions packages/core/src/domain/telemetry/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { toStackTraceString } from '../../tools/error'
import { assign, combine, jsonStringify, performDraw, includes, startsWith, arrayFrom } from '../../tools/utils'
import type { Configuration } from '../configuration'
import {
isExperimentalFeatureEnabled,
getExperimentalFeatures,
getSimulationLabel,
INTAKE_SITE_STAGING,
Expand Down Expand Up @@ -35,13 +36,13 @@ export interface Telemetry {
}

const TELEMETRY_EXCLUDED_SITES: string[] = [INTAKE_SITE_US1_FED]
const TELEMETRY_CONFIGURATION_SAMPLE_RATE = 20

const telemetryConfiguration: {
maxEventsPerPage: number
sentEventCount: number
telemetryEnabled: boolean
} = { maxEventsPerPage: 0, sentEventCount: 0, telemetryEnabled: false }
telemetryConfigurationEnabled: boolean
} = { maxEventsPerPage: 0, sentEventCount: 0, telemetryEnabled: false, telemetryConfigurationEnabled: false }

let onRawTelemetryEventCollected: ((event: RawTelemetryEvent) => void) | undefined

Expand All @@ -50,6 +51,8 @@ export function startTelemetry(configuration: Configuration): Telemetry {
const observable = new Observable<TelemetryEvent & Context>()

telemetryConfiguration.telemetryEnabled = performDraw(configuration.telemetrySampleRate)
telemetryConfiguration.telemetryConfigurationEnabled =
telemetryConfiguration.telemetryEnabled && performDraw(configuration.telemetryConfigurationSampleRate)

onRawTelemetryEventCollected = (event: RawTelemetryEvent) => {
if (!includes(TELEMETRY_EXCLUDED_SITES, configuration.site) && telemetryConfiguration.telemetryEnabled) {
Expand Down Expand Up @@ -143,7 +146,7 @@ export function addTelemetryError(e: unknown) {
}

export function addTelemetryConfiguration(configuration: RawConfigurationTelemetryEvent['configuration']) {
if (performDraw(TELEMETRY_CONFIGURATION_SAMPLE_RATE)) {
if (isExperimentalFeatureEnabled('telemetry_configuration') && telemetryConfiguration.telemetryConfigurationEnabled) {
addTelemetry({
type: TelemetryType.configuration as const,
configuration,
Expand Down
2 changes: 1 addition & 1 deletion packages/logs/src/boot/startLogs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const COMMON_CONTEXT = {
}

describe('logs', () => {
const initConfiguration = { clientToken: 'xxx', service: 'service' }
const initConfiguration = { clientToken: 'xxx', service: 'service', telemetrySampleRate: 0 }
let baseConfiguration: LogsConfiguration
let interceptor: ReturnType<typeof interceptRequests>
let requests: Request[]
Expand Down
10 changes: 6 additions & 4 deletions test/e2e/scenario/telemetry.scenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ describe('telemetry', () => {
window.DD_LOGS!.logger.log('hop', context as any)
})
await flushEvents()
expect(serverEvents.telemetry.length).toBe(1)
const event = serverEvents.telemetry[0] as TelemetryErrorEvent

// get the last telemetry event because telemetry configuration may have been collected first
const event = serverEvents.telemetry[serverEvents.telemetry.length - 1] as TelemetryErrorEvent
expect(event.telemetry.message).toBe('bar')
expect(event.telemetry.error!.kind).toBe('Error')
expect(event.telemetry.status).toBe('error')
Expand All @@ -37,8 +38,9 @@ describe('telemetry', () => {
window.DD_RUM!.addAction('hop', context as any)
})
await flushEvents()
expect(serverEvents.telemetry.length).toBe(1)
const event = serverEvents.telemetry[0] as TelemetryErrorEvent

// get the last telemetry event because telemetry configuration may have been collected first
const event = serverEvents.telemetry[serverEvents.telemetry.length - 1] as TelemetryErrorEvent
expect(event.telemetry.message).toBe('bar')
expect(event.telemetry.error!.kind).toBe('Error')
expect(event.telemetry.status).toBe('error')
Expand Down

0 comments on commit b63fb59

Please sign in to comment.