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

🔊 Deduplicate telemetry events #2746

Merged
merged 4 commits into from
May 14, 2024
Merged
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
34 changes: 32 additions & 2 deletions packages/core/src/domain/telemetry/telemetry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { Configuration } from '../configuration'
import { INTAKE_SITE_US1, INTAKE_SITE_US1_FED } from '../configuration'
import { setNavigatorOnLine, setNavigatorConnection } from '../../../test'
import {
addTelemetryError,
resetTelemetry,
startTelemetry,
scrubCustomerFrames,
Expand All @@ -20,6 +21,7 @@ function startAndSpyTelemetry(configuration?: Partial<Configuration>) {
const telemetry = startTelemetry(TelemetryService.RUM, {
maxTelemetryEventsPerPage: 7,
telemetrySampleRate: 100,
telemetryUsageSampleRate: 100,
...configuration,
} as Configuration)
const notifySpy = jasmine.createSpy('notified')
Expand Down Expand Up @@ -145,13 +147,13 @@ describe('telemetry', () => {
foo: 'bar',
}))
callMonitored(() => {
throw new Error('message')
throw new Error('foo')
})
expect(notifySpy.calls.mostRecent().args[0].foo).toEqual('bar')

telemetry.setContextProvider(() => ({}))
callMonitored(() => {
throw new Error('message')
throw new Error('bar')
})
expect(notifySpy.calls.mostRecent().args[0].foo).not.toBeDefined()
})
Expand Down Expand Up @@ -181,6 +183,34 @@ describe('telemetry', () => {
})
})

describe('deduplicating', () => {
it('should discard already sent telemetry', () => {
const { notifySpy } = startAndSpyTelemetry()
const fooError = new Error('foo')
const barError = new Error('bar')

addTelemetryError(fooError)
addTelemetryError(fooError)
addTelemetryError(barError)

expect(notifySpy).toHaveBeenCalledTimes(2)
expect(notifySpy.calls.argsFor(0)[0].telemetry.message).toEqual('foo')
expect(notifySpy.calls.argsFor(1)[0].telemetry.message).toEqual('bar')
})

it('should not consider a discarded event for the maxTelemetryEventsPerPage', () => {
const { notifySpy } = startAndSpyTelemetry({ maxTelemetryEventsPerPage: 2 })

addTelemetryUsage({ feature: 'stop-session' })
addTelemetryUsage({ feature: 'stop-session' })
addTelemetryUsage({ feature: 'start-session-replay-recording' })

expect(notifySpy).toHaveBeenCalledTimes(2)
expect(notifySpy.calls.argsFor(0)[0].telemetry.usage.feature).toEqual('stop-session')
expect(notifySpy.calls.argsFor(1)[0].telemetry.usage.feature).toEqual('start-session-replay-recording')
})
})

describe('excluded sites', () => {
;[
{ site: INTAKE_SITE_US1_FED, enabled: false },
Expand Down
29 changes: 9 additions & 20 deletions packages/core/src/domain/telemetry/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,12 @@ export interface Telemetry {

const TELEMETRY_EXCLUDED_SITES: string[] = [INTAKE_SITE_US1_FED]

const telemetryConfiguration: {
maxEventsPerPage: number
sentEventCount: number
} = {
maxEventsPerPage: 0,
sentEventCount: 0,
}

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

export function startTelemetry(telemetryService: TelemetryService, configuration: Configuration): Telemetry {
let contextProvider: () => Context
const observable = new Observable<TelemetryEvent & Context>()
const alreadySentEvents = new Set<string>()

const telemetryEnabled =
!includes(TELEMETRY_EXCLUDED_SITES, configuration.site) && performDraw(configuration.telemetrySampleRate)
Expand All @@ -76,19 +69,20 @@ export function startTelemetry(telemetryService: TelemetryService, configuration

const runtimeEnvInfo = getRuntimeEnvInfo()
onRawTelemetryEventCollected = (rawEvent: RawTelemetryEvent) => {
if (telemetryEnabledPerType[rawEvent.type!]) {
const stringifiedEvent = jsonStringify(rawEvent)!
bcaudan marked this conversation as resolved.
Show resolved Hide resolved
if (
telemetryEnabledPerType[rawEvent.type!] &&
alreadySentEvents.size < configuration.maxTelemetryEventsPerPage &&
!alreadySentEvents.has(stringifiedEvent)
) {
const event = toTelemetryEvent(telemetryService, rawEvent, runtimeEnvInfo)
observable.notify(event)
sendToExtension('telemetry', event)
alreadySentEvents.add(stringifiedEvent)
}
}
startMonitorErrorCollection(addTelemetryError)

assign(telemetryConfiguration, {
maxEventsPerPage: configuration.maxTelemetryEventsPerPage,
sentEventCount: 0,
})

function toTelemetryEvent(
telemetryService: TelemetryService,
event: RawTelemetryEvent,
Expand Down Expand Up @@ -131,10 +125,6 @@ function getRuntimeEnvInfo(): RuntimeEnvInfo {

export function startFakeTelemetry() {
const events: RawTelemetryEvent[] = []
assign(telemetryConfiguration, {
maxEventsPerPage: Infinity,
sentEventCount: 0,
})

onRawTelemetryEventCollected = (event: RawTelemetryEvent) => {
events.push(event)
Expand Down Expand Up @@ -197,8 +187,7 @@ export function addTelemetryUsage(usage: RawTelemetryUsage) {
}

function addTelemetry(event: RawTelemetryEvent) {
if (onRawTelemetryEventCollected && telemetryConfiguration.sentEventCount < telemetryConfiguration.maxEventsPerPage) {
telemetryConfiguration.sentEventCount += 1
if (onRawTelemetryEventCollected) {
onRawTelemetryEventCollected(event)
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/logs/src/domain/configuration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe('validateAndBuildLogsConfiguration', () => {
it('should display warning with wrong PCI intake configuration', () => {
validateAndBuildLogsConfiguration({
...DEFAULT_INIT_CONFIGURATION,
site: 'some-site',
site: 'us3.datadoghq.com',
usePciIntake: true,
})
expect(warnSpy).toHaveBeenCalledOnceWith(
Expand Down