Skip to content

Commit 200a387

Browse files
committed
extract rum files
1 parent 08d9503 commit 200a387

File tree

8 files changed

+182
-164
lines changed

8 files changed

+182
-164
lines changed

packages/electron/src/domain/main/bridge.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Observable } from '@datadog/browser-core'
22
import type { RumEvent } from '@datadog/browser-rum-core'
33
import { ipcMain } from 'electron'
4-
import type { CollectedRumEvent } from '../events'
4+
import type { CollectedRumEvent } from '../rum/events'
55

66
interface BridgeEvent {
77
eventType: 'rum'
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Observable } from '@datadog/browser-core'
2+
import { RumEventType } from '@datadog/browser-rum-core'
3+
import type { CollectedRumEvent } from './events'
4+
5+
export function startActivityTracking(onRumEventObservable: Observable<CollectedRumEvent>) {
6+
const onActivityObservable = new Observable<void>()
7+
const alreadySeenViewIds = new Set()
8+
onRumEventObservable.subscribe(({ event }) => {
9+
if (event.type === RumEventType.VIEW && !alreadySeenViewIds.has(event.view.id)) {
10+
alreadySeenViewIds.add(event.view.id)
11+
onActivityObservable.notify()
12+
} else if (event.type === RumEventType.ACTION && event.action.type === 'click') {
13+
onActivityObservable.notify()
14+
}
15+
})
16+
return onActivityObservable
17+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import type { Observable, Context } from '@datadog/browser-core'
2+
import { HookNames, DISCARDED, combine } from '@datadog/browser-core'
3+
import { RumEventType } from '@datadog/browser-rum-core'
4+
import type { Hooks } from '../../hooks'
5+
import type { CollectedRumEvent } from './events'
6+
import type { Batch } from '@datadog/browser-core/cjs/transport/batch'
7+
8+
export function startRumEventAssembleAndSend(
9+
onRumEventObservable: Observable<CollectedRumEvent>,
10+
rumBatch: Batch,
11+
hooks: Hooks
12+
) {
13+
onRumEventObservable.subscribe(({ event, source }) => {
14+
const defaultRumEventAttributes = hooks.triggerHook(HookNames.Assemble, {
15+
eventType: event.type,
16+
})!
17+
18+
if (defaultRumEventAttributes === DISCARDED) {
19+
return
20+
}
21+
const commonContext =
22+
source === 'renderer'
23+
? {
24+
session: { id: defaultRumEventAttributes.session!.id },
25+
application: { id: defaultRumEventAttributes.application!.id },
26+
}
27+
: combine(defaultRumEventAttributes, {
28+
// TODO source electron
29+
source: 'browser' as const,
30+
application: { id: defaultRumEventAttributes.application!.id },
31+
session: {
32+
type: 'user' as const,
33+
},
34+
_dd: {
35+
format_version: 2 as const,
36+
},
37+
})
38+
39+
const serverRumEvent = combine(event, commonContext)
40+
41+
if (serverRumEvent.type === RumEventType.VIEW) {
42+
rumBatch.upsert(serverRumEvent as unknown as Context, serverRumEvent.view.id)
43+
} else {
44+
rumBatch.add(serverRumEvent as unknown as Context)
45+
}
46+
})
47+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import type { Observable } from '@datadog/browser-core'
2+
import { generateUUID, ErrorHandling } from '@datadog/browser-core'
3+
import type { RumErrorEvent } from '@datadog/browser-rum-core'
4+
import { RumEventType } from '@datadog/browser-rum-core'
5+
import type { Trace } from '../trace'
6+
import type { CollectedRumEvent } from './events'
7+
8+
export function startConvertSpanToRumEvent(
9+
onTraceObservable: Observable<Trace>,
10+
onRumEventObservable: Observable<CollectedRumEvent>
11+
) {
12+
onTraceObservable.subscribe((trace) => {
13+
trace.forEach((span) => {
14+
if (span.error) {
15+
const rumError: Partial<RumErrorEvent> = {
16+
type: RumEventType.ERROR,
17+
date: span.start / 1e6,
18+
error: {
19+
id: generateUUID(),
20+
message: span.meta['error.message'],
21+
stack: span.meta['error.stack'],
22+
type: span.meta['error.type'],
23+
source: 'source',
24+
handling: ErrorHandling.UNHANDLED,
25+
},
26+
}
27+
onRumEventObservable.notify({ event: rumError as RumErrorEvent, source: 'main-process' })
28+
}
29+
})
30+
})
31+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import crypto from 'node:crypto'
2+
import type { RumConfiguration, RumViewEvent } from '@datadog/browser-rum-core'
3+
import { RumEventType } from '@datadog/browser-rum-core'
4+
import type { Observable } from '@datadog/browser-core'
5+
import { HookNames, timeStampNow, combine, toServerDuration, elapsed } from '@datadog/browser-core'
6+
import type { Hooks } from '../../hooks'
7+
import type { CollectedRumEvent } from './events'
8+
9+
export function startMainProcessTracking(
10+
hooks: Hooks,
11+
configuration: RumConfiguration,
12+
onRumEventObservable: Observable<CollectedRumEvent>,
13+
onActivityObservable: Observable<void>
14+
) {
15+
const mainProcessContext = {
16+
sessionId: crypto.randomUUID(),
17+
viewId: crypto.randomUUID(),
18+
}
19+
hooks.register(HookNames.Assemble, ({ eventType }) => ({
20+
type: eventType,
21+
application: {
22+
id: configuration.applicationId,
23+
},
24+
session: {
25+
id: mainProcessContext.sessionId,
26+
},
27+
view: {
28+
id: mainProcessContext.viewId,
29+
// TODO get customer package name
30+
url: 'com/datadog/application-launch/view',
31+
},
32+
}))
33+
console.log('sessionId', mainProcessContext.sessionId)
34+
const applicationStart = timeStampNow()
35+
let applicationLaunch = {
36+
type: RumEventType.VIEW,
37+
date: applicationStart as number,
38+
view: {
39+
id: mainProcessContext.viewId,
40+
is_active: true,
41+
name: 'ApplicationLaunch',
42+
time_spent: 0,
43+
// TODO update counters
44+
action: {
45+
count: 0,
46+
},
47+
resource: {
48+
count: 0,
49+
},
50+
error: {
51+
count: 0,
52+
},
53+
},
54+
_dd: {
55+
document_version: 1,
56+
},
57+
} as RumViewEvent
58+
59+
onRumEventObservable.notify({ event: applicationLaunch, source: 'main-process' })
60+
61+
onActivityObservable.subscribe(() => {
62+
applicationLaunch = combine(applicationLaunch, {
63+
view: {
64+
time_spent: toServerDuration(elapsed(applicationStart, timeStampNow())),
65+
},
66+
_dd: {
67+
document_version: applicationLaunch._dd.document_version + 1,
68+
},
69+
})
70+
onRumEventObservable.notify({ event: applicationLaunch, source: 'main-process' })
71+
})
72+
// TODO session expiration / renewal
73+
// TODO useragent
74+
}

packages/electron/src/domain/trace.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ export interface DatadogCarrier {
55
'x-datadog-sampling-priority': string
66
'x-datadog-trace-id': string
77
}
8+
9+
export type Span = any
10+
export type Trace = Span[]

packages/electron/src/entries/main.ts

Lines changed: 9 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,34 @@
1010
* - [x] setup bridge client with ipc from webviews (renderer processes)
1111
* - [x] use `exposeInMainWorld` to setup the bridge function that will setup the ipc to the main process
1212
*/
13-
import crypto from 'node:crypto'
1413
import { createServer } from 'node:http'
15-
import type { RawError, PageMayExitEvent, Encoder, Context, InitConfiguration } from '@datadog/browser-core'
14+
import type { RawError, PageMayExitEvent, Encoder, InitConfiguration } from '@datadog/browser-core'
1615
import {
17-
elapsed,
18-
timeStampNow,
1916
Observable,
2017
DeflateEncoderStreamId,
2118
createBatch,
2219
createHttpRequest,
2320
createFlushController,
2421
createIdentityEncoder,
25-
combine,
26-
toServerDuration,
2722
HookNames,
2823
DISCARDED,
29-
ErrorHandling,
30-
generateUUID,
3124
} from '@datadog/browser-core'
3225
import type { RumConfiguration, RumInitConfiguration } from '@datadog/browser-rum-core'
33-
import { createHooks, RumEventType } from '@datadog/browser-rum-core'
26+
import { createHooks } from '@datadog/browser-rum-core'
3427
import { validateAndBuildRumConfiguration } from '@datadog/browser-rum-core/cjs/domain/configuration'
35-
import type { Batch } from '@datadog/browser-core/cjs/transport/batch'
3628
import { decode } from '@msgpack/msgpack'
3729
import type { TrackType } from '@datadog/browser-core/cjs/domain/configuration'
3830
import { createEndpointBuilder } from '@datadog/browser-core/cjs/domain/configuration'
39-
import type { RumViewEvent, RumErrorEvent } from '@datadog/browser-rum'
4031
import tracer from '../domain/tracer'
4132
import type { Hooks } from '../hooks'
4233
import { createIpcMain } from '../domain/main/ipcMain'
43-
import type { CollectedRumEvent } from '../domain/events'
34+
import type { CollectedRumEvent } from '../domain/rum/events'
4435
import { setupMainBridge } from '../domain/main/bridge'
45-
46-
type Span = any
47-
type Trace = Span[]
36+
import { startActivityTracking } from '../domain/rum/activity'
37+
import { startRumEventAssembleAndSend } from '../domain/rum/assembly'
38+
import { startMainProcessTracking } from '../domain/rum/mainProcessTracking'
39+
import { startConvertSpanToRumEvent } from '../domain/rum/convertSpans'
40+
import type { Trace } from '../domain/trace'
4841

4942
function makeDatadogElectron() {
5043
return {
@@ -60,7 +53,7 @@ function makeDatadogElectron() {
6053
const pageMayExitObservable = new Observable<PageMayExitEvent>()
6154
const sessionExpireObservable = new Observable<void>()
6255
const onRumEventObservable = new Observable<CollectedRumEvent>()
63-
const onTraceObservable = new Observable<Span>()
56+
const onTraceObservable = new Observable<Trace>()
6457
const hooks = createHooks()
6558
const createEncoder = () => createIdentityEncoder()
6659

@@ -241,150 +234,3 @@ function isSdkRequest(span: any) {
241234
(span.resource as string).startsWith('browser-intake-datadoghq.com')
242235
)
243236
}
244-
245-
function startRumEventAssembleAndSend(
246-
onRumEventObservable: Observable<CollectedRumEvent>,
247-
rumBatch: Batch,
248-
hooks: Hooks
249-
) {
250-
onRumEventObservable.subscribe(({ event, source }) => {
251-
const defaultRumEventAttributes = hooks.triggerHook(HookNames.Assemble, {
252-
eventType: event.type,
253-
})!
254-
255-
if (defaultRumEventAttributes === DISCARDED) {
256-
return
257-
}
258-
const commonContext =
259-
source === 'renderer'
260-
? {
261-
session: { id: defaultRumEventAttributes.session!.id },
262-
application: { id: defaultRumEventAttributes.application!.id },
263-
}
264-
: combine(defaultRumEventAttributes, {
265-
// TODO source electron
266-
source: 'browser' as const,
267-
application: { id: defaultRumEventAttributes.application!.id },
268-
session: {
269-
type: 'user' as const,
270-
},
271-
_dd: {
272-
format_version: 2 as const,
273-
},
274-
})
275-
276-
const serverRumEvent = combine(event, commonContext)
277-
278-
if (serverRumEvent.type === RumEventType.VIEW) {
279-
rumBatch.upsert(serverRumEvent as unknown as Context, serverRumEvent.view.id)
280-
} else {
281-
rumBatch.add(serverRumEvent as unknown as Context)
282-
}
283-
})
284-
}
285-
286-
function startMainProcessTracking(
287-
hooks: Hooks,
288-
configuration: RumConfiguration,
289-
onRumEventObservable: Observable<CollectedRumEvent>,
290-
onActivityObservable: Observable<void>
291-
) {
292-
const mainProcessContext = {
293-
sessionId: crypto.randomUUID(),
294-
viewId: crypto.randomUUID(),
295-
}
296-
hooks.register(HookNames.Assemble, ({ eventType }) => ({
297-
type: eventType,
298-
application: {
299-
id: configuration.applicationId,
300-
},
301-
session: {
302-
id: mainProcessContext.sessionId,
303-
},
304-
view: {
305-
id: mainProcessContext.viewId,
306-
// TODO get customer package name
307-
url: 'com/datadog/application-launch/view',
308-
},
309-
}))
310-
console.log('sessionId', mainProcessContext.sessionId)
311-
const applicationStart = timeStampNow()
312-
let applicationLaunch = {
313-
type: RumEventType.VIEW,
314-
date: applicationStart as number,
315-
view: {
316-
id: mainProcessContext.viewId,
317-
is_active: true,
318-
name: 'ApplicationLaunch',
319-
time_spent: 0,
320-
// TODO update counters
321-
action: {
322-
count: 0,
323-
},
324-
resource: {
325-
count: 0,
326-
},
327-
error: {
328-
count: 0,
329-
},
330-
},
331-
_dd: {
332-
document_version: 1,
333-
},
334-
} as RumViewEvent
335-
336-
onRumEventObservable.notify({ event: applicationLaunch, source: 'main-process' })
337-
338-
onActivityObservable.subscribe(() => {
339-
applicationLaunch = combine(applicationLaunch, {
340-
view: {
341-
time_spent: toServerDuration(elapsed(applicationStart, timeStampNow())),
342-
},
343-
_dd: {
344-
document_version: applicationLaunch._dd.document_version + 1,
345-
},
346-
})
347-
onRumEventObservable.notify({ event: applicationLaunch, source: 'main-process' })
348-
})
349-
// TODO session expiration / renewal
350-
// TODO useragent
351-
}
352-
353-
function startConvertSpanToRumEvent(
354-
onTraceObservable: Observable<Trace>,
355-
onRumEventObservable: Observable<CollectedRumEvent>
356-
) {
357-
onTraceObservable.subscribe((trace) => {
358-
trace.forEach((span) => {
359-
if (span.error) {
360-
const rumError: Partial<RumErrorEvent> = {
361-
type: RumEventType.ERROR,
362-
date: span.start / 1e6,
363-
error: {
364-
id: generateUUID(),
365-
message: span.meta['error.message'],
366-
stack: span.meta['error.stack'],
367-
type: span.meta['error.type'],
368-
source: 'source',
369-
handling: ErrorHandling.UNHANDLED,
370-
},
371-
}
372-
onRumEventObservable.notify({ event: rumError as RumErrorEvent, source: 'main-process' })
373-
}
374-
})
375-
})
376-
}
377-
378-
function startActivityTracking(onRumEventObservable: Observable<CollectedRumEvent>) {
379-
const onActivityObservable = new Observable<void>()
380-
const alreadySeenViewIds = new Set()
381-
onRumEventObservable.subscribe(({ event }) => {
382-
if (event.type === RumEventType.VIEW && !alreadySeenViewIds.has(event.view.id)) {
383-
alreadySeenViewIds.add(event.view.id)
384-
onActivityObservable.notify()
385-
} else if (event.type === RumEventType.ACTION && event.action.type === 'click') {
386-
onActivityObservable.notify()
387-
}
388-
})
389-
return onActivityObservable
390-
}

0 commit comments

Comments
 (0)