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

✅ Add e2e test for telemetry usage #3222

Open
wants to merge 1 commit 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
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export {
TelemetryErrorEvent,
TelemetryDebugEvent,
TelemetryConfigurationEvent,
TelemetryUsageEvent,
TelemetryService,
isTelemetryReplicationAllowed,
addTelemetryConfiguration,
Expand Down
10 changes: 7 additions & 3 deletions test/e2e/lib/framework/createTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,23 @@ import { createMockServerApp } from './serverApps/mock'
const DEFAULT_RUM_CONFIGURATION = {
applicationId: APPLICATION_ID,
clientToken: CLIENT_TOKEN,
sessionReplaySampleRate: 100,
defaultPrivacyLevel: DefaultPrivacyLevel.ALLOW,
trackResources: true,
trackLongTasks: true,
telemetrySampleRate: 100,
telemetryConfigurationSampleRate: 100,
enableExperimentalFeatures: [],
allowUntrustedEvents: true,
// Force All sample rates to 100% to avoid flakiness
sessionReplaySampleRate: 100,
telemetrySampleRate: 100,
telemetryUsageSampleRate: 100,
telemetryConfigurationSampleRate: 100,
Comment on lines +26 to +30
Copy link
Collaborator Author

@thomas-lebeau thomas-lebeau Dec 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think ideally we should have test that are more resilient to sample rates:

  • only set them where needed (when we test a feature related to it)
  • explicitly trigger an event instead of relying on telemety event:

for example, in here the logs SDK is considered started because we have a telemetry configuration event and it works because we force telemetry configuration sample rate to 100%, but that's not very explicit in the test itself.

}

const DEFAULT_LOGS_CONFIGURATION = {
clientToken: CLIENT_TOKEN,
// Force All sample rates to 100% to avoid flakiness
telemetrySampleRate: 100,
telemetryUsageSampleRate: 100,
telemetryConfigurationSampleRate: 100,
}

Expand Down
15 changes: 14 additions & 1 deletion test/e2e/lib/framework/intakeRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import type {
RumViewEvent,
RumVitalEvent,
} from '@datadog/browser-rum'
import type { TelemetryEvent, TelemetryErrorEvent, TelemetryConfigurationEvent } from '@datadog/browser-core'
import type {
TelemetryEvent,
TelemetryErrorEvent,
TelemetryConfigurationEvent,
TelemetryUsageEvent,
} from '@datadog/browser-core'
import type { BrowserSegment } from '@datadog/browser-rum/src/types'
import type { BrowserSegmentMetadataAndSegmentSizes } from '@datadog/browser-rum/src/domain/segmentCollection'

Expand Down Expand Up @@ -121,6 +126,10 @@ export class IntakeRegistry {
return this.telemetryEvents.filter(isTelemetryConfigurationEvent)
}

get telemetryUsageEvents() {
return this.telemetryEvents.filter(isTelemetryUsageEvent)
}

//
// Replay
//
Expand Down Expand Up @@ -185,3 +194,7 @@ function isTelemetryErrorEvent(event: TelemetryEvent): event is TelemetryErrorEv
function isTelemetryConfigurationEvent(event: TelemetryEvent): event is TelemetryConfigurationEvent {
return isTelemetryEvent(event) && event.telemetry.type === 'configuration'
}

function isTelemetryUsageEvent(event: TelemetryEvent): event is TelemetryUsageEvent {
return isTelemetryEvent(event) && event.telemetry.type === 'usage'
}
30 changes: 30 additions & 0 deletions test/e2e/scenario/telemetry.scenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,34 @@ describe('telemetry', () => {
expect(event.service).toEqual('browser-rum-sdk')
expect(event.telemetry.configuration.track_user_interactions).toEqual(true)
})

createTest('send usage telemetry for RUM')
.withSetup(bundleSetup)
.withRum()
.run(async ({ intakeRegistry }) => {
await browser.execute(() => {
window.DD_RUM!.addAction('foo')
})

await flushEvents()
expect(intakeRegistry.telemetryUsageEvents.length).toBe(2)
const event = intakeRegistry.telemetryUsageEvents[1] // first event is 'set-global-context' done in pageSetup.ts
expect(event.service).toEqual('browser-rum-sdk')
expect(event.telemetry.usage.feature).toEqual('add-action')
})

createTest('send usage telemetry for logs')
.withSetup(bundleSetup)
.withLogs()
.run(async ({ intakeRegistry }) => {
await browser.execute(() => {
window.DD_LOGS!.setTrackingConsent('granted')
})

await flushEvents()
expect(intakeRegistry.telemetryUsageEvents.length).toBe(1)
const event = intakeRegistry.telemetryUsageEvents[0]
expect(event.service).toEqual('browser-logs-sdk')
expect(event.telemetry.usage.feature).toEqual('set-tracking-consent')
})
})
Loading