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 { createServer } from 'node:http'
1413import type { RawError , PageMayExitEvent , Encoder , InitConfiguration } from '@datadog/browser-core'
1514import {
1615 Observable ,
@@ -19,25 +18,22 @@ import {
1918 createHttpRequest ,
2019 createFlushController ,
2120 createIdentityEncoder ,
22- HookNames ,
23- DISCARDED ,
2421} from '@datadog/browser-core'
2522import type { RumConfiguration , RumInitConfiguration } from '@datadog/browser-rum-core'
2623import { createHooks } from '@datadog/browser-rum-core'
2724import { validateAndBuildRumConfiguration } from '@datadog/browser-rum-core/cjs/domain/configuration'
28- import { decode } from '@msgpack/msgpack'
2925import type { TrackType } from '@datadog/browser-core/cjs/domain/configuration'
3026import { createEndpointBuilder } from '@datadog/browser-core/cjs/domain/configuration'
31- import tracer from '../domain/tracer'
32- import type { Hooks } from '../hooks'
27+ import tracer from '../domain/trace/tracer'
3328import { createIpcMain } from '../domain/main/ipcMain'
3429import type { CollectedRumEvent } from '../domain/rum/events'
3530import { setupMainBridge } from '../domain/main/bridge'
3631import { startActivityTracking } from '../domain/rum/activity'
3732import { startRumEventAssembleAndSend } from '../domain/rum/assembly'
3833import { startMainProcessTracking } from '../domain/rum/mainProcessTracking'
3934import { startConvertSpanToRumEvent } from '../domain/rum/convertSpans'
40- import type { Trace } from '../domain/trace'
35+ import type { Trace } from '../domain/trace/trace'
36+ import { createDdTraceAgent } from '../domain/trace/traceAgent'
4137
4238function makeDatadogElectron ( ) {
4339 return {
@@ -148,89 +144,3 @@ export function startElectronSpanBatch(
148144
149145 return batch
150146}
151-
152- function createDdTraceAgent ( onTraceObservable : Observable < Trace > , hooks : Hooks ) {
153- const server = createServer ( )
154-
155- server . on ( 'request' , ( req , res ) => {
156- // Collect binary data chunks
157- const chunks : Buffer [ ] = [ ]
158- req . on ( 'data' , ( chunk : Buffer ) => {
159- chunks . push ( chunk )
160- } )
161- req . on ( 'end' , ( ) => {
162- const buffer = Buffer . concat ( chunks )
163-
164- const decoded = decode ( buffer ) as Array <
165- Array < { name : string ; type : string ; meta : { [ key : string ] : unknown } ; [ key : string ] : unknown } >
166- >
167-
168- const defaultRumEventAttributes = hooks . triggerHook ( HookNames . Assemble , {
169- eventType : 'span' as any ,
170- } ) !
171-
172- if ( defaultRumEventAttributes === DISCARDED ) {
173- return
174- }
175-
176- for ( const trace of decoded ) {
177- const filteredTrace = trace
178- . filter ( ( span ) => ! isSdkRequest ( span ) )
179- . map ( ( span ) => ( {
180- // rewrite id
181- ...span ,
182- trace_id : Number ( span . trace_id ) ?. toString ( 16 ) ,
183- span_id : Number ( span . span_id ) ?. toString ( 16 ) ,
184- parent_id : Number ( span . parent_id ) ?. toString ( 16 ) ,
185- meta : {
186- ...span . meta ,
187- '_dd.application.id' : defaultRumEventAttributes . application ! . id ,
188- '_dd.session.id' : defaultRumEventAttributes . session ! . id ,
189- '_dd.view.id' : defaultRumEventAttributes . view ! . id ,
190- } ,
191- } ) )
192-
193- if ( filteredTrace . length > 0 ) {
194- onTraceObservable . notify ( filteredTrace )
195- }
196- }
197- } )
198-
199- // Respond with the agent API format that dd-trace expects
200- res . writeHead ( 200 , { 'Content-Type' : 'application/json' } )
201- res . end (
202- JSON . stringify ( {
203- rate_by_service : {
204- 'service:dd-trace,env:prod' : 1 ,
205- } ,
206- } )
207- )
208- } )
209-
210- server . listen ( 0 , ( ) => {
211- const addressInfo = server . address ( )
212- if ( ! addressInfo ) {
213- throw new Error ( 'Failed to get server address' )
214- }
215-
216- if ( typeof addressInfo === 'string' ) {
217- throw new Error ( `Address is a string: ${ addressInfo } ` )
218- }
219-
220- const { port } = addressInfo
221- const url = `http://127.0.0.1:${ port } `
222-
223- // console.log('agents url', url)
224- tracer . setUrl ( url )
225- } )
226- }
227-
228- function isSdkRequest ( span : any ) {
229- const spanRequestUrl = span . meta [ 'http.url' ] as string | undefined
230- return (
231- ( spanRequestUrl &&
232- ( spanRequestUrl . startsWith ( 'http://127.0.0.1' ) ||
233- spanRequestUrl . startsWith ( 'https://browser-intake-datadoghq.com/' ) ) ) ||
234- ( span . resource as string ) . startsWith ( 'browser-intake-datadoghq.com' )
235- )
236- }
0 commit comments