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 2 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
16 changes: 8 additions & 8 deletions packages/core/src/domain/configuration/experimentalFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@
* So keep in mind that in certain configurations, your experimental feature flag may affect other products.
*/

let enabledExperimentalFeatures: Set<string>
let enabledExperimentalFeatures: string[] = []
bcaudan marked this conversation as resolved.
Show resolved Hide resolved

export function updateExperimentalFeatures(enabledFeatures: string[] | undefined): void {
// Safely handle external data
if (!Array.isArray(enabledFeatures)) {
return
}

if (!enabledExperimentalFeatures) {
enabledExperimentalFeatures = new Set(enabledFeatures)
}

enabledFeatures
.filter((flag) => typeof flag === 'string')
.forEach((flag: string) => {
enabledExperimentalFeatures.add(flag)
if (!enabledExperimentalFeatures.includes(flag)) enabledExperimentalFeatures.push(flag)
})
}

export function isExperimentalFeatureEnabled(featureName: string): boolean {
return !!enabledExperimentalFeatures && enabledExperimentalFeatures.has(featureName)
return enabledExperimentalFeatures.includes(featureName)
}

export function resetExperimentalFeatures(): void {
enabledExperimentalFeatures = new Set()
enabledExperimentalFeatures = []
}

export function getExperimentalFeatures(): string[] {
return enabledExperimentalFeatures
}
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
3 changes: 2 additions & 1 deletion packages/core/src/domain/telemetry/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ConsoleApiName } from '../../tools/display'
import { toStackTraceString } from '../../tools/error'
import { assign, combine, jsonStringify, performDraw, includes, startsWith } 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: getExperimentalFeatures(),
Copy link
Contributor

Choose a reason for hiding this comment

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

💭 thought: ‏should not we put that on the schema?

},
contextProvider !== undefined ? contextProvider() : {}
)
Expand Down