Skip to content

Commit

Permalink
Integrate thomas.lebeau/mf-service-modify (#2788) into staging-23
Browse files Browse the repository at this point in the history
Integrated commit sha: 7d87b9d

Co-authored-by: Thomas Lebeau <thomas.lebeau@datadoghq.com>
  • Loading branch information
dd-mergequeue[bot] and thomas-lebeau authored Jun 4, 2024
2 parents 124232f + 7d87b9d commit d3d8eb8
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/core/src/tools/utils/polyfills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ interface Assignable {
export function assign<T, U>(target: T, source: U): T & U
export function assign<T, U, V>(target: T, source1: U, source2: V): T & U & V
export function assign<T, U, V, W>(target: T, source1: U, source2: V, source3: W): T & U & V & W
export function assign<T, U, V, W, X>(target: T, source1: U, source2: V, source3: W, source4: X): T & U & V & W & X
export function assign(target: Assignable, ...toAssign: Assignable[]) {
toAssign.forEach((source: Assignable) => {
for (const key in source) {
Expand Down
44 changes: 44 additions & 0 deletions packages/rum-core/src/domain/assembly.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,50 @@ describe('rum assembly', () => {
expect(serverRumEvents[0].service).toEqual('new service')
expect(serverRumEvents[0].version).toEqual('new version')
})

describe('fields service and version', () => {
it('by default, it should not be modifiable', () => {
const { lifeCycle } = setupBuilder
.withConfiguration({
beforeSend: (event) => {
event.service = 'bar'
event.version = '0.2.0'

return true
},
})
.build()

notifyRawRumEvent(lifeCycle, {
rawRumEvent: createRawRumEvent(RumEventType.RESOURCE, { resource: { url: '/path?foo=bar' } }),
})

expect((serverRumEvents[0] as RumResourceEvent).service).toBe('default service')
expect((serverRumEvents[0] as RumResourceEvent).version).toBe('default version')
})

it('when the micro_frontend experimental flag is set, it should be modifiable', () => {
mockExperimentalFeatures([ExperimentalFeature.MICRO_FRONTEND])

const { lifeCycle } = setupBuilder
.withConfiguration({
beforeSend: (event) => {
event.service = 'bar'
event.version = '0.2.0'

return true
},
})
.build()

notifyRawRumEvent(lifeCycle, {
rawRumEvent: createRawRumEvent(RumEventType.RESOURCE, { resource: { url: '/path?foo=bar' } }),
})

expect((serverRumEvents[0] as RumResourceEvent).service).toBe('bar')
expect((serverRumEvents[0] as RumResourceEvent).version).toBe('0.2.0')
})
})
})

describe('url context', () => {
Expand Down
27 changes: 21 additions & 6 deletions packages/rum-core/src/domain/assembly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ export function startRumAssembly(
getCommonContext: () => CommonContext,
reportError: (error: RawError) => void
) {
// TODO: lift this declaration to module level once feature flag is removed
const ROOT_MODIFIABLE_FIELD_PATHS: ModifiableFieldPaths = isExperimentalFeatureEnabled(
ExperimentalFeature.MICRO_FRONTEND
)
? {
service: 'string',
version: 'string',
}
: {}

modifiableFieldPathsByEvent = {
[RumEventType.VIEW]: VIEW_MODIFIABLE_FIELD_PATHS,
[RumEventType.ERROR]: assign(
Expand All @@ -82,24 +92,29 @@ export function startRumAssembly(
'error.fingerprint': 'string',
},
USER_CUSTOMIZABLE_FIELD_PATHS,
VIEW_MODIFIABLE_FIELD_PATHS
VIEW_MODIFIABLE_FIELD_PATHS,
ROOT_MODIFIABLE_FIELD_PATHS
),
[RumEventType.RESOURCE]: assign(
{
'resource.url': 'string',
},
isExperimentalFeatureEnabled(ExperimentalFeature.WRITABLE_RESOURCE_GRAPHQL) && {
'resource.graphql': 'object',
},
isExperimentalFeatureEnabled(ExperimentalFeature.WRITABLE_RESOURCE_GRAPHQL)
? {
'resource.graphql': 'object',
}
: {},
USER_CUSTOMIZABLE_FIELD_PATHS,
VIEW_MODIFIABLE_FIELD_PATHS
VIEW_MODIFIABLE_FIELD_PATHS,
ROOT_MODIFIABLE_FIELD_PATHS
),
[RumEventType.ACTION]: assign(
{
'action.target.name': 'string',
},
USER_CUSTOMIZABLE_FIELD_PATHS,
VIEW_MODIFIABLE_FIELD_PATHS
VIEW_MODIFIABLE_FIELD_PATHS,
ROOT_MODIFIABLE_FIELD_PATHS
),
[RumEventType.LONG_TASK]: assign({}, USER_CUSTOMIZABLE_FIELD_PATHS, VIEW_MODIFIABLE_FIELD_PATHS),
[RumEventType.VITAL]: assign({}, USER_CUSTOMIZABLE_FIELD_PATHS, VIEW_MODIFIABLE_FIELD_PATHS),
Expand Down
4 changes: 2 additions & 2 deletions packages/rum-core/src/rumEvent.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1116,11 +1116,11 @@ export interface CommonProperties {
/**
* The service name for this application
*/
readonly service?: string
service?: string
/**
* The version for this application
*/
readonly version?: string
version?: string
/**
* The build version for this application
*/
Expand Down
2 changes: 1 addition & 1 deletion rum-events-format
32 changes: 32 additions & 0 deletions test/e2e/scenario/microfrontend.scenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { flushEvents, createTest } from '../lib/framework'
const HANDLING_STACK_REGEX = /^Error: \n\s+at testHandlingStack @/

const CONFIG: Partial<RumInitConfiguration> = {
service: 'main-service',
version: '1.0.0',
enableExperimentalFeatures: ['micro_frontend'],
beforeSend: (event: RumEvent, domainContext: RumEventDomainContext) => {
if ('handlingStack' in domainContext) {
Expand Down Expand Up @@ -123,4 +125,34 @@ describe('microfrontend', () => {
expect(event).toBeTruthy()
expect(event?.context?.handlingStack).toMatch(HANDLING_STACK_REGEX)
})

createTest('allow to modify service and version')
.withRum(CONFIG)
.withRumInit((configuration) => {
window.DD_RUM!.init({
...configuration,
beforeSend: (event: RumEvent) => {
if (event.type === 'resource') {
event.service = 'mf-service'
event.version = '0.1.0'
}

return true
},
})
})
.run(async ({ intakeRegistry }) => {
await flushEvents()

const viewEvent = intakeRegistry.rumViewEvents[0]
const resourceEvent = intakeRegistry.rumResourceEvents[0]

expect(viewEvent).toBeTruthy()
expect(viewEvent.service).toBe('main-service')
expect(viewEvent.version).toBe('1.0.0')

expect(resourceEvent).toBeTruthy()
expect(resourceEvent.service).toBe('mf-service')
expect(resourceEvent.version).toBe('0.1.0')
})
})

0 comments on commit d3d8eb8

Please sign in to comment.