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'
1413import { 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'
1615import {
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'
3225import 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'
3427import { validateAndBuildRumConfiguration } from '@datadog/browser-rum-core/cjs/domain/configuration'
35- import type { Batch } from '@datadog/browser-core/cjs/transport/batch'
3628import { decode } from '@msgpack/msgpack'
3729import type { TrackType } from '@datadog/browser-core/cjs/domain/configuration'
3830import { createEndpointBuilder } from '@datadog/browser-core/cjs/domain/configuration'
39- import type { RumViewEvent , RumErrorEvent } from '@datadog/browser-rum'
4031import tracer from '../domain/tracer'
4132import type { Hooks } from '../hooks'
4233import { createIpcMain } from '../domain/main/ipcMain'
43- import type { CollectedRumEvent } from '../domain/events'
34+ import type { CollectedRumEvent } from '../domain/rum/ events'
4435import { 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
4942function 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