Skip to content
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
5 changes: 3 additions & 2 deletions packages/rum/src/domain/profiling/profiler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ import {
import { LONG_TASK_ID_HISTORY_TIME_OUT_DELAY } from 'packages/rum-core/src/domain/longTask/longTaskCollection'
import { createRumSessionManagerMock, mockRumConfiguration, mockViewHistory } from '../../../../rum-core/test'
import { mockProfiler } from '../../../test'
import type { BrowserProfilerTrace } from '../../types'
import { mockedTrace } from './test-utils/mockedTrace'
import { createRumProfiler } from './profiler'
import type { ProfilerTrace, RumProfilerTrace } from './types'
import type { ProfilerTrace } from './types'
import type { ProfilingContextManager } from './profilingContext'
import { startProfilingContext } from './profilingContext'
import type { ProfileEventPayload } from './transport/assembly'
Expand Down Expand Up @@ -56,7 +57,7 @@ describe('profiler', () => {

const mockProfilerTrace: ProfilerTrace = deepClone(mockedTrace)

const mockedRumProfilerTrace: RumProfilerTrace = Object.assign(mockProfilerTrace, {
const mockedRumProfilerTrace: BrowserProfilerTrace = Object.assign(mockProfilerTrace, {
startClocks: {
relative: relativeNow(),
timeStamp: timeStampNow(),
Expand Down
5 changes: 2 additions & 3 deletions packages/rum/src/domain/profiling/profiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ import type {
ViewHistory,
} from '@datadog/browser-rum-core'
import { createFormDataTransport, LifeCycleEventType } from '@datadog/browser-rum-core'
import type { BrowserProfilerTrace, RumViewEntry } from '../../types'
import type {
RumProfilerTrace,
RumProfilerInstance,
Profiler,
RUMProfiler,
RUMProfilerConfiguration,
RumProfilerStoppedInstance,
RumViewEntry,
} from './types'
import { getNumberOfSamples } from './utils/getNumberOfSamples'
import type { ProfilingContextManager } from './profilingContext'
Expand Down Expand Up @@ -290,7 +289,7 @@ export function createRumProfiler(
instance.views.push(viewEntry)
}

function handleProfilerTrace(trace: RumProfilerTrace): void {
function handleProfilerTrace(trace: BrowserProfilerTrace): void {
// Find current session to assign it to the Profile.
const sessionId = session.findTrackedSession()?.id
const payload = assembleProfilingPayload(trace, configuration, sessionId)
Expand Down
29 changes: 7 additions & 22 deletions packages/rum/src/domain/profiling/transport/assembly.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,15 @@
import { buildTags, currentDrift } from '@datadog/browser-core'
import type { RumConfiguration } from '@datadog/browser-rum-core'
import type { RumProfilerTrace } from '../types'
import type { BrowserProfileEvent, BrowserProfilerTrace } from '../../../types'
import { buildProfileEventAttributes } from './buildProfileEventAttributes'
import type { ProfileEventAttributes } from './buildProfileEventAttributes'

export interface ProfileEvent extends ProfileEventAttributes {
attachments: string[]
start: string // ISO date
end: string // ISO date
family: 'chrome'
runtime: 'chrome'
format: 'json'
version: 4
tags_profiler: string
_dd: {
clock_drift: number
}
}

export interface ProfileEventPayload {
event: ProfileEvent
'wall-time.json': RumProfilerTrace
event: BrowserProfileEvent
'wall-time.json': BrowserProfilerTrace
}

export function assembleProfilingPayload(
profilerTrace: RumProfilerTrace,
profilerTrace: BrowserProfilerTrace,
configuration: RumConfiguration,
sessionId: string | undefined
): ProfileEventPayload {
Expand All @@ -37,15 +22,15 @@ export function assembleProfilingPayload(
}

function buildProfileEvent(
profilerTrace: RumProfilerTrace,
profilerTrace: BrowserProfilerTrace,
configuration: RumConfiguration,
sessionId: string | undefined
): ProfileEvent {
): ProfileEventPayload['event'] {
const tags = buildTags(configuration) // TODO: get that from the tagContext hook
const profileAttributes = buildProfileEventAttributes(profilerTrace, configuration.applicationId, sessionId)
const profileEventTags = buildProfileEventTags(tags)

const profileEvent: ProfileEvent = {
const profileEvent: ProfileEventPayload['event'] = {
...profileAttributes,
attachments: ['wall-time.json'],
start: new Date(profilerTrace.startClocks.timeStamp).toISOString(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { clocksOrigin } from '@datadog/browser-core'
import { RumPerformanceEntryType } from '@datadog/browser-rum-core'
import type { LongTaskContext } from '@datadog/browser-rum-core'
import type { RumProfilerTrace, RumViewEntry } from '../types'
import type { BrowserProfilerTrace, RumViewEntry } from '../../../types'
import { buildProfileEventAttributes, type ProfileEventAttributes } from './buildProfileEventAttributes'

describe('buildProfileEventAttributes', () => {
Expand All @@ -27,7 +27,7 @@ describe('buildProfileEventAttributes', () => {
}
}

function createMockProfilerTrace(overrides: Partial<RumProfilerTrace> = {}): RumProfilerTrace {
function createMockProfilerTrace(overrides: Partial<BrowserProfilerTrace> = {}): BrowserProfilerTrace {
return {
startClocks: clocksOrigin(),
endClocks: clocksOrigin(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import type { RumProfilerTrace, RumViewEntry } from '../types'
import type { BrowserProfilerTrace, RumViewEntry } from '../../../types'

export interface ProfileEventAttributes {
application: { id: string }
session?: { id: string }
view?: { id: string[]; name: string[] }
long_task?: { id: string[] }
application: {
id: string
}
session?: {
id: string
}
view?: {
id: string[]
name: string[]
}
long_task?: {
id: string[]
}
}

/**
Expand All @@ -16,35 +25,34 @@ export interface ProfileEventAttributes {
* @returns Additional attributes.
*/
export function buildProfileEventAttributes(
profilerTrace: RumProfilerTrace,
profilerTrace: BrowserProfilerTrace,
applicationId: string,
sessionId: string | undefined
): ProfileEventAttributes {
const attributes: ProfileEventAttributes = {
application: {
id: applicationId,
},
}
if (sessionId) {
attributes.session = {
id: sessionId,
}
}

// Extract view ids and names from the profiler trace and add them as attributes of the profile event.
// This will be used to filter the profiles by @view.id and/or @view.name.
const { ids, names } = extractViewIdsAndNames(profilerTrace.views)

if (ids.length) {
attributes.view = {
id: ids,
name: names,
}
}
const longTaskIds: string[] = profilerTrace.longTasks.map((longTask) => longTask.id).filter((id) => id !== undefined)

if (longTaskIds.length) {
attributes.long_task = { id: longTaskIds }
const attributes: ProfileEventAttributes = {
application: {
id: applicationId,
},
...(sessionId && {
session: {
id: sessionId,
},
}),
...(ids.length && {
view: {
id: ids,
name: names,
},
}),
...(longTaskIds.length && {
long_task: { id: longTaskIds },
}),
}
return attributes
}
Expand Down
23 changes: 2 additions & 21 deletions packages/rum/src/domain/profiling/types/rumProfiler.types.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import type { TimeoutId, ClocksState } from '@datadog/browser-core'
import type { LongTaskContext } from '@datadog/browser-rum-core'
import type { ProfilerTrace, Profiler } from './profilerApi.types'

export interface RumViewEntry {
/** Detected start time of view */
readonly startClocks: ClocksState
/** RUM view id */
readonly viewId: string
/** RUM view name */
readonly viewName: string | undefined
}
import type { RumViewEntry } from '../../../types'
import type { Profiler } from './profilerApi.types'

/**
* Additional data recorded during profiling session
Expand All @@ -21,17 +13,6 @@ export interface RumProfilerEnrichmentData {
readonly views: RumViewEntry[]
}

export interface RumProfilerTrace extends ProfilerTrace, RumProfilerEnrichmentData {
/** High resolution time when profiler trace started, relative to the profiling session's time origin */
readonly startClocks: ClocksState
/** High resolution time when profiler trace ended, relative to the profiling session's time origin */
readonly endClocks: ClocksState
/** Time origin of the profiling session */
readonly clocksOrigin: ClocksState
/** Sample interval in milliseconds */
readonly sampleInterval: number
}

/**
* Describes profiler session state when it's stopped
*/
Expand Down
1 change: 1 addition & 0 deletions packages/rum/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export type * from './sessionReplay'
export * from './sessionReplayConstants'
export type * from './profiling'
Loading