Skip to content

Commit

Permalink
Merge branch 'main' into congyao/RUM-3902-privacy-control-action-name
Browse files Browse the repository at this point in the history
  • Loading branch information
cy-moi committed Apr 22, 2024
2 parents f4e1a28 + 75e8f93 commit f0dacd9
Show file tree
Hide file tree
Showing 38 changed files with 1,000 additions and 553 deletions.
23 changes: 21 additions & 2 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
variables:
CURRENT_STAGING: staging-16
CURRENT_STAGING: staging-17
APP: 'browser-sdk'
CURRENT_CI_IMAGE: 63
BUILD_STABLE_REGISTRY: '486234852809.dkr.ecr.us-east-1.amazonaws.com'
Expand Down Expand Up @@ -140,7 +140,17 @@ build-and-lint:
- yarn build
- yarn lint
- node scripts/check-packages.js
- node scripts/report-bundle-size/index.js

test-performance:
extends:
- .base-configuration
- .test-allowed-branches
interruptible: true
script:
- yarn
- yarn build:bundle
- node ./scripts/deploy/deploy.js staging pull-request pull-request
- node scripts/performance/index.js

build-bundle:
extends:
Expand Down Expand Up @@ -211,6 +221,15 @@ check-release:
- BUILD_MODE=release yarn build
- node scripts/release/check-release.js

check-schemas:
extends:
- .base-configuration
- .test-allowed-branches
interruptible: true
script:
- yarn
- node scripts/check-schemas.js

unit-bs:
stage: browserstack
extends:
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:20.12.1-buster-slim
FROM node:20.12.2-buster-slim

ARG CHROME_PACKAGE_VERSION

Expand Down
14 changes: 7 additions & 7 deletions developer-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@
"dev": "webpack --mode development --watch"
},
"devDependencies": {
"@tabler/icons-react": "3.1.0",
"@tabler/icons-react": "3.2.0",
"@types/chrome": "0.0.266",
"@types/react": "18.2.74",
"@types/react-dom": "18.2.24",
"@types/react": "18.2.79",
"@types/react-dom": "18.2.25",
"@webextension-toolbox/webpack-webextension-plugin": "3.3.1",
"copy-webpack-plugin": "12.0.2",
"css-loader": "7.1.0",
"css-loader": "6.11.0",
"html-webpack-plugin": "5.6.0",
"style-loader": "3.3.4",
"style-loader": "4.0.0",
"webpack": "5.91.0"
},
"dependencies": {
"@datadog/browser-core": "workspace:*",
"@datadog/browser-logs": "workspace:*",
"@datadog/browser-rum": "workspace:*",
"@mantine/core": "7.7.1",
"@mantine/hooks": "7.7.1",
"@mantine/core": "7.8.0",
"@mantine/hooks": "7.8.0",
"clsx": "2.1.0",
"react": "18.2.0",
"react-dom": "18.2.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,13 @@ function TelemetryDescription({ event }: { event: TelemetryEvent }) {
if (event.telemetry.type === 'configuration') {
return <Emphasis>Configuration</Emphasis>
}
if (event.telemetry.type === 'usage') {
return (
<>
<Emphasis>Usage</Emphasis> of <Emphasis>{event.telemetry.usage.feature}</Emphasis>
</>
)
}
return <>{event.telemetry.message}</>
}

Expand Down
22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@
"@types/cors": "2.8.17",
"@types/express": "4.17.21",
"@types/jasmine": "3.10.18",
"@typescript-eslint/eslint-plugin": "7.5.0",
"@typescript-eslint/parser": "7.5.0",
"@wdio/browserstack-service": "8.35.1",
"@wdio/cli": "8.35.1",
"@wdio/jasmine-framework": "8.35.1",
"@wdio/junit-reporter": "8.32.4",
"@wdio/local-runner": "8.35.1",
"@wdio/spec-reporter": "8.32.4",
"@typescript-eslint/eslint-plugin": "7.7.0",
"@typescript-eslint/parser": "7.7.0",
"@wdio/browserstack-service": "8.36.0",
"@wdio/cli": "8.36.0",
"@wdio/jasmine-framework": "8.36.0",
"@wdio/junit-reporter": "8.36.0",
"@wdio/local-runner": "8.36.0",
"@wdio/spec-reporter": "8.36.0",
"ajv": "6.12.6",
"browserstack-local": "1.5.5",
"chrome-webstore-upload": "3.0.3",
Expand Down Expand Up @@ -79,8 +79,8 @@
"ts-loader": "9.5.1",
"ts-node": "10.9.2",
"tsconfig-paths-webpack-plugin": "4.1.0",
"typescript": "5.4.4",
"webdriverio": "8.35.1",
"typescript": "5.4.5",
"webdriverio": "8.36.0",
"webpack": "5.91.0",
"webpack-cli": "5.1.4",
"webpack-dev-middleware": "7.2.1"
Expand All @@ -90,7 +90,7 @@
"@mantine/core/type-fest": "4.15.0"
},
"volta": {
"node": "20.12.1",
"node": "20.12.2",
"yarn": "1.22.22"
},
"packageManager": "[email protected]"
Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/domain/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export interface InitConfiguration {
internalAnalyticsSubdomain?: string

telemetryConfigurationSampleRate?: number
telemetryUsageSampleRate?: number
}

// This type is only used to build the core configuration. Logs and RUM SDKs are using a proper type
Expand All @@ -90,6 +91,7 @@ export interface Configuration extends TransportConfiguration {
sessionSampleRate: number
telemetrySampleRate: number
telemetryConfigurationSampleRate: number
telemetryUsageSampleRate: number
service: string | undefined
silentMultipleInit: boolean
allowUntrustedEvents: boolean
Expand Down Expand Up @@ -130,6 +132,14 @@ export function validateAndBuildConfiguration(initConfiguration: InitConfigurati
return
}

if (
initConfiguration.telemetryUsageSampleRate !== undefined &&
!isPercentage(initConfiguration.telemetryUsageSampleRate)
) {
display.error('Telemetry Usage Sample Rate should be a number between 0 and 100')
return
}

if (
initConfiguration.trackingConsent !== undefined &&
!objectHasValue(TrackingConsent, initConfiguration.trackingConsent)
Expand All @@ -155,6 +165,7 @@ export function validateAndBuildConfiguration(initConfiguration: InitConfigurati
sessionSampleRate: initConfiguration.sessionSampleRate ?? 100,
telemetrySampleRate: initConfiguration.telemetrySampleRate ?? 20,
telemetryConfigurationSampleRate: initConfiguration.telemetryConfigurationSampleRate ?? 5,
telemetryUsageSampleRate: initConfiguration.telemetryUsageSampleRate ?? 5,
service: initConfiguration.service,
silentMultipleInit: !!initConfiguration.silentMultipleInit,
allowUntrustedEvents: !!initConfiguration.allowUntrustedEvents,
Expand Down Expand Up @@ -190,6 +201,7 @@ export function serializeConfiguration(initConfiguration: InitConfiguration) {
session_sample_rate: initConfiguration.sessionSampleRate,
telemetry_sample_rate: initConfiguration.telemetrySampleRate,
telemetry_configuration_sample_rate: initConfiguration.telemetryConfigurationSampleRate,
telemetry_usage_sample_rate: initConfiguration.telemetryUsageSampleRate,
use_before_send: !!initConfiguration.beforeSend,
use_cross_site_session_cookie: initConfiguration.useCrossSiteSessionCookie,
use_partitioned_cross_site_session_cookie: initConfiguration.usePartitionedCrossSiteSessionCookie,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/domain/telemetry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export {
startTelemetry,
isTelemetryReplicationAllowed,
addTelemetryConfiguration,
addTelemetryUsage,
} from './telemetry'

export * from './rawTelemetryEvent.types'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { TelemetryEvent, TelemetryConfigurationEvent } from './telemetryEvent.types'
import type { TelemetryEvent, TelemetryConfigurationEvent, TelemetryUsageEvent } from './telemetryEvent.types'

export const TelemetryType = {
log: 'log',
configuration: 'configuration',
usage: 'usage',
} as const

export const enum StatusType {
Expand All @@ -17,3 +18,4 @@ export interface RuntimeEnvInfo {

export type RawTelemetryEvent = TelemetryEvent['telemetry']
export type RawTelemetryConfiguration = TelemetryConfigurationEvent['telemetry']['configuration']
export type RawTelemetryUsage = TelemetryUsageEvent['telemetry']['usage']
27 changes: 27 additions & 0 deletions packages/core/src/domain/telemetry/telemetry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
scrubCustomerFrames,
formatError,
addTelemetryConfiguration,
addTelemetryUsage,
TelemetryService,
} from './telemetry'

Expand Down Expand Up @@ -72,6 +73,32 @@ describe('telemetry', () => {
})
})

describe('addTelemetryUsage', () => {
it('should collects usage when sampled', () => {
const { notifySpy } = startAndSpyTelemetry({ telemetrySampleRate: 100, telemetryUsageSampleRate: 100 })

addTelemetryUsage({ feature: 'set-tracking-consent', tracking_consent: 'granted' })

expect(notifySpy).toHaveBeenCalled()
})

it('should not notify usage when not sampled', () => {
const { notifySpy } = startAndSpyTelemetry({ telemetrySampleRate: 100, telemetryUsageSampleRate: 0 })

addTelemetryUsage({ feature: 'set-tracking-consent', tracking_consent: 'granted' })

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

it('should not notify usage when telemetrySampleRate is 0', () => {
const { notifySpy } = startAndSpyTelemetry({ telemetrySampleRate: 0, telemetryUsageSampleRate: 100 })

addTelemetryUsage({ feature: 'set-tracking-consent', tracking_consent: 'granted' })

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

it('should contains feature flags', () => {
addExperimentalFeatures(['foo' as ExperimentalFeature])
const { notifySpy } = startAndSpyTelemetry()
Expand Down
45 changes: 30 additions & 15 deletions packages/core/src/domain/telemetry/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ import type { StackTrace } from '../error/computeStackTrace'
import { computeStackTrace } from '../error/computeStackTrace'
import { getConnectivity } from '../connectivity'
import type { TelemetryEvent } from './telemetryEvent.types'
import type { RawTelemetryConfiguration, RawTelemetryEvent, RuntimeEnvInfo } from './rawTelemetryEvent.types'
import type {
RawTelemetryConfiguration,
RawTelemetryEvent,
RuntimeEnvInfo,
RawTelemetryUsage,
} from './rawTelemetryEvent.types'
import { StatusType, TelemetryType } from './rawTelemetryEvent.types'

// replaced at build time
Expand Down Expand Up @@ -48,24 +53,29 @@ const TELEMETRY_EXCLUDED_SITES: string[] = [INTAKE_SITE_US1_FED]
const telemetryConfiguration: {
maxEventsPerPage: number
sentEventCount: number
telemetryEnabled: boolean
telemetryConfigurationEnabled: boolean
} = { maxEventsPerPage: 0, sentEventCount: 0, telemetryEnabled: false, telemetryConfigurationEnabled: false }
} = {
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>()

telemetryConfiguration.telemetryEnabled =
const telemetryEnabled =
!includes(TELEMETRY_EXCLUDED_SITES, configuration.site) && performDraw(configuration.telemetrySampleRate)
telemetryConfiguration.telemetryConfigurationEnabled =
telemetryConfiguration.telemetryEnabled && performDraw(configuration.telemetryConfigurationSampleRate)

const telemetryEnabledPerType = {
[TelemetryType.log]: telemetryEnabled,
[TelemetryType.configuration]: telemetryEnabled && performDraw(configuration.telemetryConfigurationSampleRate),
[TelemetryType.usage]: telemetryEnabled && performDraw(configuration.telemetryUsageSampleRate),
}

const runtimeEnvInfo = getRuntimeEnvInfo()
onRawTelemetryEventCollected = (rawEvent: RawTelemetryEvent) => {
if (telemetryConfiguration.telemetryEnabled) {
if (telemetryEnabledPerType[rawEvent.type!]) {
const event = toTelemetryEvent(telemetryService, rawEvent, runtimeEnvInfo)
observable.notify(event)
sendToExtension('telemetry', event)
Expand Down Expand Up @@ -108,7 +118,7 @@ export function startTelemetry(telemetryService: TelemetryService, configuration
contextProvider = provider
},
observable,
enabled: telemetryConfiguration.telemetryEnabled,
enabled: telemetryEnabled,
}
}
function getRuntimeEnvInfo(): RuntimeEnvInfo {
Expand Down Expand Up @@ -172,12 +182,17 @@ export function addTelemetryError(e: unknown, context?: Context) {
}

export function addTelemetryConfiguration(configuration: RawTelemetryConfiguration) {
if (telemetryConfiguration.telemetryConfigurationEnabled) {
addTelemetry({
type: TelemetryType.configuration,
configuration,
})
}
addTelemetry({
type: TelemetryType.configuration,
configuration,
})
}

export function addTelemetryUsage(usage: RawTelemetryUsage) {
addTelemetry({
type: TelemetryType.usage,
usage,
})
}

function addTelemetry(event: RawTelemetryEvent) {
Expand Down
Loading

0 comments on commit f0dacd9

Please sign in to comment.