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 feature flags to telemetry events #1625

Merged
merged 8 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* So keep in mind that in certain configurations, your experimental feature flag may affect other products.
*/

let enabledExperimentalFeatures: Set<string>
let enabledExperimentalFeatures: Set<string> | undefined

export function updateExperimentalFeatures(enabledFeatures: string[] | undefined): void {
// Safely handle external data
Expand All @@ -20,7 +20,7 @@ export function updateExperimentalFeatures(enabledFeatures: string[] | undefined
enabledFeatures
.filter((flag) => typeof flag === 'string')
.forEach((flag: string) => {
enabledExperimentalFeatures.add(flag)
enabledExperimentalFeatures!.add(flag)
})
}

Expand All @@ -31,3 +31,7 @@ export function isExperimentalFeatureEnabled(featureName: string): boolean {
export function resetExperimentalFeatures(): void {
enabledExperimentalFeatures = new Set()
}

export function getExperimentalFeatures(): Set<string> {
return enabledExperimentalFeatures || new Set()
}
1 change: 1 addition & 0 deletions packages/core/src/domain/configuration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export {
isExperimentalFeatureEnabled,
updateExperimentalFeatures,
resetExperimentalFeatures,
getExperimentalFeatures,
} from './experimentalFeatures'
export * from './intakeSites'
12 changes: 11 additions & 1 deletion packages/core/src/domain/telemetry/telemetry.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { StackTrace } from '@datadog/browser-core'
import { callMonitored } from '../../tools/monitor'
import type { Configuration } from '../configuration'
import { INTAKE_SITE_US1, INTAKE_SITE_US1_FED } from '../configuration'
import { updateExperimentalFeatures, INTAKE_SITE_US1, INTAKE_SITE_US1_FED } from '../configuration'
import { resetTelemetry, startTelemetry, scrubCustomerFrames, formatError } from './telemetry'

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

it('should contains feature flags', () => {
updateExperimentalFeatures(['foo'])
const { notifySpy } = startAndSpyTelemetry()
callMonitored(() => {
throw new Error('message')
})

expect(notifySpy.calls.mostRecent().args[0].experimental_features).toEqual(['foo'])
})

describe('telemetry context', () => {
it('should be added to telemetry events', () => {
const { telemetry, notifySpy } = startAndSpyTelemetry()
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/domain/telemetry/telemetry.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { Context } from '../../tools/context'
import { ConsoleApiName } from '../../tools/display'
import { toStackTraceString } from '../../tools/error'
import { assign, combine, jsonStringify, performDraw, includes, startsWith } from '../../tools/utils'
import { assign, combine, jsonStringify, performDraw, includes, startsWith, arrayFrom } from '../../tools/utils'
import type { Configuration } from '../configuration'
import { INTAKE_SITE_STAGING, INTAKE_SITE_US1_FED } from '../configuration'
import { getExperimentalFeatures, INTAKE_SITE_STAGING, INTAKE_SITE_US1_FED } from '../configuration'
import type { StackTrace } from '../tracekit'
import { computeStackTrace } from '../tracekit'
import { Observable } from '../../tools/observable'
Expand Down Expand Up @@ -80,6 +80,7 @@ export function startTelemetry(configuration: Configuration): Telemetry {
format_version: 2 as const,
},
telemetry: event as any, // https://github.com/microsoft/TypeScript/issues/48457
experimental_features: arrayFrom(getExperimentalFeatures()),
},
contextProvider !== undefined ? contextProvider() : {}
)
Expand Down
17 changes: 17 additions & 0 deletions packages/core/src/tools/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Clock } from '../../test/specHelper'
import { mockClock } from '../../test/specHelper'
import {
arrayFrom,
combine,
cssEscape,
deepClone,
Expand Down Expand Up @@ -594,3 +595,19 @@ describe('elementMatches', () => {
expect(elementMatches(element, '.foo')).toEqual(false)
})
})

describe('arrayFrom', () => {
it('should return an array from a Set', () => {
const set = new Set()
set.add('foo')

expect(arrayFrom(set)).toEqual(['foo'])
})

it('should return an array from a array like object', () => {
const div = document.createElement('div')
div.classList.add('foo')

expect(arrayFrom(div.classList)).toEqual(['foo'])
})
})
16 changes: 13 additions & 3 deletions packages/core/src/tools/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,21 @@ export function includes(candidate: string | unknown[], search: any) {
return candidate.indexOf(search) !== -1
}

export function arrayFrom<T>(arrayLike: ArrayLike<T>): T[] {
export function arrayFrom<T>(arrayLike: ArrayLike<T> | Set<T>): T[] {
if (Array.from) {
return Array.from(arrayLike)
}

const array = []
for (let i = 0; i < arrayLike.length; i++) {
array.push(arrayLike[i])

if (arrayLike instanceof Set) {
arrayLike.forEach((item) => array.push(item))
} else {
Comment on lines +219 to +221
Copy link
Member

Choose a reason for hiding this comment

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

👏 praise: ‏Nice! Things we should do to support IE...

Copy link
Member

Choose a reason for hiding this comment

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

💬 suggestion: ‏Remove the setToArray function from this file and replace usages with arrayFrom

for (let i = 0; i < arrayLike.length; i++) {
array.push(arrayLike[i])
}
}

return array
}

Expand Down